#!/bin/sh

# $Id: samfs_prebackout,v 1.6.12.1 2004/11/18 18:25:49 jp148923 Exp $

#	SAM-QFS_notice_begin
#
#	Solaris 2.x Sun Storage & Archiving Management File System
#
#	Copyright (c) 2004 Sun Microsystems, Inc.
#	All Rights Reserved.
#
#	Government Rights Notice
#	Use, duplication, or disclosure by the U.S. Government is
#	subject to restrictions set forth in the Sun Microsystems,
#	Inc. license agreements and as provided in DFARS 227.7202-1(a)
#	and 227.7202-3(a) (1995), DRAS 252.227-7013(c)(ii) (OCT 1988),
#	FAR 12.212(a)(1995), FAR 52.227-19, or FAR 52.227-14 (ALT III),
#	as applicable.  Sun Microsystems, Inc.
#
#	SAM-QFS_notice_end

#       Check for mounted samfs file systems.

ERREXIT=0

/usr/sbin/mount -p | /usr/bin/awk '{print $4}' | /usr/bin/grep samfs > /dev/null
if [ $? = 0 ]; then
	echo " "
	echo "--> Before removing this patch, you must unmount each"
	echo "--> SAM-FS file system. The following samfs file systems were found."
	echo " "
	/usr/sbin/mount -p | grep samfs 
	echo " "
	ERREXIT=1
fi

if [ `ps -e | grep  sam-amld | wc -l` -ne 0 ]; then
	echo " "
	echo "--> Before removing this patch, you must kill the sam-amld"
	echo "--> process. To do this do a \"samd stop\""
	echo " "
	ERREXIT=1
fi

if [ $ERREXIT = 1 ]; then
	echo "Aborting patch removal"
	echo ""
	exit 1
fi

/bin/grep sam-fsd /etc/inittab >/dev/null
if [ $? = 0 ]; then
	sed "/sf:023456:respawn:/d" /etc/inittab > /etc/inittab.tmp
	NUMFSD=`/bin/grep sam-fsd /etc/inittab.tmp | grep -cv "#"`
	if [ $NUMFSD -eq 0 ]; then
		sed -e "/sf:023456:respawn:/d" \
			-e "s/^.*sam-fsd/#&/" \
			</etc/inittab >/etc/inittab.tmp
	else
		echo "A user specified sam-fsd entry was found in /etc/inittab"
		/bin/grep sam-fsd /etc/inittab
		echo "Commenting it out due to sam-fsd being removed"
		sed -e "s/^.*sam-fsd/#&/" /etc/inittab >/etc/inittab.tmp
	fi
	mv /etc/inittab.tmp /etc/inittab
	chmod 644 /etc/inittab
	echo "Restarting init process to remove the sam-fsd process"
	kill -HUP 1
else
	echo "No sam-fsd entry found in /etc/inittab found"
fi

chmod -x /usr/lib/fs/samfs/sam-fsd
FSDPID=`/bin/ps -e | /usr/bin/grep sam-fsd | /usr/bin/grep -v grep | /usr/bin/cut -c1-6`
if [ -n "$FSDPID" ]; then
	kill -TERM $FSDPID
fi

# Remove samioc samst driver

/usr/sbin/rem_drv samioc >/dev/null 2>&1
/usr/sbin/rem_drv samst 2>/dev/null

GUISTATE=`grep sam-mgmtrpcd /etc/inittab | cut -f3 -d:`
if [ X$GUISTATE = Xrespawn ]; then
	echo "ALL" > /opt/SUNWsamfs/.sam-mgmtrpcd
elif [ X$GUISTATE = Xonce ]; then
	echo "BOOT" > /opt/SUNWsamfs/.sam-mgmtrpcd
else
	echo "OFF" > /opt/SUNWsamfs/.sam-mgmtrpcd
fi

# Remove the sam-mgmtrpcd from the inittab file
/opt/SUNWsamfs/sbin/samadm config -n

