#!/usr/bin/env bash
#################################################################
#Script          :       RESTART
#
#Purpose         :       Starten von RESTART
#
#Programmer      :       Patrick Drescher
#
#History         :       April 1995
#
#################################################################

#export MODELDIR=$MODELDIR
#export TNETHOME=$TNETHOME

distr="null"
distr_string="null"
method="null"
meth_string="null"
trans="null"
trans_string="null"
maxsamples="null"
maxtime="null"
max_execution_time="null"
nice=4

#############################################################################
# clear files                 
#############################################################################
function clearfiles
{
  \rm -f "$MODEL_ERC.c"
  \rm -f "$MODEL_RDY.c"
  \rm -f "$MODEL_MDF.c"
  \rm -f "$MODEL.DEFINFO"
  \rm -f "$MODEL.TN"
  \rm -f "$MODEL.RESULTS"
  \rm -f "$MODEL.PARAM"

  mkdir -p "$MODEL.dir"

  if [ -e "${MODEL}.ECS" ];  then
    mv "$MODEL.ECS" "$MODEL.dir"
  fi
  if [ -e "${MODEL}.INV" ];  then
    mv "$MODEL.INV" "$MODEL.dir"
  fi
  if [ -e "${MODEL}.STRUCT" ];  then
    mv "$MODEL.STRUCT" "$MODEL.dir"
  fi
}


usage="Usage : RESTART model -{D|S} <max_execution_time> <max_thresholds> <-Ton|Toff> <conf.level> <epsilon> <seed> <max.samples> <min_firings> <max.model time> <max.real time>"

