#!/bin/sh

PROG=OroboROX

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

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

if [ x"$XDG_CONFIG_HOME" = x"" ] ; then
	if [ x"$CHOICESPATH" = x"" ] ; then
		XDG_CONFIG_HOME="$HOME/.config"
	else
		XDG_CONFIG_HOME=`echo $CHOICESPATH | sed s/:.*//`
	fi
fi

OROBOROX_CONFIG_HOME="$XDG_CONFIG_HOME/$PROG"
export OROBOROX_CONFIG_HOME

if [ ! -d "$OROBOROX_CONFIG_HOME" ] ; then
	mkdir -p "$OROBOROX_CONFIG_HOME"
fi

if [ ! -f "$OROBOROX_CONFIG_HOME/current_theme" ] ; then
	echo "$APP_DIR/Configure/Themes/MicroGUI" > "$OROBOROX_CONFIG_HOME/current_theme"
fi

BIN="$APP_DIR/$PLATFORM/$PROG"
DEBUGGER=""
DSP="$DISPLAY"

case $1 in
	--options) exec 0launch http://rox.sourceforge.net/2005/interfaces/ROX-Filer "$APP_DIR/Configure" ;;	
	--reload) exec kill -HUP `xprop -root _NET_WM_PID | cut -d' ' -f3` ;;
	--DDD) shift ;
		cd "$APP_DIR"
  		kill `pidof OroboROX` && exec rox_run DDD "$BIN"
	;;
	--debug-d1) shift ;
		if [ "$#" = 0 ] ; then
		 DSP=:1.0
		  DEBUGGER="gdb --args" ; DEBUG_OPTIONS="-n --g-fatal-warnings"
		else
		  DEBUGGER="gdb"
		fi ;;

	--debug) shift ;
		if [ "$#" = 0 ] ; then
		  DEBUGGER="gdb --args" ; DEBUG_OPTIONS="-n --g-fatal-warnings"
		else
		  DEBUGGER="gdb"
		fi ;;
	--valgrind) shift ; DEBUGGER="valgrind --tool=memcheck --num-callers=8";;
	--coregrind) shift ; DEBUGGER="valgrind --tool=corecheck -v";;
	--coregrind-d1) shift ; DEBUGGER="valgrind --tool=corecheck -v" DSP=:1.0;;
	--leaks) shift ; DEBUGGER="valgrind --tool=memcheck --num-callers=8 --leak-check=yes --show-reachable=yes";;
	--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"
		[ ! -d "$APP_DIR/=build" ] && mkdir "$APP_DIR/=build"
		
		CFLAGS="-Os"
		COMPILER=`cc -dumpversion` 2>/dev/null	
		if [ "$ARCH" = ix86 ] && [ ! x"$COMPILER" = x ] ; then			
			MAJOR=${COMPILER%.?.?}
			if [ "$MAJOR" = 4 ] ; then
				CFLAGS="$CFLAGS -march=i486 -mtune=i686 -m32"
			else
				CFLAGS="$CFLAGS -march=i486 -mcpu=i686 -m32"
			fi
		fi
		export CFLAGS

		cd "$APP_DIR/=build" && "$APP_DIR/src/configure" --enable-rox --disable-startup-notification --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
  XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/uri/0install/zero-install.sourceforge.net/share
  export XDG_DATA_DIRS
fi



if [ -x "$BIN" ]; then
  cd "$APP_DIR"
  DEBUG_OPTIONS=
  exec $DEBUGGER "$BIN" $DEBUG_OPTIONS -d $DSP "$@"
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
  	cd "$APP_DIR"
    exec "$BIN" "$@"
  fi
  exit 1
fi
