#!/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/mdrun.exe"

PKG_CONFIG_PATH="$DIR/lib/pkgconfig:$PKG_CONFIG_PATH"
export PKG_CONFIG_PATH

# 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 monodevelop mono"
fi

# 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

_MONO_OPTIONS=$MONO_OPTIONS

$MONO_EXEC $_MONO_OPTIONS "$EXE_PATH" $*

