# ident	"@(#)patch_preinstall	1.1	12/05/17 SMI"
#
# based on
#pragma ident	"@(#)patch_preinstall	1.2	10/03/05 SMI"
#
# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

# Definitions for override safety mechanism
PATCH_OVERRIDE_LIB="${PKG_INSTALL_ROOT}/usr/lib/patch/patch_override_lib"

# Check if we messaging is ready. Returns 0 on success, 1 on failure
# On successful run it leaves MSG_FILENAME variable defined
# 
# Usage:
#
# check_patch_script_messaging
#
check_patch_script_messaging()
{
	TMP_DIRNAME="/var/run"
	MSG_FILENAME="${TMP_DIRNAME}/patchrm_msg_file_${ACTIVE_PATCH}"

	if [ -w "${MSG_FILENAME}" ] ; then
		unset TMP_DIRNAME
		return 0
	else
		unset TMP_DIRNAME
		unset MSG_FILENAME
		return 1
	fi
}

# This function prepares a message to be printed to stdout towards the end
# of postpatch execution. If the message file is not ready, it at least tries
# immediate output to stdout which is currently know to work only in patch
# level scripts.
#
# Usage:
#
# print_patch_message "text_of_message_to_be_printed_out"
#
print_patch_message()
{
	if check_patch_script_messaging; then
		echo "$*" >> "${MSG_FILENAME}"
	else
		echo "$*"
	fi

	return 0
}

# script in a function so that it can be overridden if needed.
patch_preinstall()
{

# fix for CR 7152382
PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
MFSTSCAN=/lib/svc/bin/mfstscan
SVCCFG=/usr/sbin/svccfg
SVCADM=/usr/sbin/svcadm
SVCPROP=/usr/bin/svcprop
AWK=/usr/bin/awk
CP=/usr/bin/cp
RM=/usr/bin/rm

STOP_DELAY=60

MANIFEST=/var/svc/manifest/network/iscsi_initiator.xml
MANIFEST_DEATH=/etc/svc/iscsi_initiator.xml.death
SVCINST=svc:/network/iscsi/initiator:default
DEATHROW_FILE=/etc/svc/deathrow
ISCSIRPKG=SUNWiscsir

#
# Helper function. Delete the manifest hash value.
#

svc_delhash()
{
        $SVCCFG delhash -d $MANIFEST >/dev/null 2>&1
        if [ "$?" != "0" ];then
                # this Solaris release doesn't have delhash command
                pg_name=`$MFSTSCAN -t $MANIFEST`
                if $SVCPROP -q -p $pg_name smf/manifest; then
                        $SVCCFG -s smf/manifest delpg $pg_name
                fi
        fi
}

#
# Helper function. Handle services deathrow file.
#
svc_deathrow()
{
        DEATHROW_FILE=${PKG_INSTALL_ROOT}/etc/svc/deathrow
        MANIFEST_FILE=${PKG_INSTALL_ROOT}${MANIFEST}
        MANIFEST_FILE_DEATH=${PKG_INSTALL_ROOT}${MANIFEST_DEATH}

        ENTITIES=`SVCCFG_NOVALIDATE=1 $SVCCFG inventory ${MANIFEST_FILE}`
        for fmri in $ENTITIES; do
                # add to service deathrow file
                echo ${fmri} ${MANIFEST_DEATH} ${ISCSIRPKG} >> ${DEATHROW_FILE}
        done
#       mv ${MANIFEST_FILE} ${MANIFEST_FILE_DEATH}
}

#
# Helper function. To hard code a deathrow file.
#
svc_deathrow_hardcode()
{
        DEATHROW_FILE=${PKG_INSTALL_ROOT}/etc/svc/deathrow

        echo "svc:/network/iscsi/initiator:default ${MANIFEST_DEATH} ${ISCSIRPKG}" >> ${DEATHROW_FILE}
        echo "svc:/network/iscsi/initiator:default ${MANIFEST_DEATH} ${ISCSIRPKG}" >> ${DEATHROW_FILE}
}

#
# Helper function. Wait the service to be disabled.
#
wait_disable() {
        while [ ${nsec:=0} -lt $STOP_DELAY ]; do
                state=`$SVCPROP -p restarter/state $SVCINST`
                if [ "$state" = "disabled" -o "$state" = "maintenance" ]; then
                        nstate=`$SVCPROP -p restarter/next_state $SVCINST`
                        if [ "$nstate" = "none" ]; then
                                return 0
                        fi
                fi
                /usr/bin/sleep 1
                nsec=`expr ${nsec} + 1`
        done
        return 1
}

if [ ! -f ${PKG_INSTALL_ROOT}${MANIFEST} ]; then
        #
        # Manifest file may get deleted before the running of this script
        # e.g., during upgrade
        #
        svc_deathrow_hardcode
        return 0
fi

old_fmri=no

ENTITIES=`$SVCCFG inventory ${PKG_INSTALL_ROOT}${MANIFEST}`
for fmri in $ENTITIES; do
        if [ "$fmri" = "$SVCINST" ]; then
                old_fmri=yes
        fi
done

if [ "$old_fmri" = "no" ]; then
        # nothing to do as there is no old fmri
        return 0
fi

#
# To remove the old fmri
#
if [ -r /etc/svc/volatile/repository_door ]; then
        smf_alive=yes
else
        smf_alive=no
fi

if [ "$PKG_INSTALL_ROOT" != "" -a "$PKG_INSTALL_ROOT" != "/" ]; then
        smf_alive=no
fi

if [ "$smf_alive" = "no" ]; then
        svc_deathrow
else
        ENTITIES=`$SVCCFG inventory $MANIFEST`
        for fmri in $ENTITIES; do
                # If this fmri refers to an instance, or a service
                $SVCPROP -p restarter/state $fmri >/dev/null 2>/dev/null
                if [ $? -eq 1 ]; then
                        # a service
                        $SVCCFG delete $fmri 2>/dev/null
                        continue
                fi

                #
                # Disable the instance
                #
                $SVCADM disable $fmri 2>/dev/null
                if [ $? -eq 0 ]; then
                        wait_disable
                        if [ $? -eq 0 ]; then
                                #disabled
                                $SVCCFG delete -f $fmri 2>/dev/null
                                continue
                        fi
                fi

                #
                # Forcily removal

                #
                ctid=`$SVCPROP -p restarter/contract $fmri 2>/dev/null`
                tctid=`$SVCPROP -p restarter/transient_contract $fmri 2>/dev/null`

                $SVCCFG delete -f $fmri

                #
                # Kill remaining processes.
                #
                if [ -n "${tctid}" -a "${ctid}" -gt 1 ]; then
                        # kill the stopper
                        /usr/bin/pkill -9 -c $tctid
                fi
                if [ -n "${ctid}" -a "{ctid}" -gt 1 ]; then
                        # kill remaining processes
                        /usr/bin/pkill -9 -c $ctid
                fi
        done
        #
        # Delete manifest hash value
        #
        svc_delhash
        #
        # Nail it down
        #
        svc_deathrow
fi
# /fix for CR 7152382
        :

}

# load the override lib if it exists
if [ -f "${PATCH_OVERRIDE_LIB}" -a -r "${PATCH_OVERRIDE_LIB}" ] ; then
   . "${PATCH_OVERRIDE_LIB}"
fi

# execute the script function
patch_preinstall "$@"

exit 0
