#!/bin/sh
#
#  Mathematica 5.0 Kernel 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"

#  Set a few necessary enviroment variables
M_XFILESEARCHPATH="${TopDirectory}/SystemFiles/Graphics/TextResources/English/%N"
if [ -z "${XFILESEARCHPATH}" ]; then
	XFILESEARCHPATH="${M_XFILESEARCHPATH}"
else
	XFILESEARCHPATH="${M_XFILESEARCHPATH}:${XFILESEARCHPATH}"
fi
export XFILESEARCHPATH

M_PSRESOURCEPATH="${TopDirectory}/SystemFiles/Graphics/SystemResources:${TopDirectory}/SystemFiles/Fonts/Type1"
if [ -z "${PSRESOURCEPATH}" ]; then
    PSRESOURCEPATH="${M_PSRESOURCEPATH}"
else
    PSRESOURCEPATH="${PSRESOURCEPATH}:${M_PSRESOURCEPATH}"
fi
export PSRESOURCEPATH

# Check for MathKernel, fail if the binary cannot be found.
KernelPath="/opt/mathematica/SystemFiles/Kernel/Binaries/Linux"
MathKernel="${KernelPath}/MathKernel"
if [ ! -x "${MathKernel}" ]; then
    fail=1
    continue
else
    fail=0
fi

#  Reset $PATH to include Mathematica directories
PATH="/opt/mathematica/lib/bin:${PATH}"
export PATH

M_LD_LIBRARY_PATH="/opt/mathematica/lib"
if [ -z "${LD_LIBRARY_PATH}" ]; then
    LD_LIBRARY_PATH="${M_LD_LIBRARY_PATH}"
else
    LD_LIBRARY_PATH="${M_LD_LIBRARY_PATH}:${LD_LIBRARY_PATH}"
fi
export LD_LIBRARY_PATH

if [ -z "${OMP_NUM_THREADS}" ]; then
    OMP_NUM_THREADS=`cat /proc/cpuinfo | grep processor | wc -l | tr -d ' '`;
    export OMP_NUM_THREADS;
fi



if [ "${fail}" = "0" ]; then
    break
fi

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

exec "${MathKernel}" "$@" 
