#! /sbin/sh
###############
# Product: HPVM
# Fileset: HPVM-CORE
# preinstall
# @(#) $Revision: 7737 $
################
# preinstall script for bl8 branch 
#
# (c) Copyright Hewlett-Packard Company, 2005-2006
#
########################################################################

UTILS="/usr/lbin/sw/control_utils"
if [[ ! -f $UTILS ]]
then
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi
. $UTILS
exitval=$SUCCESS

###############################################################################
# Prepare for installation of new configuration file(s).
###############################################################################

newconfig_prep /etc/rc.config.d/hpvmconf
retval=$?
if [[ $retval -ne 0 ]]
then
    exitval=retval
fi

###############################################################################
# If you have to perform an operation such as converting a file format to a
# new form, that operation can be done as part of the configure script.  You
# must be able to detect that the conversion has been done so the conversion
# is not attempted more than once.
###############################################################################
# Stop the hpvm daemon before installing
if [ -z "${SW_CONFIG_AFTER_REBOOT}" ] # env var is not set
then
    if [[ "$SW_SESSION_IS_DRD" = 1 ]]
    then
        : # skip for alternate root dir
    elif [[ -f /var/opt/hpvm/common/hpvm_isrunning ]]
    then
        if [[ -x /sbin/init.d/hpvm ]]
        then
            # Stop the daemon
            # only stop the daemon if HPVM is running
            if [ -c /dev/hpvmdvr ]
            then
                # Stop the daemon
                msg "NOTE:    Stopping HPVM daemon, preparing for new installation"
                /sbin/init.d/hpvm stop 
                fi
	    fi
       fi
    fi




umask 022
#
# Copy existing config data to backup area
###########################################################

[[ -d /var/opt/hpvm/ ]] && {
    BACK_HPVM=`(cd /var/opt/hpvm/; ls)`
    }
[[ "$BACK_HPVM" != "" ]] && {
    #back up any guest config files
    BACKUP_DIR=/var/opt/hpvm/backups
    rm -rf $BACKUP_DIR
    mkdir -p $BACKUP_DIR
    for  FILE  in $BACK_HPVM
       do
       [[ "$FILE" != "ISO-images" && "$FILE" != "backups" ]] && {
          (cd /var/opt/hpvm/; tar -cf - $FILE 2> /dev/null) | (cd $BACKUP_DIR; tar -xpf - 2> /dev/null)
	  }
       done
    }

######################################################################################
# Need to deliver these files as a actual files now, they were previously symlinks.
# If /opt/hpvm/guest-images/hpux/11iv[23]/hpvm_guest_depot.11iv[23].sd are symlinks, remove them.
# Also, /opt/hpvm/guest-images/hpux/hpvm_guest_depot.sd is no longer needed, remove it.
######################################################################################

for dirname in 11iv2 11iv3; do

    [[ -h /opt/hpvm/guest-images/hpux/$dirname/hpvm_guest_depot.$dirname.sd ]] && {
        rm -f /opt/hpvm/guest-images/hpux/$dirname/hpvm_guest_depot.$dirname.sd
       IPD_delfile /opt/hpvm/guest-images/hpux/$dirname/hpvm_guest_depot.$dirname.sd
    }
    done

[[ -f /opt/hpvm/guest-images/hpux/hpvm_guest_depot.sd ]] && {
        rm -f /opt/hpvm/guest-images/hpux/hpvm_guest_depot.sd
    }
######################################################################################
# hpvmresources.1m has been moved to hpvmresource.5m, cleanup the .1m file
######################################################################################

[[ -f /opt/hpvm/share/man/man1m/hpvmresources.1m ]] && {
        rm -f /opt/hpvm/share/man/man1m/hpvmresources.1m
    }

exit $exitval
