#!/bin/sh
#
#  Mathematica 6.0 Kernel command file
#  Copyright 1988-2007 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 SystemID=Unknown.
if [ -z "${SystemIDList}" ]; then
	case `uname -s` in
		AIX)
			SystemIDList="AIX-Power64";;
		HP-UX)
			SystemIDList="HPUX-PA64";;
		IRIX64)
			SystemIDList="IRIX-MIPS64";;
		Linux)
			case `uname -m` in
				ia64)
					SystemIDList="Linux-IA64";;
				i?86)
					SystemIDList="Linux";;
				x86_64)
					SystemIDList="Linux-x86-64 Linux";;
				*)
					SystemIDList="Unknown";;
			esac;;
		OSF1)
			SystemIDList="DEC-AXP";;
		SunOS)
			case `uname -m` in
				sun*)
					SystemIDList="Solaris-SPARC UltraSPARC";;
				i86pc)
					SystemIDList="Solaris-x86-64";;
				*)
					SystemIDList="Unknown";;
			esac;;
		*)
			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'`

#  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

for SystemID in $SystemIDList; do

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

    # Munge $PATH to eliminate multiple specification of the same directory.
    test_PATH=`echo "${PATH}" | sed -e 's/ /_SPACE_/g' | sed -e  's/:/__ __/g'`
    test_PATH="__${test_PATH}__"
    new_PATH=""
    for check_dir in $test_PATH; do
        check_dir_sed=`echo ${check_dir} | sed -e 's/\//\\\\\\//g'`
        new_PATH_check=`echo ${new_PATH} | sed -e 's/'${check_dir_sed}'//g'`
        if [ "${new_PATH_check}" = "${new_PATH}" ]; then
               new_PATH=`echo "${new_PATH} ${check_dir}" | sed -e 's/^ //g'`
        fi
    done
    new_PATH=`echo ${new_PATH} | sed -e 's/__ __/:/g' | sed -e 's/^__//g' | sed -e 's/__$//g' | sed -e 's/_SPACE_/ /g'`
    PATH="${new_PATH}"

    #  Reset $PATH to include Mathematica directories
    PATH="${TopDirectory}/${ScriptLeafDirectory}:${TopDirectory}/SystemFiles/Graphics/Binaries/${SystemID}:${PATH}"
    export PATH

    # Set up library paths
    M_LIBRARY_PATH="${TopDirectory}/SystemFiles/Libraries/${SystemID}"

    SED_LIBRARY_PATH=`echo "${M_LIBRARY_PATH}" | sed -e 's/\\//\\\\\//g'`

    if [ "${SystemID}" = "HPUX-PA64" ]; then
    SHLIB_PATH=`echo "${SHLIB_PATH}" | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'://g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"':/:/g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/::/:/g' | sed -e 's/:$//'`
    if [ -z "${SHLIB_PATH}" ]; then 
        SHLIB_PATH="${M_LIBRARY_PATH}"
    else
        SHLIB_PATH="${M_LIBRARY_PATH}:${SHLIB_PATH}"
    fi
    export SHLIB_PATH

    elif [ "${SystemID}" = "AIX-Power64" ]; then
    LIBPATH=`echo "${LIBPATH}" | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'://g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"':/:/g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/::/:/g' | sed -e 's/:$//'`
    if [ -z "${LIBPATH}" ]; then
        LIBPATH="${M_LIBRARY_PATH}"
    else            
        LIBPATH="${M_LIBRARY_PATH}:${LIBPATH}"
    fi      
    export LIBPATH

    elif [ "${SystemID}" = "IRIX-MIPS64" ]; then
    LD_LIBRARY64_PATH=`echo "${LD_LIBRARY64_PATH}" | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'://g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"':/:/g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/::/:/g' | sed -e 's/:$//'`
    if [ -z "${LD_LIBRARY64_PATH}" ]; then
        LD_LIBRARY64_PATH="${M_LIBRARY_PATH}"
    else
        LD_LIBRARY64_PATH="${M_LIBRARY_PATH}:${LD_LIBRARY64_PATH}"
    fi
    export LD_LIBRARY64_PATH

    elif [ "${SystemID}" = "UltraSPARC" ]; then
    LD_LIBRARY_PATH_64=`echo "${LD_LIBRARY_PATH_64}" | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'://g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"':/:/g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/::/:/g' | sed -e 's/:$//'`
    if [ -z "${LD_LIBRARY_PATH_64}" ]; then
        LD_LIBRARY_PATH_64="${M_LIBRARY_PATH}"
    else
        LD_LIBRARY_PATH_64="${M_LIBRARY_PATH}:${LD_LIBRARY_PATH_64}"
    fi
    export LD_LIBRARY_PATH_64
    fi
    
    LD_LIBRARY_PATH=`echo "${LD_LIBRARY_PATH}" | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'://g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"':/:/g' | \
        sed -e 's/:'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/^'"${SED_LIBRARY_PATH}"'$//g' | \
        sed -e 's/::/:/g' | sed -e 's/:$//'`
    if [ -z "${LD_LIBRARY_PATH}" ]; then
        LD_LIBRARY_PATH="${M_LIBRARY_PATH}"
    else
        LD_LIBRARY_PATH="${M_LIBRARY_PATH}:${LD_LIBRARY_PATH}"
    fi
    export LD_LIBRARY_PATH

    if [ "${SystemID}" = "Linux" -o "${SystemID}" = "Linux-x86-64" -o "${SystemID}" = "Linux-IA64" ]; then
        if [ -z "${OMP_NUM_THREADS}" ]; then
            OMP_NUM_THREADS=`cat /proc/cpuinfo | grep processor | wc -l | tr -d ' '`;
            export OMP_NUM_THREADS;
        fi
    fi
    if [ "${SystemID}" = "UltraSPARC" -o "${SystemID}" = "Solaris-x86-64" ]; then
        if [ -z "${OMP_NUM_THREADS}" ]; then
            OMP_NUM_THREADS=`/usr/sbin/psrinfo | wc -l | tr -d ' '`;
            export OMP_NUM_THREADS;
        fi
    fi
    if [ "${SystemID}" = "HPUX-PA64" -o "${SystemID}" = "HP-RISC" ]; then
        if [ -z "${MLIB_NUMBER_OF_THREADS}" ]; then
            MLIB_NUMBER_OF_THREADS=`/usr/sbin/ioscan -k -C processor | grep processor | wc -l`;
            export MLIB_NUMBER_OF_THREADS;
        fi
    fi

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

if [ -f /etc/redhat-release ]; then
  rhel_check=`cat /etc/redhat-release | awk '{print $1 " " $2 " " $3}'`
  if [ "${rhel_check}" = "Red Hat Enterprise" ]; then
    rhel_ver_check=`cat /etc/redhat-release | grep '(Taroon)'`
    if [ ! -z "${rhel_ver_check}" ]; then
        LD_ASSUME_KERNEL=2.4.1
        export LD_ASSUME_KERNEL
    fi
  fi
fi

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

exec "${MathKernel}" "$@" 
