#!/bin/sh
#
###########################################################################
###########################################################################
#
#	Installation script for PowerAlert (UNIX)
#
#	@(#)pa_install	11.00	10/02/2001
#
#
###########################################################################
###########################################################################

#
#

CDROM_DIR=`pwd`					# save the dir where the stuff comes from

TMP_DIR="/tmp"

SHUTDOWN_INI="remotesd.ini"
SERVER_INI="paserver.ini"

#PAPLUS_RSD=pap_rsd			# remote shutdown monitor executable name

PAPLUS_SERVER=paserver			# server executable name

CLIENT_DIR="console"			# client directory
DRIVERS_DIR="drivers"			# drivers directory
INI_DIR="ini"				# initialization directory
LANGUAGE_DIR="language"			# language directory
LOG_DIR="log"				# log directory
SERVER_DIR="engine"			# server directory
USERS_DIR="users"			# users directory
UN_INSTALL="pa_uninstall"		# uninstall program
UN_INSTALL_PATH="/etc/pa_uninstall"	# uninstall path
 
DEFAULT_INSTALLATION_DIR="/poweralert"      # was "/opt/paplus"

BASH_PROFILE=/root/.bash_profile

#	some utility variables to make things easier to read

NO="no"
YES="yes"

  INVALID_RESPONSE="InVaLiD_ReSpOnSe"

PAPLUS_CMD=pa


#	try to determine wether we need to use "-n" or "\c" to suppress a newline

DASH_N=`echo -n`

if [ "$DASH_N" = "-n" ]
then
	NO_NEWLINE_PARAM=""		# -n not supported
	NO_NEWLINE_TERM="\\c"
else
	NO_NEWLINE_PARAM="-n"		# -n supported
	NO_NEWLINE_TERM=""
fi


if [ "$DEVELOPMENT_ENV" = "yes" ]
then
	SOURCE_MEDIA="/pa_tmp"
	DEFAULT_INSTALLATION_DIR="/pa_installation"
else
	SOURCE_MEDIA="/mnt/cdrom"
fi

#	setup the initial values of our control variables

DONE=$NO

AUTO_START_FLAG=$YES                       #Ask if this needs to be here ?

DEFAULT_AUTO_START_FLAG=$YES               #Ask if this needs to be here ?

DEFAULT_UPS_IS_LOCAL=$YES                  #Ask if this needs to be here ?

DRIVER_FILE_SUFFIX=".drv"

DEFAULT_UPS_SERVER="No server specified"   #Ask if this needs to be here ? 


# set the defaults since we'll config one way or the other (local or remote)

UPS_COMM_PORT=$DEFAULT_UPS_COMM_PORT
UPS_SPEED=$DEFAULT_UPS_SPEED
UPS_TYPE=$DEFAULT_UPS_TYPE
UPS_NOM_VOLTAGE=$DEFAULT_UPS_NOM_VOLTAGE
DRIVER_FILE=$DEFAULT_DRIVER_FILE
UPS_SERVER=""


# init files and directories

 
RC0_DIR=
RC2_DIR=
RC3_DIR=
RC4_DIR=
RC5_DIR=

INIT_D_DIR=

RC0_K_FILE=
RC2_S_FILE=
RC2_K_FILE=
RC3_S_FILE=
RC3_K_FILE=
RC4_S_FILE=
RC4_K_FILE=
RC5_S_FILE=
RC5_K_FILE=

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


#	center a line of text on the screen

centerLine() {

	echo "$*" | awk '
#!/bin/awk

{
	screen_width = 80;

	lots_of_spaces = "                                                                                                           ";

	printf("%s%s\n", substr(lots_of_spaces, 1, (screen_width - length($0))/2), $0);
}
'
}

# display a nice screen header

newScreen() {

	clear

	echo

	centerLine "$* - PowerAlert 11.0 Installation"

	echo
}



#	suppress new lines when using this echo...

