#!/bin/sh
#set -xv
: ${G1DCG:?}
#
# 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
                        $G1ECHO ""
                done
                }
}


#
# Function to ask if user wants to continue
#
confirmToUpdate()
{
$G1ECHO "\nWelcome to the configuration tool for the ETM setup scripts."
$G1ECHO "\nWould you like to update the setup scripts for your ETM installation?"
$G1ECHO "(y)es - continue"
$G1ECHO "(n)o stop the configuration process\n"
$G1ECHO "(y/n)==> \c"
yorn=
read yorn Y
case "$yorn" in
	[nN]*)
	$G1ECHO "\nExiting GeoTax configuration process.\n"
        exit 1;
	;;
        [yY]*)	:
        ;;
esac
}

#
# Function to get full path to G1DIR
getG1DIRPath()
{
G1DIR=${G1DCG}/server/modules

if [ ! -d "$G1DIR/gtx/bin" ]; then
	Clearit
	$G1ECHO "\n  Warning: The G1DCG environment variable is set incorrectly or the\n"
	$G1ECHO "ETM module is incorrectly installed. The ETM module was not found\n"
	$G1ECHO "in ${G1DIR}/gtx\n".
	$G1ECHO "\nExiting...\n"
	exit 1
else
	touch $G1DIR/gtx/bin/setup > /dev/null
	if [ $? -eq 0 ]; then
		G1RTS=$G1DIR/gtx/acushare
	else
		$G1ECHO "\n You do not have permissions to update the setup scripts in the ETM module.  Exiting...\n"
		exit 1
	fi
fi
}


#
# Function to get full path to existing database
askDBPath()
{
$G1ECHO "\nDo you have an existing database you would like to use?\n"
$G1ECHO "If no then the default location will be used: ${G1DIR}/db\n"

$G1ECHO "(y)es - enter path on next screen"
$G1ECHO "(n)o use default\n"
$G1ECHO "(y/n)==> \c"

G1GTXDBTMP=
G1GTXDB=
yorn=
read yorn Y
case "$yorn" in
	[nN]*)
	G1GTXDB="${G1DIR}/db"
	;;
	[yY]*)
		Clearit
		$G1ECHO "\nPlease enter the full path to your db location.\n"
		$G1ECHO "==> \c"

		read G1GTXDBTMP
		Clearit
		$G1ECHO "\nYou entered:"
		$G1ECHO "\n"$G1GTXDBTMP"\n"
		$G1ECHO "\nIs that correct?\n"
		$G1ECHO "(y)es - continue"
		$G1ECHO "(n)o re-enter the path\n"
		$G1ECHO "(y/n)==> \c"
		
		yorn2=
		read yorn2 Y
		case "$yorn2" in
			[nN]*)
				Clearit
				askDBPath
			;;
			[yY]*)
				G1GTXDB=$G1GTXDBTMP
			;;
		esac
	;;

esac
}


get_g1sys()
{
G1SYS=
_HOSTNAME=`hostname | sed 's/\..*//'`

case `uname -s` in
            AIX|aix )   G1SYS=AIX		        ;;
 	 HP-UX|hp-ux)	G1SYS=HPUX       		;;
	LINUX|Linux )   G1SYS=LINUX                     ;;
        SUNOS|SunOS )   G1SYS=SOLARIS			;;
        	  * )	G1SYS=OTHER			;;
esac

# set the echo command
case $G1SYS in
	SOLARIS)
		if test -d  /usr/5bin
		then
			G1ECHO="/usr/5bin/echo"
		else
			G1ECHO="echo"
		fi
        ;;
	LINUX)	G1ECHO="echo -e"
        ;;
	*)	G1ECHO="echo"
        ;;
esac

export G1SYS G1ECHO
}

