#!/bin/sh
# $Id: samsyschk,v 1.3.6.1 2004/09/10 14:43:58 jb149512 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

INSLOG=/tmp/SAM_install.log

if [ ! -f /etc/opt/SUNWsamfs/remote_install ]; then
#	echo "A remote_install was not performed"
	exit 0
fi

echo "Remote install was performed. Performing initial system checks"
echo "and system configurations that cannot be done when doing a"
echo "remote installation."
echo ""

echo "Checking for mounted samfs file systems."
echo ""
/usr/sbin/mount -p | /usr/bin/awk '{print $4}' | /usr/bin/grep samfs >/dev/null
if [ $? = 0 ]; then
    echo "The following samfs filesystems are still mounted from"
	echo "previous installation. These need to be unmounted prior"
	echo "starting the system. Unable to cleanly start the system"
	echo "until these filesystems have been unmounted"
	echo ""
    /usr/sbin/mount -p | /usr/bin/grep samfs
    echo ""
    exit 1
fi

if [ `ps -e | grep  sam-amld | wc -l` -ne 0 ]; then
    echo "The sam-amld process is still running. The system was not"
	echo "shutdown cleanly from a prior installation before the remote"
	echo "installation was performed"
	echo ""
	echo "Make sure no \"sam-\" process are running. If unable to resolve"
	echo "the issues, a reboot of the system maybe required in order to"
	echo "guarantee a clean startup"
    echo ""
    exit 1
fi

# Remove the sam-mgmtrpcd from the inittab file and stop process if running
/opt/SUNWsamfs/sbin/samadm config -n

echo "Removing samst driver in case it has already been installed"
echo ""
/usr/sbin/rem_drv samst 2>/dev/null

echo "Removing samioc driver in case it has already been installed"
echo ""
/usr/sbin/rem_drv samioc 2>/dev/null

echo "Removing samaio driver in case it has already been installed"
echo ""
/usr/sbin/rem_drv samaio 2>/dev/null

echo "Validate that samst, samfs or samioc modules are not loaded."
echo "If any are loaded, we will try to unload them"
echo "Current modinfo showing any modules loaded"
modinfo | grep samst
modinfo | grep samfs
modinfo | grep samaio
modinfo | grep samioc
echo ""

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
            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 ""
					echo "module $SAMTYPE; modid - $modid"
					echo "will not unload cleanly; verify what"
					echo "process is still using it and resolve"
					echo "the dependency. Make sure no \"sam-\" process"
					echo "are running"
					echo ""
					exit 1
				}
            }
		}
    fi
done
echo " "

/bin/rm -f /opt/SUNWsamfs/man/windex

#
# Set forceload for all supported OS levels for 4.1 release and newer
#
cp /etc/system /etc/system.SUNWsamfs
echo '* BEGIN SUNWsamfs file system addition' >> /etc/system
echo '* DO NOT EDIT above line or the next 2 lines  below...' >> /etc/system
echo 'forceload: fs/samfs' >> /etc/system
echo '* END SUNWsamfs file system addition' >> /etc/system

#
# Set the user scripts to the correct administrator group
#
GROUPADM=bin
if [ -f /opt/SUNWsamfs/.samgroup ]; then
	GROUPADM=`cat $PKG_INSTALL_ROOT/opt/SUNWsamfs/.samgroup`
fi
echo ""
echo "Setting administrator scripts to be executable by $GROUPADM"
echo ""
/opt/SUNWsamfs/sbin/set_admin $GROUPADM
echo ""

/bin/rm -f $INSLOG
/bin/sync

#
# Add the samioc driver
#
echo "Adding samioc driver."
echo ""
/usr/sbin/add_drv -m '* 0666 root sys' samioc >> $INSLOG 2>&1
if [ $? -eq 1 ]; then
	echo "add_drv returned an error adding driver samioc. See $INSLOG"
	echo "for the additional messages. After resolving any issues, run"
	echo "\"/usr/sbin/add_drv -m '* 0666 bin bin' samioc\" to install"
	echo "samioc."
	echo " "
fi

if [ -d /var/sadm/pkg/SUNWsamfsr ]; then
	VERSION=`$PKGPARAM SUNWsamfsr VERSION | grep debug`
else
	VERSION=`$PKGPARAM SUNWqfsr VERSION | grep debug`
fi

if [ X$VERSION != X ]; then

	# Add samaio driver.
	echo ""
	echo "Adding samaio driver"
	/usr/sbin/add_drv -m '* 0666 root sys' samaio >>$INSLOG 2>&1
	if [ $? -eq 1 ]; then
		echo "add_drv returned an error."
		echo "See $INSLOG for the additional messages."
		echo ""
		echo "Once issue is resolved, run \"/usr/sbin/add_drv samaio\" to"
		echo "add the samaio driver, followed by \"/usr/sbin/devfsadm -i samaio\""
		echo "to build samaio devices."
		echo ""
	fi

	# Build samaio devices

	echo Building samaio devices
	echo "Issuing /usr/sbin/devfsadm -i samaio >> $INSLOG 2>&1"
	/usr/sbin/devfsadm -i samaio >>$INSLOG 2>&1
fi

#
# Only do the samst for SAM packages
#
if [ -d /var/sadm/pkg/SUNWsamfsr ]; then
	echo "Configuring devices.  Please wait, this may take a while."
	echo ""
	echo "Adding samst driver"
	/usr/sbin/add_drv samst >> $INSLOG 2>&1
	if [ $? -eq 1 ]; then
		echo "add_drv returned an error. This can occur if there are no tape"
		echo "or optical devices connected to the system or the driver is"
		echo "already installed. See $INSLOG for the additional messages."
		echo "Once issue is resolved, run \"/usr/sbin/devfsadm -i samst\""
		echo "to build samst devices. Look at $INSLOG for error messages"
		echo " "
	else
		# Build samst devices
		echo "Building samst devices"
		if [ ! -f /opt/SUNWsamfs/sbin/samdev ]; then
			echo "Issuing /usr/sbin/devfsadm -i samst >> $INSLOG 2>&1"
			/usr/sbin/devfsadm -i samst >>$INSLOG 2>&1
		else
			echo "Issuing /opt/SUNWsamfs/sbin/samdev >> $INSLOG 2>&1"
			/opt/SUNWsamfs/sbin/samdev >> $INSLOG 2>&1
		fi
	fi
fi

if [ -f /etc/sysevent/config/SUNW,SUNWsamfs,sysevent.conf ]; then
	echo "Restarting the sysevent daemon" >> $INSLOG 2>&1
	pkill -HUP syseventd >> $INSLOG 2>&1
fi

/bin/sync
/bin/sleep 5
echo "Configuring manpages"
/bin/catman -w -M /opt/SUNWsamfs/man
/bin/sync

MGMMODE=`cat /opt/SUNWsamfs/.sam-mgmtrpcd`
if [ X$MGMMODE != XOFF ]; then
	echo ""
	echo "Starting SAM-QFS Manager daemon"

	if [ X$MGMMODE = XALL ]; then
		/opt/SUNWsamfs/sbin/samadm config -a
	else
		/opt/SUNWsamfs/sbin/samadm config -b
		/opt/SUNWsamfs/sbin/samadm start
	fi
	echo ""
	echo "The sam-mgmtrpd daemon is started. Please check the"
	echo "log files for any errors that were detected during startup."
	echo ""
fi

echo "Completed with remote post install setup"
rm /etc/opt/SUNWsamfs/remote_install
