#!/bin/sh
# This script is used to install/update PRONTO from CD-ROM onto a Unix machine.
# NOTE: This script requires the runtimes.lst file to exist in the unix
# distribution directory on the CD.
#
# Modification History
# When			Who		What
# 15 June 2006	P.M.	Changed UnixWare to SCO6.
# 17 June 2005	P.M.	Added support for OpenPower Linux.
# 13 June 2003	P.M.	Changed echo for Linux.
# 14 Apr 2003	P.M.	Add XML library entries to /usr/lib.
# 30 July 2002	P.M.	Use lha instead of lharc.
# 25 July 2002	P.M.	Create xmllib directory.
# 24 Aug 2001	P.M.	Added DB2 option.
# 24 May 2001	P.M.	Ensure ORACLE_HOME/ORACLE_SID set for Oracle install.
# 28 Nov 2000	P.M.	Now use runtimes.lst file.
# 7 May 1999	P.M.	Written.

###########################################################################

get_mount_point ()
{
$ECHO "Enter the directory where the PRONTO CD-ROM is mounted.
"

MNT=`echo $0 | sed "s,/unix/.*,,"`
if [ "$MNT" != "" -a -d "$MNT" ] ; then
	cd "$MNT"
	MNT=`pwd`
elif [ "$0" = "./install" -o "$0" = "./INSTALL" ] ; then
	cd $START_CWD/..
	MNT=`pwd`
else
	if [ -d /cdrom ] ; then
		MNT="/cdrom"
	elif [ -d /CDROM ] ; then
		MNT="/CDROM"
	else
		MNT="/mnt"
	fi
fi
ans=""
while [ "$ans" = "" ] ; do
	$ECHO "Directory (default $MNT): \c"
	read ans
	if [ "$ans" = "" ] ; then
		ans="$MNT"
	fi
	if [ ! -d "$ans" ] ; then
		$ECHO "Directory $ans does not exist"
		ans=""
	else
		MNT="$ans"
	fi
done
}

###########################################################################

get_parent_dir ()
{
$ECHO "
Enter the parent directory where PRONTO is to be loaded.
e.g. if the PRONTO directory is /home/pronto then the parent is /home.
"
if [ "$PRONTO" != "" -a -d "$PRONTO" ] ; then
	cd $PRONTO/..
	PARENT=`pwd`
elif [ -d /pronto ] ; then
	PARENT="/"
elif [ -d /home ] ; then
	PARENT="/home"
elif [ -d /u ] ; then
	PARENT="/u"
else
	PARENT="/usr"
fi
ans=""
while [ "$ans" = "" ] ; do
	$ECHO "Parent directory (default $PARENT): \c"
	read ans
	if [ "$ans" = "" ] ; then
		ans="$PARENT"
	fi
	if [ ! -d "$ans" ] ; then
		$ECHO "Directory $ans does not exist"
		ans=""
	else
		PARENT="$ans"
	fi
done
if [ "$PARENT" = "/" ] ; then
	PRONTO="/pronto"
else
	PRONTO="$PARENT/pronto"
fi
export PRONTO
}

###########################################################################

not_found ()
{
$ECHO "
You can specify any of the available PRONTO-4GL runtime versions provided
on the CD.  They are located in $MNT/unix.
Run this install script specifying the lha program to use, and the
archive file containing the required version of the PRONTO runtime.
For example, the following command would install the AIX Oracle version
of the PRONTO runtime:

$0 lha.aix aix_or.lzh
"
exit 1
}

###########################################################################

get_system ()
{
SYS=`uname -a | awk '{ print $1 }'`
if [ "$SYS" = "AIX" ] ; then
	DEFOS=1
elif [ "$SYS" = "SCO_SV" ] ; then
	DEFOS=2
elif [ "$SYS" = "UNIX_SV" ] ; then
	DEFOS=3
elif [ "$SYS" = "UnixWare" ] ; then
	DEFOS=3
elif [ "$SYS" = "OpenUNIX" ] ; then
	DEFOS=3
elif [ "$SYS" = "OSF1" ] ; then
	DEFOS=4
elif [ "$SYS" = "HP-UX" ] ; then
	DEFOS=5
elif [ "$SYS" = "SunOS" ] ; then
	DEFOS=6
elif [ "$SYS" = "Linux" ] ; then
	# Get machine (hardware) type
	M=`uname -m`
	case "$M" in
	*ppc*)
		DEFOS=8
		;;
	*86*)
		DEFOS=7
		;;
	*)
		DEFOS=""
		;;
	esac