no_nl_echo() {

	echo $NO_NEWLINE_PARAM "$*$NO_NEWLINE_TERM"

}


#	check for a "yes" or "no" response
#
#	return $YES / $NO / $INVALID_RESPONSE
#
#	second argument is default return value if 1st argument is empty
#

checkYorN() {

	if [ -z "$1" ]
	then

		echo $2					# return default value

	else

		case "$1" in

		y|Y|YES|YEs|Yes|yES|yeS|yes|yEs|YeS)
			echo "$YES"
			;;

		n|N|NO|No|nO|no)
			echo "$NO"
			;;

		*)
			echo $INVALID_RESPONSE
			;;
		esac
	fi
}


# Make sure that user is running as root.
#
# If not exit the instalation
#

checkRoot() {
    CURRENT_USER=`id | cut -f2 -d\( | cut -f1 -d\)`
 
    if [ "$CURRENT_USER" != "root" ]
    then
        echo "You must be 'root' to install PowerAlert."
		echo "Please log in as 'root' and re-run this file."
		
        exit 1
    fi
}


make_dir(){
if [ ! -d $TARGET_DIR ]
then
        if [ $TARGET_DIR = $INSTALLATION_DIR ]
        then
		no_nl_echo "Creating installation directory... "
		mkdir -p $INSTALLATION_DIR
	else
		no_nl_echo "Creating $TARGET_DIR directory... "
		mkdir $TARGET_DIR
	fi 

	if [ ! "$?" = "0" ]
	then
		echo
		echo "There was an error in creating the installation directory."
		echo
		echo "This could be because you do not have permission to create files there."
		echo
		echo "The installation of PowerAlert has been aborted."
		echo
		exit 1
fi

	echo "done."
	echo

	sleep 1			# just to sort of "pace" things a little bit
fi

}


###########################################################################
###########################################################################
#
#	actual installation starts here...
#
###########################################################################
###########################################################################

# Make sure the user is running as root.
checkRoot

newScreen Overview

cat <<_EOF_
During the installation of PowerAlert, you will be asked several
questions.  Most of these questions will have default responses enclosed
in square brackets "[ ]".  If the default value is suitable for your
installation, you can simply press <ENTER>.  If you would like to specify
a different value for any specific question, just type it in and press <ENTER>
to submit your response.  After answering all of the installation questions,
you will be given a chance to review and confirm your responses.  Once you
confirm your responses, PowerAlert will be installed on your UNIX system
using the responses you entered.


Press <ENTER> to continue the PowerAlert installation.

_EOF_

read CONTINUE_RESPONSE


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


#
#	present a list of operating systems to the user and allow them to pick
#	one
#
select_os_type() {


	RESPONSE="$INVALID_RESPONSE"

	while [ "$RESPONSE" = "$INVALID_RESPONSE" ]
	do

		echo
		echo "The following operating systems are supported by PowerAlert:"
		echo

		echo "	1) Sun Solaris for SPARC"
		echo "	2) Sun Solaris for Intel"
		echo "	3) Santa Cruz Operation UNIX"
		echo "	4) Hewlett Packard HP-UX 9.x"
		echo "	5) Hewlett Packard HP-UX 10.x and 11.x"
		echo "	6) International Business Machines AIX"
		echo "	7) Silicon Graphics IRIX"
		echo "	8) Digital UNIX"
		echo "	9) Linux"
	

		echo

		no_nl_echo "Please select the operating system you are using: "

		read RESPONSE

		case "$RESPONSE" in

		1)	TARGET_SYSTEM=SOLARIS_2_SPARC
			break
			;;

		2)	TARGET_SYSTEM=SOLARIS_2_INTEL
			break
			;;

		3)	TARGET_SYSTEM=SCO_UNIX
			break
			;;

		4)	TARGET_SYSTEM=P_UNIX_9
			break
			;;

		5)	TARGET_SYSTEM=HP_UNIX
			break
			;;

		6)	TARGET_SYSTEM=IBM_AIX
			break
			;;

		7)	TARGET_SYSTEM=SGI_IRIX
			break
			;;

		8)	TARGET_SYSTEM=DEC_UNIX
			break
			;;

		9)	TARGET_SYSTEM=LINUX
			break
			;;

		*)	echo
			echo "Please respond with a number corresponding to the above selections"
			echo

			RESPONSE="$INVALID_RESPONSE"
			;;
		esac
	done
}



