#!@bash@

# This line will be automatically uncommented when you "make install"
#installed=1

if [ -z $installed ] ; then
    echo "*** Running uninstalled @target@ ***"

    # When we run uninstalled, run in the foreground by default.
    fg_default=1

    TARGET_EXE="./@target@"

    export _BEAGLED_INDEX_HELPER_PATH="."

    # The stuff in webserver, Tiles and images is only needed when
    # web services are enabled.
    export MONO_PATH="../Util:../BeagleClient:./webserver:../Tiles:../images${MONO_PATH+:$MONO_PATH}"

    export LD_LIBRARY_PATH="../glue/.libs${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}"
    
    export BEAGLE_FILTER_PATH="../Filters${BEAGLE_FILTER_PATH+:$BEAGLE_FILTER_PATH}"
    export BEAGLE_BACKEND_PATH=".${BEAGLE_BACKEND_PATH+:$BEAGLE_BACKEND_PATH}"

else

    # Otherwise default to running in the background
    fg_default=0

    TARGET_EXE="@pkglibdir@/@target@"

    export _BEAGLED_INDEX_HELPER_PATH="@pkglibdir@"
    export MONO_PATH="@pkglibdir@${MONO_PATH+:$MONO_PATH}"
    export LD_LIBRARY_PATH="@pkglibdir@${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}"
fi

EVO_LIBDIR="@evolibdir@"
if [ -n "$EVO_LIBDIR" ]; then
    export LD_LIBRARY_PATH="$EVO_LIBDIR${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}"
fi

monogrind=0
heap_buddy=0
debug=0
mdb=0

BEAGLED_ARGS="$*"
for i in $BEAGLED_ARGS; do
    case "$i" in
    --help | --list-backends | --fg | --foreground ) fg_user=1 ;;
    --bg | --background ) fg_user=0 ;;
    --monogrind ) monogrind=1 ;;
    --heap-buddy ) heap_buddy=1 ;;
    --mdb ) mdb=1 ;;
    --debug ) debug=1 ;;
    esac
done

# If the user didn't specify to run in fg/bg, then use the default
if [ -z "$fg_user" ]; then
    fg=$fg_default
else
    fg=$fg_user
fi

# Handle logging, and optionally append the relevent default fg/bg argument if
# it was not user-specified.
if [ $fg -eq 1 ]; then
    [ -z "$fg_user" ] && FGBG_ARG="--fg";
    export BEAGLE_LOG_IN_THE_FOREGROUND_PLEASE=1
else
    [ -z "$fg_user" ] && FGBG_ARG="--bg";
    unset BEAGLE_LOG_IN_THE_FOREGROUND_PLEASE
fi

if [ $heap_buddy -eq 1 ]; then
   export _GET_ON_THE_HEAP_BUDDY_BANDWAGON=1
      if [ -z "$BEAGLE_HEAP_BUDDY_DIR" ]; then
      export BEAGLE_HEAP_BUDDY_DIR=.
   fi
   MONO_EXTRA_ARGS="--profile=heap-buddy:$BEAGLE_HEAP_BUDDY_DIR/outfile.beagled $MONO_EXTRA_ARGS"
   rm -f $BEAGLE_HEAP_BUDDY_DIR/outfile.beagled $BEAGLE_HEAP_BUDDY_DIR/outfile.helper.*
fi

if [ $debug -eq 1 ]; then
   export BEAGLE_DEBUG_FLAG_IS_SET=1
   MONO_EXTRA_ARGS="--debug $MONO_EXTRA_ARGS"
fi

# The BEAGLE_MONO_RUNTIME variable can be used to specify the path
# to an alternative runtime.
if [ -z "$BEAGLE_MONO_RUNTIME" ]; then
   export BEAGLE_MONO_RUNTIME="mono"
else
   echo "*** Using mono runtime at $BEAGLE_MONO_RUNTIME ***"
fi

if [ $monogrind -eq 1 ]; then
    export GC_DONT_GC=1
    CMDLINE="monogrind $TARGET_EXE $MONO_EXTRA_ARGS $BEAGLED_ARGS $FGBG_ARG"
elif [ $mdb -eq 1 ]; then
    CMDLINE="mdb $TARGET_EXE -args $BEAGLED_ARGS $FGBG_ARG"
else
    CMDLINE="$BEAGLE_MONO_RUNTIME --debug $MONO_EXTRA_ARGS $TARGET_EXE $BEAGLED_ARGS $FGBG_ARG"
fi

PROCESS_NAME="beagled"

if [ $fg -eq 1 ]; then
    exec -a $PROCESS_NAME $CMDLINE
    exit 1
else
    exec -a $PROCESS_NAME $CMDLINE &
fi

# If beagled (in --bg mode) has exited with some errors, we need 
# to inform the users about the same. This 500 milli-seconds sleep
# is to wait for the daemon to start and exit, in case of errors ;)
# Fixes http://bugzilla.gnome.org/show_bug.cgi?id=171975

sleep 0.5

if ! ps -p $! > /dev/null; then
    echo "Beagle Daemon exited with errors.  See ~/.beagle/Log/current-Beagle for more details."
    exit 1
fi
