#! /bin/sh
#
# $Id: nmc_config.sh,v 1.29.2.11.2.2.2.1 2009/10/29 22:58:10 grands1 Exp $ Copyright (c) 2000-2009 EMC Corporation.
#

#
# Copyright (c) 2000-2008 EMC Corporation.
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.
#

#
# Function:
#	ask
#
# Description:
#	Prompts the user for data entry.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			The prompt string to display.
#	$2			The default value for return.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	result			The data entered by the user, or the value
#				of $default if the user accepted the default.
#
ask()
{
    prompt=$1
    default=$2
    echo
    if [ -z "${default}" ]; then
	case "$ARCH" in
	    Linux ) echo -n "${prompt}? ";;
	    HP-UX ) echo "${prompt}? \c";;
	    AIX ) echo "${prompt}? \c";;
	esac
    else
	case "$ARCH" in
	    Linux ) echo -n "${prompt} [${default}]? ";;
	    HP-UX ) echo "${prompt} [${default}]? \c";;
	    AIX ) echo "${prompt} [${default}]? \c";;
	esac
    fi

    read ans
    if [ -z "$ans" ]; then
	result="$default"
    else
	result="$ans"
    fi
}

#
# Function:
#	yesno
#
# Description:
#	Prompts the user for a yes or no answer.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			The question to display.
#	$2			The default value, either 'y' or 'n'.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	result			Value of 'y' or 'n'.
#
yesno()
{
    while true ; do
	ask "$1" "$2"
	if [ `expr "X${result}" : 'X[yY]'` -ne 0 ]; then
	    result=y
	    break
	elif [ `expr "X${result}" : 'X[nN]'` -ne 0 ]; then
	    result=n
	    break
	else
	    echo "Please respond \`y' or \`n'"
	fi
    done
}

#
# Function:
#	validdir
#
# Description:
#	Checks validity of a directory.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			The prompt string to display.
#	$2			Default return value if no response.
#	$3			Verify directory existence, default no
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	result			Pathname to directory.
#
# Notes:
#
validdir()
{
    direxist=$3 
    while true; do
	ask "$1" "$2"
	if [ `expr X${result} : 'X/'` -lt 1 ]; then
	    echo
	    echo "ERROR: \"${result}\" must be an absolute path."
	elif [ ! -z "${direxist}" -a "${direxist}" = "yes" ]; then
	    if [ ! -d "${result}" ]; then
	        echo
	        echo "ERROR: The specified directory does not exist."
	    else
		break 
	    fi
	else
	    break
	fi	
    done
    # result is set from ask()
}

#
# Function:
#	valid_user_group
#
# Description:
#	Checks if the given user and group is valid
#	and if the user is member of that group.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			User name.
#	$2			Group name.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	result			valid or the error.
#
valid_user_group()
{
    USERID=`cat /etc/passwd | grep "^$1:" | cut -d ":" -f 3`
    if [ "X$USERID" != "X" ] ; then
        expr $USERID + 1 > /dev/null 2>&1
	if [ $? -eq 0 ]; then
	    if [ $USERID -lt 0 ] ; then
		validuserid=false
	    else
	    	validuserid=true
	    fi
	else
	    validuserid=false
	fi
    else
	validuserid=false
    fi 
    PRIMARYGRPID=`cat /etc/passwd | grep "^$1:" | cut -d ":" -f 4`
    if [ "$validuserid" = "true" -a  "X$PRIMARYGRPID" != "X" ] ; then
	GROUPID=`cat /etc/group | grep "^$2:" | cut -d ":" -f 3`
	GROUPMEMBERS=`cat /etc/group | grep "^$2:" | cut -d ":" -f 4`
	if [ "X$GROUPID" != "X" ] ; then
	    if [ "$PRIMARYGRPID" = "$GROUPID" ] ; then
		result="valid";
	    else 
    		if [ "X$GROUPMEMBERS" != "X" ] ; then
		    PRESENT=`echo "$GROUPMEMBERS" | grep "$1"`
		    if [ -n "$PRESENT" ] ; then
	    		result="valid"
		    else
			result="no_membership"
		    fi
		else
		    result="no_membership"
		fi
	    fi
	else
	    result="no_group"
	fi
    else
	result="no_user"
    fi
}

