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

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




#  Determine the SystemID by examining the output of `uname -s` and 
#  `uname -m`. Failsafe to SystemIDList=Unknown.
if [ -z "${SystemIDList}" ]; then
	case `uname -s` in
		AIX)
			SystemIDList="AIX-Power64 IBM-RISC";;
		HP-UX)
			SystemIDList="HPUX-PA64 HP-RISC";;
		IRIX64)
			SystemIDList="IRIX-MIPS64";;
		Linux)
			case `uname -m` in
				alpha)
					SystemIDList="Linux-AXP";;
				ia64)
					SystemIDList="Linux-IA64";;
				i?86)
					SystemIDList="Linux";;
				x86_64)
					SystemIDList="Linux-x86-64 Linux";;
				*)
					SystemIDList="Unknown";;
			esac;;
		OSF1)
			SystemIDList="DEC-AXP";;
		SunOS)
			SystemIDList="UltraSPARC Solaris";;
		*)
			SystemIDList="Unknown";;
	esac
fi

#  Find the full pathname and name of this script.
Script="${0}"
ProgramName=`basename "${0}"`

#  If ${SystemIDList} = Unknown, print an error message and exit
if [ "${SystemIDList}" = "Unknown" ]; then
	echo "${ProgramName} cannot determine operating system."
	exit 1
fi

#  Test to see if the script is being called via a symlink, if so, examine
#  the output of ls -l on the symlink to find the link target. Reset
#  ${Script}.
LinkDirectory=`pwd`
while `exec test -L "${Script}"`; do
	ScriptDirectory=`dirname "${Script}"`
	Script=`ls -l "${Script}" | sed -e 's/.*-> //g'`
	cd "${ScriptDirectory}"
done
ScriptDirectory=`dirname "${Script}"`
ScriptDirectory=`cd "${ScriptDirectory}"; pwd`
cd "${LinkDirectory}"

#  Determine $TopDirectory by removing "Executables" from
#  "${ScriptDirectory}".
ScriptLeafDirectory="Executables"
TopDirectory=`cd "${ScriptDirectory}"; pwd | \
	sed -e 's/\/'"${ScriptLeafDirectory}"'//g'`

#  Reset $PATH to include Mathematica and X11 directories
if [ -d "/usr/bin/X11" ]; then
    PATH="${TopDirectory}/${ScriptLeafDirectory}:/usr/bin/X11:${PATH}"
fi
if [ -d "/usr/openwin/bin" ]; then
    PATH="${TopDirectory}/${ScriptLeafDirectory}:/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}

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

if [ ! "${fail}" = "0" ] ; then
	for SystemID in $SystemIDList; do
		MathematicaPath="${TopDirectory}/SystemFiles/FrontEnd/Binaries/${SystemID}"
		MathematicaFE="${MathematicaPath}/Mathematica"
		echo "Mathematica front end executable"
		echo "${MathematicaFE}"
		echo "not found. Your Mathematica installation may be incomplete"
		echo "or corrupted."
	done
    	exit 1
fi

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