#!/bin/sh
# AppBuilder-0.3
# copyright 2006 Gilbert Ashley <amigo@ibiblio.org>
# See the documentation

# Set the name of the main executable here.
PROG=src2pkg-dnd
APP_NAME=src2pkg-dnd

# Check for links and get APP_DIR
if [ ! -L "$0" ] ; then
	APP_DIR=`dirname "$0"`
	APP_DIR=`cd "$APP_DIR";pwd`
elif [ ! -z `which readlink 2> /dev/null` ] ; then
	APP_DIR=`readlink -f "$0"`
	APP_DIR=`dirname "$APP_DIR"`
else
	echo "$PROG: Can't get real path to AppRun!" 1>&2
fi
export APP_DIR
EXEC_NAME=`basename "$0"`
# Add APP_DIR/Resources to the path and read in FUNCTIONS
PATH="$APP_DIR":"$APP_DIR/Resources":$PATH
source "$APP_DIR/Resources/AppFunctions"

# Option: Detect the PLATFORM
# PLATFORM=`get_platform`
# Or specify a simple local binary path(wrapped binary or shell):
PLATFORM=

# Add the BIN_DIR to the path
BIN_DIR="$APP_DIR/$PLATFORM"
PATH="$APP_DIR/$PLATFORM":$PATH
export PATH

# Use CHOICES dir if needed
get_choices_dir ;
# xterm -hold -e "echo CHOICES_DIR=$CHOICES_DIR"
# Here is where we put the default choices.
# Uncomment this if we need to set or retrieve any options.
if [[ $CHOICES_DIR != '' && ! -e $CHOICES_DIR ]] ; then
	mkdir -p $CHOICES_DIR
	echo '-N -Q' > $CHOICES_DIR/archive_options
	echo '-R' > $CHOICES_DIR/script_options
	echo '1' > $CHOICES_DIR/pause
	echo '--convert-simple' > $CHOICES_DIR/convert_options
fi
#
if [[ -e $CHOICES_DIR ]] ; then
	ARCHIVE_OPTIONS=`cat $CHOICES_DIR/archive_options`
	SCRIPT_OPTIONS=`cat $CHOICES_DIR/script_options`
	PAUSE=`cat $CHOICES_DIR/pause`
	CONVERT_OPTIONS=`cat $CHOICES_DIR/convert_options`
fi


# Here's a few examples -starting from the most sensible to the craziest
# I personally use this: ARCHIVE_OPTIONS='-A'
# Just use this to write new starting scripts for you, without doing more:
##ARCHIVE_OPTIONS='-A -R'
# Use this to just test builds without trying to create a package
# ARCHIVE_OPTIONS='-T'
# Use this to auto-build and write a script if the build is successful
#ARCHIVE_OPTIONS='-A -R'
# Use this to create a package and install right away -man are you nuts?
# ARCHIVE_OPTIONS='-I -W'

# 
# This lets you use different options for when a *.src2pkg (or *.src2pkg.auto)
# script is dropped on the icon instead of an archive. 
# Useful options here might be -W, -V, -VV
##SCRIPT_OPTIONS='-R'

# src2pkg will run in a new xterm for each package.
# set this to '1' if you want the program to pause  after each build
# setting to anything else means src2pkg will just keep going
##PAUSE=1


HELP_TEXT="	src2pkg-dnd drag-n-drop AppDir frontend to src2pkg

  This AppDir allows you to compile sources and create packages from them
  by simply dragging the source archive and releasing it on the icon for this
  program. The best way to use this tool is to create a copy of it in your 
  \$HOME directory. Then open it in your favorite text editor and set any
  default options you want at the top of the file. Then create a shortcut or
  link to it on your desktop. Now, simply dropping one or more archives on the
  icon will cause src2pkg to configure and compile them and then create
  finished packages of them. Each package will built as if you had run src2pkg
  in the same directory as the where the tarball is located. Have fun!
"


cd $APP_DIR ;

#
if [[ $# = 0 ]] ; then
	# What to do if icon is simply clicked.
	if [[ $DISPLAY ]] ; then
		#xterm -hold -e "echo Drop a tarball on me to have it compiled and packaged."
		xterm -title "src2pkg-dnd - Help" -hold -e echo "$HELP_TEXT"
		exit 0
	else
		echo "$HELP_TEXT"
		exit 0
	fi
elif [[ $1 == '--help' ]] ; then
	if [[ $DISPLAY ]] ; then
		#xterm -hold -e "echo Drop a tarball on me to have it compiled and packaged."
		xterm -title "src2pkg-dnd - Help" -hold -e echo "$HELP_TEXT"
		exit 0
	else
		echo "$HELP_TEXT"
		exit 0
	fi
elif [[ $1 == '--configure' ]] ; then
	# Put configure options interface here if needed
	if [ -d  $CHOICES_DIR/$APP_DIR ] ; then
	  rox $CHOICES_DIR/$APP_DIR ; exit 0
	else
	  rox $CHOICES_DIR ; exit 0 
	fi
elif [[ $1 == '--compile' ]] ; then
	shift
	standard_compile || exit 1
elif [[ $# > 0 ]] ; then
	# What to do if a file or files is dropped on the AppDir
	for f in "$@" ; do
		path_to_file=$(dirname $f)
		filename=$(basename $f)
		#if [[ "$ARCHIVE_OPTIONS" != "$SCRIPT_OPTIONS" ]] ; then
		case $filename in
			*.SlackBuild) SRC2PKG_OPTIONS=$CONVERT_OPTIONS ;;
			*.src2pkg|*.src2pkg.auto) SRC2PKG_OPTIONS=$SCRIPT_OPTIONS ;;
			*) SRC2PKG_OPTIONS=$ARCHIVE_OPTIONS ;;
		esac
		#fi
		
		if [[ $DISPLAY ]] ; then
			cd $path_to_file ;
			# CWD=$(pwd)
			if [[ $PAUSE = 1 ]] ; then
				xterm -title "src2pkg-dnd - $filename" -hold -e "src2pkg $SRC2PKG_OPTIONS $filename && echo 'Done! Close window to proceed.'"
			else
				xterm -title "src2pkg-dnd - $filename" -e "src2pkg $SRC2PKG_OPTIONS $filename"
			fi
		else
			cd $path_to_file
			src2pkg $SRC2PKG_OPTIONS $filename
		fi
	done
	exit 0
fi

# This is useful if you make symlinks of the program binary names in 
# your path and then link them to the AppRun.
EXEC_FILE_PATH="$APP_DIR/$PLATFORM/$EXEC_NAME"
if [[ -x "$EXEC_FILE_PATH" && $EXEC_NAME != 'AppRun' ]] ; then
	exec "$EXEC_FILE_PATH" "$@"
elif [ $EXEC_NAME != 'AppRun' ] ; then
	echo "Unable to find $EXEC_NAME in binary directory." 
fi

BIN=$BIN_DIR/$PROG

if [ -x "$BIN" ]; then
  exec $DEBUGGER "$BIN" "$@"
  
  else
  echo "ERROR from $0:" >&2
  echo "I cannot find an executable file for your host type ($PLATFORM)." >&2
  echo "Trying to compile..." >&2
  if [ -n "$DISPLAY" ]; then
    xterm -e "$0" --compile
  else
    "$0" --compile
  fi
  if [ -x "$BIN" ]; then
    exec "$BIN" "$@"
  fi
  exit 1
fi
