#!/bin/sh
#
# Command file to run Mn_Fit
# Interpret any options that are set
#
debug="exec"
fname="mn_fit"
xnode=''
uservsn=''
[ -z "$TMPDIR" ] && TMPDIR=/tmp
export TMPDIR
#
if [ -z "$MN_FIT" ]; then
  MN_FIT=XXX_MN_FIT_ENV_XXX
  export MN_FIT
fi
#
if [ -z "$MN_FIT_SYS" ]; then
  MN_FIT_SYS=${MN_FIT}/`uname -s`
  export MN_FIT_SYS
fi
#
# Set up the default graphics package
#
tgks='x'
#
# Define the executable directory
#
direxe="$MN_FIT_SYS/exe/"
#
while [ "$#" -gt 0 ]
do
    case $1 in
        -r*)
            fname="mn_root"
            ;;
        -t*)
            fname="mn_test"
            ;;
        -dev*)
            fname="mn_dev"
            ;;
        -di*)
            fname="mn_dsp"
            ;;
        -o*)
            fname="mn_old"
            ;;
        -u*)
            uservsn=1
            fname=mn_user
            if [ $# -gt 1 ]; then
                case $2 in
                    -*)
                        ;;
                    *)
                        fname=$2
                        shift
                        ;;
                esac
            fi
            ;;
        -x*)
            tgks="x"
            ;;
        -n*)
            if [ $# -gt 1 ]; then
                case $2 in
                    -*)
                        ;;
                    *)
                        xnode=$2
                        shift
                        ;;
                esac
            fi
            ;;
        -db*)
            # debug="xdb" # Use for HPUX
            # debug="cvd" # Use for SGI
            debug="gdb" # Use for Linux (ddd is also good, if not better)
            ;;
        -?|?|h)
            echo 'Valid options are:'
            echo '   -r   Root version'
            echo '   -t   Test version'
            echo '   -di  Display version'
            echo '   -x   X windows interface'
            echo '   -n   X windows display node'
            echo '   -u   User version'
            echo '   -db  Debug Mn_Fit'
            exit
            ;;
        -*)
            echo 'Invalid option: ' $1
            exit
            ;;
        *)
            MACROFILE=$1
            ;;
    esac
    shift
done
#
readline=''
#
# Define ROOTSYS as it was at the time the executable was made
#
if [ $fname = "mn_root" ]; then
  ROOTSYS=XXX_ROOTSYS_ENV_XXX
  if [ -n "$ROOTSYS" ]; then
    export ROOTSYS
    LD_LIBRARY_PATH=$ROOTSYS/lib:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH
  fi
fi
#
fname=${fname}_${tgks}${readline}.exe
if [ "$uservsn" ]; then
    direxe='./'
    echo "Running user version of Mn_Fit: $fname"
fi
#
# X windows version set DISPLAY if needed
#
if [ $tgks = "x" ]; then
    if [ "$xnode" ]; then
        export DISPLAY; DISPLAY="${xnode}:0.0"
        echo "DISPLAY set to $DISPLAY"
    elif [ ! "$DISPLAY" ]; then
        export DISPLAY; DISPLAY="`hostname`:0.0"
        echo "DISPLAY set to $DISPLAY"
    fi
fi
#
if [ "$MACROFILE" ]; then
  $debug $direxe$fname < $MACROFILE
else
  $debug $direxe$fname
fi
#
exit
