#!/bin/sh
#
# restore to /opt/vps, from tape created by vpsback
#
# 01    02-jan-97       jar     originate
# 02    10-mar-97       jar     added restore of system package stuff
# 03    10-mar-97       jar     added check for rmt/1 as well as rmt/0
# 04    09-oct-97       jar     updated wording for vpshome replacement

if [ `/usr/ucb/whoami` != root ]; then
        echo Please run vpsrest as root
        exit
fi

# get vpshome
if [ -f /etc/vpshome ]; then
	vpshome=`cat /etc/vpshome`
	if [ -d $vpshome ]; then
		echo
		echo
		echo $vpshome will be replaced if you proceed with restore .
		echo " Do you want to proceed with restore? Enter n to cancel [y]: \c"
		read restq
		if [ $restq = "n" ]; then
			echo
			echo " restore cancelled"
	 		exit 1
	   	fi
		echo "Preparing system package information area for update..."
		cat /var/sadm/install/contents | egrep -v "#" | egrep -v PERI > /var/sadm/install/contents.vpsrest
		if [ $? != 0 ]; then
			echo "could not create new /var/sadm/install/contents file..."
			echo vpsrest terminating...
			exit 1
		fi
		echo "removing PERI package information from system....."
		rm -rf /var/sadm/pkg/PERI*  > /dev/null
  		echo " removing existing vps....."
		rm -rf $vpshome
	fi
fi

tapeloaded=no
tapeloaded1=no
while [ $tapeloaded != yes -a $tapeloaded1 != yes ]; do
##
## Get the tape loaded...
##
echo
echo "PLEASE LOAD TAPE VOLUME, HIT RETURN WHEN READY..."
read ans

mt -f /dev/rmt/0 status >/dev/null
if [ $? = 0 ]; then
        tapeloaded=yes
        echo Using tape in /dev/rmt/0
        tapedev=/dev/rmt/0
fi
mt -f /dev/rmt/1 status >/dev/null
if [ $? = 0 ]; then
        tapeloaded1=yes
        echo Using tape in /dev/rmt/1
        tapedev=/dev/rmt/1
fi
done
 
cd /
echo
echo
echo " restoring vps from tape......"
tar xpvf $tapedev
if [ $? != 0 ]; then
	echo "restore failed........"
	exit 1
else
	# determine new vpshome
	vpshome=`cat /etc/vpshome`
	echo "VPS files successfully restored....."
fi

if [ -f ${vpshome}/pkg.contents.PERI ]; then
	echo "updating system package information....."
	cat ${vpshome}/pkg.contents.PERI >> /var/sadm/install/contents.vpsrest
	if [ $? != 0 ]; then
       		 echo "could not create new /var/sadm/install/contents file..."
	       	 echo vpsrest terminating... 
       		 exit 1
	fi

	echo "# Last modified by vpsrest for PERI packages" >> /var/sadm/install/contents.vpsrest
	echo "# `date +%C`"  >> /var/sadm/install/contents.vpsrest

	mv /var/sadm/install/contents.vpsrest /var/sadm/install/contents
	if [ $? != 0 ]; then
		 echo "could not replace /var/sadm/install/contents file..."	
		exit 1
	fi
	rm -rf ${vpshome}/pkg.contents.PERI > /dev/null
fi
