#!/bin/sh
#
#ident "@(#)preremove	1.9 01 Oct 1993 SMI"
#
# Copyright 1993 Sun Microsystems, Inc. All Rights Reserved
#

killlogd() {
	PID=`ps -ef |grep $OSILOGD |grep -v grep|awk '{ print $2 }'`
	if [ -n "$PID" ]; then
		kill -KILL  $PID 2>&1 >/dev/null
	fi
}

killnetd() {
	PID=`ps -ef |grep $OSINETD |grep -v grep|awk '{ print $2 }'`
	if [ -n "$PID" ]; then
		rm -f $NETDTMP  2>&1 >/dev/null
		kill -TERM $PID  >/dev/null
		#
		# wait until the daemon is really dead
		#
		while [ -n "$PID" ]; do
			PID=`ps -ef |grep $OSINETD |grep -v grep|awk '{ print $2 }'`
		done
	fi
}


# Exit codes for installation scripts 
e_ok=0      
e_fatal=1      # stop installation on this exit
e_warning=2    # Installation will go on. 
e_int=3        # Interrupted. Stop installation
e_reboot=10    # User must reboot after installation of all selected packages
e_rebootnow=20 # User must reboot right after installation of current package
               # To be added to one of the single-digit exit code above

# Trap interrupt
trap `exit $e_int` 15

#
# Its better to have oopi at the end of the list because
# the others modules uses it.
# You will not be able to unload it in othe case.
#
# DRVS="otpi oclt otcl otmr oopi"         # List of drivers 

DRVS="otpi oclt otmr oopi"         # List of drivers 
REM_DRV="/usr/sbin/rem_drv"
NAME_DRV="/etc/name_to_major"
SPECIFIC="/dev/oopi0 /dev/oopi1"

OSILOGD=osilogd
OSINETD=osinetd


#
# ***   the /usr/sbin/add_drv script uses BASEDIR variable to
#       acces /etc/name_to_major.
#       We should redifined this variable to be independant of
#       the value used by pkgadd
#
BASEDIR=/ ; export BASEDIR

#
# Kill the daemon
#
killnetd
killlogd

for D in $DRVS; do
	if grep "^$D" $NAME_DRV 2>1 >/dev/null ; then
		echo "Removing $D driver ..."
		$REM_DRV $D
		#
		# We should not have to do this manually. The system (rem_drv)
		# should remove these files automatically (OS BUG?).
		# 
		rm -f /devices/pseudo/clone*:$D # for 1093 compatibility
		rm -f /dev/$D
	else
		echo "The $D driver is already removed"
	fi
done
#
# Remove Specific OSI Installation stuff
#
for I in $SPECIFIC; do
	if [ -c "$I" -o  -h $I ]; then
		rm -f $I
	fi
done

#
# Licensing
#

#
# Copyright (c) 1992 by Sun Microsystems, Inc.
# @(#)preremove_prod	1.7 11/19/92 Copyright 1993 SMI
#
###########################################
# The following lines should be modified for use by individual products
###########################################
# Identifies the basename of the license file and license
# location file, before the ,loc or,lic are attached to it
# e.g., if license_file=featfile
# the license file will then be called featfile,lic

license_file=osistk8.0

####################################################
# Subsequent lines should not be modified by individual products
#####################################################
licdir="/etc/opt/licenses"
licfile=${license_file}.lic
locfile=${license_file}.loc

# Find the location of the license files
if [ ! -f ${licdir}/${locfile} ]
then
	echo Cannot remove licenses - no license location file ${licdir}/${locfile}
	exit 0
fi
temppath=`cat ${licdir}/${locfile}`

if [ -f ${temppath}/${licfile}* ]; then
	notdone=0
	while [ $notdone -eq 0 ]
	do
		ans=`ckyorn -e "please answer y or n: "  -h  \
		  "please answer y or n:" \
		-p "License files exist for this product. Do you want them saved[y|n]?"`
		if [ $ans = "y" -o $ans = "yes" -o $ans = "Y"  \
	    	-o $ans = "YES" -o $ans = "Yes" ]; then

		#
		# Copy licenses from product directory to standard license
		# directory
			for infile in `ls ${temppath}/$licfile*`
			do
				i=0
				nd=0
				while [ $nd -eq 0 ]
				do
					outputfile=$licdir/$licfile,$i
					if [ ! -f $outputfile ]
					then
						nd=1
					else
						i=`expr $i + 1`
					fi
				done
				basefile=`basename ${infile}`
				cp ${temppath}/${basefile} $outputfile
				if [ $? -ne 0 ]
				then
					echo Exiting
					exit 1
				fi
				rm ${temppath}/${basefile}
			done
			echo "Licenses copied to $licdir"
			notdone=1
		elif [ $ans = "n" -o $ans = "no" -o $ans = "N"  \
	    	-o $ans = "NO" -o $ans = "No" ]; then
			rm ${temppath}/${licfile}*
			echo "License files removed from ${temppath}"
			notdone=1
		else
			notdone=0
		fi
	done
fi
# Remove the loc file, since it no longer applies
rm ${licdir}/${locfile} > /dev/null 2>&1

exit 0

# Happy End
exit $e_ok
