#!/bin/sh

# $Id: samfs_prebackout,v 1.5.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 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 " "
	exit 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 " "
	exit 1
fi

echo " "

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

# Remove samst driver

/usr/sbin/rem_drv samst 2>/dev/null

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


#
#     Save the administrator group
#
/bin/ls -l /opt/SUNWsamfs/sbin/chmed | /bin/awk '{ print $4 }' > /opt/SUNWsamfs/.admingrp

/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

ETCDIR=/etc/opt/SUNWsamfs
SCRIPTS=/etc/opt/SUNWsamfs/scripts
EXAMPLE=/opt/SUNWsamfs/examples
KERNDRV=/kernel/drv
DATE=`/usr/bin/date +%m%d%y-%H%M`

if [ -f ${KERNDRV}/samst.conf ] ; then
	# If nothing in examples or installed copy differs, make a backup.
	# Filter out the module id line.
	BACKUP=1
	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
		BACKUP=$?
		/bin/rm /tmp/$$.in1 /tmp/$$.in2
	fi
	if [ "$BACKUP" != 0 ]; then
		/bin/cp ${KERNDRV}/samst.conf ${KERNDRV}/samst.conf.${DATE}
		echo "samst.conf backed up to ${KERNDRV}/samst.conf.${DATE}"
	else
		/bin/rm ${KERNDRV}/samst.conf
	fi
fi

if [ -f ${ETCDIR}/inquiry.conf ] ; then
	# If nothing in examples or installed copy differs, make a backup.
	# Filter out the module id line.
	BACKUP=1
	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
		BACKUP=$?
		/bin/rm /tmp/$$.in1 /tmp/$$.in2
	fi
	if [ "$BACKUP" != 0 ]; then
		/bin/cp ${ETCDIR}/inquiry.conf ${ETCDIR}/inquiry.conf.${DATE}
		echo "inquiry.conf backed up to ${ETCDIR}/inquiry.conf.${DATE}"
	else
		/bin/rm ${ETCDIR}/inquiry.conf
	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 nothing in examples or installed copy differs, make a backup.
		# Filter out the module id line.
		BACKUP=1
		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
			BACKUP=$?
			/bin/rm /tmp/$$.in1 /tmp/$$.in2
		fi
		if [ "$BACKUP" != 0 ]; then
			/bin/cp ${SCRIPTS}/${object} ${SCRIPTS}/${object}.${DATE}
			echo "${object} backed up to ${SCRIPTS}/${object}.${DATE}"
		else
			/bin/rm ${SCRIPTS}/${object}
		fi
	fi
done

if [ "$STAT" != 0 ] ; then
    echo "--> Before patching this package, you must unmount each"
    echo "--> SAM-FS filesystem and kill the sam-amld process."
	echo "--> Unable to remove module."
    echo " "
    exit 1
fi

exit 0
