#! /sbin/sh
###############
# Product: HPVM
# Fileset: HPVM-CORE
# postinstall
# @(#) $Revision: 7849 $
################
#
# (c) Copyright Hewlett-Packard Company, 2005
#
########################################################################
#                                                                      #
# The global variables SUCCESS, FAILURE, WARNING, EXCLUDE, PATH, ROOT,
# SW_CTL_SCRIPT_NAME, _pf, PRODUCT, and FILESET are all set by control_utils.
########################################################################

UTILS=/usr/lbin/sw/control_utils
if [[ ! -f $UTILS ]]
then
    msg error "Cannot find the sh functions library $UTILS"
    exit 1
fi
. $UTILS

exitval=$SUCCESS                # Anticipate success


###########################################################################
# Copy and merge configuration file(s) into place, if possible.
##########################################################################

# List tunables that can be merged here.  These should only be numeric,
# as spaces and other extraneous characters will result in a non-merge.
MERGEABLE_TUNABLES="HPVM_ENABLE VMMLOGSIZE AUTORDEVENABLE"

newconfig_cp /etc/rc.config.d/hpvmconf
retval=$?

# If newconfic_cp returns 103, then that indicates the original factory
# file was modified by the user.  So, try to merge the user's changes
# into the installed configuration file.
MERGE_FLAG=0
MERGE_TRIED=1
MERGE_SUSPECT=2
MERGE_DONE=3
if [[ "$SW_SESSION_IS_DRD" = 1 ]]
then
  : # Skip if DRD, may need check for alternate root
elif [[ $retval -eq 103 ]]
then

    MERGE_FLAG=$MERGE_TRIED
    #  First save off the current, user-modified file
    USER_MODIFIED=${_NC_PREV}.user_modified
    cp_retain $_NC_WRKG $USER_MODIFIED

    #  Now, copy the new configuration file in place since newconfig_cp didn't
    cp_retain $_NC_NEW $_NC_WRKG


    #  Now loop through the selected tunables in user's working (modified)
    #  configuration and, if different from previously
    #  delivered tunables, then change them in the config file
    for TUNABLE in $MERGEABLE_TUNABLES ; do
	USER_TUNABLE=`ch_rc -l -p $TUNABLE $USER_MODIFIED`
	PREV_TUNABLE=`ch_rc -l -p $TUNABLE $_NC_PREV`

	#  Only merge those tunables for which both files have values
	if [[ -n $PREV_TUNABLE && -n $USER_TUNABLE ]]; then

	    #  If tunables are different, modify working to use the new one
	    if [[ $PREV_TUNABLE -ne $USER_TUNABLE ]]; then
		ch_rc -a -p $TUNABLE=$USER_TUNABLE $_NC_WRKG
		CH_RETVAL=$?

		#  If ch_rc fails, then merge is certainly suspect.
		if [[ $CH_RETVAL -ne 0 ]]; then
		    MERGE_FLAG=$MERGE_SUSPECT
		elif [[ $MERGE_FLAG -ne $MERGE_SUSPECT ]]; then
		    #  If a merge was done successfully, then mark accordingly
		    MERGE_FLAG=$MERGE_DONE
		fi
	    fi
	else
	    #  If one of the tunables didn't have a value, then the merge is
	    #  suspect.
	    MERGE_FLAG=$MERGE_SUSPECT
	fi

    done
fi

if [[ $MERGE_FLAG -eq $MERGE_DONE ]]; then
    msg NOTE "The contents of \"$_NC_WRKG\" \ differ from the that \
	original factory version delivered with the product."
    msg BLANK "A new version of \"$_NC_PATH\" has been modified to accommodate \
	some of those differences."
    msg BLANK "This result has been placed on the system."
    msg NOTE "The existing version of \"$_NC_WRKG\" has been archived to \
	\"$USER_MODIFIED\"."
    msg BLANK "For reference, the new factory version of \"$_NC_PATH\" is \
	located at \"${_NC_NEW}\"."
    #  Now that configuration is in place and OK, set return value to 0
    retval=0

