#!/bin/sh

# $Id: samfs_postpatch,v 1.9.2.2 2006/04/13 15:33:38 priyak 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 SUNWsamfsr > /dev/null 2>&1
if [ $? = 0 ]; then

INSLOG=/tmp/SAM_install.log
ETCDIR=/etc/opt/SUNWsamfs
VARDIR=/var/opt/SUNWsamfs
EXEDIR=/opt/SUNWsamfs/sbin
SCRIPTS=/etc/opt/SUNWsamfs/scripts
EXAMPLE=/opt/SUNWsamfs/examples
KERNDRV=/kernel/drv

/bin/rm -f $INSLOG

echo ""

if [ ! -f ${KERNDRV}/samst.conf ]; then
	/bin/cp ${EXAMPLE}/samst.conf ${KERNDRV}/samst.conf
else
	/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 [ $? -ne 0 ]; then
		echo "samst.conf may have been updated for this release."
		echo "${EXAMPLE}/samst.conf is the latest released version."
		echo "Please update this file with any site specific changes"
		echo "and copy it to ${KERNDRV}/samst.conf ."
		echo "When you have done this you may need to run"
		echo "\"/usr/sbin/devfsadm -i samst\" if any devices were added."
		echo " "
	fi
	/bin/rm /tmp/$$.in1 /tmp/$$.in2
fi

if [ ! -f ${ETCDIR}/inquiry.conf ]; then
	/bin/cp ${EXAMPLE}/inquiry.conf ${ETCDIR}/inquiry.conf
else
	/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 [ $? -ne 0 ]; then
		echo "inquiry.conf may have been updated for this release."
		echo "${EXAMPLE}/inquiry.conf is the latest released version."
		echo "Please update this file with any site specific changes and"
		echo "copy it to ${ETCDIR}/inquiry.conf ."
		echo " "
	fi
	/bin/rm /tmp/$$.in1 /tmp/$$.in2
fi

#
# Copy in default versions of the required scripts if they don't exist.
# Notify the site if the customized versions have changed.  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
		/bin/cp ${EXAMPLE}/${object} ${SCRIPTS}/${object}
	else
		/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 [ $? -ne 0 ]; then
			echo "${object} may have been updated for this release."
			echo "${EXAMPLE}/${object} is the latest released version."
			echo "Please update this file with any site specific changes"
			echo "and copy it to ${SCRIPTS}/${object} ."
			echo ""
		fi
		/bin/rm /tmp/$$.in1 /tmp/$$.in2
	fi
	/usr/bin/chmod 0750     ${SCRIPTS}/${object}
	/usr/bin/chown root:bin ${SCRIPTS}/${object}
done

