#!/bin/sh
#set -xv
#
#       This shell script loads the GeoTAX State-supplied File
#       (TS-158 Format) from the input media and puts them in the 
#       format needed.  
#
#       A log of status messages sent to the terminal following the response
#       to the load cd prompt is put into the stts158load.log file found in
#       the /data directory.
#
#==============================================================================
check_run() {
xxx=
dbloc=$G1GTXDB
SRCDIR=$G1GTX/data
Clearit;
$G1ECHO "\n\tThe state-supplied (TS-158 Format) file load environment is currently set to:\n"
$G1ECHO "\tState-supplied file input device (or input file) location:" $SRCDIR
$G1ECHO "\tState-supplied file final location:" $dbloc"\n"
$G1ECHO "\tEnter c to (c)ontinue"
$G1ECHO "\t   or m to (m)odify output file location"
$G1ECHO "\t   or q to (q)uit\n"
$G1ECHO "\t   ===> \c"
read xxx
case $xxx in
        q|Q) $G1ECHO "\n\tUser requested termination.\n"
             exit
        ;;
esac
case $xxx in
        m|M)
                $G1ECHO "\n\tWhich do you want to change?\n"
                $G1ECHO "\tEnter 1 to change the input file location"
                $G1ECHO "\t   or 2 to change the output file location"
                $G1ECHO "\t   or 3 to change both\n"
                $G1ECHO "\t  ===> \c"
                read modio
                case $modio in
                        1)      get_pathin
                        ;;
                        2)      get_pathout
                        ;;
                        3)      get_pathin
                                get_pathout
                        ;;
                        *)      check_run
                        ;;
                esac
        ;;
        c|C) :
        ;;
          *) check_run
        ;;
esac

export SRCDIR

if [ ! -d "$dbloc" ]	# check to see if the install area is there
then
        $G1ECHO "\n\tState-supplied file (TS-158 Format) final location "$dbloc" does not exist."
        $G1ECHO "\tDo you want to create it now?\n"
        $G1ECHO "\tEnter (y)es to create the new state-supplied file area."
        $G1ECHO "\t      (n)o to exit.\n"
        $G1ECHO "\t   ===> \c"
        read yyyy
        if [ "$yyyy" = y ]
	then
		mkdir $dbloc
        else
	    $G1ECHO "\n\tYou must create the new state-supplied file install area"
            $G1ECHO "\tor change the G1GTXDB variable in the setstts158db script"
            $G1ECHO "\tfound in the G1DIR/gtx/bin directory.\n"
            exit 1
        fi
fi
update_setup
}
#===========================================================================
# Get the full path for the db input area
get_pathin()
{
Clearit;
$G1ECHO "\n\tPlease enter full path where the state-supplied input file is located"
$G1ECHO "\t ==> \c"
read SRCDIR
if [ "$SRCDIR" = "" -o "$SRCDIR" = " " ]
then
        $G1ECHO "\n\tYou must enter a valid full path."
        $G1ECHO "\tPress Enter to continue....."
        read pressenter
        get_pathin
fi
$G1ECHO "\n\tThe location of the state-supplied input file will be: " $SRCDIR
$G1ECHO "\tIs this correct?\n"
$G1ECHO "\tEnter (y)es to continue."
$G1ECHO "\t      (n)o to try again.\n"
$G1ECHO "\t   ===> \c"
read yyyy
case $yyyy in
        [yY]*)  yyyy=
        ;;
            *)  get_pathin
        ;;
esac

}
#===========================================================================
# Get the full path for the new db load area
get_pathout()
{
Clearit;
$G1ECHO "\n\tPlease enter full path where you would like to install"
$G1ECHO "\tthe state-supplied file (TS-158 format). ==> \c"
read dbloc
if [ "$dbloc" = "" -o "$dbloc" = " " ]
then
        $G1ECHO "\n\tYou must enter a valid full path."
        $G1ECHO "\tPress Enter to continue....."
        read pressenter
        get_path
fi
$G1ECHO "\n\tThe new state-supplied file (TS-158 format) will be located in: " $dbloc
$G1ECHO "\tIs this correct?\n"
$G1ECHO "\tEnter (y)es to continue."
$G1ECHO "\t      (n)o to try again.\n"
$G1ECHO "\t   ===> \c"
read yyyy
case $yyyy in
        [yY]*)  yyyy=
        ;;
            *)  get_pathout
        ;;
esac