else
	SYS2=`uname -a | awk '{ print $2 }'`
	if [ "$SYS" = "$SYS2" ] ; then
		DEFOS=2
	else
		DEFOS=""
	fi
fi

while [ "$OS" = "" ] ; do

$ECHO "
Select Your Operating System and Machine type.

1) IBM pSeries (AIX)
2) SCO OpenServer5
3) SCO OpenServer6 / Unixware
4) DEC/Compaq/HP Alpha (OSF/TrueUnix)
5) HP 9000 (HP-UX)
6) Sun Sparc (Solaris)
7) Linux (Intel x86)
8) Linux (OpenPower - IBM pSeries)
99) Other
q) Quit
"
if [ "$DEFOS" != "" ] ; then
	$ECHO "System (default $DEFOS): \c"
else
	$ECHO "System: \c"
fi

read ans
if [ "$ans" = "" ] ; then
	ans="$DEFOS"
fi

case "$ans" in
1|aix)
	OS="AIX"
	OSCODE="aix"
	LHAEXT="aix"
	LHAEXTUPPER="AIX"
	;;
2|sco5)
	OS="SCO OpenServer5"
	OSCODE="scounix"
	LHAEXT="sco5"
	LHAEXTUPPER="SCO5"
	;;
3|unixware|sco6)
	OS="SCO OpenServer6/Unixware"
	OSCODE=""
	LHAEXT="sco6"
	LHAEXTUPPER="SCO6"
	;;
4|dec)
	OS="DEC/Compaq/HP Alpha (OSF/TrueUnix)"
	OSCODE="alpha"
	LHAEXT="dec"
	LHAEXTUPPER="DEC"
	;;
5|hp)
	OS="HP-UX"
	OSCODE="hp"
	LHAEXT="hp"
	LHAEXTUPPER="HP"
	;;
6|sun)
	OS="SUN Sparc Solaris"
	OSCODE=sun
	LHAEXT="sun"
	LHAEXTUPPER="SUN"
	;;
7|linux)
	OS="Linux (Intel x86)"
	OSCODE="linux"
	LHAEXT="lin"
	LHAEXTUPPER="LIN"
	;;
8|plinux)
	OS="Linux (OpenPower)"
	OSCODE="plinux"
	LHAEXT="plin"
	LHAEXTUPPER="PLIN"
	;;
99|other)
	OS="Other"
	DB="Unknown"
	OSCODE=""
	LHAEXT=""
	LHAEXTUPPER=""
	not_found
	exit 1
	;;
q|Q|quit|exit)
	$ECHO Installation aborted.
	exit 1
	;;
esac
done
if [ "$LHAEXT" = "" ] ; then
	LHAEXT="$OSCODE"
fi
XMLFILE="$MNT/unix/${LHAEXT}_xml.lzh"
XMLFILEUPPER="$MNT/UNIX/${LHAEXT}_XML.LZH"
}

###########################################################################

get_database ()
{
if [ -r /etc/oratab ] ; then
	DEFDB=1
elif [ "$ORACLE_HOME" != "" -a "$ORACLE_SID" != "" ] ; then
	DEFDB=1
elif [ "$DB2INSTANCE" != "" ] ; then
	DEFDB=2
else
	DEFDB=3
fi

while [ "$DB" = "" ] ; do

$ECHO "
Select the database that will be used by PRONTO in this implementation.

1) Oracle
2) DB2
3) C-ISAM
q) Quit

Database (default $DEFDB)? \c"

read ans
if [ "$ans" = "" ] ; then
	ans="$DEFDB"
fi

case "$ans" in
1|oracle)
	DB="Oracle"
	DBCODE="oracle"
	if [ "$ORACLE_HOME" = "" -o "$ORACLE_SID" = "" ] ; then
		$ECHO " "
		$ECHO "The ORACLE_HOME and ORACLE_SID environments must be"
		$ECHO "set in order for this installation/upgrade to succeed."
		$ECHO "Please set these environments to their correct values"
		$ECHO "and start the installation again."
		exit 1
	fi
	# Ensure Oracle's lib directory can be found
	LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:$ORACLE_HOME/lib64
	LIBPATH=$LIBPATH:$ORACLE_HOME/lib:$ORACLE_HOME/lib64
	SHLIB_PATH=$SHLIB_PATH:$ORACLE_HOME/lib:$ORACLE_HOME/lib64
	export LD_LIBRARY_PATH LIBPATH SHLIB_PATH
	;;

