#!/bin/sh
#
#     Stop if operator issues a "kill" command
#
      trap 'echo Program aborted; exit' 15
#
#     Set SIMO_RUNDIR if not specified
#
      bindir=`dirname $0`

      if ( test -z "$SIMO_RUNDIR")
      then
	    export SIMO_RUNDIR=`dirname $0`
      fi
#
# Add JRE directory to PATH
#
      if [ -n "`echo $PATH | grep 'jre[^:]*server' `" ]
      then
	  echo "jre server found in PATH"

      elif [ -n "`echo $LD_LIBRARY_PATH | grep 'jre[^:]*server' `" ]
      then
	  echo "jre server found in LD_LIBRARY_PATH"

      elif [ -d /cygdrive/c/Marintek/MopSim/jre/jre/bin/server ]
      then
	  tmp="/cygdrive/c/Marintek/MopSim/jre/jre/bin/server"
	  tmp1="/cygdrive/c/Marintek/MopSim/jre/jre/bin/"
	  echo "Adding $tmp to PATH"
	  export PATH="$tmp:$tmp1:$PATH"

      elif [ -d /cygdrive/c/Marintek/MSE/jre/jre/bin/server ]
      then
	  tmp="/cygdrive/c/Marintek/MSE/jre/jre/bin/server"
	  tmp1="/cygdrive/c/Marintek/MSE/jre/jre/bin/"
	  echo "Adding $tmp to PATH"
	  export PATH="$tmp:$tmp1:$PATH"

      elif [ -d $bindir/../../jre/jre/bin/server ]
      then
	  if [ "${bindir:1:2}" = ":/" ]
	  then
	      tmp="/cygdrive/${bindir:0:1}/${bindir:3}/../../jre/jre/bin/server"
	      tmp1="/cygdrive/${bindir:0:1}/${bindir:3}/../../jre/jre/bin/"
	  else
	      tmp="$bindir/../../jre/jre/bin/server"
	      tmp1="$bindir/../../jre/jre/bin/"
	  fi
	  echo "Adding $tmp to PATH"
	  export PATH="$tmp:$tmp1:$PATH"
      else
	  echo "jre server not found, hla / visualization enables RIFLEX versions will not run"
      fi

#
# Set HLALIB_PATH
#
      if [ -n "$HLALIB_PATH" ]
      then
	  echo "HLALIB_PATH is already set to $HLALIB_PATH"

      elif [ -r /cygdrive/c/Marintek/MopSim/HLALib.jar ]
      then
	  export HLALIB_PATH="c:/Marintek/MopSim/HLALib.jar"
	  echo "HLALIB_PATH has been set to $HLALIB_PATH"

      elif [ -r /cygdrive/c/Marintek/MSE/HLALib.jar ]
      then
	  export HLALIB_PATH="c:/Marintek/MSE/HLALib.jar"
	  echo "HLALIB_PATH has been set to $HLALIB_PATH"

      elif [ -r $bindir/../../HLALib.jar ]
      then
	  if [ "${bindir:1:2}" = ":/" ]
	  then
	      tmp="$bindir/../../HLALib.jar"
	  else
	      tmp="${bindir:10:1}:${bindir:11}/../../HLALib.jar"
	  fi
	  export HLALIB_PATH="$tmp"
	  echo "HLALIB_PATH has been set to $HLALIB_PATH"

      else
	  echo "HLALib.jar not found, hla / visualization enables RIFLEX versions will not run"
      fi

#
#
#
      if test "$1" = "dummy"
        then
             chsys=""
             sysfil=""
      else
             chsys=$1
             sysfil=-$1
      fi
#
      if test "$2" = "dummy"
        then
             chini=""
             inifil=""
      else
             chini=$2
             inifil=-$2
      fi
#
      module=$3
      mode=$4
      chmac="@do $5"
