# check for root
user=`/bin/id | /usr/bin/grep root | /usr/bin/wc -l`
if [ $user -eq 0 ]; then
	echo "You must be root to execute this script."
	exit 1
fi

# for TR agent, patch requires a function in SUNWcfcl that wasn't available
# until lv 6.2.2 - make sure we have a new enough version that has it
PKG=SUNWcfcl
/usr/bin/pkginfo -q $PKG
if [ $? -eq 0 ]; then
    BASEDIR=`/usr/bin/pkginfo -r $PKG 2>&1`
    if [ $? -ne 0 ]; then
	BASEDIR=/
    fi
    /usr/bin/grep HBA_OpenAdapterByWWN $BASEDIR/usr/include/hbaapi.h > /dev/null
    if [ $? -ne 0 ]; then
	echo "This patch requires a newer version of SAN Foundation Kit"
	echo "(at least 4.2.2).  Patch will not be installed."
	exit 1
    fi
fi

# stop TR if installed
/usr/bin/pkginfo -q SUNWnsmbj
if [ $? -eq 0 ]; then
    # don't stop it unless they've configured - otherwise this file isn't
    # created yet and you get a bad-sounding error message
    if [ -e /opt/SUNWnsm/bin/sstr_ctl ]; then
        /etc/init.d/sstrd stop
    fi
fi

# stop CS if installed
/usr/bin/pkginfo -q SUNWdm
if [ $? -eq 0 ]; then
    /etc/init.d/sscs stop
    /etc/init.d/sscs.wbem stop
    /etc/init.d/sshd stop
    os=`/sbin/uname -r`
    if [ "$os" = "5.8" ]; then
	/etc/init.d/prngd stop
    fi
fi

# stop DE if installed
/usr/bin/pkginfo -q SUNWsade
if [ $? -eq 0 ]; then
    /etc/init.d/init.ssde stop
fi

# workaround for bug 4786593 in pkgadd
pkgs="SUNWdm SUNWgre SUNWgrr SUNWgrui SUNWgrw SUNWgrwt3 SUNWnsmdb"
for apkg in $pkgs;
do
    spacefile=/var/sadm/pkg/$apkg/install/space
    if [ -f $spacefile ]; then
	/bin/rm $spacefile
    fi
done