#
# Function:
#	validport
#
# Description:
#	Prompt for a TCP/IP port number and optionally validate
#	that the port is not in use.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			The prompt string to display.
#	$2			The default value for return.
#	$3			1 if in use is okay, 0 if must be free.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	result			A valid port number.
#
validport()
{
    usedok=$3
    while true; do
	ask "$1" "$2"

	# Verify that number is numeric
	expr $result + 1 > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    echo
	    echo "ERROR: ${result} is not a valid port number!"
	    continue
	fi

	if [ $result -lt 1024 -o $result -gt 49151 ]; then
	    echo
	    echo "ERROR: Valid port range is between 1024 and 49151"
	    continue
	fi

	if [ $result -eq ${LGTO_DBSRVPORT} ]; then
	    echo
	    echo "ERROR: Port ${LGTO_DBSRVPORT} is reserved for database server"
	    continue
	fi
	
	if [ $result -eq 9002 ]; then
	    echo
	    echo "ERROR: Port 9002 is the preferred port for EMC Backup Advisor product"
	    continue
	fi

	if [ $usedok -eq 0 ]; then
	    inuse=`netstat -na|grep " LISTEN"|grep  "[.:]${result} "`
	    if [ ! -z "${inuse}" ]; then
		echo
		echo "WARNING: Port ${result} is already in use."
		port=$result
		yesno "Do you wish to specify a different port number" y
		if [ "$result" = "n" ] ; then
		    result=$port
		    break
		fi
	    else
		break
	    fi
	fi
    done
    # result is set from ask()
}

#
# Function:
# 	is_ascii_path	
#
# Description:
#	Checks if the specified path is ascii 
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#	test_path		path to check
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	result		 	y or n	
#
is_ascii_path()
{
    test_path=$1
    # empty out all acceptable characters 
    result_temp_path=`echo $test_path | sed 's?[a-zA-Z0-9\^\?_/\$&:!,\.\(\)\*=~# @\+%-]??g'`
    # Check if there are any characters still present
    if [ "X$result_temp_path" != "X" ]; then
	result=n
    else
	result=y
    fi
}

#
# Function:
#	runcmd
#
# Description:
#	Runs the command and arguments specified by the positional
#	parameters, and logs the output to $instlog. If the command
#	returns a non-zero exit status, this script exits with a
#	status of 1.
#
# Arguments:
#
#	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			Command to execute.
#	$2, $3, ...		Command arguments.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#
runcmd()
{
    echo "** running: $*" >> "$instlog"
    $* >> "$instlog" 2>&1
    if [ $? -ne 0 ] ; then
	echo "ERROR: Command $* failed!"
	exit 1
    fi
}

#
# Function:
#	checkforupgrade
#
# Description:
#	Checks presence of old config file and reads its values
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_OLDCONF
#	LGTO_OLDCONFVER
#	DEFAULT_BASEPORT
#	DEFAULT_GSTPORT
#	DEFAULT_DBDIR
#
checkforupgrade()
{
    # Check if an old config file is present
    if [ -d ${base}/etc ]; then
	CONFFILES=`ls -lat ${base}/etc | grep "\.gstd.conf"`
    fi
    if [ -n "$CONFFILES" ]; then
	OLDCONF=`ls -lt ${base}/etc/.gstd.conf* | awk 'NR == 1 { print $NF }'`
	# Get the version of the old Console installation
	# Get the last component of the file name
	OLDCONFVER=`echo $OLDCONF | awk -F/ 'NR == 1 { print $NF }'`
	#Get the version
	OLDCONFVER=`echo $OLDCONFVER | cut -d "." -f 4,5 | sed 's?[^0-9]??g'`

	if [ -f "$OLDCONF" ]; then
	    echo
	    echo "NOTE"
	    echo "===="
	    echo "Install has detected the configuration file of a previous lgtonmc"
	    echo "package. Install will attempt to read the configuration parameters"
	    echo "in this file and present them as default values where appropriate."
	    echo "Please modify any value that is incorrect or needs to be changed."
	    echo

	    DEFAULT_BASEPORT=`awk '/http_svc_port/ { print substr($4,1,length($4)-1) }' "$OLDCONF"`
	    DEFAULT_GSTPORT=`awk '/clnt_svc_port/ { print substr($4,1,length($4)-1) }' "$OLDCONF"`
	    DEFAULT_DBSRVPORT=`awk '/db_svc_port/ { print substr($4,1,length($4)-1) }' "$OLDCONF"`
	    # For database pathname take care of situation where pathname has spaces
	    DEFAULT_DBDIR=`awk '/current_db_dir/ {
		    DBNM = $4
		    i = 5
		    while ( i <= NF ) {
			DBNM = DBNM " " $i
			++i
		    }
		}
		END {
		    print substr(DBNM,2,length(DBNM)-3)
		}' "$OLDCONF"`
	    if [ ! -f "$DEFAULT_DBDIR/$DBNAME" ]; then
		DEFAULT_DBDIR=
	    fi

	    LGTO_OLDCONF="$OLDCONF"
	    LGTO_OLDCONFVER="$OLDCONFVER"
	fi
    fi
}


