#! /sbin/sh
# PGP E-ppliance 2.0 Upgrade
#
# clean 
#
# PGP Security, A Network Associates Business
# Copyright (c) 1993 - 2001, Network Associates Technology, Inc.

UPGRADEPREFFILE="./.preference"
RESTORESYMFILE="restoresymtable"

#######################################################################
# change dir to the root of upgrade source
#######################################################################
UPGRADEROOT=`dirname $0`
BASENAME=`basename $0`
cd ${UPGRADEROOT} > /dev/null 2>&1
if [ $? -ne 0 ]; then
	 echo "UPGRADE::ERR Failed to change directory ${UPGRADEROOT}"
	 exit 3
fi

. ./Utils.sh

Log "##########################################################################"
Log "# Clean "
Log "##########################################################################"

################################################################################
# check the user permission
################################################################################
# must be the root user
Log "# Check User Permission"
USER=`/usr/ucb/whoami`
if [ "$USER" != "root" ]; then
	 Log "UPGRADE::ERR User $USER does not have the sufficient privileges"
	 Log "UPGRADE::ERR Please login as a root user"
	 exit 1
fi

#######################################################################
# load up upgrade preference and state
#######################################################################
Log "# Load Upgrade Preference"
if [ -f ${UPGRADEPREFFILE} ]; then
	 . ${UPGRADEPREFFILE}
else
	 Log "UPGRADE::ERR Failed to find ${UPGRADEPREFFILE}"
	 exit 3
fi

if [ "${UPGRADESTATE}" != "PGP_UPGRADE_FIN" ]; then
	 Log "UPGRADE::WRN The upgrade has  not been  accomplished yet"
	 Log "UPGRADE::WRN Cleaning  the upgrade  source and  internal"
	 Log "UPGRADE::WRN backup will neither restore your e-ppliance"
	 Log "UPGRADE::WRN nor resume the interrupted upgrade."
	 echo ""
	 echo "Would you like to continue? [y/n]"
	 echo "[y] - clean the system [n] - quit : \c"
	 read foo
	 if [ "${foo}" != "y" -a "${foo}" != "Y" ]; then
		  exit 0
	 fi
fi

if [ -z "${MQUEUEDIR}" ]; then
	 MQUEUEDIR="/var/spool/mqueue"
	 Log "UPGRADE::WRN mqueue directory has not been set yet"
	 Log "UPGRADE::WRN set mqueue to ${MQUEUEDIR}"
fi

echo "The upgrade will clean all of the  /var/virtmp partition"
echo "removing backup, upgrade state, log, and upgrade sources."
echo "The upgrade will restore queued emails of from the local"
echo "backup to  /var/tmp  if they exist.  Please  be  advised"
echo "that  you  will not  be able to  restore your e-ppliance"
echo "using the upgrade utility once the clean is complete."
echo "The upgrade status log file will be saved into /var/tmp."
echo ""
echo "Would you like to continue? [y/n] : \c"
read foo
if [ "${foo}" != "y" -a "${foo}" != "Y" ]; then
	 exit 0
fi

mount -a > /dev/null 2>&1

################################################################################
# the upgrade package must reside in /var/virtmp partition
################################################################################
# check if c0t0d0s5 is mounted on /var/virtmp
Log "# Validate /var/virtmp partition"
VAR_VIRTMPDEV="/dev/dsk/c0t0d0s5"
VAR_VIRTMPRAWDEV="/dev/rdsk/c0t0d0s5"
VAR_VIRTMPMNT="/var/virtmp"

TMP_DISK_PART_INFO=`df -k ${UPGRADEROOT} | grep ${VAR_VIRTMPDEV}`
TMP_DSK_DEV=`echo ${TMP_DISK_PART_INFO} | awk '{ print $1 }'`

if [ "${TMP_DSK_DEV}" != "${VAR_VIRTMPDEV}" ]; then
	 Log "UPGRADE::ERR The upgrade package must reside in ${VAR_VIRTMPDEV} partition"
	 exit 2
fi

TMP_DSK_MNT=`echo ${TMP_DISK_PART_INFO} | awk '{ print $6 }'`
if [ "${TMP_DSK_MNT}" != "${VAR_VIRTMPMNT}" ]; then
	 Log "UPGRADE::ERR ${VAR_VIRTMPMNT} must be mounted on ${TMP_DSK_DEV}"
	 exit 2
fi

################################################################################
# test if ./gzip is executable
################################################################################
if [ ! -x ./gzip ]; then
	 Log "UPGRADE::ERR ./gzip not found or not executable"
	 exit 3
fi

