#!/bin/sh

# This line sets the path to the PID file that makes the popup menu command line option
# work.  The old behavior was to write this to the CHoices directory, but that doesn't 
# work if the user doesn't have write permissions there.  To revert to the old behavior, 
# just comment this line out.
export PID_FILE_PATH=/tmp/roxmenu-`whoami`.pid

# Set the string to pass to kill -s.  This seems to differ from one version of kill to the next,
# so let's just make it easy to change.
SIGUSR1='USR1'
SIGUSR2='USR2'

# Set the name of the executable here.
PROG=ROX-Menu
APP_NAME=`dirname "$0"`
APP_NAME=`basename "$APP_NAME"`
EXEC_NAME=`basename "$0"`
if [ "$APP_NAME" == '.' -o "$APP_NAME" == '..' -o ! -d `dirname "$0"` ]
then
	APP_NAME="ROX-Menu"
fi
export APP_NAME

# Get choices path.  Adapted some choices.sh code to avoid using the
# Python code to find $APP_DIR, which is a bit slow on my machine.
CHOICESPATH=${CHOICESPATH:-"$HOME/Choices:usr/local/share/Choices:/usr/share/Choices"}
for dir in `echo $CHOICESPATH | tr ':' ' '` ; do
	if [ -d "$dir/$APP_NAME" ]; then
		CHOICES_DIR="$dir/$APP_NAME"
		export CHOICES_DIR
	fi
done

if [ -z $PID_FILE_PATH ] ; then
	PID_FILE_PATH=$CHOICES_DIR/pid.tmp
fi

# Kill stale PID files
OLDPID=`cat $PID_FILE_PATH 2> /dev/null`
HAS_OLD_PID=`ps -A | grep "$OLDPID .*$PROG"`
if [ -z "$HAS_OLD_PID" ] ; then
	rm -f "$PID_FILE_PATH"
fi

if [ "$1" == '--menu' ] ; then
	ROX_MENU_SIG=$SIGUSR1
elif [ "$1" == '--sys-menu' ] ; then
	ROX_MENU_SIG=$SIGUSR2
fi

if [ ! -z $ROX_MENU_SIG ] ; then
	export ROX_MENU_SIG
	if [ -r "$PID_FILE_PATH" ] ; then
		kill -s $ROX_MENU_SIG `cat "$PID_FILE_PATH"`
		exit 0
	else
		ROX_MENU_PID=` ps -A | grep 'ROX-Menu' | sed 's/ *\([0-9]*\).*/\1/'`
		if [ -n "$ROX_MENU_PID" ] ; then
			kill -s $ROX_MENU_SIG $ROX_MENU_PID
			exit 0
		fi
#		killall -s USR1 "$PROG"
	fi
fi 

if [ ! -L "$0" ] ; then
	APP_DIR=`dirname "$0"`
	APP_DIR=`cd "$APP_DIR";pwd`
else
	if [ -n `which readlink 2> /dev/null` ] ; then
		APP_DIR=`readlink -f "$0"`
		APP_DIR=`dirname "$APP_DIR"`
	# Some people won't have Python, or will have an old version.
	elif [ -n `(python -V 2>&1) | grep '\([2-9].[2-9]..\)\|\(3.1..\)'` ] 
	then
		APP_DIR=`python -c \
'import os.path, sys
apprun = os.path.realpath(sys.argv[1])
print os.path.dirname(apprun)' "$0"`
	# We may or may not have a problem.  Warn the user.
	else
		echo "$PROG: Can't get real path to AppRun!" 1>&2
	fi
fi
export APP_DIR

if [ -z "$LD_LIBRARY_PATH" ]; then
	LD_LIBRARY_PATH=`$APP_DIR/rox_run ROX-CLib --runtime`
else
	LD_LIBRARY_PATH=`$APP_DIR/rox_run ROX-CLib --runtime`:$LD_LIBRARY_PATH
fi
export LD_LIBRARY_PATH

ARCH=`uname -m`
case $ARCH in
	i?86) ARCH=ix86 ;;
esac

PLATFORM=`uname -s`-$ARCH
BIN="$APP_DIR/$PLATFORM"
DEBUGGER=""

case $1 in
	# Catch calls to other executables.
	# This is useful for general command-line use.
	--exec)
		EXEC_FILE_PATH="$APP_DIR/$PLATFORM/$2"
		if [ -x "$EXEC_FILE_PATH" ] ; then
			shift ; shift
			exec "$EXEC_FILE_PATH" "$@"
		else
			echo "Unable to find $EXEC_NAME in binary directory."
			echo "Use $0 --lsexec to list the existing binaries."
		fi
	;;
	# List the possible executables.
	--lsexec) ls "$APP_DIR/$PLATFORM" ;;
	# Let the user get at the Choices directory easily.
	--setup) rox $CHOICES_DIR ; exit 0 ;;
	# Delete the following if the application does not come with source.
	--debug) shift ; DEBUGGER=gdb ;;
	--compile)
		shift
		if [ ! -d "$APP_DIR/src" ] ; then
			echo "ERROR from $0:" >&2
			echo "Cannot compile - source code is missing!" >&2
			exit 1
		fi
		echo "Compiling $APP_DIR... please wait..." >&2
		if [ ! -x "$APP_DIR/src/configure" ]; then
		  echo "No 'configure' script! Trying to run autoconf..."
		  (cd "$APP_DIR/src"; autoconf)
		fi
		rm -f "$APP_DIR/src/config.cache"
		cd "$APP_DIR/src" \
			&& ./configure --bindir="$BIN" && make clean \
			&& make && make clean && echo Done >&2 && exit 0
		echo Compile failed >&2
		echo Press Return... >&2
		read WAIT
		exit 1
esac

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

BIN=$BIN/$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