#	try to determine the system we are one so that we set things
#	correctly

TARGET_SYSTEM=$INVALID_RESPONSE		# means we either didn't set it or
					# couldn't figure it out
case "`uname -s`" in

SunOS)		# Some sort of Solaris system

	case "`uname -a | cut -f6 -d' '`" in

	sparc)
		TARGET_SYSTEM=SOLARIS_2_SPARC
		;;

	i386)
		TARGET_SYSTEM=SOLARIS_2_INTEL
		;;

	esac
	;;

SCO_SV)
	TARGET_SYSTEM=SCO_UNIX
	;;

HP-UX)
	case "`uname -r`" in
	*10* | *11*)
		TARGET_SYSTEM=HP_UNIX
		;;
	*9*)
		TARGET_SYSTEM=HP_UNIX_9
		;;
	esac
	;;

AIX)
	TARGET_SYSTEM=IBM_AIX
	;;

IRIX)
	TARGET_SYSTEM=SGI_IRIX
	;;

OSF1)
	TARGET_SYSTEM=DEC_UNIX
	;;

Linux)
	TARGET_SYSTEM=LINUX
	;;

esac

 
if [ "$TARGET_SYSTEM" != "$INVALID_REPONSE" ]
then

#	we think we know the OS in use but let's verify with the user to be sure

	newScreen "Operating System Identification"

	RESPONSE=$INVALID_RESPONSE

	case "$TARGET_SYSTEM" in

	SOLARIS_2_SPARC)
		echo "Your system appears to be running Solaris 2 for SPARC."
		;;

	SOLARIS_2_INTEL)
		echo "Your system appears to be running Solaris 2 for Intel."
		;;

	SCO_UNIX)
		echo "Your system appears to be running SCO UNIX."
		;;

	HP_UNIX_9)
		echo "Your system appears to be running Hewlett-Packard HP-UX 9.x."
		;;

	HP_UNIX)
		echo "Your system appears to be running Hewlett-Packard HP-UX 10.x or 11.x."
		;;

	IBM_AIX)
		echo "Your system appears to be running IBM AIX."
		;;

	SGI_IRIX)
		echo "Your system appears to be running Silicon Graphics IRIX."
		;;

	DEC_UNIX)
		echo "Your system appears to be running Digital OSF1."
		;;

	LINUX)
		echo "Your system appears to be running Linux."
		;;

	esac

	echo

	while [ true ]
	do
		no_nl_echo "Is this correct [\"$YES\"]? "

		read RESPONSE

		RESPONSE=`checkYorN "$RESPONSE" "$YES"`

		if [ "$RESPONSE" = "$INVALID_RESPONSE" ]
		then
			echo
			echo "Please answer \"yes\" or \"no\""
		else
			break
		fi
	done

	if [ "$RESPONSE" = "$NO" ]
	then
		select_os_type
	fi

else	# we couldn't tell so the user needs to identify it for us...

	select_os_type

fi


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


#	since we use a common install file for each platform, we need to setup
#	some variables based on the system we are installing on

case "$TARGET_SYSTEM" in

SOLARIS_2_SPARC)

	SYSTEM="Solaris 2 (SPARC)"

	PAP_OS="SOLARIS_2_SPARC"

	RC0_DIR="/etc/rc0.d"
	RC2_DIR="/etc/rc2.d"
	
	INIT_D_DIR="/etc/init.d"

	RC0_K_FILE="$RC0_DIR/K99paplus"
	RC2_S_FILE="$RC2_DIR/S99paplus"
	RC2_K_FILE="$RC2_DIR/K99paplus"

	PAPLUS_CMD_DIR="/usr/sbin"

	DEFAULT_UPS_COMM_PORT="/dev/term/b"


	DEFAULT_LOGGING_DIR="/var/adm/paplus"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/sol2sp.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/sol2sp.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/SOL2SP.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/SOL2SP.PA;1"
	
	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/usr/sbin/wall -a"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

