#!/bin/sh

# Controller script that configures, runs, and archives a simulation.
# Calls the following scripts: 
#   1) call PathELM_HOME, PathModel, PathOutput, and PathArchive to establish paths
#   2) call "Check" script to view/change runtime parms,
#   3) call "CopyInput" script to make Notes and copy some input files
#   4) call "go" script to execute the model,
#   5) call "ArchiveRun" script to archive the necessary input and output data (after end of simulation)

# As with all ELM scripts, stay with Bourne shell (/bin/sh) for universal application.
# 	Note: for getting a variable from a child shellcommand, ". shellcommand" does similar/same thing 
# 	as "source shellcommand" (latter didn't work in Sun solaris implmentation)



echo ""
echo "*** Run the model and archive the results."
echo ""

# *******************
# first take care of environment variables and paths
# *******************

####
#### ELM_HOME Path
# determines if the (fundamental) ELM_HOME environment variable appears valid
PathELM_HOME

####
#### portable function for getting yes/no answer
get_yesno()
{
    ANS="X"
    while [ "$ANS" != "y" -a "$ANS" != "n" -a "$ANS" != "Y" -a "$ANS" != "N" ]
    do
		echo  " (y/n): "
        read ANS
    done
}

####
#### if lacking input arguments
if test $# != 2
  then
    echo "###### Error: you must supply 1) an existing ProjectName "
    echo "and 2) a new descriptive suffix to designate the run. "
    echo "(example: Run ELM myFirstRun)"
    exit
fi

####
#### Model Path
# establish the ModelPath for the model Project data/executable from the PathModel script
. PathModel

####
#### Driver Path
DriverPath=$ELM_HOME/SME/SMDriver/
export DriverPath

####
#### Project Name
ProjName=$1
export ProjName

####
#### Run Name
RunName=$2
export RunName

####
#### Output Path
# establish the output path for model results from PathOutput script
. PathOutput $ProjName

####
#### Archive Path
# establish the path for archiving the model from PathArchive script
. PathArchive

############
#IMPORTANT: ensure that there is not another archived run with same name (that will be overwritten)
############
if test -d $ELM_ARCHIVE_PATH/$ProjName/$RunName
  then
    echo "###### Error: That output run name exists in the archive - use another run name suffix."
    echo "Existing archive: $ELM_ARCHIVE_PATH/$ProjName/$RunName"
    exit
fi


# *******************
# now start doing stuff
# *******************

####
#### run the Check script to check/modify the Runtime parms and what is to be output
Check $ProjName

#### 
#### ask to run the CopyInput script 
echo ""
echo "** Are you willing to use vi to make some USEFUL notes on the simulation?"
get_yesno
if [ "$ANS" = "y" -o "$ANS" = "Y" ]
  then 
	# run the CopyInput script to make notes on the upcoming simulation, and copies of important input files
	CopyInput 
  else
	echo "###### Error: Sorry - it is mandatory to make useful notes on the simulation.  Bye."
  	exit
fi

#### 
#### run the model
go $ProjName

#### 
#### archive the model input and output
ArchiveRun $ProjName $RunName

echo ""
echo "Done with running and archiving the $ProjName simulation designated as $RunName. "