2|db2)
	DB="DB2"
	DBCODE="db2"
	;;

3|c-isam|cisam)
	DB="C-ISAM"
	DBCODE="cisam"
	;;

q|Q|quit|exit)
	$ECHO Installation aborted.
	exit 1
	;;
esac
done
}

###########################################################################

get_lzh ()
{
T=/tmp/pro1$$
A=/tmp/pro2$$
if [ -r "$MNT/unix/runtimes.lst" ] ; then
	RUNTIMES=$MNT/unix/runtimes.lst
elif [ -r "$MNT/UNIX/RUNTIMES.LST" ] ; then
	RUNTIMES=$MNT/UNIX/RUNTIMES.LST
else
	$ECHO "Required file not found: $MNT/unix/runtimes.lst"
	$ECHO "Aborting!"
	not_found
	exit 1
fi

# Due to inconsistencies between different awk implementations, passing
# parameter values is very difficult, so we generate a file with the
# awk program and correct values in it.
$ECHO "BEGIN {i = 1} \$1 == \"$OSCODE\" && \$2 == \"$DBCODE\" {print i \"|\" \$3 \"|\" \$4, \$5, \$6, \$7, \$8, \$9, \$10, \$11, \$12; i += 1}" >$A
sort $RUNTIMES | awk -f $A >$T

DEFRUNTIME=`cat $T | awk 'BEGIN {FS="|"; i=0} {if ($1>i) {i=$1}} END {print i}'`
if [ "$DEFRUNTIME" -eq 0 ] ; then
	$ECHO " "
	$ECHO "No PRONTO-4GL runtimes matching your criteria were found."
	rm $T
	not_found
	exit 1
fi

$ECHO "
The following PRONTO-4GL $OS $DB versions are defined:
"
cat $T | awk 'BEGIN {FS="|"} {print $1 ")", $3}'

$ECHO "q) Quit"

RUNTIME=""
while [ "$RUNTIME" = "" ] ; do
$ECHO "
Enter the required PRONTO-4GL version (default $DEFRUNTIME): \c"

read ans
if [ "$ans" = "" ] ; then
	ans="$DEFRUNTIME"
fi
if [ "$ans" = 'q' -o "$ans" = 'Q' ] ; then
	$ECHO "Installation aborted."
	rm $T
	exit 1
fi
if [ "$ans" -ge 1 -a "$ans" -le "$DEFRUNTIME" ] ; then
	RUNTIME=$ans
fi
done
#RUNLZH=`cat $T | awk -v "rt=$RUNTIME" 'BEGIN {FS="|"} $1==rt {print $2}'`
#RUNDESC=`cat $T | awk -v "rt=$RUNTIME" 'BEGIN {FS="|"} $1==rt {print $3}'`
RUNLZH=`cat $T | awk "BEGIN {FS=\"|\"} \\$1==$RUNTIME {print \\$2}"`
RUNDESC=`cat $T | awk "BEGIN {FS=\"|\"} \\$1==$RUNTIME {print \\$3}"`
rm $T
}

###########################################################################

get_confirmation ()
{
$ECHO "




PRONTO Installation/Update Details
==================================

PRONTO directory   : $PRONTO
PRONTO-4GL runtime : $RUNDESC
"

ans=""
while [ "$ans" = "" ] ; do
	$ECHO " "
	$ECHO "Confirm [y/n]? \c"
	read ans
	case "$ans" in
	y|Y|yes|YES)
		;;
	n|N|no|NO)
		$ECHO "Installation aborted"
		exit 1
		;;
	*)
		ans=""
		;;
	esac
done

# Now generate the name of the required archive file for this platform.
# NOTE: it may have already been specified on the command line!

if [ ! -d "$MNT/unix" -a -d "$MNT/UNIX" ] ; then
	if [ "$LHAFILE" = "" ] ; then
		RUNLZH=`echo $RUNLZH | awk '{print toupper($1)}'`
		LHAFILE="$MNT/UNIX/$RUNLZH"
	fi
	if [ "$LHA" = "" ] ; then
		LHA="$MNT/UNIX/LHARC.$LHAEXTUPPER"
	fi
else
	if [ "$LHAFILE" = "" ] ; then
		LHAFILE="$MNT/unix/$RUNLZH"
	fi
	if [ "$LHA" = "" ] ; then
		LHA="$MNT/unix/lha.$LHAEXT"
	fi
