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

################################################################################
# functions
################################################################################
CheckPackFile ()
{
	 ls $1 > /dev/null 2>&1
	 if [ $? -ne 0 ]; then
		  echo "UPGRADE::ERR Failed to find a upgrade source file $1"
		  exit 3
	 fi

	 return 0
}

################################################################################
# change dir to the root of upgrade source
################################################################################
PWD=`pwd`
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

################################################################################
# the upgrade package must reside in /var/virtmp partition
################################################################################
VAR_VIRTMPDEV="/dev/dsk/c0t0d0s5"
mount -a > /dev/null 2>&1
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
	 echo "UPGRADE::ERR The upgrade package must reside in ${VAR_VIRTMPDEV} partition"
	 exit 2
fi

################################################################################
# verify the upgrade package structure
################################################################################
#./upgrade ./pgpXXX.root.gz ./pgpXXX.var.gz ./pgpXXX.usr.gz ./utils.tar

# untar the upgrade utils and run upgrade
UTILFILE="./utils.tar"
if [ -f ${UTILFILE} ]; then
	 tar -xf ${UTILFILE}
 	 if [ $? -ne 0 ]; then
		  echo "UPGRADE::ERR Failed to extract the upgrade utilities from ${UTILFILE}"
		  exit 3
	 fi
else
	 echo "UPGRADE::ERR Failed to find ${UTILFILE}"
	 exit 3
fi

CheckPackFile "./pgp*.root.gz"
CheckPackFile "./pgp*.var.gz"
CheckPackFile "./pgp*.usr.gz"

# all upgrade utils resides in ./PGPEPL2.0.Upgrade
UTILPATH="./PGPEPL2.0.Upgrade"

if [ -d ${UTILPATH} ]; then
	 cd ${UTILPATH}
else
	 echo "UPGRADE::ERR Failed to locate directory ${UTILPATH}"
	 exit 3
fi

################################################################################
# upgrade e-ppliance
################################################################################
sh ./UpgradeEPL2.0.sh $*

################################################################################
# finish
################################################################################
cd ${PWD}
exit 0
