#!/bin/sh

# $Id: qfs_prepatch,v 1.6.6.1 2004/06/10 17:25:37 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.

/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 " "
	exit 1
fi

STAT=0

/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

#
#       Unload the QFS modules
#
for SAMTYPE in samfs samsys
do
	modid=`/usr/sbin/modinfo | /usr/bin/grep $SAMTYPE |  cut -c0-4`
	if [ "$modid" -ne "" ] ; then
		echo "... unloading $SAMTYPE - module $modid (1st attempt)"
		/usr/sbin/modunload -i $modid  ||  {
			sleep 2
			echo "... unloading $SAMTYPE - module $modid (2nd attempt)"
			/usr/sbin/modunload -i $modid  ||  {
				sleep 2
				echo "... unloading $SAMTYPE - module $modid (3rd attempt)"
				/usr/sbin/modunload -i $modid  ||  {
					echo "    module $modid - $SAMTYPE"
					echo " "
					STAT=1
				}
			}
		}
	fi
done
echo " "

/bin/grep sam-mgmtrpcd /etc/inittab >/dev/null
if [ $? = 0 ]; then
	echo "Removing sam-mgmtrpcd entry from /etc/inittab, and restarting init"
	sed "/sfad:3:respawn/d" /etc/inittab > /etc/inittab1.tmp
	mv /etc/inittab1.tmp /etc/inittab
	chmod 644 /etc/inittab
	kill -HUP 1
	touch /opt/SUNWsamfs/.sam-mgmtrpcd
fi

RPCDPID=`/usr/bin/pgrep sam-mgmtrpcd`
if [ -n "$RPCDPID" ]; then
	chmod -x /opt/SUNWsamfs/sbin/sam-mgmtrpcd
	kill -TERM $RPCDPID
fi

if [ "$STAT" != 0 ] ; then
    echo "--> Before patching this package, you must unmount each"
	echo "--> QFS filesystem."
    echo " "
    exit 1
fi

exit 0
