#!/bin/sh

# $Id: qfs_prepatch,v 1.7.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

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

/usr/sbin/mount -p | /usr/bin/awk '{print $4}' | /usr/bin/grep samfs > /dev/null
if [ $? = 0 ]; then
	echo " "
	echo "--> Before adding this patch, you must unmount each"
	echo "--> QFS filesystem."
	echo " "
	ERREXIT=1
fi

if [ $ERREXIT = 1 ]; then
	echo "Aborting patch addition"
	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 the samioc drivers

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

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 the QFS modules
#
for SAMTYPE in samfs samioc
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 " "
					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 add"
					echo " "
					exit 1
				}
			}
		}
	fi
done
echo " "

/bin/rm -f $PKG_INSTALL_ROOT/opt/SUNWsamfs/man/windex
/bin/rm -f $PKG_INSTALL_ROOT/usr/cluster/lib/rgm/rtreg/SUNW.qfs

exit 0