elif [[ $MERGE_FLAG -eq $MERGE_SUSPECT || $MERGE_FLAG -eq $MERGE_TRIED ]]; then
    #  If the merge had problems, behave in generic way by copying the current
    #  working version of the configuration file in place
    cp_retain $USER_MODIFIED $_NC_WRKG
    #  Display appropriate messages, note retval is assumed to be 103 here.
    newconfig_msgs $retval

else
    #  Otherwise, let newconfig_msgs report as usual.
    if [[ $retval -ne 1 ]]; then
	newconfig_msgs $retval
    fi

    #  If new configuration is OK, then set retval accordingly.
    if [[ $retval -lt 102 ]]; then
	retval=0
    fi
fi

if [[ $retval -ne 0 ]]
then
    exitval=$retval
fi

##########################################################################
# Handle long-running daemons attached to the old libhpvm.so library.
##########################################################################
# First ensure we should be here -- DRD or alternate root can't do this.
if [[ "$SW_SESSION_IS_DRD" = 1 ]]
then
    : # skip for DRD
elif ! [[ "$SW_ROOT_DIRECTORY" -ef / ]]
then
    : # skip for alternate root dir
elif [[ "${SW_SESSION_IS_UPDATE:-}" = "1" ]]
then
   : # skip is we are an update install
elif [[ -n "${SW_CONFIG_AFTER_REBOOT}" ]]
then
    : # skip for promised reboot during installation
else
    # Standard installation.  Find the processes using the obsoleted libraries.
    # (The 'extra' echo is to trim spaces for the benefit of ps.)
    pids=$(echo $(fuser 2>/dev/null /opt/hpvm/lib/hpux??/#*.so))
    if [[ "$pids" != "" ]]
    then
        typeset -i10 npids=-1
        typeset pidlist
        UNIX95=1 ps -p "$pids" -o pid= -o comm= |
        while read pid cmd
        do
            case "$cmd" in
                utild)
                    # This one can just be killed -- it restarts fine out of
                    # inittab.
                    kill $pid
                    ;;
                scopeux)
                    # This part of OpenView Performance Agent 
                    # Stop and restart via /sbin/init.d/ovpa
		    if [[ -x /sbin/init.d/ovpa ]]
		    then
			: #do nothing at this time, but since we know about don't complain either
                        #/sbin/init.d/ovpa stop
		        #/sbin/init.d/ovpa start
			fi
                    ;;
                cimservera|cimserverd|cimserver)
                    # This one wants special handling... 'bounce' it the
                    # approved way.
                    CIMSERVER=/opt/wbem/sbin/cimserver
                    if [[ -f $CIMSERVER ]] && [[ -x $CIMSERVER ]]
                    then
                        if $CIMSERVER -s && $CIMSERVER
                        then
                            msg NOTE "cimserver restarted"
                        else
                            msg WARNING "cimserver(1M) restart failed - \
                                manual restart may be required"
                            if [[ $exitval = $SUCESS ]]
                            then
                                exitval=$WARNING
                            fi
                        fi
                    else
                        # Somehow, the real cimserver is missing.
                        # Note this one as something we couldn't handle.
                        pidlist[npids+=1]=$pid
                    fi
                    ;;
                *)
                    # Accumulate ones we don't know how to handle.
                    pidlist[npids+=1]=$pid
                    ;;
            esac
        done
        if ((npids>=0))
        then
            msg WARNING "The following processes are still attached \
                         to obsoleted shared libraries:"
            # Figure out the prefix length by using 'msg' to find it.
            # This keeps the output indentation consistent, without messing up
            # the columns from ps.
            prefix="$(msg BLANK x 2>&1)"
            prefix="${prefix%x}"
            ps -p "${pidlist[*]}" -x | sed "s/^/$prefix/"
            if [[ $exitval = $SUCESS ]]
            then
                exitval=$WARNING
            fi
        fi
    fi
fi

##########################################################################
# Exit status
##########################################################################


exit $exitval