SOLARIS_2_INTEL)

	SYSTEM="Solaris 2 (SPARC)"

	PAP_OS="SOLARIS_2_INTEL"

	RC0_DIR="/etc/rc0.d"
	RC2_DIR="/etc/rc2.d"

	INIT_D_DIR="/etc/init.d"

	RC0_K_FILE="$RC0_DIR/K99paplus"
	RC2_S_FILE="$RC2_DIR/S99paplus"
	RC2_K_FILE="$RC2_DIR/K99paplus"

	PAPLUS_CMD_DIR="/usr/sbin"

	DEFAULT_UPS_COMM_PORT="/dev/term/a"

	DEFAULT_LOGGING_DIR="/var/adm/paplus"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/sol2in.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/sol2in.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/SOL2IN.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/SOL2IN.PA;1"

	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/usr/sbin/wall -a"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

SGI_IRIX)

	SYSTEM="SGI IRIX"

	PAP_OS="SILICON_GRAPHICS_IRIX"

	RC0_DIR="/etc/rc0.d"
	RC2_DIR="/etc/rc2.d"

	INIT_D_DIR="/etc/init.d"

	RC0_K_FILE="$RC0_DIR/K99paplus"
	RC2_S_FILE="$RC2_DIR/S99paplus"
	RC2_K_FILE="$RC2_DIR/K99paplus"

	PAPLUS_CMD_DIR="/usr/sbin"

	DEFAULT_UPS_COMM_PORT="/dev/ttyd1"

	DEFAULT_LOGGING_DIR="/var/adm/paplus"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/sgi.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/sgi.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/SGI.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/SGI.PA;1"

	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/sbin/wall"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

LINUX)

	SYSTEM="Linux"

	PAP_OS="LINUX"

	INITTAB_FILE="/etc/inittab"

	INIT_D_DIR="/etc/rc.d/init.d"

	PAPLUS_CMD_DIR="/usr/sbin"

	DEFAULT_UPS_COMM_PORT="/dev/ttyS0"

	DEFAULT_LOGGING_DIR="/var/log/paplus"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/linux.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/linux.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/LINUX.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/LINUX.PA;1"

	CPIO_FLAGS="idumB"

	BROADCAST_COMMAND="/usr/bin/wall"

        # Some of the following files are not neccessary, since they are not used in the Linux installation.
 

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $RC3_S_FILE $RC3_K_FILE $RC5_S_FILE $RC5_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

DEC_UNIX)

	SYSTEM="Digital UNIX"

	PAP_OS="DEC_UNIX"

	RC0_DIR="/sbin/rc0.d"
	RC2_DIR="/sbin/rc2.d"

	INIT_D_DIR="/sbin/init.d"

	RC0_K_FILE="$RC0_DIR/K99paplus"
	RC2_S_FILE="$RC2_DIR/S99paplus"
	RC2_K_FILE="$RC2_DIR/K99paplus"

	PAPLUS_CMD_DIR="/usr/sbin"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/dec.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/dec.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/DEC.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/DEC.PA;1"

	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/usr/sbin/wall"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

SCO_UNIX)

	SYSTEM="Santa Cruz Operation UNIX"

	PAP_OS="SCO_UNIX"

	RC0_DIR="/etc/rc0.d"
	RC2_DIR="/etc/rc2.d"

	INIT_D_DIR="/etc/init.d"

	RC0_K_FILE="$RC0_DIR/K99paplus"
	RC2_S_FILE="$RC2_DIR/S99paplus"
	RC2_K_FILE="$RC2_DIR/K99paplus"


	PAPLUS_CMD_DIR="/etc"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/sco.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/sco.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/SCO.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/SCO.PA;1"

	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/etc/wall"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

