#!/usr/bin/env bash

prefix=@prefix@
libdir=@expanded_libdir@
exec_asm="Banshee.exe"
MONO_EXE="@expanded_libdir@/@PACKAGE@/$exec_asm"
BANSHEE_EXEC_NAME=$(basename $0)
BANSHEE_CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/banshee-1"

export LD_LIBRARY_PATH=@expanded_libdir@/@PACKAGE@:@expanded_libdir@/@PACKAGE@/Extensions:@expanded_libdir@/@PACKAGE@/Backends:@expanded_libdir@${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export GST_PLUGIN_PATH=@expanded_libdir@/@PACKAGE@/gstreamer-0.10${GST_PLUGIN_PATH:+:$GST_PLUGIN_PATH}
if [ $BANSHEE_EXEC_NAME = "muinshee" ]; then
    BANSHEE_CLIENT="Muinshee"
    export MONO_PATH=@expanded_libdir@/@PACKAGE@/Extensions
fi

[ ${BANSHEE_EXEC_NAME%%-1} = "banshee" ] && BANSHEE_EXEC_NAME="banshee-1"
[ -n "$BANSHEE_DEBUG" -o -f "${BANSHEE_CONFIG_DIR}/always-debug" ] && BANSHEE_DEBUG="--debug"
[ -n "$BANSHEE_TRACE" ] && BANSHEE_TRACE="--trace=$BANSHEE_TRACE"
[ -n "$BANSHEE_PROFILE" ] && BANSHEE_PROFILE="--profile=$BANSHEE_PROFILE"

for arg in $*; do
    case "x--debug" in ("x$arg")
        BANSHEE_DEBUG=$arg
    esac

    case "x--trace=" in ("x${arg:0:8}")
        BANSHEE_TRACE=$arg
    esac

    case "x--profile=" in ("x${arg:0:10}")
        BANSHEE_PROFILE=$arg
    esac

	case "x--redirect-log" in ("x$arg")
		[ -z "$(pidof $BANSHEE_EXEC_NAME)" ] && BANSHEE_REDIRECT_LOG="${BANSHEE_CONFIG_DIR}/log"
	esac

	case "x--client=" in ("x${arg:0:9}")
		BANSHEE_CLIENT="${arg:9}"
	esac
done

if [ ! -z "$BANSHEE_CLIENT" ]; then
	BANSHEE_CLIENT="--client=${BANSHEE_CLIENT}"
fi

# We are testing the SGen compacting GC
export MONO_ENV_OPTIONS="--gc=sgen"

if [ -n "$BANSHEE_DEBUG" -o -n "$BANSHEE_TRACE" -o -n "$BANSHEE_PROFILE" ]; then
    MONO_OPTIONS="$BANSHEE_DEBUG $BANSHEE_TRACE $BANSHEE_PROFILE"
    echo "** Running Mono with $MONO_OPTIONS **"
fi

# Finally - environment is set up, time to run our beloved
exec_args="-a $BANSHEE_EXEC_NAME mono $MONO_OPTIONS $MONO_EXE $BANSHEE_DEBUG $BANSHEE_CLIENT"

if [ -z "$BANSHEE_REDIRECT_LOG" ]; then
	exec $exec_args "$@"
else
	mkdir -p `dirname "$BANSHEE_REDIRECT_LOG"`
	(echo "exec $exec_args " "$@"; echo; exec $exec_args "$@") &> $BANSHEE_REDIRECT_LOG
fi


