#!/bin/sh -f

# Special settings to run MuPAD without any local modifications

SHARE_BIN=`dirname $0`

if [ `basename $SHARE_BIN` = bin ] ; then
  SHARE=`dirname $SHARE_BIN`
  if [ `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`
export SYSINFO
LD_LIBRARY_PATH=${MuPAD_ROOT_PATH}/${SYSINFO}/lib:/usr/local/X11R6/motif-2.0/lib:/usr/local/X11R6/lib:$MuPAD_ROOT_PATH/$SYSINFO/bin:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

XKEYSYMDB=$MuPAD_ROOT_PATH/share/unix/XKeysymDB
export XKEYSYMDB

# xmupad crashes on newer linux distributions, if LC_CTYPE is
# set to a UTF-8 encoding.  
export LC_CTYPE=C

SHORT_LANG=`echo ${LANG}|tr 'A-Z' 'a-z'|cut -b-2`
[ -z "$SHORT_LANG" ] && SHORT_LANG=`echo ${LC_MESSAGES}|tr 'A-Z' 'a-z'|cut -b-2`
[ -z "$SHORT_LANG" ] && SHORT_LANG=`echo ${LC_ALL}|tr 'A-Z' 'a-z'|cut -b-2`
[ -z "$SHORT_LANG" ] && SHORT_LANG=`echo ${LANGUAGE}|tr 'A-Z' 'a-z'|cut -b-2`
# at the moment only german is supported
if [ ! -d $MuPAD_ROOT_PATH/share/unix/locale/$SHORT_LANG ]; then
  LANGUAGE=
  LANG=
  LC_MESSAGES=
  LC_ALL=
  SHORT_LANG=
  export LANGUAGE LANG LC_MESSAGES LC_ALL
fi
export SHORT_LANG

MuPAD_BIN=$MuPAD_ROOT_PATH/$SYSINFO/bin
MuPAD_MOD=$MuPAD_ROOT_PATH/$SYSINFO/modules
MuPAD_LIB=$MuPAD_ROOT_PATH/share/lib
MuPAD_HELP=$MuPAD_ROOT_PATH/share/doc/ascii
if [ -d ${MuPAD_HELP}-${SHORT_LANG} ]; then
  MuPAD_HELP=${MuPAD_HELP}-${SHORT_LANG}
fi

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

FONT_PATH=${MuPAD_DOC_PATH}/fonts//:
export FONT_PATH

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

ARCH=$SYSINFO
export ARCH

if [ "x$1" = "x-r" ] ; then
  echo $MuPAD_ROOT_PATH
  exit 0
fi

if [ "x$1" = "x-V" ] ; then
  $MuPAD_BIN/mupad -V
  exit 0
fi

exec $MuPAD_BIN/mxmupad "$@"