#
# Function:
#	getwebusergrp
#
# Description:
#	Prompts for the web server user and group.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_HTTPUSER		web server user
#	LGTO_HTTPGROUP		web server group
#
getwebusergrp()
{
    result="invalid" 
    if [ "$ARCH" != "HP-UX" ] ; then
	valid_user_group nobody nobody
    fi
    if [ "$result" = "valid" ]; then
	defaultusergroup="nobody/nobody"
    fi

    echo
    echo "For optimum security, the embedded web server inside this product must"
    echo "run as a non root user. Please specify a local user name and group name"
    echo "the web server must run as. It is recommended that the user and group"
    echo "you specify have limited privileges and file access permissions. Please"
    echo "create such a local user and group first if required."
    if [ "$ARCH" != "HP-UX" ] ; then
	echo "Please specify in the format user/group. For example nobody/nobody."
    else
	echo "Please specify in the format user/group. For example www/www."
    fi

    askwebuser=1
    attempt_count=0
    while [ $askwebuser -eq 1 ]; do
	ask "Please specify the user/group for the web server" "$defaultusergroup"
	if [ -z "$result" ]; then
	    continue
	fi
	username=`echo $result | awk -F "/" ' { print $1 } '`
	groupname=`echo $result | awk -F "/" ' { print $2 } '`
	if [ -z "$username" -o -z "$groupname" ] ;then
	    echo
	    echo "ERROR: Valid user name and group name must be specified"
	    continue
	fi
	if [ "$username" = "root" ] ; then
	    echo
	    echo "ERROR: User name cannot be root"
	    continue
	fi
	valid_user_group "$username" "$groupname"
	if [ "$result" = "valid" ]; then
	    echo
	    echo "Using $username as user name and $groupname as group name"
	    LGTO_HTTPUSER="$username"
	    LGTO_HTTPGROUP="$groupname"
	    break
	fi
	if [ "$result" = "no_user" ] ; then
	    echo
	    echo "ERROR: $username is not a valid OS user" 
	fi
	if [ "$result" = "no_group" ] ; then
	    echo
	    echo "ERROR: $groupname is not a valid OS group"
	fi
	if [ "$result" = "no_membership" ] ; then
	    echo
	    echo "ERROR: $username is not a member of the group $groupname"
	fi

	attempt_count=`expr $attempt_count + 1`
	if [ $attempt_count -lt 3 ]; then
	   continue
	else
	    attempt_count=0 
	fi
	
	echo
	echo "The validity of the specified user name and group name could not be"
	echo "confirmed. It is recommended that you fix the problem before proceeding" 
	echo "with the install." 
	yesno "Do you want to proceed with the install anyway" n 
    
	if [ "$result" = "y" ]; then
	    echo
	    echo "Using $username as user name and $groupname as group name"
	    askwebuser=0
	    LGTO_HTTPUSER="$username"
	    LGTO_HTTPGROUP="$groupname"
	fi
    done
}
	
#
# Function:
#	getwebport
#
# Description:
#	Prompts for the web server port.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_HTTPPORT		web server port
#
getwebport()
{
    # get embedded web server port
    # Set default value if not set already
    if [ "X$DEFAULT_BASEPORT" = "X" ]; then
	DEFAULT_BASEPORT=9000
    fi
    validport "What port should the web server use" $DEFAULT_BASEPORT 0
    LGTO_HTTPPORT="$result"
}

#
# Function:
#	getdbport
#
# Description:
#	Gets for the database  server port.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_DBSRVPORT		database server port
#
getdbport()
{
    if [ "X$DEFAULT_DBSRVPORT" = "X" ]; then
	DEFAULT_DBSRVPORT=2638
    fi
    while true; do
	inuse=`netstat -na|grep " LISTEN"|grep  "[.:]${DEFAULT_DBSRVPORT} "`
	if [ ! -z "${inuse}" ]; then
	    DEFAULT_DBSRVPORT=`expr ${DEFAULT_DBSRVPORT} + 1`
	else
	    LGTO_DBSRVPORT=${DEFAULT_DBSRVPORT}
	    break
	fi
    done
}

