#!/bin/ksh
#
# startup script for AUTOPSY
#

AUTOPSYHOME=/home/guentert/AUTOPSY/inst/autopsy-1.1
export AUTOPSYHOME
export AUTOPSYDEV

ostype=`uname`
osrel=`uname -r`

if [ $ostype = "IRIX" -o $ostype = "IRIX64" ]; then
  arch=sgi
elif [ $ostype = "AIX" ]; then
  arch=aix
elif [ $ostype = "OSF1" ]; then
  arch=dec
elif [ $ostype = "Linux" ]; then
  arch=lnx
elif [ $ostype = "SunOS" ]; then
  arch=sun
else
  arch=unknown
fi
arch=exe

printUsage () {
  echo "Usage: autopsy -ht [-o dev] [-f macro] files"
  echo ""
  echo "       -h        print this message"
  echo "       -t        TTY interface (no graphics)"
  echo "       -o name   alternate input/output device"
  echo "       -f file   execute macro file (- for standard input)"
  echo "       files     EASY or BRUKER files"
}

nograph=
xopt=
input=
while getopts hto:2asr:f: option; do
  case $option in
    h) printUsage
       exit 0;;
    t) nograph=y;;
    2) xopt=-stereo;;
    f) input=$OPTARG;;
    o) AUTOPSYDEV=$OPTARG;;
    *) printUsage
       echo ""
       echo "passing unknown options to Xt"
       break;;
  esac
done
shift `expr $OPTIND - 1`

display=$DISPLAY

while [ -n "$*" ]; do
  case $1 in
    -bg | -ba*) xopt="$xopt $1 $2"; shift 2;;
    -bd) xopt="$xopt $1 $2"; shift 2;;
    -bw | -borderw*) xopt="$xopt $1 $2"; shift 2;;
    -borderc*) xopt="$xopt $1 $2"; shift 2;;
    -d*) xopt="$xopt $1 $2"; display=$2; shift 2;;
    -fg | -for*) xopt="$xopt $1 $2"; shift 2;;
    -fn | -fon*) xopt="$xopt $1 $2"; shift 2;;
    -g*) xopt="$xopt $1 $2"; shift 2;;
    -n*) xopt="$xopt $1 $2"; shift 2;;
    -t*) xopt="$xopt $1 $2"; shift 2;;
    -x*) xopt="$xopt $1 $2"; shift 2;;
    -* | +*) xopt="$xopt $1"; shift;;
    *) break;;
  esac
done

if [ ! -d $HOME/autopsy ]; then
  mkdir $HOME/autopsy
fi
startmacro=$HOME/autopsy/options.mac
cp /dev/null $startmacro

if [ -n "$*" ]; then
  (
    for i in $*; do
      case $i in
        *.param) echo "OpenEasy $i";;
	*)       echo "OpenBruker $i";;
      esac
    done
  ) >> $startmacro
fi

if [ -n "$input" -a "$input" != "-" ]; then
  if [ -r $input ]; then
    echo "XMacUser $input" >> $startmacro
  else
    echo "XMacStand $input" >> $startmacro
  fi
fi

trap "rm -f $startmacro" 0 1 2 15

if [ -n "$nograph" ]; then
  AUTOPSYDEV=TTY/NO
fi

if [ "$input" = "-" -o -n "$nograph" ]; then
  $AUTOPSYHOME/autopsy.$arch $xopt
else
  $AUTOPSYHOME/autopsy.$arch $xopt < /dev/null
fi