#
#     Create SIMO_DEF.DAT and write it's content
#
      echo $mode > SIMO_DEF.DAT
      echo $chsys >> SIMO_DEF.DAT
      echo $chini >> SIMO_DEF.DAT
#
#
      if test "$mode" = "batch"
        then
             echo $chmac > TEMP.DAT
      fi
#
      statusflag=0
      if test "$module" = "inpmod"
        then
             if test "$mode" = "batch"
               then
                    ${SIMO_RUNDIR}/inpmod < TEMP.DAT
                    statusflag=$?
                    rm TEMP.DAT
               else
                    ${SIMO_RUNDIR}/inpmod
                    statusflag=$?
             fi
             rm SIMO_DEF.DAT
#
      elif test "$module" = "stamod"
        then
             if test "$mode" = "batch"
               then
                    ${SIMO_RUNDIR}/stamod < TEMP.DAT
                    statusflag=$?
                    rm TEMP.DAT
               else
                    ${SIMO_RUNDIR}/stamod
                    statusflag=$?
             fi
             rm SIMO_DEF.DAT
#
      elif test "$module" = "dynmod"
        then
             if test "$mode" = "batch"
               then
                    ${SIMO_RUNDIR}/dynmod < TEMP.DAT
                    statusflag=$?
                    rm TEMP.DAT
               else
                    ${SIMO_RUNDIR}/dynmod
                    statusflag=$?
             fi
             rm SIMO_DEF.DAT
#
      elif test "$module" = "outmod"
        then
             if test "$mode" = "batch"
               then
                    ${SIMO_RUNDIR}/outmod < TEMP.DAT
                    statusflag=$?
                    rm TEMP.DAT
               else
                    ${SIMO_RUNDIR}/outmod
                    statusflag=$?
             fi
             rm SIMO_DEF.DAT
      elif test "$module" = "plomod"
        then
#
#     Create file O with macro F for opening of plot file
#     The file is run by : ex-m OP fi
#
             echo ">M,FI" > OP
             echo "op-pl plo$sysfil$inifil.ffi" >> OP
             echo ">E" >> OP
             echo ">ENDFILE" >> OP
	     export SIMO_PLO_INCA="${SIMO_RUNDIR}/etc/plomod.inca"
	     export GPGS_DEVICES="${SIMO_RUNDIR}/etc/gpgs_devices.dat"
             if test "$mode" = "batch"
               then
                    ${SIMO_RUNDIR}/plomod < $5
                    statusflag=$?
                    rm TEMP.DAT
               else
                    ${SIMO_RUNDIR}/plomod
                    statusflag=$?
             fi
             rm OP
#
      elif test "$module" = "s2xmod"
        then
             if test "$mode" = "batch"
               then
                    ${SIMO_RUNDIR}/s2xmod < TEMP.DAT
                    statusflag=$?
                    rm TEMP.DAT
               else
                    ${SIMO_RUNDIR}/s2xmod
                    statusflag=$?
             fi
             rm SIMO_DEF.DAT
      elif test "$module" = "express"
        then
#
#
#     Create macro file OP_SI.MAC for opening of simo time series file
#     The file is run by : @do OP_SI
#
             echo "open_simo $chsys $chini" > OP_SI.MAC
             echo "info_simo" >> OP_SI.MAC
             if test "$mode" = "batch"
               then
                    ${SIMO_RUNDIR}/express < TEMP.DAT
                    statusflag=$?
                    rm TEMP.DAT
               else
                    ${SIMO_RUNDIR}/express
                    statusflag=$?
             fi
             rm OP_SI.MAC
#
      elif test "$module" = "table"
      then
         if test -r pro$sysfil$inifil.lis
            then
            ${SIMO_RUNDIR}/etc/table<pro$sysfil$inifil.lis>pro$sysfil$inifil.tab
            echo "Table file : pro$sysfil$inifil.tab"
         else
            echo " "
            echo "Error, OUTMOD print file : pro$sysfil$inifil.lis does not exist"
         fi
      fi
exit $statusflag
