#!/bin/sh

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

if [ -z $installed ] ; then
    echo "*** Running uninstalled BeagleDaemon.exe ***"

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

    TARGET_EXE="./BeagleDaemon.exe"

    export _BEAGLED_INDEX_HELPER_PATH="."

    export MONO_PATH="../Util:../BeagleClient${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}"

    if [ -z $BEAGLE_WEBSERVER_DIR ] ; then
	    export BEAGLE_WEBSERVER_DIR="./webinterface"
    fi

    if [ -z $BEAGLE_CONF_DIR ]; then
	    export BEAGLE_CONF_DIR="../tools/config-files"
    fi

else

    # Otherwise default to running in the background
    fg_default=0

    TARGET_EXE="/usr/lib64/beagle/BeagleDaemon.exe"

    export _BEAGLED_INDEX_HELPER_PATH="/usr/lib64/beagle"
    export MONO_PATH="/usr/lib64/beagle${MONO_PATH+:$MONO_PATH}"
    export LD_LIBRARY_PATH="/usr/lib64/beagle${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}"
fi

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

monogrind=0
heap_buddy=0
heap_shot=0
mono_debug=0
mdb=0

BEAGLED_ARGS="$*"
for i in $BEAGLED_ARGS; do
    case "$i" in
    --help | --list-backends | --fg | --foreground | --version) fg_user=1 ;;
    --bg | --background ) fg_user=0 ;;
    --monogrind ) monogrind=1 ;;
    --heap-buddy ) heap_buddy=1 ;;
    --heap-shot ) heap_shot=1 ;;
    --mdb ) mdb=1 ;;
    --mono-debug ) mono_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.*
elif [ $heap_shot -eq 1 ]; then
   export _HEY_LETS_DO_A_HEAP_SHOT=1
   if [ -z "$BEAGLE_HEAP_SHOT_DIR" ]; then
      export BEAGLE_HEAP_SHOT_DIR=.
   fi
   MONO_EXTRA_ARGS="--profile=heap-shot:$BEAGLE_HEAP_SHOT_DIR/outfile.beagled $MONO_EXTRA_ARGS"
   rm -f $BEAGLE_HEAP_SHOT_DIR/outfile.beagled_* $BEAGLE_HEAP_SHOT_DIR/outfile.helper_*
fi



if [ $mono_debug -eq 1 ]; then
   export BEAGLE_MONO_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 -args $TARGET_EXE $BEAGLED_ARGS $FGBG_ARG"
else
    CMDLINE="$BEAGLE_MONO_RUNTIME $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
