#!/bin/sh
#
# Builds an executable of the model project code from the make file.
# 
# Calls the following ELM scripts:
# PathELM_HOME, PathModel, 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)


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

# 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

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

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

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

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

# the Tool is leftover from parallel ELM implementation
# it is always "Serial" in v2.2 and probably most later versions
Tool=Serial
export Tool

####
#### Driver Path
DriverPath=${ELM_HOME}/SME/SMDriver
export DriverPath
echo "DriverPath= $DriverPath"
echo "#######"

####
#### Create object file directories if needed (a one-time thing for a new ELM install on an OS)
dirObj="Driver_Sources SpatMod Tools UnitMod"
for idir in $dirObj
do
if test -d $DriverPath/Sources/$idir/$OSTYPE
  then
    this=that;
  else
    echo "#### I see this is a new install of ELM under the OS \"$OSTYPE\"."
    echo "Thus, we're going to create a new directory to hold your compiled object files within:"
	echo "$DriverPath/Sources/$idir/ "
	echo "The current listing of $DriverPath/Sources/$idir/ is:"
	ls -al $DriverPath/Sources/$idir/ 
	echo "This appears safe, but is it ok with you to make the directory \"$OSTYPE\" in this location? (y/n) "
	get_yesno
	  if [ "$ANS" != "y" -a "$ANS" != "Y" ]
	  then 
	    echo "OK.  Nothing done."
	    exit
	  else
	  	mkdir $DriverPath/Sources/$idir/$OSTYPE
	  fi
fi
done

# remind users what compiler/OS versions have been used 
echo "ELM compilation and execution has been tested using:"
echo "gcc version 3.2 ( (Sun) Solaris, Release 5.8, sparc processor)"
echo "gcc version 3.2.2 ( (Red Hat) Linux, Release 2.4.20-27.9smp, i686 processor)"
echo "gcc version 3.3 ( (Apple) Darwin, Release 6.8, Power Macintosh processor)"
echo "######"

####
####
####
#### Now we do the compiling

cd $DriverPath/Sources/Driver_Sources/

# $2 is for clean. Don't remove.
if make -f  Driver.make $2 2> comp_msg
	then
		echo "$1 $2 compilation completed successfully."
	else
		echo "###### Error: Compilation failed *****"
		echo
		more $DriverPath/Sources/Driver_Sources/comp_msg
		echo "Error file: $DriverPath/Sources/Driver_Sources/comp_msg"
fi

cd $DriverPath
echo
echo "Done with build of $ModelPath/$ProjName"
