#! /bin/sh

prefix=/usr
exec_prefix=${prefix}

# %%$@%$# why oh why isn't it $sharedir/monodoc
monodocdir=$prefix/lib/monodoc

case x$1 in
    x--assemble)
	shift;
	echo "--assemble is depreciated. Use mdassembler." 1>&2
	exec mdassembler "$@"
	;;
    x--get-sourcesdir)
	echo "--get-sourcesdir is depreciated. Use pkg-config monodoc --variable=sourcesdir." 1>&2
	exec pkg-config monodoc --variable=sourcesdir
	;;
    x--normalize)
	shift;
	echo "--normalize is depreciated. Use mdnormalizer." 1>&2
	exec mdnormalizer "$@"
	;;
    x--update)
	echo "$0 --update does not work.  Use 'monodocer' instead." 1>&2
	exit 1
	;;
    x--cs-to-ecma)
	shift;
	echo "--cs-to-ecma is depreciated. Use mdcs2ecma." 1>&2
	exec mdcs2ecma "$@"
	;;
    x--validate)
	shift;
	echo "--validator is depreciated. Use mdvalidater." 1>&2
	exec mdvalidater "$@"
	;;
    x--help)
	echo "Usage is:"
	echo "monodoc [options]"
	echo ""
	echo "where [options] is one of the following:"
	echo "  TOPIC              Start the browser at TOPIC"
	echo "      (ex. N:System, T:System.Object, M:System.Object.Equals,"
	echo "      and P: for properties, F: for fields, E: for events, etc.)"
	echo "  --help             	Print this message"
	echo "  --about            	About the monodoc browser"
	echo "  --html TOPIC       	Print the HTML contents of TOPIC"
	echo "  --make-index       	Create the documentation index"
 	echo "  --make-search-index	Create the searchable documentation index"
	echo "          	Don't use Mozilla to render the contents"
	echo
	echo "The following options are available for authoring documentation:"
	echo "  --assemble         Runs the doc assembler"
	echo "  --cs-to-ecma DOCFILE ASSEMBLY OUTPUT LIBRARY-NAME"
	echo "                     Converts C# documentation to Monodoc format"
	echo "  --edit path        Edit (unassembled) documentation at path"
	echo "  --get-sourcesdir   Prints the location of monodoc sources"
	echo "  --merge-changes CHANGE_FILE [TARGET_DIR]"
	echo "  --normalize        Format a documentation file consistently"
	echo "  --validate         Validate the XML files"
	exit 0
	;;
esac

getdirectory () {
    w=`which $1`
    if test -h $w; then
	(cd `dirname $w`/`dirname \`readlink $w\``; pwd)
    else
	dirname $w
    fi
}

if test -n "$MOZILLA_FIVE_HOME"; then
	MOZILLA_HOME=$MOZILLA_FIVE_HOME
elif test x != x; then
    if [ -f /chrome/comm.jar ]; then
        MOZILLA_HOME=
    fi 
elif grep GRE_PATH /etc/gre.d/*.conf > /dev/null ; then
	MOZILLA_HOME=$(grep -h GRE_PATH= /etc/gre.d/*.conf | cut -d '"' -f 2 -d = | head -n 1)
elif [ $(which xulrunner 2> /dev/null) ] > /dev/null ; then
    MOZILLA_FIVE_HOME=`getdirectory xulrunner`
    MOZILLA_HOME=$MOZILLA_FIVE_HOME
elif [ $(which mozilla 2> /dev/null) ] && grep MOZILLA_FIVE_HOME= "$(which mozilla)" > /dev/null ; then
	MOZILLA_HOME=$(grep MOZILLA_FIVE_HOME= $(which mozilla) | cut -d '"' -f 2)
elif [ $(which firefox 2> /dev/null) ] && grep MOZILLA_FIVE_HOME= "$(which firefox)" > /dev/null ; then
	MOZILLA_HOME=$(grep MOZILLA_FIVE_HOME= $(which firefox) | cut -d '"' -f 2)
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 
export MOZILLA_HOME 
export LD_LIBRARY_PATH

exec /usr/bin/mono $monodocdir/browser.exe $OPTIONS "$@" $REST
