#!/bin/sh

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

MONO_FRAMEWORK_PATH=/Library/Frameworks/Mono.framework/Versions/Current
export DYLD_FALLBACK_LIBRARY_PATH="$MONO_FRAMEWORK_PATH/lib:/lib:/usr/lib:$DYLD_FALLBACK_LIBRARY_PATH"

#we need lots of file handles
ulimit -n 1024

#prevent Macports from messing up mono and pkg-config
export PATH="$MONO_FRAMEWORK_PATH/bin:$PATH"

DIR=$(cd "$(dirname "$0")"; pwd)

EXE_PATH="$DIR/lib/monodevelop/bin/mdtool.exe"

#use stuff bundled into the app bundle
export MONO_GAC_PREFIX="$DIR:$MONO_GAC_PREFIX"
export PATH="$DIR/bin:$PATH"
export DYLD_FALLBACK_LIBRARY_PATH="$DIR/lib:$DYLD_FALLBACK_LIBRARY_PATH"
export PKG_CONFIG_PATH="$DIR/lib/pkgconfig:$DIR/share/pkgconfig:$PKG_CONFIG_PATH"

# The Mono "External" folder allows users (such as MonoDroid) to install
# things without worrying that they'll be removed next time mono is upgraded
# Mono doesn't support it get so we hack it via env vars for now.
MONO_FRAMEWORK_EXT=/Library/Frameworks/Mono.framework/External
export PKG_CONFIG_PATH="$MONO_FRAMEWORK_EXT/pkgconfig:$PKG_CONFIG_PATH"
export MSBuildExtensionsPath="$MONO_FRAMEWORK_EXT/xbuild:$MSBuildExtensionPath"
# N.B. MONO_FRAMEWORK_EXT/monodoc is handled inside MD itself
# N.B.  no point in hacking MONO_FRAMEWORK_EXT/gac because it's for runtime

## use an app-specific registry if possible
#if [ -w "$DIR" ]; then
#	export MONO_ADDINS_REGISTRY="$DIR"
#fi

## Moonlight SDK
if [ -d "$DIR/lib/moonlight" ]; then
	export MOONLIGHT_SDK_PATH="$DIR/lib/moonlight"
fi

### END MAC-SPECIFIC SETUP
### Beyond this, all is from the Linux mdtool.in. Only the values of EXE_PATH and MONO_EXEC differ

# NOTE: remove this for stable releases
if [ -z "$MD_NO_DEBUG" ]; then
	_MONO_OPTIONS=${MONO_OPTIONS:---debug}
else
	_MONO_OPTIONS=$MONO_OPTIONS
fi

exec "$DIR/bin/mdtool" $_MONO_OPTIONS "$EXE_PATH" $*

