#!/bin/sh -f

# Special settings to run MuPAD without any local modifications

# needed on Zaurus PDA
if test ! \( -x /bin/dirname -o -x /usr/bin/dirname \) ; then
  dirname()
  {
    echo ${1%/`basename $1`}
  }
fi

SHARE_BIN=`dirname $0`

if test `basename $SHARE_BIN` = bin  ; then
  SHARE=`dirname $SHARE_BIN`
  if test `basename $SHARE` = share ; then
    MuPAD_ROOT_PATH=`dirname $SHARE`
  else
    MuPAD_ROOT_PATH=..
  fi
else
  MuPAD_ROOT_PATH=../..
fi
export MuPAD_ROOT_PATH

SYSINFO=`$MuPAD_ROOT_PATH/share/bin/sysinfo`
MuPAD_BIN=$MuPAD_ROOT_PATH/$SYSINFO/bin

if test -n "$LD_LIBRARY_PATH" ; then
   LD_LIBRARY_PATH=${MuPAD_ROOT_PATH}/${SYSINFO}/lib:${LD_LIBRARY_PATH}
else
   LD_LIBRARY_PATH=${MuPAD_ROOT_PATH}/${SYSINFO}/lib
fi
export LD_LIBRARY_PATH

if test -n "$PATH" ; then
   PATH=$MuPAD_ROOT_PATH/share/bin:${MuPAD_BIN}:$PATH
else
   PATH=$MuPAD_ROOT_PATH/share/bin:${MuPAD_BIN}
fi
export PATH

# This Bourne or Korn Shell script runs MuPAD with a TTY interface.

# These are the directories in which you installed MuPAD

# MuPAD_ROOT_PATH is the root directory of the MuPAD-System
# MuPAD_MOD       is the directory of MuPAD dynamic modules
# MuPAD_BIN       is the directory of the MuPAD programms
# MuPAD_MOD       is the directory of MuPAD modules
# MuPAD_LIB       is the root directory of the MuPAD-Library
# MuPAD_HELP	  is the directory of the Help dependent stuff

MuPAD_MOD=$MuPAD_ROOT_PATH/$SYSINFO/modules
MuPAD_LIB=$MuPAD_ROOT_PATH/share/lib

SHORT_LANG=`echo ${LANG}|tr 'A-Z' 'a-z'|cut -b -2`
test -z "$SHORT_LANG" && SHORT_LANG=`echo ${LC_MESSAGES}|tr 'A-Z' 'a-z'|cut -b -2`
test -z "$SHORT_LANG" && SHORT_LANG=`echo ${LC_ALL}|tr 'A-Z' 'a-z'|cut -b -2`
test -z "$SHORT_LANG" && SHORT_LANG=`echo ${LANGUAGE}|tr 'A-Z' 'a-z'|cut -b -2`
# at the moment only german is supported
export SHORT_LANG

#  use TAR file if it exists
if test -f $MuPAD_ROOT_PATH/share/doc/ascii-$SHORT_LANG.tar ; then
  MuPAD_HELP=$MuPAD_ROOT_PATH/share/doc/ascii-$SHORT_LANG.tar#ascii
elif test -d $MuPAD_ROOT_PATH/share/doc/ascii-$SHORT_LANG ; then
  MuPAD_HELP=$MuPAD_ROOT_PATH/share/doc/ascii-$SHORT_LANG
elif test -f $MuPAD_ROOT_PATH/share/doc/ascii.tar ; then
  MuPAD_HELP=$MuPAD_ROOT_PATH/share/doc/ascii.tar#ascii
else
  MuPAD_HELP=$MuPAD_ROOT_PATH/share/doc/ascii
fi

MuPAD_DOC_PATH=$MuPAD_ROOT_PATH/share/doc/dvi
if test -d ${MuPAD_DOC_PATH}-${SHORT_LANG} ; then
  MuPAD_DOC_PATH=${MuPAD_DOC_PATH}-${SHORT_LANG}
fi
export MuPAD_DOC_PATH

# If pager is less, make sure the option '-r' is used
LESS="${LESS} -refq"
export LESS
LESSSECURE=1
export LESSSECURE

# If pager is more, we need -f to stop linebreaks.
MORE="${MORE} -f"
export MORE

# This ensures that MuPAD will find all of its modules.

exec $MuPAD_BIN/mupad -l $MuPAD_LIB -m $MuPAD_MOD -h $MuPAD_HELP "$@"
