#!/bin/bash

#this script should be in $PREFIX/bin
PREFIX="${0%%/bin/monodevelop}"
MONO_EXEC="exec -a monodevelop mono"
EXE_PATH="$PREFIX/lib/monodevelop/bin/MonoDevelop.exe"

MD_FORCE_DEBUG=yes
MD_REDIRECT_LOG="${XDG_CONFIG_HOME:-$HOME/.config}/MonoDevelop/log"

#############################
## BEGIN MOZILLA DETECTION ##
#############################

#(NOTE: any changes to this block should be kept in sync with the one in configure.in)
MOZFILE="libgtkembedmoz.so"
find_libgtkembedmoz ()
{
	MOZDIR=$MOZILLA_FIVE_HOME
	if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
	
	MOZDIR=$(grep -h GRE_PATH= /etc/gre.d/*.conf 2>/dev/null | cut -d '"' -f 2 -d = | head -n 1) #"
	if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
	
	mozilla_script=$(which mozilla 2> /dev/null)
	firefox_script=$(which firefox 2> /dev/null)
	
	if [ -z $mozilla_script ] && [ -z $firefox_script ]; then return; fi
	for runtime_script in "$firefox_script $mozilla_script"; do
		MOZDIR=$(grep "MOZ_DIST_LIB=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
		if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
		MOZDIR=$(grep "MOZILLA_FIVE_HOME=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
		if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
		MOZDIR=$(grep "MOZILLA_LIBDIR=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
		if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
		MOZDIR=$(grep "moz_libdir=" $runtime_script 2> /dev/null | cut -d '"' -f 2 | cut -d '=' -f 2)
		if test -e "$MOZDIR/$MOZFILE"; then echo $MOZDIR; return; fi
	done
}

if test -e "/$MOZFILE"; then
	MOZILLA_HOME=""
else
	MOZILLA_HOME=`find_libgtkembedmoz`
	if test ! -e "$MOZILLA_HOME/$MOZFILE"; then
		MOZILLA_HOME=
		echo "WARNING: Cannot find Mozilla directory containing $MOZFILE. Some Addins may not be able to function. Please set MOZILLA_FIVE_HOME to your Mozilla directory."
	fi
fi

if [ -n $LD_LIBRARY_PATH ]; then
	export LD_LIBRARY_PATH="$MOZILLA_HOME:$LD_LIBRARY_PATH"
else
	export LD_LIBRARY_PATH="$MOZILLA_HOME"
fi

export MOZILLA_FIVE_HOME="$MOZILLA_HOME"

###########################
## END MOZILLA DETECTION ##
###########################

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-log)
		unset MD_REDIRECT_LOG
		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

if [ -n "$MD_REDIRECT_LOG" ]; then
	mkdir -p `dirname "$MD_REDIRECT_LOG"`
	$MONO_EXEC "${MONO_OPTIONS[@]}" "$EXE_PATH" "${ARGS[@]}" 2>&1 | tee "$MD_REDIRECT_LOG"
else
	$MONO_EXEC "${MONO_OPTIONS[@]}" "$EXE_PATH" "${ARGS[@]}"
fi
