#!/bin/sh
#
# simdemo : Compiles and/or runs Simscript demo programs.
# =======  
# This script is called by individual users (after installattion) to run
# SIMSCRIPT example programs UNDER THEIR $HOME/simdemos directory.
# The script is used for both kinds of installation (root/standard).
# IT is not an install script!!
#
# INST_DEMOS is set/inserted at installation time. We can't use SIMHOME/...
# because this script is also to be used by the root installation.
#---------------------------------------------------------------------------


#--- where to find the examples; set at install time
prog=`basename $0`
STANDARD_INSTALL=y
INST_DEMOS=$SIMHOME/sim_examples

#-- were examples installed at all?
if [ "$INST_DEMOS" = "_not_installed_" ]; then
  echo "*** Examples were not installed. Cannot run simdemo."
  exit 1
fi

#-- check if SIMHOME is used and defined
if [ "$STANDARD_INSTALL" = "y"  -a  "$SIMHOME" = "" ]; then
  echo "$prog ERROR: Env. var SIMHOME undefined. Cannot run. Aborted."
  exit 1
fi
#

#--- prompt of user's shell ???? ---
PS1=">"

echo 
echo "    simdemo : Compile/run Simscript demo programs."
echo

echo -n "--> Do you want an explanation of simdemo (y/n)? [n] "
read key
if [ "$key" = "y" ]; then
  cat << 'End-of-message'

	  simdemo : Compile/run Simscript example programs

    Simdemo shows a list of Simscript example programs you can have compiled,
  linked and then run automatically. The selected program will be first 
  copied into a subdirectory "simdemos" of your home directory (i.e. 
  $HOME/simdemos/<program>) and then compiled there. Therefore you can not
  only look at but also change the example programs. The 'top-level' docu-
  mentation is generally located in the Preamble.
    When you select a program that has already been copied to your 
  $HOME/simdemos, it will not be copied again, but just run.

End-of-message
  echo -n "--> Press <return> to continue ..."
  read key
fi

#
# Check if we can make the example directory (exists or not)
#

DEMO_HOME="$HOME/simdemos"

#-- doesn't exist ?? then make
if [ ! -d $DEMO_HOME ]; then
  echo 
  echo ">>> Your simdemos directory ($DEMO_HOME) doesn't exist yet."
  echo ">>> Creating the directory $DEMO_HOME."
  mkdir $DEMO_HOME
fi
#-- if it already exists, it may be from previous demo runs. No message.
#-- when a model is selected that already exists there: message, no copy



#
# Now offer selection, copy, compile and run selection; then gome back.
#

cd $INST_DEMOS

sel=99
while [ $sel != 0 ]; do
  cat << 'End-of-message'
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

>>>  Available Simscript example programs:

 (1)  bank      A bank with one waiting queue and multiple tellers.
 (2)  dynhist   Demos automatic graphical display of internal variables.
 (3)  eject     Pilot ejection model.
 (4)  food      A fast food restaurant simulation with lots of graphics.
 (5)  goldmine  A goldmine with 2 shafts and 1 lift.
 (6)  image     Like newshape but also demos the use of forms and graphs
 (7)  newshape	Demos animation/ the VELOCITY construct of SIMSCRIPT
 (8)  newport   A port model
 (9)  taxi      Demos resources and graphic entities and graphs
 (10) tv        Resources and Queues in a TV test station
 (11) calship   Port model that demonstrates multiple graphics windows
 (12) bounce    Demos continuous simulation
 
 (0) exit       Exit this demo program.

End-of-message

  echo -n " --> Which would you like? "
  read sel
  echo
  MODEL=""
  case "$sel" in
   "") 	echo "*** Please enter a number."
	continue
	;;
   "0")	break	#-- jump out of the loop
	;;
   "1") MODEL=bank
	CTRLC=n
	;;
   "2") MODEL=dynhist
	CTRLC=n
	;;
   "3") MODEL=eject
	CTRLC=n
	;;
   "4") MODEL=food
	CTRLC=y
	;;
   "5") MODEL=goldmine
	CTRLC=n
	;;
   "6") MODEL=image
	CTRLC=n
	;;
   "7") MODEL=newshape
	CTRLC=n
	;;
   "8") MODEL=newport
	CTRLC=n
	;;
   "9") MODEL=taxi
	CTRLC=y
	;;
   "10") MODEL=tv
	CTRLC=y
	;;
   "11") MODEL=calship
	CTRLC=n
	;;
   "12") MODEL=bounce
	CTRLC=n
	;;

   *)   echo "*** Please enter a digit within range."
	continue
	;;
  esac
  #-- when we get here, MODEL holds the model name
  #-- we are in INST_DEMOS, the sim_examples directory
  TARGETD="$DEMO_HOME/$MODEL"
  #---- is there a FILE with the selected name? don't overwrite ----
  if [ -f $TARGETD  -a  ! -d $TARGETD ]; then
    echo "*** A file with the selected model name exists in $DEMO_HOME!"
    echo "*** Delete/rename the file '$MODEL' in $DEMO_HOME."
    continue
  #---- is the directory already there, then don't copy ---
  elif [ -d $TARGETD ]; then
    echo ">>> Example program $MODEL already exists in $DEMO_HOME. Not copied."
    echo
  else
    #---- copy the directory
    echo -n ">>> Copying the example $MODEL into $DEMO_HOME..."
    cp -r $MODEL $DEMO_HOME
    echo " done."
  fi
  #
  #--- Now the model is in place in $TARGETD. Go and make there.
  cd $TARGETD
  echo ">>> Making the model/example program."
  echo "$PS1 make"
  make 
  #
  if [ $CTRLC = "y" ]; then
    echo ">>> To exit the run, press control-C in this window."
    echo ">>> Press any key to start the run."
    read dummy
  fi
  echo ">>> Running the model (calling executable)."
  echo "$PS1 $MODEL"
  $MODEL
  echo
  echo -n "--> Model run completed successfully. Press <return> to continue."
  read key
  echo
  echo
  echo ">>> Returning to simdemos program."
  echo
  cd $INST_DEMOS

done

#
echo "End of simdemo."