#
# Function:
#	getgstport
#
# Description:
#	Prompts for the GST server port.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_GSTPORT		GST server port
#
getgstport()
{
    # get GST server port
    # Set default value if not set already
    if [ "X$DEFAULT_GSTPORT" = "X" ]; then
	DEFAULT_GSTPORT=`expr $LGTO_HTTPPORT + 1`
	# Make sure default port is in valid range
	if [ $DEFAULT_GSTPORT -gt 49151 ]; then
	    DEFAULT_GSTPORT=9001
	fi
	# Do not use EMC Backup Advisor port
	if [ $DEFAULT_GSTPORT -eq 9002 ]; then
	    DEFAULT_GSTPORT=9003
	fi
    fi
    while true; do
	validport "What port should the GST server use" $DEFAULT_GSTPORT 0
	if [ $result -eq $LGTO_HTTPPORT ]; then
	    echo
	    echo "ERROR: Web server and GST server port numbers cannot be the same"
	else
	    LGTO_GSTPORT="$result"
	    break
	fi
    done
}

#
# Function:
#	getdbdir
#
# Description:
#	Prompts for the database directory.
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_DATADIR
#	LGTO_OVERWRITE
#
getdbdir()
{
    # get database dir
    # Set default value if not set already
    if [ "X$DEFAULT_DBDIR" = "X" ]; then
   	DEFAULT_DBDIR="${base}/lgto_gstdb"
    fi 
    while true ; do
	validdir "What directory should be used for the ${PKG} database" "$DEFAULT_DBDIR"
	datadir="$result"
	is_ascii_path "$datadir"
	if [ "$result" = "n" ]; then
	    echo
	    echo "ERROR: The path contains invalid characters: $result_temp_path"
	    continue
	fi

	if [ -f "$datadir/$DBNAME" ]; then
	    yesno "$datadir/$DBNAME already exists, do you want to retain this database" y
	    if [ "$result" = "y" ]; then
		LGTO_OVERWRITE="N"
		break
	    fi
	fi
	parent=`dirname "$datadir"`
	if [ -d "$parent" ] ; then
	    # check if database exists
	    if [ -f "$datadir/$DBNAME" ] ; then
		yesno "$datadir/$DBNAME already exists, is it okay to remove it" n
		if [ "$result" = "y" ] ; then
		    # We will remove the log file also if present
		    LGTO_OVERWRITE="Y"
		    break
		fi
	    elif [ -f "$datadir/$DBLOG" ] ; then
		# For some reason, if only the log file is present - remove it
		yesno "$datadir/$DBLOG already exists, is it okay to remove it" n
		if [ "$result" = "y" ] ; then
		    LGTO_OVERWRITE="Y"
		    break
	        fi
	    else
		break
	    fi
	else
	    yesno "Parent directory $parent does not exist, should it be created" y 
	    if [ "$result" = "y" ] ; then
		break
	    fi
	fi
    done
    LGTO_DATADIR="$datadir"
}


#
# Function:
#	getnsrdir
#
# Description:
#	
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_NSRDIR
#
getnsrdir()
{
    case "$ARCH" in
	Linux ) DEFAULT_NSRDIR="/usr/sbin";;
	HP-UX ) DEFAULT_NSRDIR="/opt/networker/bin";;
	AIX ) DEFAULT_NSRDIR="/usr/bin";;
    esac

    # get NetWorker directory
    while true ; do
	validdir "Where are the NetWorker binaries installed" "$DEFAULT_NSRDIR" "yes"
	if [ -f "${result}/nsrexecd" ] ; then
	    break
	else
	    echo
	    echo "ERROR: nsrexecd(1m) is not present in ${result}! Please re-enter."
	fi
    done
    LGTO_NSRDIR="$result"
}


#
# Function:
#	getbootval
#
# Description:
#	
#
# Arguments:
#
# 	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#	LGTO_KEEPBOOT
#
getbootval()
{
    yesno "Start daemons at end of configuration" n
    LGTO_KEEPBOOT="$result"
}


#
# main
#
PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
export PATH
ARCH=`uname`
PKG="LGTOnmc"
DBNAME="lgto_gst.db"
DBLOG="lgto_gst.log"
case "$ARCH" in
    Linux )
	    base=`rpm -q --qf "%{INSTALLPREFIX}\n" lgtonmc 2> /dev/null`
	    ;;
    AIX )
	    base="/opt/lgtonmc"
	    ;;
    HP-UX ) 
	    base="/opt/lgtonmc"
	    ;;