#
#       Unload SAM-FS modules
#
for SAMTYPE in samst samfs samsys
do
	modid=`/usr/sbin/modinfo | /usr/bin/grep $SAMTYPE | cut -c0-4`
	if [ "$modid" -ne "" ] ; then
		echo "... unloading $SAMTYPE - module $modid"
		/usr/sbin/modunload -i $modid  ||  {
			sleep 2
			/usr/sbin/modunload -i $modid  ||  {
				sleep 2
				/usr/sbin/modunload -i $modid  ||  {
					echo "module $SAMTYPE; modid - $modid"
					echo "will not unload cleanly; verify what"
					echo "process is still using it and resolve"
					echo "the dependency"
					echo ""
					echo "Aborting patch remove"
					echo " "
					exit 1
				}
			}
		}
	fi
done
echo " "

ETCDIR=/etc/opt/SUNWsamfs
SCRIPTS=/etc/opt/SUNWsamfs/scripts
EXAMPLE=/opt/SUNWsamfs/examples
KERNDRV=/kernel/drv

if [ -f ${KERNDRV}/samst.conf ] ; then
	# If examples and installed copy are identical; rm samst.conf
	# Filter out the module id line.
	if [ -f ${EXAMPLE}/samst.conf ]; then
		/bin/grep -v 'Id:' ${KERNDRV}/samst.conf > /tmp/$$.in1
		/bin/grep -v 'Id:' ${EXAMPLE}/samst.conf > /tmp/$$.in2
		/bin/diff /tmp/$$.in1 /tmp/$$.in2 > /dev/null
		if [ $? -eq 0 ]; then
			/bin/rm ${KERNDRV}/samst.conf
		fi
		/bin/rm /tmp/$$.in1 /tmp/$$.in2
	fi
fi

if [ -f ${ETCDIR}/inquiry.conf ] ; then
	# If examples and installed are identical; rm inquiry.conf
	# Filter out the module id line.
	if [ -f ${EXAMPLE}/inquiry.conf ]; then
		/bin/grep -v 'Id:' ${ETCDIR}/inquiry.conf > /tmp/$$.in1
		/bin/grep -v 'Id:' ${EXAMPLE}/inquiry.conf > /tmp/$$.in2
		/bin/diff /tmp/$$.in1 /tmp/$$.in2 > /dev/null
		if [ $? -eq 0 ]; then
			/bin/rm ${ETCDIR}/inquiry.conf
		fi
		/bin/rm /tmp/$$.in1 /tmp/$$.in2
	fi
fi

#
# Remove the default versions of the required scripts if they exist
# and the site has not made any customizations to them.
# Create a backup copy if the site has a customized version.
# Note that dev_down.sh and load_notify.sh are not required scripts.
#
SH_LIST="archiver.sh recycler.sh save_core.sh ssi.sh"
for object in $SH_LIST; do
	if [ -f ${SCRIPTS}/${object} ] ; then
		# If examples and installed are identical; rm script 
		# Filter out the module id line.
		if [ -f ${EXAMPLE}/${object} ]; then
			/bin/grep -v 'Id:' ${SCRIPTS}/${object} > /tmp/$$.in1
			/bin/grep -v 'Id:' ${EXAMPLE}/${object} > /tmp/$$.in2
			/bin/diff /tmp/$$.in1 /tmp/$$.in2 > /dev/null
			if [ $? -eq 0 ]; then
				/bin/rm ${SCRIPTS}/${object}
			fi
			/bin/rm /tmp/$$.in1 /tmp/$$.in2
		fi
	fi
done

/bin/rm -f $PKG_INSTALL_ROOT/opt/SUNWsamfs/man/windex

# Clean up Cluster link

/bin/rm -f $PKG_INSTALL_ROOT/usr/cluster/lib/rgm/rtreg/SUNW.qfs

exit 0