#input netname
if [ $# -lt 1 ]; then
	echo $usage
  exit 1
fi
if [ $# -eq 1 ]; then
 if [ "$1" = "-h" -o "$1" = "-help" ]; then
    echo $usage
    exit 1
 fi
fi

# check environment
if [ "$TNETHOME" = "" ]; then
	echo "Environment variable TNETHOME is not set"
	exit
fi
if [ ! -d "$TNETHOME" ]; then
	echo "Environment variable TNETHOME is not a directory"
	exit
fi
if [ "$MODELDIR" = "" ]; then
	echo "Environment variable MODELDIR is not set"
	exit
fi
if [ ! -d "$MODELDIR" ]; then
	echo "Environment variable MODELDIR is not a directory"
	exit
fi

# set path names
BIN_DIR=${TNETHOME}/EDSPN/Simulation/bin
SIM_DIR=${TNETHOME}/EDSPN/Simulation
SHARED_BIN_DIR=${TNETHOME}/EDSPN/Shared/bin
NAME=$1
MODEL="$MODELDIR/$NAME"

# check net
if [ ! -e "$MODEL.TN" ];  then
     # if only xml-file exists, use xml2tn exporter to create TN-file
     if [ -e "$MODEL.xml" ];  then
        cd "${TNETHOME}/EDSPNGen"
        ./xml2tn.sh "$MODEL.xml"
        cd $currentdir
     else
        echo "No such net"
        echo
        exit -1
     fi
fi


if [ $# -ge 2 ]; then
        distr=$2
else
        distr="-D";
fi

if [ $# -ge 3 ]; then
       max_execution_time=$3
else
       max_execution_time="600.0";
fi

if [ $# -ge 4 ]; then
       max_thresholds=$4
else
       max_thresholds="10";
fi

if [ $# -ge 5 ]; then
       trans=$5
else
       trans="-Ton";
fi

if [ $# -ge 6 ]; then
       level=$6
else
       level="90";
fi

if [ $# -ge 7 ]; then
       epsilon=$7
else
       epsilon="10";
fi

if [ $# -ge 8 ]; then
       seed=$8
else
       seed="1";
fi

if [ $# -ge 9 ]; then
       maxsamples=$9
else
       maxsamples="0";
fi

if [ $# -ge 10 ]; then
       min_firings=$10
else
       min_firings="50";
fi

if [ $# -ge 11 ]; then
       maxtime=$11
else
       maxtime="0.0";
fi

if [ $# -ge 12 ]; then
       max_real_time=$12
else
       max_real_time="0.0";
fi

case $distr in
  -S)
	   distr_string="Sequential "
	   distr="S"
	   ;;
  -D)
	   distr_string="Parallel "
	   distr="D"
	   ;;
  *)
	   echo $usage
	   exit 1
     ;;
esac

case $trans in
  -Ton)
	   trans_string="on."
	   trans="Ton"
	   ;;
  -Toff)
	   trans_string="off."
	   trans="Toff"
	   ;;
  *)
	   echo $usage
	   exit 1
esac

echo $distr_string" Simulation with RESTART of net:"
echo "$MODEL"

#############################################################################
# marking dependencies, definition of rewardmeasures
#
# execute 'proc_TN'
#############################################################################
"${SHARED_BIN_DIR}/proc_tn" -s "$NAME"
if [ $? -ne 0 ]; then
  echo
  echo ERROR STATE $STATUS occurred while PROC_TN execution.
  # $unlock_model $NAME
  echo SOLUTION OF MODEL $NAME FAILED.
  echo
  clearfiles
  exit -1
fi
# echo "proc_tn done."

#############################################################################
# structural analysis
#
# execute 'struc_tn'
#############################################################################
"${SHARED_BIN_DIR}/struct_tn" -i "$NAME"
if [ $? -ne 0 ]; then
  echo
  echo ERROR STATE $STATUS occurred while STRUCT_TN execution.
  # $unlock_model $NAME
  echo SOLUTION OF MODEL $NAME FAILED.	
  echo
  clearfiles
  exit -1
fi
# echo "struct_tn done."

#############################################################################
# prepare restart
#
# execute 'sim_rst_prep'
#############################################################################
"${BIN_DIR}/sim_rst_prep" "$MODEL"
if [ $? -ne 0 ]; then
   echo "could not finish preparation of RESTART"
   clearfiles
   exit 1
fi

echo ""
echo "Preparation of RESTART finished."
echo ""


#echo Starting presimulation: max_execution_time = ${max_execution_time}
#echo SIMULATE ${NAME}_RST -S -SV -Ton 80 50 20 1 50 0 0.0 $max_execution_time -Voff
"${TNETHOME}/EDSPN/Simulation/scripts/SIMULATE" "${NAME}_RST" -S -SV -Ton 80 50 20 1 0 50 0.0 $max_execution_time -Voff
if [ $? -ne 0 ]; then
   echo "could not finish preestimation of RESTART"
   clearfiles
   exit 1
fi

echo ""
echo "Preestimation of RESTART finished."
echo ""


# copy intermediate file needed by set.rst back to main modeldir
cp "$MODEL_RST.dir/*_RST.RESULTS" "$MODEL_RST.RESULTS"

#echo Try to find out the first optimal thresholds: Max. number of thresholds = ${max_thresholds}
"${BIN_DIR}/sim_rst_set" "${MODEL}" ${max_thresholds}
if [ $? -ne 0 ]; then
   echo "could not finish threshold-setting of RESTART"
   clearfiles
   exit 1
fi

echo ""
echo "Threshold-setting of RESTART finished."
echo ""

#echo Starting main simulation with script SIMULATE with the following paramters: 

#echo SIMULATE ${NAME} -${distr} -RS -${trans} ${level} 50 ${epsilon} ${seed} ${maxsamples} ${min_firings} ${maxtime} ${max_real_time} -Voff
"${TNETHOME}/EDSPN/Simulation/scripts/SIMULATE" "${NAME}" -${distr} -RS -${trans} ${level} 50 ${epsilon} ${seed} ${maxsamples} ${min_firings} ${maxtime} ${max_real_time} -Voff
if [ $? -ne 0 ]; then
   echo "could not finish main simulation of RESTART"
   clearfiles
   exit 1
fi

echo ""
echo "Main simulation of RESTART finished."

clearfiles
