#!/bin/sh
#
#   sunpc_UNinstall:  SunPC 4.0 UNinstall program.
#
#   sunpc_UNinstall:  Must be run as 'root' on sun4, sun4c, or sun4m
#           architecture systems.
#
#           Must be running SunOS 5.0 or greater.
#
#   sunpc_UNinstall:
#
#		Basically, undoes everything that sunpc_install does, including
#		the sunpcload script.
#
DEVLINKS="/usr/sbin/devlinks"
ADD_DRV="/usr/sbin/add_drv"
REM_DRV="/usr/sbin/rem_drv"
MODLOAD="/usr/sbin/modload"
MODUNLOAD="/usr/sbin/modunload"
DRVALIASES="/etc/driver_aliases"

DEVLINKS_TAB="/etc/devlink.tab"

AWK="/bin/awk"
ECHO="/bin/echo"
CP="/bin/cp"
GREP="/bin/grep"
ID="/bin/id"
SED="/usr/bin/sed"
RM="/usr/bin/rm -f"
MV="/usr/bin/mv"
MODINFO="/usr/sbin/modinfo"
ARCH=`/usr/bin/uname -m`
UNAME_N=`/usr/bin/uname -n`
OSREVINFO=`/usr/bin/uname -r`

OSREVLEVEL=`echo "$OSREVINFO"|/bin/grep "5.1"`

#
# Tell the user what we are doing
#
${ECHO} "The sunpc_UNinstall program removes files which were"
${ECHO} "added by the SunPC startup program (sunpc_install)."
${ECHO}
${ECHO} "This program does not remove the files in your ~/pc directory,"
${ECHO} "your SunPC custom configuration information."
${ECHO}
${ECHO} "You must be a superuser to run the sunpc_UNinstall program."
${ECHO}
#
# Check that the user is root.
#
ISROOT=`${ID} | ${AWK} '/root/ {print}'`
if [ -z "$ISROOT" ]
then
    ${ECHO} "\nMust be root to run UNinstall program."
    ${ECHO} "\nsunpc_UNinstall script is exiting."
    exit 1
fi

#
# Find out where this script lives to determine the distribution location.
#
FROM_PATH=`echo "$0" | /bin/grep "/"`
 
if [ -n "$FROM_PATH" ]
then
	#in this case $0 is a directory component.
	FIRSTARG=`echo ${FROM_PATH} | /bin/awk -F/ '{ print $1 }'`
	
	if [ -z "$FIRSTARG" ]; then
		SOURCE3=`echo ${0} | /bin/awk -F/ '{ for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	else
        	SOURCE3=`echo ${0} | /bin/awk -F/ '{ if ($1 == "") printf "/"; for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	fi
else
	#in this case $0 is not a directory component.
	SOURCE3="./"
fi

cd $SOURCE3
cd ..
SUNPCHOME=`pwd`
export SUNPCHOME

#
# Check to see if we are running S2.4 (By checking the server version of 3400). If
# its running, we must not allow sunpc_install to run because loading the driver
# will cause the servers hwc streams module to be popped and repushed resulting
# in the loss of the system cursor. ServerVersion Exit status codes are:
#
#	Exit code of 0: Server is running and the version matches the argument.
#	Exit code of 1: No Server Running.
#	Exit code of 2: Server is running but does not match the rev level.
#	Exit code of 3: Non-Sun Server is running.
#
$SUNPCHOME/bin/ServerVersion -revge 3400
SERVERVERSION_RET=$?

if [ "$SERVERVERSION_RET" -eq "0" ]
then
	echo "You must first exit OpenWindows and then rerun sunpc_UNinstall."
	exit 1
fi

#
# Remove the kernel driver from the system, rem_drv fixes up the
# /etc/driver_alias file too.
#
SUNPCDRV_INSTALLED=`${GREP} sunpcdrv /etc/driver_aliases`
if [ -n "$SUNPCDRV_INSTALLED" ]
then
	${REM_DRV} sunpcdrv
fi

#
# Remove kernel files
#
rm /kernel/drv/sunpcdrv
rm /kernel/drv/sunpcdrv.conf

#
# Remove entries in /etc/devlink.tab file.
#
${MV} ${DEVLINKS_TAB} ${DEVLINKS_TAB}.sunpc2
${SED} -e '/sunpcdrv/ d' -e '/SunPC/ d' ${DEVLINKS_TAB}.sunpc2 > ${DEVLINKS_TAB}

#
# Remove storekernname program and the command script which runs it
# at boot time.
#
${RM} /kernel/drv/storekernname.${ARCH}
${RM} /etc/rc2.d/S10storekernname

#
# Remove SunPC 4.0 icons from /usr/include/images.
#
${RM} /usr/include/images/SunPC.disk.filemgr.icon
${RM} /usr/include/images/sunpcrc.filemgr.icon

#
# Remove SunPC entries from the magic file.
#
${MV} /etc/magic /etc/magic.sunpc2
${SED} '/SunPC/ d' /etc/magic.sunpc2 > /etc/magic 