HP_UNIX_9)

	SYSTEM="Hewlett Packard UNIX 9.x"

	PAP_OS="HP_UNIX"

	INITTAB_FILE="/etc/inittab"

	PAPLUS_CMD_DIR="/etc"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/hpux.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/hpux.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/HPUX.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/HPUX.PA;1"

	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/etc/wall"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

HP_UNIX)

	SYSTEM="Hewlett Packard UNIX 10.x"

	PAP_OS="HP_UNIX"

	RC0_DIR="/sbin/rc0.d"
	RC2_DIR="/sbin/rc2.d"

	INIT_D_DIR="/sbin/init.d"

	RC0_K_FILE="$RC0_DIR/K99paplus"
	RC2_S_FILE="$RC2_DIR/S99paplus"
	RC2_K_FILE="$RC2_DIR/K99paplus"

	PAPLUS_CMD_DIR="/usr/sbin"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/hpux.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/hpux.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/HPUX.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/HPUX.PA;1"

	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/usr/sbin/wall"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

IBM_AIX)

	SYSTEM="International Business Machines AIX"

	PAP_OS="IBM_AIX"

	INITTAB_FILE="/etc/inittab"

	PAPLUS_CMD_DIR="/usr/sbin"

	PAPLUS_INPUT_FILE_LOWER="$CDROM_DIR/aix.pa"
	PAPLUS_INPUT_FILE_LOWER_EXT="$CDROM_DIR/aix.pa;1"
	PAPLUS_INPUT_FILE_UPPER="$CDROM_DIR/AIX.PA"
	PAPLUS_INPUT_FILE_UPPER_EXT="$CDROM_DIR/AIX.PA;1"

	CPIO_FLAGS="icdumB"

	BROADCAST_COMMAND="/usr/sbin/wall"

	FILES_THAT_SHOULD_BE_REMOVED="$RC0_K_FILE $RC2_S_FILE $RC2_K_FILE $PAPLUS_CMD_DIR/pa"
	;;

*)	echo
	echo "INTERNAL ERROR - unknown system type \"$TARGET_SYSTEM\""
	echo

	exit 1
	;;
esac


###########################################################################
###########################################################################
#
#	collect installation information here...
#
###########################################################################
###########################################################################


while [ "$DONE" = "$NO" ]
do
	newScreen "Installation Questions"

	echo "Enter the directory where PowerAlert should be installed"
	no_nl_echo "[\"$DEFAULT_INSTALLATION_DIR\"]: "

	read RESPONSE

	if [ -z "$RESPONSE" ]
	then
		RESPONSE=$DEFAULT_INSTALLATION_DIR		# handle the default response
	fi

	INSTALLATION_DIR=$RESPONSE					# otherwise take it as is

	if [ ! -d "$INSTALLATION_DIR" ]
	then
		echo
		echo "This directory does not exist and will be created during the"
		echo "PowerAlert installation process."
		echo
	fi


	AUTO_START_FLAG=$INVALID_RESPONSE

	while [ "$AUTO_START_FLAG" = "$INVALID_RESPONSE" ]
	do

		echo
		echo "Would you like PowerAlert to start automatically at system boot"
		no_nl_echo "time [\"$DEFAULT_AUTO_START_FLAG\"]? "

		read RESPONSE

		RESPONSE=`checkYorN "$RESPONSE" "$DEFAULT_AUTO_START_FLAG"`

		if [ "$RESPONSE" = "$INVALID_RESPONSE" ]
		then
			echo
			echo "Please answer \"yes\" or \"no\""
		else
			AUTO_START_FLAG=$RESPONSE
		fi
	done


	UPS_IS_LOCAL="yes"