export dbloc
}
#===========================================================================
getts158name()
{
Clearit;
$G1ECHO "\n\tPlease enter the file name of your state-supplied input file,"
$G1ECHO "\tthen press <Enter> to continue...."
$G1ECHO "\n\t=====> \c"
read stts158name
export stts158name
if [ -f "$SRCDIR/$stts158name" ]
then
        $G1ECHO "\n\tState-supplied input file located....."
	$G1ECHO "\n\tCreating the GeoTAX state-supplied file (TS-158 format)...\n"
else
        $G1ECHO "\n\tFile was not found at "$G1GTX/data/$stts158name
        $G1ECHO "\n\tPlease try again or press Control C (^C) to quit."
        $G1ECHO "\tPress <Enter> to try again...."
        read xxx
        getts158name
fi
}
#===========================================================================
check_filesout()
{
if  [ `grep 'Error' $G1DBLOG | wc -l` -ne 0 -o \
      `grep 'ERROR' $G1DBLOG | wc -l` -ne 0 -o \
      `grep 'error' $G1DBLOG | wc -l` -ne 0 ]
    then
    bad_load;
else
if  [ `grep 'Return Code' $G1DBLOG | wc -l` -ne 0 ]
    then
    bad_load;
else
   if [ -s "$dbloc"/g1gtgsn ]
   then
       $G1ECHO "*******************************************************"
       $G1ECHO "STATE-SUPPLIED FILE (TS-158 FORMAT) LOADED SUCCESSFULLY"
       $G1ECHO "END OF GEOTAX STATE-SUPPLIED FILE INSTALLATION" 
       $G1ECHO "*******************************************************"
       $G1ECHO "*******************************************************" >> $G1DBLOG
       $G1ECHO "STATE-SUPPLIED FILE (TS-158 FORMAT) LOADED SUCCESSFULLY" >> $G1DBLOG
       $G1ECHO "END GEOTAX STATE SUPPLIED FILE INSTALLATION" >> $G1DBLOG
       $G1ECHO "*******************************************************" >> $G1DBLOG
   else
       bad_load;
   fi
 fi
fi
}
#
# Function to clear the screen. Try the clear shell command
# first, then try the tput command with a clear parameter and then
# try to clear each position (line) of the screen the hard way.
#
Clearit()
{
{ clear; } 2>/dev/null  ||
        { tput clear; } 2>/dev/null  ||
                { for i in \
                p01 p02 p03 p04 p05 p06 p07 p08 p09 p10   \
                p11 p12 p13 p14 p15 p16 p17 p18 p19 p20   \
                p21 p22 p23 p24 p25 p26 p27 p28 p29 p30   \
                p31 p32 p33 p34 p35 p36 p37 p38 p39 p40
                do
                        echo ""
                done
                }
}

#===========================================================================
bad_load()
{
       $G1ECHO "============> ERROR <============"
       $G1ECHO "   STATE-SUPPLIED (TS-158 FORMAT) FILE DID NOT LOAD\n"
       $G1ECHO "Please check log file (stts158load.log)"
       $G1ECHO "in \$G1DIR/data for possible causes."
       $G1ECHO "*************************************" >> $G1DBLOG
       $G1ECHO "=========> ERROR <=========" >> $G1DBLOG
       $G1ECHO "STATE-SUPPLIED FILE (TS-158 FORMAT) DID NOT LOAD" >> $G1DBLOG
       $G1ECHO "Possible causes...." >> $G1DBLOG
       $G1ECHO "  Not enough disk space available...." >> $G1DBLOG
       $G1ECHO "  Incorrect output path specification...." >> $G1DBLOG
       $G1ECHO "  Write permissions...." >> $G1DBLOG
       $G1ECHO "  Bad input media...." >> $G1DBLOG
       $G1ECHO "  Upper/lower case filenames...." >> $G1DBLOG
       $G1ECHO "*************************************" >> $G1DBLOG
}
#===========================================================================
update_setup()
{
   ${G1GTX}/bin/scr_updtdb "$dbloc"
}
#===========================================================================
Clearit;
check_run;
Clearit;
getts158name;
. $G1GTX/bin/setstts158db
if [ -s "$dbloc"/g1gtgsn ]
then
       rm -f "$dbloc"/g1gtgsn >/dev/null 2>&1
       rm -f "$dbloc"/g1gtgsd >/dev/null 2>&1
       rm -f "$dbloc"/g1gtgst >/dev/null 2>&1
       rm -f "$dbloc"/g1gtgsn.vix >/dev/null 2>&1
       rm -f "$dbloc"/g1gtgsd.vix >/dev/null 2>&1
       rm -f "$dbloc"/g1gtgst.vix >/dev/null 2>&1
fi
$G1ECHO "*************************************" > $G1DBLOG
$G1ECHO "GEOTAX STATE-SUPPLIED (TS-158 FORMAT) FILE INSTALLATION\n" >> $G1DBLOG
$G1ECHO "Installed on `date`\n" >>$G1DBLOG
$G1RTS/bin/runcbl $G1GTX/lib/GTTSMF.acu 
if [ $? -ne 0 ]
then
        $G1ECHO "\nThe database load program GTTSMF did not execute."
        $G1ECHO "Please contact Group 1 Support for assistance.\n"
        exit
fi
check_filesout;