esac
savepsm="${base}/bin/savepsm"
gstparam="${base}/bin/gstparam"
gstetc="${base}/etc"
gstconf="${base}/etc/gstd.conf"
gstd="${base}/bin/gstd"
gstping="${base}/bin/gstping"
gstconfig="${base}/bin/gstconfig"
gstlocalize="${base}/bin/gst_localize"
gstlogs="${base}/logs"
gstcst="${base}/cst"
apachelogs="${base}/apache/logs"
instlog="${gstlogs}/install.log"
gstweb="${base}/web"
gstmod="${base}/mod"
stparam_file="${gstweb}/params.html"
gstjar="/${PKG}/java/gst.jar"
gwtjar="/${PKG}/java/gwt.jar"
gwtresjar="/${PKG}/java/gwt_res.jar"
comtskjar="/${PKG}/java/comtsk.jar"
initfile="${gstetc}/gst"
initdbpath="$gstetc/lgto_gst.db"
savepsmsh="${gstetc}/savepsm.sh"
syblib="${base}/sybasa/lib"
bindir="${base}/bin"
jconnjar="/${PKG}/java/jconn2.jar"
jaxpjar="/${PKG}/java/jaxp.jar"
jcchartjar="/${PKG}/java/jcchart.jar"
jcfieldjar="/${PKG}/java/jcfield.jar"
jcpagelayoutjar="/${PKG}/java/jcpagelayout.jar"
formsjar="/${PKG}/java/forms.jar"
looksjar="/${PKG}/java/looks.jar"
antlrjar="/${PKG}/java/antlr.jar"
retrospectjar="/${PKG}/java/retrospect.jar"
gappsjar="/${PKG}/java/gapps.jar"
gappsresjar="/${PKG}/java/gapps_res.jar"
yfilesjar="/${PKG}/java/y.jar"
ysvgjar="/${PKG}/java/ysvg.jar"
batikjar="/${PKG}/java/batik.jar"
jide_commonjar="/${PKG}/java/jide-common.jar"
jide_componentsjar="/${PKG}/java/jide-components.jar"
jide_dockjar="/${PKG}/java/jide-dock.jar"

gemsjar="/${PKG}/java/gconsole.jar"
gemsresjar="/${PKG}/java/gconsole_res.jar"
gemshelpjar="/${PKG}/java/nmc_help.jar"

nwjar="/${PKG}/java/nwadmin.jar"
nwresjar="/${PKG}/java/nwadmin_res.jar"

umask 0022

# stop any existing GST server
case "$ARCH" in
    Linux )
	    pkill gstd
	    ;;
esac

# create logs directory
mkdir -p "$gstlogs"
if [ $? -ne 0 ] ; then
    echo "Could not create $gstlogs"
    exit 1
fi

# create apache logs directory
mkdir -p "$apachelogs"
if [ $? -ne 0 ] ; then
    echo "Could not create $apachelogs"
    exit 1
fi

# the following functions mimic the Solaris request script
checkforupgrade
getwebusergrp
getdbport
getwebport
getgstport
getdbdir
getnsrdir
getbootval

cat <<EOF

Creating installation log in ${instlog}.

Performing initialization. Please wait...

EOF

# Insert install date in logfile
echo "  " >> "$instlog"
echo "Install begins `date`" >> "$instlog"
echo "  " >> "$instlog"

# Insert OS version in logfile
echo "  " >> "$instlog"
echo "OS Version is `uname -a`" >> "$instlog"
echo "  " >> "$instlog"

# create database
if [ "$LGTO_OVERWRITE" = "Y" ] ; then
    if [ -f "$LGTO_DATADIR/lgto_gst.db" ]; then
	echo "Removing $LGTO_DATADIR/lgto_gst.db" >> "$instlog"
	rm -f "$LGTO_DATADIR/lgto_gst.db" >> "$instlog" 2>&1
    	if [ $? -ne 0 ] ; then
	    echo "Could not remove $LGTO_DATADIR/lgto_gst.db" 
	    exit 1
	fi 
    fi
    if [ -f "$LGTO_DATADIR/lgto_gst.log" ]; then
	echo "Removing $LGTO_DATADIR/lgto_gst.log" >> "$instlog"
	rm -f "$LGTO_DATADIR/lgto_gst.log" >> "$instlog" 2>&1
    	if [ $? -ne 0 ] ; then
	    echo "Could not remove $LGTO_DATADIR/lgto_gst.log"
	    exit 1
	fi 
    fi
    if [ -f "$LGTO_DATADIR/gstd_db.conf" ]; then
	echo "Removing $LGTO_DATADIR/gstd_db.conf" >> "$instlog"
	rm -f "$LGTO_DATADIR/gstd_db.conf" >> "$instlog" 2>&1
    	if [ $? -ne 0 ] ; then
	    echo "Could not remove $LGTO_DATADIR/gstd_db.conf"
	    exit 1
	fi 
    fi
