#!/bin/sh

# Simply runs the model executable, 
# either as a stand-alone or as a call from other scripts such as "Run".

# Calls the following ELM scripts:
# PathELM_HOME, PathModel, PathOutput, PathOSTYPE

# 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)


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

# let's put an empty line in here to start 
echo ""

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

####
#### Operating Sytem Type
# call script to determines if the OSTYPE variable appears valid 
#
PathOSTYPE


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

####
#### if lacking input argument
if test $# = 0
  then
    echo "###### Error: you must supply the ProjectName argument."
    echo "Example: go ELM"
	echo "Available ProjectNames in your ModelPath= $ModelPath "
    ls $ModelPath
    exit
fi

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

if test -d $ModelPath/$ProjName
  then
    this=that;
  else
    echo "###### Error:  That model Project does not exist."
	echo "Available Project Names in your ModelPath= $ModelPath "
    ls $ModelPath
    exit
fi

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


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

# change directory to location of executable - a debug file is written to dir where one executes the model
cd $ModelPath/$ProjName/Load/

####
#### Run the executable model
 

if test -f ./$ProjName.$OSTYPE
  then
    ./$ProjName.$OSTYPE
  else
    echo "###### Error:  That executable ./$ProjName.$OSTYPE does not exist. Run the \"build\" script to create one."
	echo "Available executables in $ModelPath/$ProjName/Load/ are:"
    ls 
    exit
fi


echo
echo "Done running $ModelPath/$ProjName/Load/$ProjName.$OSTYPE"
echo "***"




