#!/bin/sh
#
###########################################################################
###########################################################################
#
#	UnInstallation script for PowerAlert (UNIX)
#
#	@(#)pa_uninstall	11.0	10/02/2001
#
###########################################################################
###########################################################################

#
#

 
DEFAULT_INSTALLATION_DIR="/poweralert"   

#	some utility variables to make things easier to read

NO="no"
YES="yes"

INVALID_RESPONSE="InVaLiD_ReSpOnSe"

PAPLUS_CMD=pa

BASH_PROFILE=/root/.bash_profile

#	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

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

RC0_DIR=
RC1_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 Uninstallation"

	echo
}



#	suppress new lines when using this echo...

no_nl_echo() {

	echo $NO_NEWLINE_PARAM "$*$NO_NEWLINE_TERM"

}

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 uninstall PowerAlert."
		echo "Please log in as 'root' and re-run this file."
		
        exit 1
    fi
}
###########################################################################
###########################################################################
#
#	actual installation starts here...
#
###########################################################################
###########################################################################

# Make sure the user is running as root.
checkRoot
newScreen Overview

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


#
#	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
	DIR_SELECTED=$NO 
	while [ "$DIR_SELECTED" = "$NO" ] 
	do
		newScreen "Uninstallation Process"

		echo "Enter the directory where PowerAlert was installed"
		echo
		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."
			echo "Press Enter to Continue"
			echo
			read RESPONSE
		else
			DIR_SELECTED=$YES
		fi
	done

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


	newScreen "Confirmation"


	echo "Uninstallation information:"

	echo

	echo "  Installation directory:			$INSTALLATION_DIR"

	echo
	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

$INSTALLATION_DIR/engine/$PAPLUS_CMD stop console
$INSTALLATION_DIR/engine/$PAPLUS_CMD stop all 


# If an init.d directory exists, make sure that uninstall 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

rm -f $PAPLUS_CMD_DIR/$PAPLUS_CMD

rm -rf $INSTALLATION_DIR


#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
	rm -f $INIT_D_DIR/$PAPLUS_CMD
fi

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

# does this system use inittab??

if [ ! -z "$INITTAB_FILE" ]
then

	#Remove any entries in inittab
 
	eval sed < $INITTAB_FILE > $INITTAB_FILE.new "-e /\"PowerAlert \"/d"
	mv $INITTAB_FILE.new -f $INITTAB_FILE




else	# it must use rcX.d entries then
	if [ -d "$RC2_DIR" ]
	then
		rm -f $RC2_S_FILE
		rm -f $RC2_K_FILE
	fi
	if [ -d "$RC0_DIR" ]
	then
		rm -f $RC0_K_FILE
	fi

	if [ -d "$RC3_DIR" ]
	then
		rm -f $RC3_K_FILE
	        rm -f $RC3_S_FILE
	fi

	if [ -d "$RC4_DIR" ]
	then
		rm -f $RC4_S_FILE
		rm -f $RC4_K_FILE
	fi

	if [ -d "$RC5_DIR" ]
	then
		rm -f $RC5_S_FILE
		rm -f $RC5_K_FILE
	fi

fi

rm -f /etc/pa_uninstall

echo
echo
echo

centerLine "PowerAlert 11.0 Uninstallation Complete"
echo
echo
exit 0