update_setup_files()
{
`cat $G1DIR/gtx/bin/setup | \
     awk "!/^G1SYS/{print} \
           /^G1SYS/{print \"G1SYS='$G1SYS'   \"}" |
     awk "!/^G1DIR/{print} \
           /^G1DIR/{print \"G1DIR='$G1DIR'   \"}" |
     awk "!/^G1GTXDB/{print} \
           /^G1GTXDB/{print \"G1GTXDB='$G1GTXDB' \"}" |
     awk "!/^G1RTS/{print} \
           /^G1RTS/{print \"G1RTS='$G1RTS'   \"}" |
     awk "!/^COBDIR/{print} \
           /^COBDIR/{print \"COBDIR='$COBDIR'   \"}" > \
            $G1DIR/gtx/bin/setup.tmp`
`mv $G1DIR/gtx/bin/setup.tmp $G1DIR/gtx/bin/setup`

# Also for the c shell

`cat $G1DIR/gtx/bin/setup.csh | \
     awk "!/^setenv G1SYS/{print} \
           /^setenv G1SYS/{print \"setenv G1SYS '$G1SYS'   \"}" |
     awk "!/^setenv G1DIR/{print} \
           /^setenv G1DIR/{print \"setenv G1DIR '$G1DIR'   \"}" |
     awk "!/^setenv G1GTXDB/{print} \
           /^setenv G1GTXDB/{print \"setenv G1GTXDB '$G1GTXDB'   \"}" |
     awk "!/^setenv G1RTS/{print} \
           /^setenv G1RTS/{print \"setenv G1RTS '$G1RTS'   \"}" |
     awk "!/^setenv COBDIR/{print} \
           /^setenv COBDIR/{print \"setenv COBDIR '$COBDIR'   \"}" > \
            $G1DIR/gtx/bin/setup.cshtmp`
`mv $G1DIR/gtx/bin/setup.cshtmp $G1DIR/gtx/bin/setup.csh`

}

detRTS()
{
    # determine cobol runtime shlib name and bitsize
     EXTLIBS32="-L/usr/lib/hpux32 -lunwind -lstd_v2 -lstream -lCsup -lcl -ldld -lpthread"
     EXTLIBS64="-L/usr/lib/hpux64 -lunwind -lstd_v2 -lstream -lCsup -lcl -ldld -lpthread"

     if [ -e libruncbl\.a ]
     then
       CBLFILE=`find . -name "libruncbl\.s?"`
       export EXTLIBS=$EXTLIBS32
     elif [ -e libruncbl64\.a ]
     then
       CBLFILE=`find . -name "libruncbl64\.s?"`
       export EXTLIBS=$EXTLIBS64
     else
       $G1ECHO "Fatal error: Unable to detect required COBOL runtile library in $PWD"
       exit 2
     fi
}

relinkHP()
{
if [ "$G1SYS" = "HPUX" ]
    then
       # Go to runtime system's lib area.
       cd $G1RTS/lib
       # determine acucobol runtime shlib name and bitsize
       detRTS
       # Backup the makerun and the libruncbl
         cp ./makerun ./makerun_saved
         cp ./${CBLFILE} ./${CBLFILE}_saved
       make relinkrun
       # If there was a problem...
             if [ "$?" -ne "0" ]
             then
                 # Return things to the way they were
                 cp ./makerun_saved ./makerun
                 cp ./${CBLFILE}_saved ./${CBLFILE}
             else
                 # Removed saved copies.
                 rm ./makerun_saved
                 rm ./${CBLFILE}_saved
             fi
             cd $G1DIR/gtx/bin
fi

}


######################
# Main part of script
#####################

Clearit
unset G1SYS
unset G1DIR
unset G1RTS
unset G1GTXDB
#G1SYS=
#G1DIR=
#G1RTS=
#G1GTXDB=

trap '$G1ECHO "Configuration Aborted\n"; exit' 2

get_g1sys
confirmToUpdate
Clearit
getG1DIRPath
Clearit
askDBPath
update_setup_files
relinkHP

$G1ECHO "\nG1SYS is $G1SYS"
$G1ECHO "G1DIR is $G1DIR"
$G1ECHO "G1RTS is $G1RTS"
$G1ECHO "G1GTXDB is $G1GTXDB"
$G1ECHO "\nSetup scripts successfully updated."