#	UPS_IS_LOCAL=$INVALID_RESPONSE
#
#	while [ "$UPS_IS_LOCAL" = "$INVALID_RESPONSE" ]
#	do

#		echo
#		no_nl_echo "Is the UPS to be monitored attached to this system [\"$DEFAULT_UPS_IS_LOCAL\"]? "

#		read RESPONSE

#		RESPONSE=`checkYorN "$RESPONSE" "$DEFAULT_UPS_IS_LOCAL"`

#		if [ "$RESPONSE" = "$INVALID_RESPONSE" ]
#		then
#			echo
#			echo "Please answer \"yes\" or \"no\""
#		else
#			UPS_IS_LOCAL=$RESPONSE
#		fi
#	done


#	if [ "$UPS_IS_LOCAL" = "$YES" ]

#
#
	# don't ask if dumb


	# if the UPS isn't local, just get the server's name

#	echo
#	echo "Enter the name of the server to obtain UPS status information"
#	no_nl_echo "from: "
#
#	read RESPONSE

#	UPS_SERVER=$RESPONSE
	


###########################################################################
###########################################################################
#
#	verify what the user entered and allow them to fix any errors...
#
###########################################################################
###########################################################################


	newScreen "Confirmation"


	echo "General installation information:"

	echo

	echo "  Installation directory:			$INSTALLATION_DIR"

	echo

	echo "  Automatically start at boot time:		$AUTO_START_FLAG"

	echo
	echo

#	echo "  UPS Engine:					$UPS_SERVER"
#	echo

	ALL_OK=$INVALID_RESPONSE

	DEFAULT_ALL_OK=$YES

	while [ "$ALL_OK" = "$INVALID_RESPONSE" ]
	do

		echo
		no_nl_echo "Are the above parameters correct [\"$DEFAULT_ALL_OK\"]? "

		read RESPONSE

		RESPONSE=`checkYorN "$RESPONSE" "$DEFAULT_ALL_OK"`

		if [ "$RESPONSE" = "$INVALID_RESPONSE" ]
		then
			echo
			echo "Please answer \"yes\" or \"no\""
		else
			ALL_OK=$RESPONSE
		fi
	done

	if [ "$ALL_OK" = "$YES" ]
	then

		break

	else

		# set the default values to the current values so the user doesn't
		# lose the data just entered

		DEFAULT_INSTALLATION_DIR="$INSTALLATION_DIR"

		DEFAULT_AUTO_START_FLAG="$AUTO_START_FLAG"

		DEFAULT_UPS_IS_LOCAL="$UPS_IS_LOCAL"

		DEFAULT_UPS_SERVER="$UPS_SERVER"

	fi
done


 

###########################################################################
###########################################################################
#
#	start the actual installation process now...
#
###########################################################################
###########################################################################

TRIPPLITE_PAP_HOME=$INSTALLATION_DIR

export TRIPPLITE_PAP_HOME

newScreen "In progress"

TARGET_DIR=$INSTALLATION_DIR
make_dir

cp $UN_INSTALL /etc
chmod 744 $UN_INSTALL_PATH

cd $INSTALLATION_DIR

#for TARGET_DIR in $CLIENT_DIR $DRIVERS_DIR $INI_DIR $LANGUAGE_DIR $LOG_DIR $SERVER_DIR $USERS_DIR
#do
#	make_dir
#done


##### extract the files from the system-specific cpio archive

no_nl_echo "Extracting files... "

# various UNIXes have different ways of interpreting filenames on CDs. 
# We try a couple here
if [ -r "$PAPLUS_INPUT_FILE_LOWER" ]
then
	# standard - all lowercase 
	PAPLUS_INPUT_FILE="$PAPLUS_INPUT_FILE_LOWER"
elif [ -r "$PAPLUS_INPUT_FILE_LOWER_EXT" ]
then
	# some platforms add ';1' to the end.
	PAPLUS_INPUT_FILE="$PAPLUS_INPUT_FILE_LOWER_EXT"
