#!/bin/sh

# Author: Marc Christensen (mchristensen@novell.com)
#         Michael Hutchinson (mhutchinson@novell.com)

# fetch the path relative to the launch point where this shell script exists. (taken from macpack)
# $0 should contain the full path from the root i.e. /Applications/<folder>.app/Contents/MacOS/<script>
APP_PATH=`echo $0 | awk '{split($0,patharr,"/"); idx=1; while(patharr[idx+3] != "") { if (patharr[idx] != "/") {printf("%s/", patharr[idx]); idx++ }} }'`

MONO_FRAMEWORK_PATH=/Library/Frameworks/Mono.framework/Versions/Current
export DYLD_FALLBACK_LIBRARY_PATH=$APP_PATH/Contents/MacOS/lib/monodevelop/bin:$MONO_FRAMEWORK_PATH/lib:/lib:/usr/lib

PREFIX="$APP_PATH/Contents/MacOS"
export PREFIX

# Work around a bug in 'exec' in older versions of macosx
OSX_VERSION=$(uname -r | cut -f1 -d.)
if [ $OSX_VERSION -lt 9 ]; then  # If OSX version is 10.4
   MONO_EXEC="mono"
else
   MONO_EXEC="exec -a mdtool mono"
fi

## END MAC-SPECIFIC SETUP ##
### Beyond this, all is from the Linux mdtool.in, except the PREFIX and MONO_EXEC tweaks

EXE_PATH="$PREFIX/lib/monodevelop/bin/mdrun.exe"

MD_FORCE_DEBUG=yes

for arg in "$@"; do 
case x$arg in
	x--profile*)
		MONO_OPTIONS+=("$arg")
		shift
		;;
	x--debug*)
		export MONODEVELOP_DISPATCH_DEBUG=yes
		unset MD_FORCE_DEBUG
		MONO_OPTIONS+=("$arg")
		shift
		;;
	x--trace*)
		MONO_OPTIONS+=("$arg")
		shift
		;;
	x--no-debug)
		unset MD_FORCE_DEBUG
		shift
		;;
	*)
		ARGS+=("$arg")
		shift
		;;
esac		
done

if [ -z MD_FORCE_DEBUG ]; then
	MONO_OPTIONS+=("--debug")
fi

if [ -n "${MONO_OPTIONS[0]}" ]; then
	echo "** Running with Mono options: ${MONO_OPTIONS[@]} **"
fi

$MONO_EXEC "${MONO_OPTIONS[@]}" "$EXE_PATH" "${ARGS[@]}"

