#!/bin/sh

# Does the final processing needed on the Model.outList text file that was
# created by the ModelOutlist_creator workbook (OpenOffice/Excel).

# Used as a stand-alone after ModelOutlist_creator.

# It does three simple things:
# 1) replaces tabs with no-space (ctrl v ctrl i used to put the "\t" equivalent in)
# 2) replaces " with no-space 
# 3) moves the result to the RunParms directory of the model

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

# 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


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


if test $# != 2
then
    echo "###### Error:  you must supply 1) an existing ELM ProjectName "
    echo "and 2) the name of the Model.outList file you are about to finalize. "
    echo "This file should be in your current directory. "
    echo "(example: finishOutList ELM2.2 Model.outList_test)"
    echo " "
	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

if test -f $2
  then
    this=that;
  else
    echo "###### Error:  The file $2 is not in your curent directory."
	echo "Available files are: "
    ls 
    exit
fi

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

echo "** Your input, please: Your are going to put a new Model.outList file in"
echo "$ModelPath/$ProjName/RunParms/"
echo "Is that OK?"
get_yesno
if [ "$ANS" = "y" -o "$ANS" = "Y" ]
  then 
	echo "Removing tab and \" characters from the $2 file..."
  else
	echo "OK.  Nothing done."
  	exit
fi

####
#### Now do the work

# the first script removes all of the tab characters
# the second script removes all of the quote characters
sed  -e 's/	//g' -e 's/"//g'  $2 > $2.tmp 

# move the file
mv $2.tmp $ModelPath/$ProjName/RunParms/Model.outList

echo ""
echo "Done. The Model.outList, at"
echo "$ModelPath/$ProjName/RunParms/Model.outList"
echo "is ready to use."