fi
if [ ! -d "$LGTO_DATADIR" ] ; then
    echo "Creating directory $LGTO_DATADIR" >> "$instlog"
    mkdir -p "$LGTO_DATADIR" >> "$instlog" 2>&1
    if [ $? -ne 0 ] ; then
	echo "Could not create $LGTO_DATADIR"
	exit 1
    fi 
fi

# Set LD_LIBRARY_PATH
case "$ARCH" in
    Linux )
	    LD_LIBRARY_PATH="${bindir}:${syblib}:$LD_LIBRARY_PATH"
	    export LD_LIBRARY_PATH 
	    ;;
    AIX )
	    LIBPATH="${bindir}:${syblib}:$LIBPATH"
	    export LIBPATH
	    ;;
    HP-UX ) 
	    SHLIB_PATH="${bindir}:${syblib}:$SHLIB_PATH"
	    export SHLIB_PATH
	    ;;
esac

# configure system
# Make previous .gstd.conf file the current one
if [ "X" != "X$LGTO_OLDCONF" ]; then
    runcmd cp -f "$LGTO_OLDCONF" "$gstconf"
fi

runcmd "$gstparam" -f "$gstconf" -d -n pkg_lang_packs
runcmd "$gstparam" -f "$gstconf" -d -n pkg_langjar_files

runcmd "$gstparam" -f "$gstconf" -d -n http_svc_port
runcmd "$gstparam" -f "$gstconf" -a -n http_svc_port -t int -v $LGTO_HTTPPORT

runcmd "$gstparam" -f "$gstconf" -d -n clnt_svc_port
runcmd "$gstparam" -f "$gstconf" -a -n clnt_svc_port -t int -v $LGTO_GSTPORT

runcmd "$gstparam" -f "$gstconf" -d -n db_svc_port
runcmd "$gstparam" -f "$gstconf" -a -n db_svc_port -t int -v $LGTO_DBSRVPORT 

runcmd "$gstparam" -f "$gstconf" -d -n module_dir
runcmd "$gstparam" -f "$gstconf" -a -n module_dir -t string -v "$gstmod"

runcmd "$gstparam" -f "$gstconf" -d -n pkg_web_dirmap
runcmd "$gstparam" -f "$gstconf" -a -n pkg_web_dirmap -t list -l \
    -n alias -t string -v "/$PKG" -n path -t string -v "$gstweb"

runcmd "$gstparam" -f "$gstconf" -d -n pkg_jar_files
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gstjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gwtjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gwtresjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$comtskjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jconnjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jaxpjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jcchartjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jcfieldjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jcpagelayoutjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$formsjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$looksjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$antlrjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$retrospectjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gappsjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gappsresjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$yfilesjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$ysvgjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$batikjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jide_commonjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jide_componentsjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$jide_dockjar"

runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gemsjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gemsresjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$gemshelpjar"

runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$nwjar"
runcmd "$gstparam" -f "$gstconf" -a -n pkg_jar_files -t string -v "$nwresjar"

runcmd "$gstparam" -f "$gstconf" -d -n web_server_user 
runcmd "$gstparam" -f "$gstconf" -a -n web_server_user -t string -v "$LGTO_HTTPUSER"
runcmd "$gstparam" -f "$gstconf" -d -n web_server_group
runcmd "$gstparam" -f "$gstconf" -a -n web_server_group -t string -v "$LGTO_HTTPGROUP"

runcmd "$gstparam" -f "$gstconf" -d -n jre_location

echo "Configuring current_db_dir to $LGTO_DATADIR in gstd.conf" >> "$instlog"
"$gstparam" -f "$gstconf" -d -n current_db_dir
"$gstparam" -f "$gstconf" -a -n current_db_dir -t string -v "$LGTO_DATADIR" >> "$instlog" 2>&1
if [ $? -ne 0 ] ; then
    echo "Failed to configure current_db_dir to $LGTO_DATADIR in $gstconf"
    exit 1
