#! /bin/sh
#
# $Id: sunpatch.sh,v 6.5.18.5.40.2 2005/08/04 18:57:28 caude Exp $ Copyright (c) 2001-2005 EMC Corporation
#

#
# Copyright (c) 2001-2005 EMC Corporation.
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.
#

## This is the pre/post-patch and pre/post-backout script. The case
## statement at the end decides what to do based on the value of $0.

client_postinstall()
{
if [ -f /usr/bin/nsr/nsrfile ]; then
	rm -f /usr/bin/nsr/nsrfile
	removef SUNWebsc /usr/bin/nsr/nsrfile
	removef -f SUNWebsc
fi
}

driver_postinstall()
{
## If we are running Solaris 7 or higher on 64-bit sparc or 32-bit i386,
## we need to use a different lus driver and scsi library.
if [ `uname -r` != "5.5.1" -a `uname -r` != "5.6" ]; then
    if [ \( `uname -p` = "i386"  -a `isainfo -b` = "32" \) -o \
	 \( `uname -p` = "sparc" -a `isainfo -b` = "64" \) ]; then

	mv -f /usr/kernel/drv/lus32 /usr/kernel/drv/lus
	removef SUNWebsd /usr/kernel/drv/lus
	removef SUNWebsd /usr/kernel/drv/lus32
	installf SUNWebsd /usr/kernel/drv/lus f

	mv -f /usr/lib/nsr/libscsi27.so.1 /usr/lib/nsr/libscsi.so.1
	removef SUNWebsd /usr/lib/nsr/libscsi.so.1
	removef SUNWebsd /usr/lib/nsr/libscsi27.so.1
	installf SUNWebsd /usr/lib/nsr/libscsi.so.1 f

	removef -f SUNWebsd
	installf -f SUNWebsd
    fi
fi

# clean up any previous additions to /etc/devlink.tab
DEVLINK=/etc/devlink.tab
cp $DEVLINK $DEVLINK.save || exit 2
sed1='/^#Legato Juke begin/,/^#Legato Juke end/d'
sed2='/^#Legato uscsi begin/,/^#Legato uscsi end/d'
sed3='/^#Legato Optical begin/,/^#Legato Optical end/d'
sed -e "$sed1" -e "$sed2" -e "$sed3" < $DEVLINK > /tmp/devlink.$$ && \
rm -f $DEVLINK && mv /tmp/devlink.$$ $DEVLINK
if [ $? -ne 0 ]; then
    rm -f $DEVLINK
    mv $DEVLINK.save $DEVLINK
    rm -f /tmp/devlink.$$
    exit 2
fi

# add an entry for the lus and op drivers to /etc/devlink.tab
echo "#Legato uscsi begin - added by $PKGINST install - do not EDIT" >> $DEVLINK
echo 'type=ddi_pseudo;name=lus;minor=0	lus' >> $DEVLINK
echo 'type=ddi_pseudo;name=op	optical\N0' >> $DEVLINK
echo "#Legato uscsi end - added by $PKGINST install - do not EDIT" >> $DEVLINK

# reload the lus and op drivers
rem_drv lus > /dev/null 2>&1
add_drv -m '* 0666 bin bin' lus > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "Legato SCSI passthrough driver configured and loaded"
else
    echo "Legato SCSI passthrough driver is configured but did not load"
fi
rem_drv op > /dev/null 2>&1
add_drv -n -m '* 0600 bin bin' op > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "Legato optical driver configured - will load at next boot."
else
    echo "Legato optical driver did not configure properly"
fi
}

driver_postremove()
{
# reload the lus and op drivers
rem_drv lus > /dev/null 2>&1
add_drv -m '* 0666 bin bin' lus > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "Legato SCSI passthrough driver configured and loaded"
else
    echo "Legato SCSI passthrough driver is configured but did not load"
fi
rem_drv op > /dev/null 2>&1
add_drv -n -m '* 0600 bin bin' op > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "Legato optical driver configured - will load at next boot."
else
    echo "Legato optical driver did not configure properly"
fi
}

start()
{
if [ -x /etc/init.d/networker ]; then
    ( cd / ; /etc/init.d/networker start )
else
    echo "You should now restart the Sun StorEdge(TM) Enterprise Backup daemons."
fi
}

stop()
{
nsr_procs=`/bin/ps -ef | egrep 'ansrd|nsrd|nsrindex|nsrindxd|nsrmmd|nsrmmdbd|nsrexecd|nsrib|nsriba' | grep -v egrep`

if [ "X$nsr_procs" != "X" ]; then
    if [ -x /usr/sbin/nsr/nsr_shutdown ]; then
	/usr/sbin/nsr/nsr_shutdown -a -q
    else
        echo "It appears that there are Sun StorEdge(TM) Enterprise Backup daemons running."
        echo "Please run 'nsr_shutdown -a' before proceeding."
	exit 1
    fi
fi
}

## main
case "`basename $0`" in
prepatch)	stop
		;;
postpatch)	pkginfo -q SUNWebsc && client_postinstall
		pkginfo -q SUNWebsd && driver_postinstall
		start
		;;
prebackout)	stop
		;;
postbackout)	pkginfo -q SUNWebsd && driver_postremove
		start
		;;
esac
exit 0