elif [ -r "$PAPLUS_INPUT_FILE_UPPER" ]
then
	# all uppercase
	PAPLUS_INPUT_FILE="$PAPLUS_INPUT_FILE_UPPER"
elif [ -r "$PAPLUS_INPUT_FILE_UPPER_EXT" ]
then
	# some platforms add ';1' to the end.
	PAPLUS_INPUT_FILE="$PAPLUS_INPUT_FILE_UPPER_EXT"
else
	echo
	echo "Could not find install package for $SYSTEM.  Exiting"
	exit 1
fi
	

cpio -$CPIO_FLAGS <$PAPLUS_INPUT_FILE 2>/dev/null

if [ ! "$?" = "0" ]
then
	echo
	echo "There was an error in extracting the files from the CPIO archive."
	echo
	echo "This could be because there is not enough free space in the file system where"
	echo "the installation directory is located or you do not have permission"
	echo "to create files there."
	echo
	echo "The installation of PowerAlert has been aborted."
	echo
	exit 1
fi

echo "done."
echo

chmod 0755 *
chmod 0755 $CLIENT_DIR/paclient
chmod 0755 $SERVER_DIR/*
chown root.root *
chown root.root */*

sleep 1


##### install the "pa" command to the appropriate dir (reference install dir)

no_nl_echo "Installing \"pa\" command in \"$PAPLUS_CMD_DIR\"... "

# If an init.d directory exists, make sure that unistall will remove the pap
# file there as well
if [ -n "$INIT_D_DIR" -a -d "$INIT_D_DIR" ]
then
	FILES_THAT_SHOULD_BE_REMOVED="$FILES_THAT_SHOULD_BE_REMOVED $INIT_D_DIR/$PAPLUS_CMD $TRIPPLITE_PAP_HOME/$PAPLUS_CMD"
fi

# In case pa already exists, delete it first
rm -f $PAPLUS_CMD_DIR/$PAPLUS_CMD

eval sed <$SERVER_DIR/$PAPLUS_CMD >$PAPLUS_CMD_DIR/$PAPLUS_CMD "-e \"s!PAP_INSTALLATION_DIR!$INSTALLATION_DIR!\"" "-e \"s!PAP_OPERATING_SYSTEM!$PAP_OS!\"" "-e \"s!FILES_TO_BE_REMOVED!$FILES_THAT_SHOULD_BE_REMOVED!\"" "-e \"s!PAP_INITTAB_FILE!$INITTAB_FILE!\""

# Remove the env variable first if exists
eval sed < $BASH_PROFILE > $BASH_PROFILE.new "-e /\"PowerAlert\"/d"
mv $BASH_PROFILE.new -f $BASH_PROFILE

eval sed < $INSTALLATION_DIR/lib/bash_profile >>$BASH_PROFILE "-e \"s!INSTALLATION_DIR!$INSTALLATION_DIR!\""

chmod 0755 $PAPLUS_CMD_DIR/$PAPLUS_CMD
rm -f $TRIPPLITE_PAP_HOME/$PAPLUS_CMD

#if an init.d directory exists put the pa script there and make a link
# to it in $PAPLUS_CMD_DIR.
if [ -n "$INIT_D_DIR" -a -d "$INIT_D_DIR" ]
then
	mv -f $PAPLUS_CMD_DIR/$PAPLUS_CMD $INIT_D_DIR/$PAPLUS_CMD
	ln -sf $INIT_D_DIR/$PAPLUS_CMD $PAPLUS_CMD_DIR/$PAPLUS_CMD
#	ln -sf $INIT_D_DIR/$PAPLUS_CMD $TRIPPLITE_PAP_HOME/$PAPLUS_CMD
fi

echo "done."
echo


##### is this a server or a remote shutdown system?

cd $SERVER_DIR

if [ "$UPS_IS_LOCAL" = "$YES" ]
then
	no_nl_echo "Configuring to run the PowerAlert UPS Engine... "

