#
#ident	"@(#)removepkg.r	1.2  (Prime) 9/9/88"
#
# PURPOSE: Delete installed software
# ----------------------------------------------------------------------
FILE=/tmp/R$$
trap 'rm -rf ${FILE}; exit 1' 1 2 3 9 15

RC=`id | sed -e 's/uid=//' -e 's/(.*//'`
if [ "$RC" != "0" ]
then
	message -d "You (${LOGNAME}) do not have permission to perform software \
installation.  Please consult your Operations/System Administration Guide for more \
information on assigning permissions to privileged operations."
	exit 1
fi

if [ -z "$TERM" ]
#CChan. Changed TERM type from AT386-M
then TERM=vt100
fi
CLEAR=`tput clear 2>/dev/null`

ERROR5="There are currently no software applications installed that can be removed."

CONTENTS=/usr/lib/installed/CONTENTS
INDIR=/usr/lib/installed

if [ ! -d /usr/options ]
then
	echo "$ERROR5"
	exit 1
fi
cd /usr/options

if [ $# = 0 ]
then
	list=`ls *.name 2>/dev/null`
	if [ -z "$list" ]
	then
		echo "$ERROR5"
		exit 1
	fi
	for i in *
	do
		echo "$i\t\c"
		cat $i | sed -e "/^$/d"
	done > ${FILE}
	max=`expr \`cat $FILE | wc -l\``
	if [ $max -lt 1 ]
	then
		echo "$ERROR5"
		exit 1
	fi
	while [ 1 ]
	do
		pr -n -l1 -t ${FILE} | cut -d'	' -f1,3-
		max=`expr \`cat $FILE | wc -l\``
		echo "\nSelect a number (1 - $max) from this list to remove: \c"
		read index
		expr $index \* 20 > /dev/null 2>&1
		if [ $? != 0 ]
		then echo "That number is invalid.  Please specify a number between 1 and $max."
     		continue
		elif [ $index -gt $max ]
		then echo "That number is too large.  Please specify a number between 1 and $max."
     		continue
		elif [ $index -lt 1 ]
		then echo "That number is too small.  Please specify a number between 1 and $max."
     		continue
		else break
		fi
	done
	PROG=`sed -n "${index}p" $FILE | cut -d'	' -f1 | sed -e "s/\.name//`
	rm -rf ${FILE}
else
	NAME=`echo ${1} | tr "[A-Z]" "[a-z]"`
	if [ -f ${NAME}.name ]
	then PROG=$NAME
	else
		for i in *
		do
			grep -i "$NAME" $i > /dev/null 2>&1
			if [ $? = 0 ]
			then PROG=`echo $i | sed -e 's/\.name//'`
			     break
			fi
		done
		if [ -z "$PROG" ]
		then echo "\nThere is no software package currently installed\nresembling: $1\n"
		     exit 1
		fi
	fi
fi

cd /
KEY=${PROG}.name
NAME=`cat /usr/options/${PROG}.name`
grep $PROG $CONTENTS > /dev/null 2>&1
if [ $? = 0 ]
then

message -c "Do you really want to remove $NAME?"
if [ "$?" != "0" ]; then exit; fi
	trap '' 1 2 3 9 15
export KEY NAME

UNFILE=${INDIR}/Remove/${KEY}
if [ -r "$UNFILE" ]
then
	chmod +x $UNFILE
	echo "$CLEAR "
	$UNFILE
	if [ $? = "0" ]
	then
		sync; sync
		grep -v "^$KEY " $CONTENTS > /tmp/$$
		mv /tmp/$$ $CONTENTS
		rm $UNFILE
		rm -f ${INDIR}/Files/${KEY} /usr/options/${KEY}
		sync; sync; sync
		echo "$CLEAR "
		if [ -f /etc/.new_unix ]
		then
			sync; sync
			exec /etc/conf/bin/idreboot
		else
			message -d "The $NAME package is now removed."
		fi
		exit
	fi
	exit
else
	if [ -f ${INDIR}/Files/${KEY} ]
	then FILES_MESS="  The file ${INDIR}/Files/${KEY} contains a list of the \
files and directories installed or created by the package.  You may wish to use this \
file to help in removing the package."
	fi
	message -d "Cannot find removal script for $NAME.  You will have to \
remove this package manually using UNIX System tools from the UNIX System Shell.${FILES_MESS}"
fi
#else
#MSG=
#while [ 1 ]
#do
#message -c "${MSG}Insert the removable medium for the $NAME you wish to remove."
#if [ "$?" != "0" ]; then exit; fi
#/etc/mount /dev/dsk/f0q15d /mnt -r > /dev/null 2>&1
#if [ $? = 0 ]
#then DRIVE=/dev/dsk/f0q15d
#     /etc/umount /mnt > /dev/null 2>&1
#     break
#fi
#/etc/mount /dev/dsk/f0d9d /mnt -r > /dev/null 2>&1
#if [ $? = 0 ]
#then DRIVE=/dev/dsk/f0d9d
#     /etc/umount /mnt > /dev/null 2>&1
#     break
#fi
#MSG="Cannot determine the type of floppy.\n"
#done
#/etc/mount ${DRIVE} /install -r > /dev/null 2>&1
#if [ ! -r /install/install/UNINSTALL ]
#then echo >&2 '\tThis package will not remove itself.  Consult the instructions \
#that came with the medium.'
#     /etc/umount /install > /dev/null 2>&1
#fi
#trap "trap '' 1 2 3 9 15; cd /; rm -rf /tmp/$$UNINSTALL; /etc/umount /install; exit 1" 1 2 3
#cd /tmp
#cp /install/install/UNINSTALL $$UNINSTALL
#chmod +x $$UNINSTALL
#/tmp/$$UNINSTALL ${DRIVE} /install "`basename ${DRIVE}` drive" ||
#	echo 'WARNING:  Package removal may not have completed properly.'
#trap '' 1 2 3
#cd /
#rm -rf /tmp/$$UNINSTALL
#/etc/umount /install > /dev/null 2>&1
fi