fi 

# configure component files
runcmd "$gstconfig"

# Configure Startup parameters file
# Create the file if required
if [ ! -f "$stparam_file" ] ; then
    echo "{" > "$stparam_file"
    echo "};" >> "$stparam_file"
fi
runcmd "$gstparam" -f "$stparam_file" -d -n initial_launch 
if [ "$LGTO_OVERWRITE" != "N" ] ; then
    runcmd "$gstparam" -f "$stparam_file" -a -n initial_launch -t boolean -v true 
    runcmd "$gstparam" -f "$stparam_file" -d -n "GST_LOGON_BANNER" 
    "$gstparam" -f "$stparam_file" -a -n "GST_LOGON_BANNER" -t string -v "Warning: Authorized user only" >> "$instlog" 2>&1 
    if [ $? -ne 0 ] ; then
    	echo "Failed to configure GST_LOGON_BANNER in $stparam_file"
    	exit 1
    fi 
else 
    runcmd "$gstparam" -f "$stparam_file" -a -n initial_launch -t boolean -v false 
fi

# setup /etc/init.d files/links
echo "** Creating gst rc file **" >> "$instlog"

if [ "$ARCH" = "Linux" ] ; then
    runcmd rm -f /etc/init.d/gst
    sed -e "s,^GST_HOME=.*,GST_HOME=$base," < "$initfile" > /etc/init.d/gst
    if [ $? -ne 0 ] ; then
	echo "Could not create /etc/init.d/gst"
	exit 1
    fi
    runcmd chmod 755 /etc/init.d/gst
    runcmd chkconfig --add gst
    # Post processing to take care of bug 161870 in chkconfig 1.3.13.2
    STARTLEVELS="3 5"
    STOPLEVELS="0 1 2 6"
    for i in ${STARTLEVELS}; do
	if [ -f /etc/rc.d/rc${i}.d/S-1gst ]; then
	    runcmd mv /etc/rc.d/rc${i}.d/S-1gst /etc/rc.d/rc${i}.d/S97gst
        fi
    done
    for i in ${STOPLEVELS}; do
	if [ -f /etc/rc.d/rc${i}.d/K-1gst ]; then
	    runcmd mv /etc/rc.d/rc${i}.d/K-1gst /etc/rc.d/rc${i}.d/K03gst
	fi
    done
fi

if [ "$ARCH" = "HP-UX" ] ; then
    runcmd rm -f /sbin/init.d/gst
    sed -e "s,^GST_HOME=.*,GST_HOME=$base," < "$initfile" > /sbin/init.d/gst
    if [ $? -ne 0 ] ; then
	echo "Could not create /sbin/init.d/gst"
	exit 1
    fi
    runcmd chmod 755 /sbin/init.d/gst
    # /etc/init.d is a syblink to /sbin/init.d on HP
    runcmd /bin/rm -f /sbin/rc2.d/S???gst
    runcmd /bin/ln -s /sbin/init.d/gst /sbin/rc2.d/S905gst

    runcmd /bin/rm -f /sbin/rc1.d/K???gst
    runcmd /bin/ln -s /sbin/init.d/gst /sbin/rc1.d/K195gst
fi

if [ "$ARCH" = "AIX" ] ; then
    runcmd rm -f /etc/rc.gst
    sed -e "s,^GST_HOME=.*,GST_HOME=$base," < "$initfile" > /etc/rc.gst
    if [ $? -ne 0 ] ; then
	echo "Could not create /etc/rc.gst"
	exit 1
    fi
    runcmd chmod 755 /etc/rc.gst
    if [ "`/bin/grep ^rcgst /etc/inittab`" ]; then
	# Do not use runcmd, ignore error as we are ok if mkitab works 
    	echo "** Running rmitab **" >> "$instlog"
	rmitab rcgst
    fi	
    echo "** Running mkitab **" >> "$instlog"
    mkitab -i rcnsr "rcgst:2:wait:sh /etc/rc.gst start"
    if [ $? -ne 0 ] ; then
	echo "mkitab failed!"
	exit 1
    fi
fi

