
#	ATT: ident	"sadmin:admin/menu/softwaremgmt/removepkg	2.7"

#ident	"@(#)sadmin/admin/menu/softwaremgmt:removepkg	25.1"

#menu# remove previously installed package from built-in disk
#help#
#help#	This subcommand shows you a list of currently installed optional
#help#	software packages.
#help#	It then will perform whatever actions are necessary to remove any
#help#	software packages you specify.

flags="-qq -k$$"
trap 'exit 0' 1 2  15
PATH=/bin:/usr/bin:/usr/lbin:/etc export PATH
REW="rmt1 9mt1"
NOREW="rmt0 9mt0"

lp=`dirname $0`/listpkg
if [ -r ${lp} ]
then
	/bin/sh ${lp}
fi

choices=`cd /usr/options; /bin/ls *.name|sed 's/\.name$//'`
flags="-qq -k$$"
pkgs=`checklist ${flags} -fe -l ' ' -H'
	Type in the names of the software packages you want removed
	from the system.  You will be given the opportunity to save
	the packages on tape before they are removed.' \
	'Enter the names of the packages to be removed [?, q]:' ${choices}`

echo

if  checkyn ${flags} -f -h\? -H'
	If you answer "yes" to this question, all files in the packages
	you have selected will be copied to tape before removing them
	from the system.  The tape may be used at a later time to reinstall
	the packages.
	If you answer "no", the packages will be deleted without backups.' \
	'Do you want to backup these packages on tape before removal ?'
then
	LOG=/tmp/log.uninstall

	while :
	do
	echo
	norew=`selectdevice \
	  -q "Choose the no-rewind device to use for backing up the packages." \
	  -c $$ /dev ${NOREW}`
	echo
	rew=`selectdevice \
	  -q "Choose the rewind device to use for backing up the packages." \
	  -c $$ /dev ${REW}`
	if [ "$norew" = "$rew" ]
	then
		echo "\nThe rewind and no-rewind devices must be different.  Try again."
		continue
	fi
	break
	done
	for i in ${pkgs}; do
		echo $i
	done > /tmp/rmpkgtoc
	echo "\nMount a tape on ${norew} and press return to start the backup."
	read J
	< $rew
	echo "Writing the following Table of Contents to ${norew}.\n"
	cat /tmp/rmpkgtoc|sed 's/^/	/'
	echo; echo /tmp/rmpkgtoc|cpio -o > ${norew} 2>/dev/null
	cd /
	echo "You should label this tape as follows:\n" > $LOG
	for i in ${pkgs}; do
		if [ ! -s /usr/options/${i}.rlst ]
		then
			echo "/usr/options/${i}.rlst does not exist.  This package can not be removed!"
			continue
		fi
		echo "Writing $i files to ${norew}"
		grep "\.${i}$" /usr/options/${i}.rlst|sed "s/.${i}$//"| \
		while read name; do
			cp ${name} ${name}.${i}
		done
		cat /usr/options/${i}.rlst|cpio -ov > ${norew} 2>/tmp/$$rmpkg
		J=`tail -1 /tmp/$$rmpkg|sed '/[1-9][0-9]* blocks/s/ blocks//'`
		echo "${i} package archive:	${J} cpio blocks." >>$LOG
	done
	echo "\nRewinding the tape. Please wait."
	< $rew
	echo "You may now remove the tape in ${rew}.\n"
	cat $LOG; rm $LOG /tmp/rmpkgtoc /tmp/*rmpkg
	
fi
echo
if  checkyn ${flags} -f -h\? -H'
	If you answer "yes", the packages you have selected
	will be removed from the system.' \
	'Are you sure you want to remove these packages ?'
then
	for i in ${pkgs}
	do
		echo "Removing the '${i}' package from the system."
		/bin/sh /usr/options/${i}.un
	done
fi