#	chmod 444 $PAPLUS_RSD		# disable remote shutdown daemon
	chmod 555 $PAPLUS_SERVER	# enable UPS server
else	# this is a remote shutdown system
	no_nl_echo "Configuring to run the PowerAlert Remote Shutdown Engine... "

#	chmod 555 $PAPLUS_RSD		# enable remote shutdown daemon
	chmod 444 $PAPLUS_SERVER	# disable UPS server
fi

echo "done."
echo

sleep 1

cd ..

# set some hard-coded things in the driver file and Install date
#
# we set the UPS install date to today.  This may not be the actual _hardware_
# install date, but they can use serversetup to change it if it isn't
TODAYS_DATE=`date +\%Y\%m\%d`

#eval sed <$DRIVERS_DIR/${TRIPPLITE_SMARTPRO_DRIVER_FILE}$DRIVER_FILE_SUFFIX >$TMP_DIR/${TRIPPLITE_SMARTPRO_DRIVER_FILE}$DRIVER_FILE_SUFFIX "-e \"s!INITIAL_UPS_NOM_VOLTAGE!$UPS_NOM_VOLTAGE!\" -e \"s!INITIAL_DATE_UPS_INSTALLED!$TODAYS_DATE!\""

#removed the following, since the driver files are copied from the CD-ROM to the target directory (drivers) by the cpio command.

cd $INSTALLATION_DIR

echo "done."
echo

sleep 1



##### should PowerAlert start automatically?

if [ "$AUTO_START_FLAG" = "$YES" ]
then

	# does this system use inittab??

	if [ ! -z "$INITTAB_FILE" ]
	then

		no_nl_echo "Adding an entry for PowerAlert to \"$INITTAB_FILE\"... "
		#Remove any old entries in case of re-installation
		eval sed < $INITTAB_FILE > $INITTAB_FILE.new "-e /\"PowerAlert \"/d"
		mv $INITTAB_FILE.new -f $INITTAB_FILE


		eval sed < lib/inittab >>$INITTAB_FILE "-e \"s!PAPLUS_CMD_DIR!$PAPLUS_CMD_DIR!\""

		echo "done."
		echo

	else	# it must use rcX.d entries then

		no_nl_echo "Configuring PowerAlert for auto start at system boot... "
		if [ -d "$RC2_DIR" ]
		then
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC2_S_FILE
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC2_K_FILE
		fi

		if [ -d "$RC0_DIR" ]
		then
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC0_K_FILE
		fi

		if [ -d "$RC3_DIR" ]
		then
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC3_S_FILE
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC3_K_FILE
		fi

		if [ -d "$RC4_DIR" ]
		then
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC4_S_FILE
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC4_K_FILE
		fi

		if [ -d "$RC5_DIR" ]
		then
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC5_S_FILE
			ln -sf $PAPLUS_CMD_DIR/$PAPLUS_CMD $RC5_K_FILE
		fi

		echo "done."
		echo
	fi
fi

echo
echo
echo

centerLine "PowerAlert 11.0 Installation Complete"
echo
echo
centerLine "Please log off and log in as a PowerAlert user and run the pasetup"
echo
echo
echo

exit 0				# force exit, since need to run pasetup first 

RESPONSE=$INVALID_RESPONSE

while [ "$RESPONSE" = "$INVALID_RESPONSE" ]
do
	echo
	no_nl_echo "Would you like to start PowerAlert at this time [\"$YES\"]? "

	read RESPONSE

	RESPONSE=`checkYorN "$RESPONSE" "$YES"`

	if [ "$RESPONSE" = "$INVALID_RESPONSE" ]
	then
		echo
		echo "Please answer \"yes\" or \"no\""
	fi
done

if [ "$RESPONSE" = "$YES" ]
then
	echo

	$PAPLUS_CMD_DIR/$PAPLUS_CMD stop
	sleep 1
	$PAPLUS_CMD_DIR/$PAPLUS_CMD start
fi



