#!/bin/sh
#
#  MathReader 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 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";;
		IRIX)
			SystemIDList="SGI";;
		Linux)
			case `uname -m` in
				alpha)
					SystemIDList="Linux-AXP";;
				ia64)
					SystemIDList="Linux-IA64";;
				i?86)
					SystemIDList="Linux";;
				x86_64)
					SystemIDList="Linux-x86-64";;
				*)
					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}.
if `exec test -L "${Script}"`; then
	LinkScriptDirectory=`dirname "${Script}"`
	LinkTarget=`ls -l "${Script}" | sed -e 's/.*-> //g'`
	LinkTargetDirectory=`dirname "${LinkTarget}"`

	ScriptDirectory=`cd "${LinkScriptDirectory}"; \
		cd "${LinkTargetDirectory}"; pwd`
else
	ScriptDirectory=`dirname "${Script}"`
fi

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

#  Reset $PATH to include MathReader 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} 2> /dev/null
	done
fi
xset fp rehash ${userDisplay}

for SystemID in $SystemIDList; do
	MathematicaPath="${TopDirectory}/SystemFiles/FrontEnd/Binaries/${SystemID}"
	MathematicaFE="${MathematicaPath}/MathReader"
	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}/MathReader"
		echo "MathReader front end executable"
		echo "${MathematicaFE}"
		echo "not found. Your MathReader installation may be incomplete"
		echo "or corrupted."
	done
    	exit 1
fi

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