fi

# Verify that this platform is present on the CD

if [ ! -r "$LHAFILE" ] ; then
	$ECHO " "
	$ECHO "The required PRONTO-4GL archive file for the requested platform"
	$ECHO "could not be found."
	$ECHO "File: $LHAFILE"
	not_found
	exit 1
fi
if [ ! -r "$LHA" ] ; then
	$ECHO "The required LHA program for this platform could not be found."
	$ECHO "File: $LHA"
	not_found
	exit 1
fi
}

###########################################################################

do_install ()
{
$ECHO "

Creating directories..."
if [ ! -d "$PRONTO" ] ; then
	mkdir "$PRONTO"
	if [ $? -ne 0 ] ; then
		$ECHO "Could not create $PRONTO"
		exit 1
	fi
fi
if [ ! -d "$PRONTO/skeleton" ] ; then
	mkdir "$PRONTO/skeleton"
	if [ $? -ne 0 ] ; then
		$ECHO "Could not create $PRONTO/skeleton"
		exit 1
	fi
fi
if [ ! -d "$PRONTO/terminfo" ] ; then
	mkdir "$PRONTO/terminfo"
	if [ $? -ne 0 ] ; then
		$ECHO "Could not create $PRONTO/terminfo"
		exit 1
	fi
fi
if [ ! -d "$PRONTO/relnotes" ] ; then
	mkdir "$PRONTO/relnotes"
	if [ $? -ne 0 ] ; then
		$ECHO "Could not create $PRONTO/relnotes"
		exit 1
	fi
fi
MVBIN=YES
if [ -r "$PRONTO/bin/prospl" ] ; then
	$ECHO " "
	$ECHO "Save existing PRONTO bin in bin.old [y/n] (default y)? \c"
	read ans
	if [ "$ans" = 'n' -o "$ans" = 'N' ] ; then
		MVBIN=NO
	else
		if [ -d "$PRONTO/bin.old" ] ; then
			rm -rf "$PRONTO/bin.old"
		fi
	fi
fi
if [ "$MVBIN" = "YES" ] ; then
	if [ -d "$PRONTO/bin" ] ; then
		mv $PRONTO/bin $PRONTO/bin.old
	fi
fi
if [ ! -d "$PRONTO/bin" ] ; then
	mkdir "$PRONTO/bin"
	if [ $? -ne 0 ] ; then
		$ECHO "Could not create $PRONTO/bin"
		exit 1
	fi
fi
if [ "$DB" = "C-ISAM" -a ! -d "$PRONTO/isam" ] ; then
	mkdir "$PRONTO/isam"
	if [ $? -ne 0 ] ; then
		$ECHO "Could not create $PRONTO/isam"
		exit 1
	fi
	# Make GLS directory structure
	if mkdir "$PRONTO/isam/gls" ; then
		if mkdir "$PRONTO/isam/gls/lc11" ; then
			mkdir "$PRONTO/isam/gls/lc11/en_us"
		fi
	fi
	if [ ! -r "$PRONTO/isam/gls/lc11/en_us/0333.lco" ] ; then
		if [ -r "$MNT/unix/0333.lco" ] ; then
			cp "$MNT/unix/0333.lco" "$PRONTO/isam/gls/lc11/en_us"
		elif [ -r "$MNT/UNIX/0333.LCO" ] ; then
			cp "$MNT/UNIX/0333.LCO" "$PRONTO/isam/gls/lc11/en_us/0333.lco"
		fi
	fi
fi
if [ ! -d "$PRONTO/bms" ] ; then
	mkdir "$PRONTO/bms"
	chmod 777 "$PRONTO/bms"
fi
if [ ! -d "$PRONTO/cus" ] ; then
	mkdir "$PRONTO/cus"
	chmod 777 "$PRONTO/cus"
fi
if [ ! -d "$PRONTO/data" ] ; then
	mkdir "$PRONTO/data"
	mkdir "$PRONTO/data/demodata"
	chmod 777 "$PRONTO/data/demodata"
fi

$ECHO "Extracting files from archive...
"
cp "$LHA" /tmp/lha
chmod 755 /tmp/lha
cd $PARENT
/tmp/lha xf "$LHAFILE"
if [ $? -ne 0 ] ; then
	rm -f /tmp/lha
	$ECHO " "
	$ECHO " "
	$ECHO "NOTE: Errors were encountered extacting the files!"
	ans=""
	while [ "$ans" = "" ] ; do
		$ECHO " "
		$ECHO "Continue with installation [y/n]? \c"
		read ans
		case "$ans" in
		y|Y|yes|YES)
			;;
		n|N|no|NO)
			$ECHO "Installation aborted"
			exit 1
			;;
		*)
			ans=""
			;;
		esac
	done