echo "** Copying savepsm to $LGTO_NSRDIR **" >> "$instlog"
runcmd rm -f $LGTO_NSRDIR/savepsm
runcmd /bin/cp $savepsm $LGTO_NSRDIR
echo "** Copying savepsm.sh to $LGTO_NSRDIR **" >> "$instlog"
runcmd rm -f $LGTO_NSRDIR/savepsm.sh
sed -e "s,XGST_INSTALL_DESTDIRX,$base,
	s,XNSR_BIN_PATHX,$LGTO_NSRDIR," < "$savepsmsh" > $LGTO_NSRDIR/savepsm.sh
if [ $? -ne 0 ] ; then
    echo "Could not create $LGTO_NSRDIR/savepsm.sh"
    exit 1
fi
runcmd chmod 755 "$LGTO_NSRDIR/savepsm.sh"

# Run the localization command
runcmd "$gstlocalize"

#check if ldap has been configured
if [ -f ${gstcst}/Config.xml ] ; then
    LDAPMODE=`cat ${gstcst}/Config.xml | grep "authorityName"`
fi

# For new database or non ldap mode, make sure auth method is always native 
if [ "$LGTO_OVERWRITE" != "N" -o -z "${LDAPMODE}" ] ; then
    if [ -f ${gstcst}/Config.xml ] ; then
	runcmd rm -f  ${gstcst}/Config.xml
    fi
    if [ -f ${gstcst}/csp.clb ]; then
	runcmd rm -f ${gstcst}/csp.clb
    fi
    if [ -f ${gstcst}/csp.clb.bak ] ; then
	runcmd rm -f ${gstcst}/csp.clb.bak
    fi
    if [ -f ${gstcst}/csp.clb.FCD ]; then
	runcmd rm -f ${gstcst}/csp.clb.FCD
    fi
    if [ -f ${gstcst}/csp.clb.bak.FCD ] ; then
	runcmd rm -f ${gstcst}/csp.clb.bak.FCD
    fi
    if [ -f ${gstcst}/CST_ActiveDirectory.props ] ; then
	runcmd rm -f ${gstcst}/CST_ActiveDirectory.props
    fi
    if [ -f ${gstcst}/upgrade_cst.tag ] ; then
        runcmd rm -f ${gstcst}/upgrade_cst.tag
    fi
fi

# If no previous CST Config.xml is present, copy from template
if [ ! -f ${gstcst}/Config.xml ] ; then
    runcmd /bin/cp ${gstcst}/Config.xml.template ${gstcst}/Config.xml
fi

# If CST needs upgrading, drop the tag file in place
if [ -s ${gstcst}/csp.clb ]; then
    touch ${gstcst}/upgrade_cst.tag
fi

# touch all the jar files, to force web start to refetch them
for jarfile in ${gstweb}/java/*.jar; do
    touch $jarfile
done

# Move the database
echo "** Move the database to $LGTO_DATADIR **" >> "$instlog"
if [ "$LGTO_OVERWRITE" != "N" ] ; then
    finaldbpath="$LGTO_DATADIR/lgto_gst.db"
    echo "Moving $initdbpath to $finaldbpath" >> "$instlog"	
    runcmd mv "$initdbpath" "$finaldbpath"
    if [ $? -ne 0 ] ; then
	echo "Could not move $initdbpath to $LGTO_DATADIR"
	exit 1
    fi 
else
    echo "Deleting $initdbpath" >> "$instlog"
    runcmd rm -f "$initdbpath"
fi

# delete any old .gstd.conf file
if [ "X" != "X$LGTO_OLDCONF" ]; then
    runcmd rm -f "$LGTO_OLDCONF"
fi

# Start the GST server
if [ "$LGTO_KEEPBOOT" = "y" ] ; then
    echo "Starting the GST server" >> "$instlog"
    if [ "$ARCH" = "AIX" ] ; then
	/etc/rc.gst start
	if [ $? -ne 0 ] ; then
	    echo "Warning: Failed to start daemon."
	    echo "         Please run the script /etc/rc.gst manually."
	fi
    elif [ "$ARCH" = "HP-UX" ] ; then
	/sbin/init.d/gst start
	if [ $? -ne 0 ] ; then
	    echo "Warning: Failed to start daemon."
	    echo "         Please run the script /sbin/init.d/gst manually."
	fi
    else
	/etc/init.d/gst start
	if [ $? -ne 0 ] ; then
	    echo "Warning: Failed to start daemon."
	    echo "         Please run the script /etc/init.d/gst manually."
	fi
    fi
fi

echo "Installation successful."

# Don't let the user run this twice...
rm $0

exit 0
