#!/bin/sh

# $Id: qfs_prebackout,v 1.9.2.5 2006/08/14 21:20:12 rh27482 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


pkginfo SUNWqfsr > /dev/null 2>&1
if [ $? = 0 ]; then


#       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 removing this patch, all qfs type file systems must"
	echo "--> be unmounted. The following qfs file systems are mounted:"
	echo ""
	/usr/sbin/mount -p | grep " samfs "
	echo ""
	echo "FATAL ERROR: Aborting removing patch"
	echo ""
	exit 1
fi

# Remove File System Manager daemon entry from inittab
# Disable fsmgmtd before unload samioc module

RELEASE=`grep Solaris /etc/release`
RELREV=`echo $RELEASE | sed -e "s/^.*Sol/Sol/" | cut -f2 -d' '`
if [ $RELREV -ge 10 ]; then
	SAMMGMT_SERVICE_NAME="svc:/application/management/fsmgmt:default"
	echo "Disabling auto-restart feature for File System Manager daemon"
	/usr/sbin/svcadm disable $SAMMGMT_SERVICE_NAME
else
	/bin/grep fsmgmtd /etc/inittab >/dev/null
	if [ $? = 0 ]; then
		# Remove the fsmgmtd from the inittab file
		sed "/sfad:3:/d" /etc/inittab >/etc/inittab.tmp
		mv /etc/inittab.tmp /etc/inittab
		sync
		echo "Restarting init process to remove the fsmgmtd process"
		kill -HUP 1
	fi
fi

#
# Remove the entry for sam-fsd in /etc/inittab so that it stops
# it from being respawned. If the sam-fsd entry is not the default
# one then only comment it out; it is a customer defined entry.

/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
	sync
	echo "Restarting init process to remove the sam-fsd process"
	echo ""
	kill -HUP 1
else
	echo "No sam-fsd entry found in /etc/inittab found"
fi

#chmod -x /usr/lib/fs/samfs/sam-fsd

# Make sure that a sam-fsd was not started by something other than init

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
sleep 5

# remove the samaio and samioc drivers

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

#
# Unload modules.  The samioc module is automatically unloaded when
# samfs unloads, but we'll also include it in this list.
#
for SAMTYPE in samfs samaio 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 process"
					echo "is still using it and resolve the dependency"
					echo ""
					echo "Aborting removing patch"
					echo ""
					exit 1
				}
			}
		}
	fi
done
echo " "

#
# Remove the default versions of the required scripts if they exist
# and the site has not made any customizations to them.
# Leave script if the site has a customized version.
#
SCRIPTS=/etc/opt/SUNWsamfs/scripts
EXAMPLE=/opt/SUNWsamfs/examples

SH_LIST="sendtrap"
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

fi

pkginfo SUNWfsmgrr > /dev/null 2>&1
if [ $? = 0 ]; then

BIN_DIR=/usr/sbin
SMCWEBSERVER=${BIN_DIR}/smcwebserver


#  Stop the smcwebserver if the webserver is up and running
ANS=`${SMCWEBSERVER} status -p | grep running | cut -d"=" -f2`
if [ "$ANS" = "yes" ]; then
    # Stop Web Server
    ${SMCWEBSERVER} stop
fi

fi

exit 0
