#!/bin/sh

# $Id: samfs_prebackout,v 1.6.24.2 2005/04/13 19:59:33 ta129963 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

# Check to make sure that sam-amld process has been terminated

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

# Check for any mounted samfs file systems. All samfs type file systems
# must be unmounted before the patch can be applied

/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 samfs type file systems must"
	echo "--> be unmounted. The following samfs file systems are mounted."
	echo ""
	/usr/sbin/mount -p | grep samfs 
	echo ""
	ERREXIT=1
fi

if [ $ERREXIT = 1 ]; then
	echo "FATAL ERROR: Aborting removing patch"
	echo ""
	exit 1
fi

echo ""

# 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
	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

# 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 samaio samioc samst drivers

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

#
#       Unload SAM-FS modules
#
for SAMTYPE in samst 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 is"
					echo "using it and resolve the dependancy. Make sure"
					echo "that samu is not running"
					echo ""
					echo "Aborting patch remove"
					echo ""
					exit 1
				}
			}
		}
	fi
done
echo ""


# Save current state of GUI
# Get Solaris major revision.
#
RELEASE=`grep Solaris $PKG_INSTALL_ROOT/etc/release`
RELREV=`echo $RELEASE | sed -e "s/^.*Sol/Sol/" | cut -f2 -d' '`

if [ $RELREV -eq 10 ]; then
	service_state=`/usr/bin/svcs -H -o state sam-mgmt`
	if [ X$service_state = Xonline ]; then
		GUISTATE=respawn
	fi
else
	GUISTATE=`grep sam-mgmtrpcd $PKG_INSTALL_ROOT/etc/inittab | cut -f3 -d:`
fi
if [ X$GUISTATE = Xrespawn ]; then
		echo "ALL" > $PKG_INSTALL_ROOT/opt/SUNWsamfs/.sam-mgmtrpcd
elif [ X$GUISTATE = Xonce ]; then
	echo "BOOT" > $PKG_INSTALL_ROOT/opt/SUNWsamfs/.sam-mgmtrpcd
else
	echo "OFF" > $PKG_INSTALL_ROOT/opt/SUNWsamfs/.sam-mgmtrpcd
fi

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

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 copy 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 sendtrap"
for object in $SH_LIST; do
	if [ -f ${SCRIPTS}/${object} ] ; then
		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

exit 0