################################################################################
# restore the queued emails in MQUEUEDIR
################################################################################
if [ "${REMOVE_EMAILS}" = "FALSE" ]; then
	 Log "# Restore Queued EMails"
	 BACKUPPATH="`pwd`/Backup"
	 if [ -d ${BACKUPPATH} ]; then
		  VARBACKUP="${BACKUPPATH}/pgp${PRODUCT_MODEL}.mail.PRE_EPL2.0.gz"
		  if [ -f "${VARBACKUP}" ]; then
				RESTOREVARDST="/var/tmp"
				if [ ! -d "${RESTOREVARDST}" ]; then
					 mkdir -p ${RESTOREVARDST} > /dev/null 2>&1
				fi

				# enable interrupt handling 
				trap 'kill $! > /dev/null 2>&1; Log "\n# User Interrupt! Upgrade aborted"; exit 2' 2 15
				sh ./Indicator.sh &
				## ./gzip -cd ${VARBACKUP} | \
				##	 ( cd ${RESTOREVARDST}; ufsrestore rf - ) > /dev/null 2>&1
				./gzip -cd ${VARBACKUP} | \
					 ( cd ${RESTOREVARDST}; tar xf - ) > /dev/null 2>&1

				kill $! > /dev/null 2>&1
				if [ -f ${RESTOREVARDST}/${RESTORESYMFILE} ]; then
					 rm -f ${RESTOREVARDST}/${RESTORESYMFILE}
				fi

				# enable interrupts
				trap 2 15

				Log "The backup of queued  e-mails  has been restored into"
				Log "${RESTOREVARDST}."
				Log "The user should verify the contents of that temporary"
				Log "directory,  and  move  those queued  e-mails into the"
				Log "current sendmail queue. Please note that the contents"
				Log "of smap queue  are  no longer compatible with the PGP"
				Log "e-ppliance 2.0."
		  fi
	 fi
fi

################################################################################
# shut down all running services
################################################################################
Log "# Shut Down Firewall Services"
NETPERM_TABLE="/usr/local/etc/netperm-table"
if [ -f "${NETPERM_TABLE}" ]; then
	 SERVICELIST=`grep ": state on" $NETPERM_TABLE | cut -d':' -f1`
else
	 Log "UPGRADE::ERR Failed to locate firewall configuration file $NETPERM_TABLE"
	 exit 1
fi
if [ -n "${SERVICELIST}" ]; then
	 for process in ${SERVICELIST}; do
		  if [ "$process" != "tn-gw" ] && [ "$process" != "Local-Telnet" ]; then
				PID=`ps -ef | grep "\-as $process" | grep -v "grep" | awk '{ print $2 }'`
				kill -9 $PID > /dev/null 2>&1
		  fi
	 done
fi

################################################################################
# save the ./epl2.0.upgrade.log to /var/tmp/epl2.0.upgrade.log
################################################################################
Log "# Save the upgrade log file to /var/tmp/`basename ${UPGRADELOGFILE}`"
cp ${UPGRADELOGFILE} /var/tmp > /dev/null 2>&1
if [ $? -ne 0 ]; then
	 Log "UPGRADE::ERR Failed to move ${UPGRADELOGFILE} to /var/tmp" 
	 exit 3
fi

cd /var/tmp

################################################################################
# enable the clean routine
################################################################################
Log "# Enable /var/virtmp Clean Routine"
if [ -f /usr/local/etc/mgmt/disabled.cleanse_vtmp.sh ]; then
	 mv /usr/local/etc/mgmt/disabled.cleanse_vtmp.sh \
		  /usr/local/etc/mgmt/cleanse_vtmp.sh > /dev/null 2>&1
fi

################################################################################
# finish
################################################################################
echo "echo \"Construct ufs /var/virtmp \"              "      >  /etc/rc3.d/S19clean
echo "umount ${VAR_VIRTMPMNT} > /dev/null 2>&1         "      >> /etc/rc3.d/S19clean
echo "newfs ${VAR_VIRTMPRAWDEV} > /dev/null 2>&1 <<EOF "      >> /etc/rc3.d/S19clean
echo "y"                                                      >> /etc/rc3.d/S19clean
echo "EOF"                                                    >> /etc/rc3.d/S19clean
echo "if [ \$? -ne 0 ]; then "                                >> /etc/rc3.d/S19clean
echo "\techo \"Failed to construct /var/virtmp partition.\" " >> /etc/rc3.d/S19clean
echo "\texit 1 "                                              >> /etc/rc3.d/S19clean
echo "fi"                                                     >> /etc/rc3.d/S19clean
echo "fsck -y -of ${VAR_VIRTMPRAWDEV} > /dev/null 2>&1 "      >> /etc/rc3.d/S19clean
echo "mount ${VAR_VIRTMPDEV} ${VAR_VIRTMPMNT} "               >> /etc/rc3.d/S19clean
echo "rm -f /etc/rc3.d/S19clean"                              >> /etc/rc3.d/S19clean

Log "# Reboot the system"
Log ""
Log "In order to make the clean effective, the upgrade must reboot"
Log "your system."
Log ""
echo "Would you like to reboot your system now? [y/n] : \c"
read foo
if [ "${foo}" != "y" -a "${foo}" != "Y" ]; then
	 exit 0
else
	 sync; reboot
fi
exit 0
