#!/bin/sh
#
#  Mathematica 5.0 Front End command file
#  Copyright 1988-2003 Wolfram Research, Inc.

#  Make certain that ${PATH} includes /usr/bin and /bin
PATH="/usr/bin:/bin:${PATH}"

#  Determine $TopDirectory
TopDirectory="/opt/mathematica"

#  Reset $PATH to include Mathematica and X11 directories
if [ -d "/usr/bin/X11" ]; then
    PATH="/usr/bin/X11:${PATH}"
fi
if [ -d "/usr/openwin/bin" ]; then
    PATH="/usr/openwin/bin:${PATH}"
fi
export PATH

#  Determine which X display is being used.
userDisplay=`echo "$@" | \
    sed 's/.*-display \{1,\}.*:[^ ]\{1,\}.*/setting user display/'`
if [ "${userDisplay}" = "setting user display" ]
then
    userDisplay=`echo "$@" | sed 's/.*\(-display[ ]*[^ ]*\).*/\1/'`
else
    userDisplay=""
fi

#  Check to see if the Mathematica fonts are already being served by
#  the Xserver or xfs
HaveFonts=`xlsfonts -fn '-wri-Mathematica*-*-*-*-*-*-*-*-*-*-*-*-*' 2>&1 \
    ${userDisplay} | grep -c "wri"`

if [ ${HaveFonts} -lt 2 ]; then
	WRIFonts="${TopDirectory}/SystemFiles/Fonts/Type1 \
	${TopDirectory}/SystemFiles/Fonts/BDF"
	for Font in $WRIFonts; do
		xset fp+ "${Font}" ${userDisplay}
	done
fi
xset fp rehash ${userDisplay}

MathematicaPath="${TopDirectory}/SystemFiles/FrontEnd/Binaries/Linux"
MathematicaFE="${MathematicaPath}/Mathematica"
if [ ! -x "${MathematicaFE}" ]; then
	fail=1
	continue
else
	fail=0
	break
fi

if [ ! "${fail}" = "0" ] ; then
	echo "Mathematica front end executable"
	echo "${MathematicaFE}"
	echo "not found. Your Mathematica installation may be incomplete"
	echo "or corrupted."
    exit 1
fi

exec "${MathematicaFE}" -topDirectory "${TopDirectory}" "$@"