fi
# Now install the XML libraries if they are not already there.
if [ ! -d "$PRONTO/xmllib" ] ; then
	if [ -r "$XMLFILE" -o -r "$XMLFILEUPPER" ] ; then
		mkdir "$PRONTO/xmllib"
		if [ $? -ne 0 ] ; then
			$ECHO "Could not create $PRONTO/xmllib"
			exit 1
		fi
		chmod 755 $PRONTO/xmllib
		cd $PRONTO/xmllib
		if [ -r "$XMLFILE" ] ; then
			/tmp/lha xf "$XMLFILE"
		elif [ -r "$XMLFILEUPPER" ] ; then
			/tmp/lha xf "$XMLFILEUPPER"
		fi
		chmod 755 $PRONTO/xmllib/*
		# To avoid hassles with library paths, add them to /usr/lib
		cd $PRONTO/xmllib
		for L in lib*
		do
			if [ ! -r /usr/lib/$L ] ; then
				ln -s $PRONTO/xmllib/$L /usr/lib/$L
			fi
		done
		cd $PARENT
	fi
fi
rm -f /tmp/lha

$ECHO "



Running the standard PRONTO-4GL installation/setup program...

"

cd $PARENT
chmod +x $PRONTO/bin/install $PRONTO/bin/proregister $PRONTO/bin/proadmin
chmod +x $PRONTO/bin/protic
pronto/bin/install

# Invoke the applications installation program

if [ -r "$PRONTO/lib/proctrl" -a -x "$PRONTO/lib/sh_environs" ] ; then
	. "$PRONTO/lib/sh_environs"
	if [ "$BMS" != "" ] ; then
		$ECHO " "
		$ECHO "Invoke PRONTO application software installation [y/n]? \c"
		read ans
		if [ "$ans" = 'Y' -o "$ans" = 'y' ] ; then
			if [ -r $MNT/PROINST.OP6 ] ; then
				cp $MNT/PROINST.OP6 /tmp/proinst.op6
				su - psd -c "$PRONTO/bin/prospl /tmp/proinst $MNT $BMS/.."
				rm -f /tmp/proinst.op6
			else
				su - psd -c "$PRONTO/bin/prospl $MNT/proinst $MNT $BMS/.."
			fi
		fi
	fi
fi
}

###########################################################################
# Main line
###########################################################################

SYS=`uname -a | awk '{ print $1 }'`
if [ "$SYS" = "Linux" ] ; then
	ECHO="echo -e"
else
	ECHO=echo
fi

OS=""
OSCODE=""
DB=""
DBCODE=""
LHA=""
LHA1=""
LHAFILE=""
LHAFILE1=""

$ECHO "
PRONTO CD-ROM based installation/update for Unix/Linux Systems
==============================================================
"
if [ $# = 2 ] ; then
	LHA1="$1"
	LHAFILE1="$2"
elif [ $# != 0 ] ; then
	$ECHO "Usage: $0 [ lha-name archive-name ]"
	exit 1
fi

START_CWD=`pwd`
cd /		# cd here for existence tests
if [ "$LHA1" = "" ] ; then
	get_mount_point
elif [ ! -r "$LHA1" -o ! -r "$LHAFILE1" ] ; then
	get_mount_point
	LHA="$LHA1"
	LHAFILE="$LHAFILE1"
	if [ ! -r "$LHA1" ] ; then
		LHA="$MNT/unix/$LHA1"
		if [ ! -r "$LHA" ] ; then
			$ECHO "$LHA1: file not found"
			exit 1
		fi
	fi
	if [ ! -r "$LHAFILE1" ] ; then
		LHAFILE="$MNT/unix/$LHAFILE1"
		if [ ! -r "$LHAFILE" ] ; then
			$ECHO "$LHAFILE1: file not found"
			exit 1
		fi
	fi
fi
get_parent_dir
if [ "$LHA" = "" ] ; then
	get_system
fi
if [ "$LHAFILE" = "" ] ; then
	get_database
	get_lzh
	get_confirmation
fi
do_install