#
# Do NOT copy in default versions of the optional scripts if they don't exist.
# It is up to the site to do the copy if they want the script to be invoked.
# Notify the site if the customized versions have changed.
#
SH_LIST="dev_down.sh load_notify.sh log_rotate.sh"
for object in $SH_LIST; do
	if [ -f ${SCRIPTS}/${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 [ $? -ne 0 ]; then
			echo "${object} may have been updated for this release."
			echo "${EXAMPLE}/${object} is the latest released version."
			echo "Please update this file with any site specific changes"
			echo "and copy it to ${SCRIPTS}/${object} ."
			echo ""
		fi
		/bin/rm /tmp/$$.in1 /tmp/$$.in2
		/usr/bin/chmod 0750     ${SCRIPTS}/${object}
		/usr/bin/chown root:bin ${SCRIPTS}/${object}
	fi
done

#
# Reset the administrator group to the previous value.
#
GROUPADM=`cat /opt/SUNWsamfs/.samgroup`
/opt/SUNWsamfs/sbin/set_admin $GROUPADM

#
# Restart sam manager daemon if necessary
#
MGMMODE=`cat /opt/SUNWsamfs/.fsmgmtd`
FSMADM_CLIENTS=`cat /opt/SUNWsamfs/.fsmgmtd_clients`
if [ X$MGMMODE != XOFF ]; then
	# If local machine start now, else samsyschk script will start it when
	# it is called on the local machine at startup.
	if [ X$MGMMODE = XALL ]; then
		/opt/SUNWsamfs/sbin/fsmadm config -a
	else
		/opt/SUNWsamfs/sbin/fsmadm config -b
	fi

	/opt/SUNWsamfs/sbin/fsmadm add "$FSMADM_CLIENTS"
	echo "Please check the log files for any errors"
	echo "that were detected during startup"
	echo ""
fi

if [ -f /etc/sysevent/config/SUNW,SUNWsamfs,sysevent.conf ]; then
	# Check if sysevent is running
	syspid=`/usr/bin/pgrep syseventd`
	if [ "${syspid}" != "" ]; then
		echo "Restarting syseventd" >> $INSLOG 2>&1
		pkill -HUP syseventd >> $INSLOG 2>&1
		if [ $? != 0 ]; then
			echo Restart syseventd failed >> $INSLOG 2>&1
		else
			syspid=`/usr/bin/pgrep syseventd`
			echo Restarted syseventd[pid=$syspid] >> $INSLOG 2>&1
		fi
	else
		/usr/lib/sysevent/syseventd >> $INSLOG 2>&1
	fi
fi

/bin/catman -w -M /opt/SUNWsamfs/man

# Add samst driver.

echo "Configuring devices.  Please wait, this may take a while."
/bin/sync
SAMST_ERR=0
/usr/sbin/add_drv samst >> $INSLOG 2>&1
if [ $? -eq 1 ]; then
	SAMST_ERR=1
fi

# Build samst devices

echo ""
echo "Issuing /usr/sbin/devfsadm -i samst >> $INSLOG 2>&1"
/usr/sbin/devfsadm -i samst >> $INSLOG 2>&1
if [ $? != 0 ]; then
    SAMST_ERR=1
fi

if [ $SAMST_ERR != 0 ]; then
	echo ""
	echo "Adding the samst driver or building the samst devices failed"
	echo "This can occur if the driver is already installed, or if there"
	echo "are no direct attached libraries or optical devices connected"
	echo "to the system. See $INSLOG for the additional messages. "
fi

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

# 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 adding the samaio driver."
	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"
	echo ""
fi


/bin/grep -w "sam-fsd" /etc/inittab >/dev/null
if [ $? = 0 ]; then
	NUMFSD=`/bin/grep sam-fsd /etc/inittab | grep -cv "#"`
	echo " "
	if [ $NUMFSD -eq 0 ]; then
		sed -e /#*sam-fsd/s/#// < /etc/inittab > /etc/inittab.tmp
		NUMFSD=`/bin/grep sam-fsd /etc/inittab.tmp | grep -cv "#"`
		if [ $NUMFSD -eq 1 ]; then
			echo "INFO: sam-fsd entry detected in /etc/inittab"
			mv /etc/inittab.tmp /etc/inittab
			chmod 644 /etc/inittab
			sync
			echo "Please ensure run levels are correct; recommended is 023456"
			/bin/grep sam-fsd /etc/inittab
			echo " "
			echo "Restarting the init process to start sam-fsd"
			kill -HUP 1
		elif [ $NUMFSD -ne 0 ]; then
			echo "WARNING multiple sam-fsd would be active if we uncommented"
			echo "the sam-fsd lines; leaving /etc/inittab as is."
			/bin/grep sam-fsd /etc/inittab
			rm /etc/inittab.tmp
		else
			mv /etc/inittab.tmp /etc/inittab
			chmod 644 /etc/inittab
			sync
		fi
	else
		echo "INFO: sam-fsd entry detected in /etc/inittab."
		echo "Please ensure run levels are correct; recommended is 023456"
		/bin/grep sam-fsd /etc/inittab
		echo " "
		echo "Restarting the init process to start sam-fsd"
		kill -HUP 1
	fi
fi


fi

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


BIN_DIR=/usr/sbin
SMREG=${BIN_DIR}/smreg
SMCWEBSERVER=${BIN_DIR}/smcwebserver
INSTALL_DIR=/opt/SUNWfsmgr
APP_NAME=samqfsui
INSTALLED_APP_NAME=fsmgrAdmin_2.1
WEB_INF_DIR=${INSTALL_DIR}/${APP_NAME}/WEB-INF
LIB_DIR=${WEB_INF_DIR}/lib
JNI_JAR_NAME=fsmgmtjni.jar
REMOVE_APP_NAME=fsmgrAdmin_2.1

# Unregistering the jni jar (remove the file from the deployment dir)

if [ -s ${SMREG} ]; then

    ## un-register JNI JAR file
    ${SMREG} remove -l -s ${REMOVE_APP_NAME} ${JNI_JAR_NAME}
    ${SMREG} remove -a ${REMOVE_APP_NAME}

fi

# Register the jni jar (add the file to the deployment dir)
if [ -s ${SMREG} ]; then

    ## register JNI JAR file
    ${SMREG} add -a ${INSTALL_DIR}/${APP_NAME}
    ${SMREG} add -l -s ${INSTALLED_APP_NAME} ${LIB_DIR}/${JNI_JAR_NAME}
fi

# Always start Web Server after system reboot
${SMCWEBSERVER} enable

# start Web Server
${SMCWEBSERVER} start

fi

exit 0

