#!/bin/sh

# Checks for validity of an existing OutputPath for model output 
# and  exports it if valid.
# This can be used as either as a stand-alone or as a call from other scripts such as "Run".

# Calls the following ELM scripts:
# PathELM_HOME, PathModel

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

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

####
#### if lacking input argument
if test $# = 0
  then
    echo "###### Error: you must supply 1) an existing ProjectName "
    echo "(example: PathOutput ELM)"
    exit
fi

ProjName=$1
export ProjName


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

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

####
#### Output Path
# the base model output path (without the ProjName) is read from Driver.parm 
dparm=$ModelPath/$ProjName/RunParms/Driver.parm
OutputPath=`sed -n '1p' $dparm | awk '{print $1}'`  #first field of first line is the output path

# check that the base model OutPutPath exists
if test -d "$OutputPath"
  then
  # kinda junky here! solaris didn't like a "! test -d xyz"
  	this=that;
  else
    echo "###### Error: $OutputPath "
    echo "does not exist on your network for output of the model in:"
    echo "$ModelPath/$ProjName"
    echo "Give a valid base directory for model output, editing the first line in:"
    echo "$ModelPath/$ProjName/RunParms/Driver.parm"
    exit
fi

# check that the full path for model output exists
if test -d "$OutputPath/$ProjName/Output"
  then
  # kinda junky here! solaris didn't like a "! test -d xyz"
  	this=that;
  else
    echo "  "
    echo "###### Error: $OutputPath/$ProjName/Output "
    echo "does not exist on your network for output of the model in:"
    echo "$ModelPath/$ProjName"
    echo "  "
    echo "###### If the valid BASE part ($OutputPath)"
    echo "is indeed where you want to have your BASE path for output,"
    echo "then run the script mkOutDirs, providing 2 arguments:"
    echo "1) your base directory (w/o ProjectName) for model output ($OutputPath), and"
    echo "2) the Model ProjectName ($ProjName). Appropriate output directories will be created. "
    echo " "
    echo "###### If you instead want to change your BASE path for output,"
    echo "give a valid base directory for model output, editing the first line in:"
    echo "$ModelPath/$ProjName/RunParms/Driver.parm"
    exit
fi
echo "OutputPath= $OutputPath"
export OutputPath
