#!/bin/sh
#
# Need to keep synchronized with AppRun

PROG=freefs

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

$APP_DIR/choice_install FreeFS $APP_DIR/gtkrc gtkrc

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

PLATFORM=`uname -s`-$ARCH

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

case $1 in
    --compile)
	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=$APP_DIR/$PLATFORM \
			&& make clean && make BINDIR=$APP_DIR/$PLATFORM \
			&& echo Done >&2 && exit 0
	echo Compile failed >&2
	echo Press Return... >&2
	read WAIT
	exit 1
esac

BIN=$APP_DIR/$PLATFORM/$PROG

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