#ident	"@(#)sadmin:admin/menu/softwaremgmt/removepkg	35.1"
#menu# remove previously installed package from built-in disk
#help#
#help#	This subcommand show you a list of currently installed optional
#help#	software packages.
#help#	It then will perform whatever actions are necessary to remove
#help#	software packages you specify.
#help#	The removable medium used to "installpkg" the software is needed to
#help#	remove it.

flags="-qq -k$$"
trap 'exit 0' 1 2  15

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

dir=${0}.d
if [ -d ${dir} ]
then
	patterns=`ls ${dir} 2>/dev/null`
else
	patterns=
fi

ddrive=`selectdevice -b $$ /dev/SA ${patterns}`
ndrive=`drivename ${ddrive}`

if [ -n "${patterns}" ]
then
	. ${dir}/`selpattern ${ddrive}`
fi

question="
Insert the removable medium for the package you wish to remove
into the ${ndrive}.
Press <RETURN> when ready.  Type  q  to quit."

case ${ddrive} in
*/diskette1 | */diskette2 | */hddiskette1 | */hddiskette2 )
	while true
	do
		fulllabel=`disklabel -q "${question}" $$ ${ddrive}`
		fsname=
		eval `labelfsname "${fulllabel}"`
		case "${fsname}" in
		instal | install )
			trap '	trap "" 1 2
				cd /;  diskumount -n "${ndrive}" ${ddrive}' 0

			/etc/umount ${ddrive} 2>/dev/null
			chmod 700 /install
			/etc/mount ${ddrive} /install -r  ||  exit 1
			;;
		'' )
			echo >&2 '
	This removable medium is NOT labeled and will not work.
	Try another.'
			continue
			;;
		* )
			echo >&2 "
	This removable medium is labeled '${label}' and is not a package.
	The file system name is '${fsname}'.  It should be 'install'.
	The full label is:
${fulllabel}
	Try another."
			continue
		esac

		if [ ! -r /install/install/UNINSTALL ]
		then
			echo >&2 '	This package does will not remove itself.
	Consult the instructions that came with the medium.'
	diskumount -n "${ndrive}" ${ddrive}
			continue
		fi

		trap "	trap '' 1 2
			rm -f /tmp/$$UNINSTALL
			cd /
			diskumount -n '${ndrive}' ${ddrive}" 0

		cd /tmp
		cp /install/install/UNINSTALL $$UNINSTALL
		chmod +x $$UNINSTALL
		trap : 1 2
		/tmp/$$UNINSTALL ${ddrive} /install "${ndrive}"  ||
			echo 'WARNING:  Package removal may not have completed properly.'
		trap '' 1 2
		rm -f /tmp/$$UNINSTALL
		diskumount -n "${ndrive}" ${ddrive}
		trap : 0
		trap exit 1 2
	done
	exit 0
	;;
*/ctape )
	ddrive=/dev/r40tn
	rewindcmd="/etc/m350ctl -r ${ddrive}"
	;;
* )
	admerr $ME "Device '${ndrive}' not supported"
	exit 1
	;;
esac

trap 'exit 0' 1 2  15

INSDIR=/usr/tmp/igfdir
REMOVESCR=${INSDIR}/UNINSTALL

echo "$question"
read rep
if [ x$rep = xq ]
then
	exit 0
fi

#
# Get package description and name during this pass
# Remember:
#	$OUT in quotes, "", will have new-lines, and space
#	searaters if not inclosed in qoutes.
#
OUT=`ixf -xdp ${ddrive}`
rtn=$?
#
# Check return value
#
if [ $rtn -eq 1  -o $rtn -eq 2 ]
then
	admerr $0 'Failed to read removalbe medium, exiting'
	eval ${rewindcmd}
	exit 1
elif [ $rtn -ne 13 ]
then
	admerr $0 'No SCRIPTS entry on media, removepkg not supported'
	exit 1
fi
#
# Check command output
#
if [ `echo "$OUT" | wc -l` = 2 ]
then
	set $OUT
	PACK_NAME=$1 ; shift
	PACK_DESC="$*"
else
	PACK_DESC="$*"
fi
echo  "\nRemoving \"$PACK_DESC\""

echo "\nPress <RETURN> to continue, type q to quit."
read rep
if [ x$rep = xq ]
then
	exit 0
fi

Here=`pwd`
mkdir -p $INSDIR 2>/dev/null
cd $INSDIR 2>/dev/null
ixf -s $INSDIR ${ddrive}

if [ $? -ne 0 ]
then
	admerr $0 'Failed to completely remove package, exiting'
	eval ${rewindcmd}
	rm -rf $INSDIR
	exit 1
fi

if [ -f $REMOVESCR ]
then
	if [ ! -x $REMOVESCR ]
	then
		chmod 755 $REMOVESCR
	fi

	$REMOVESCR
	Rtn=$?
else
	Rtn=0
fi

cd $Here
#
# Remove temporary directory and package name file used
# by 'listpkg'
#
rm -rf $INSDIR
if [ $Rtn = 0 ]
then
	rm -f /usr/options/$PACK_NAME
	echo "\n\"$PACK_DESC\" is now removed"
fi
exit 0
