#! /sbin/sh
###############
# Product: HPVM 
# Fileset: HPVM-KRN 
# unconfigure 
# @(#) $Revision: 7737 $
################
#
# (c) Copyright Hewlett-Packard Company, 2000
#
########################################################################
#                                                                      #

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

############################################################################
# Remove /opt/hpvm/bin from /etc/PATH
############################################################################
# The name of the path to remove.
cmdpath=${SW_ROOT_DIRECTORY}opt/hpvm/bin
# Add the HPVM path into /etc/PATH.
mod_pathfile -d P ${cmdpath}
if [[ $? -ne 0 ]]
then
    # The path could not be removed from /etc/PATH so write a message to the log
    # file and set the exit value.
    msg WARNING "The path \"${cmdpath}\" could not be removed from /etc/PATH.  \
		 This value must be manually removed from the file."
    [[ ${exitval} -ne $FAILURE ]] && exitval=$WARNING
fi

############################################################################
# Remove /opt/hpvm/share/man from /etc/MANPATH
############################################################################
# The name of the path to remove.
manPath=${SW_ROOT_DIRECTORY}opt/hpvm/share/man
# Remove the path from /etc/MANPATH.
mod_pathfile -d MP ${manPath}
if [[ $? -ne 0 ]]
then
    # The path could not be removed from /etc/MANPATH so write a message to the
    # log file and set the exit value.
    msg WARNING "The path \"${manPath}\" could not be removed from \
	/etc/MANPATH.  This value must be manually removed from the file."
    [[ ${exitval} -ne $FAILURE ]] && exitval=$WARNING
fi 

############################################################################
# If this is DRD then skip all of the following. This can only be
# When the unconfigure is on the running disk
############################################################################
if [[ ${SW_SESSION_IS_DRD} != 1 ]]; then

    ##########################################################################
    # Kill vswitch if it is running.
    ##########################################################################


    if ! kill_named_procs vswitch
       then
        msg WARNING "Could not kill all vswitch processes. " 
        exitval=$WARNING
    fi

    ##########################################################################
    # Kill vswitch if it is running.
    # Halt all hpvm guests, and kill any processes they are using.
    ##########################################################################

    # Halt and Terminate the hpvm guests before removal of software.
    /sbin/init.d/hpvm stop > /dev/null 2>&1
    # Kill all the processes accessing /dev/ia64vmm
    fuser -k /dev/ia64vmm > /dev/null 2>&1
    # Find the char special devices named vm* and kill processes using them
    ls -l /tmp/vm* 2> /dev/null | grep ^c | awk '{print $10}' | grep -Ex 'vm[0-9]+' | xargs fuser -k > /dev/null 2>&1
    # Kill all vswitches
    ps -ef | grep -v grep | grep vswitch | awk '{print $2}' | xargs kill > /dev/null 2>&1

    # Remove symlinks for hpvm specific webpages, libhpvmfssclnt.so

    rm -f /usr/lib/hpux64/libhpvmfssclnt.so > /dev/null 2>&1
    fi
exit $exitval
