#!/bin/sh

if [ "$#" = 0 -o "x$1" = "x--install" ]; then
  exec `dirname "$0"`/interactive.py "$@"
fi

# Use 0 in the next 2 lines if you want anti-aliased fonts turned off,
# or 1 to turn them on.
GDK_USE_XFT=1; export GDK_USE_XFT
QT_XFT=1; export QT_XFT

PROG=ROX-Session

APP_DIR=`dirname "$0"`
APP_DIR=`cd "$APP_DIR";pwd`; export APP_DIR

if [ "x$PLATFORM" = x ]; then
	ARCH=`uname -m`
	case "$ARCH" in
		i?86) ARCH=ix86 ;;
	esac

	PLATFORM=`uname -s`-"$ARCH" export PLATFORM
fi

DEBUGGER=""

case $1 in
	--debug) shift ; DEBUGGER=gdb ;;
	--valgrind) shift ; DEBUGGER=valgrind ;;
	--setup) exec $APP_DIR/Setup ;;
	--options) dbus-send --print-reply --type=method_call --dest=net.sf.rox.Session /Session net.sf.rox.Session.Control.ShowOptions > /dev/null; exit 0;;
	--messages) dbus-send --print-reply --type=method_call --dest=net.sf.rox.Session /Session net.sf.rox.Session.Control.ShowMessages > /dev/null; exit 0;;
	--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 --enable-rox	    \
					--with-platform="$PLATFORM" \
					"$@" \
			&& make clean && make && echo Done >&2 && exit 0
		echo Compile failed >&2
		echo Press Return... >&2
		read WAIT
		exit 1
esac

if [ -z "$XDG_DATA_DIRS" -a -d /uri/0install/zero-install.sourceforge.net ]; then
  # Try to get MIME database through Zero Install, if possible
  0refresh zero-install.sourceforge.net/share/mime 2003-08-07
  XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/uri/0install/zero-install.sourceforge.net/share
  export XDG_DATA_DIRS
fi

BIN="$APP_DIR/$PLATFORM/$PROG"

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