#!/sbin/sh
########
#  Product: VMProvider
#  configure
########
#
# (c)Copyright 2004-2006 Hewlett-Packard Development Company, L.P.
#
########

#######################################################################################################
# Checking the version of installed cimserver                                                        ##
# If that version is  greater than or equal to x.02.05.xx + then cp HPVMReg.mof.cim25 to HPVMReg.mof ##
# If that version is  less than x.02.05.xx + then cp HPVMReg.mof.cim20 to HPVMReg.mof                ##
# Otherwise print a warning that cimserver is not installed                                          ##
#######################################################################################################

whatCIMSERVERinstalled()
{
    version=`/opt/wbem/sbin/cimserver -v` >/dev/null 2>&1

    if [[ $version = "" ]]
    then
      return 1
    fi

    maj_version=`echo $version | cut -d '.' -f 2` >/dev/null 2>&1
    min_version=`echo $version | cut -d '.' -f 3` >/dev/null 2>&1
    full_version=`echo $maj_version$min_version` >/dev/null 2>&1

    if [[ $full_version -ge 0205 ]]
    then
      return 25
    else
      return 20
    fi
} 

whatCIMSERVERinstalled; cimserver_version=$?

###############################
# cimserver -v command failed #
###############################

if [[ $cimserver_version -eq 1 ]]
then
  msg "WARNING: cimserver -v failed.  cimserver is required by VMProvider"
  exitval=$WARNING
else

   #Set up Path variables
   if [[ $SW_ROOT_DIRECTORY = "/" ]]
   then
      HPVM_MOF_DIR=/opt/hpvmprovider/mof
   else
      HPVM_MOF_DIR=${SW_ROOT_DIRECTORY}/opt/hpvmprovider/mof
   fi

   ###########################################
   # cimserver version >= 2.05 is installed   #
   # cp HPVMReg.mof.cim25 HPVMReg.mof  #######
   ###########################################
   if [[ $cimserver_version -eq 25 ]]
   then

        #Checking for files HPVMReg.mof.cim25 HPVMReg.mof
         if [[ -d $HPVM_MOF_DIR ]]
         then
             cd ${HPVM_MOF_DIR}

             if [[ -f HPVMReg.mof.cim25 ]]
             then
                cp HPVMReg.mof.cim25 HPVMReg.mof >/dev/null 2>&1
                chmog 444 bin bin $HPVM_MOF_DIR/HPVMReg.mof >/dev/null 2>&1
                exitval=$SUCCESS
             fi 
         fi
   ########################################
   # cimserver version <2.05 is installed  #
   # cp HPVMReg.mof.cim20 HPVMReg.mof  ####
   ########################################
   else
     #Checking for files HPVMReg.mof.cim20 HPVMReg.mof
     if [[ -d $HPVM_MOF_DIR ]]
     then
         cd ${HPVM_MOF_DIR}

         if [[ -f HPVMReg.mof.cim20 ]]
         then
            cp HPVMReg.mof.cim20 HPVMReg.mof >/dev/null 2>&1
            chmog 444 bin bin $HPVM_MOF_DIR/HPVMReg.mof >/dev/null 2>&1
            exitval=$SUCCESS
         fi 
     fi
   fi
fi

    # used to print nicely
    . /usr/lbin/sw/control_utils

    export wbem_home=/opt/wbem
    export hpvm_home=/opt/hpvmprovider
    export mod_name=HPVMProviderModule
    export hpvm_lib=$hpvm_home/lib/libHPVMProvider.1
    export namespace=root/cimv2/hpvm
    export namespace_pending=root/cimv2/hpvm/pending
    export namespace_pending_xml="<NAMESPACE NAME=\\\"pending\\\"\\/>"

    arch=$(uname -m)
    if [ $arch = "ia64" ]; then
        lib_ext=so
    else
        lib_ext=sl
    fi 
    export lib_link=$wbem_home/providers/lib/libHPVMProvider.$lib_ext

    if [ ! -n "$SUCCESS" ]; then
        SUCCESS=0
    fi
    if [ ! -n "$FAILURE" ]; then
        FAILURE=1
    fi
    export SUCCESS
    export FAILURE
    export exitval=$SUCCESS   # Anticipate success

#------------------------------------------------------------------------
    # print a message if there is a failure.
    hpvm_print_message ()
    {
        ret=$1
        message=$2
        if [ -n "$message" ]; then
            if [ $ret -ne 0 ]; then
                exitval=$FAILURE
                msg ERROR "$message"
            fi
        fi
    }

#------------------------------------------------------------------------
    hpvm_print_message_and_exit ()
    {
        hpvm_print_message $1 "$2"
        if [ $exitval -ne $SUCCESS ] ; then
            exit $exitval
        fi
    }

#------------------------------------------------------------------------
    export mof_files="\
        $hpvm_home/mof/Core27_Qualifiers.mof \
        $hpvm_home/mof/Core27_CoreElements.mof \
        $hpvm_home/mof/Core28_CoreElements.mof \
        $hpvm_home/mof/Core27_Physical.mof \
        $hpvm_home/mof/Core27_Capabilities.mof \
        $hpvm_home/mof/Core27_Device.mof \
        $hpvm_home/mof/System27_SystemElements.mof \
        $hpvm_home/mof/HP_SoftPartitionCS.mof \
        $hpvm_home/mof/HPVM_Capabilities.mof \
        $hpvm_home/mof/HPVM_EntitlementCapabilities.mof \
        $hpvm_home/mof/HPVM_GuestCapabilities.mof \
        $hpvm_home/mof/HPVM_HostCapabilities.mof \
        $hpvm_home/mof/HPVM_Guest.mof \
        $hpvm_home/mof/HPVM_Host.mof \
        $hpvm_home/mof/HPVM_DistributedHost.mof \
        $hpvm_home/mof/HPVM_IODevice.mof \
        $hpvm_home/mof/HPVM_Switch.mof \
        $hpvm_home/mof/HPVM_IODeviceDB.mof \
        $hpvm_home/mof/HPVM_GuestIODeviceDB.mof \
        $hpvm_home/mof/HPVM_RestrictedIODeviceDB.mof \
        $hpvm_home/mof/HPVM_ServerIODeviceDB.mof \
        $hpvm_home/mof/HPVM_InternalDataAcquisition.mof \
        $hpvm_home/mof/HPVM_MSEServer.mof \
        $hpvm_home/mof/HPVM_HostLanDevice.mof"

    export classnames="\
        HPVM_InternalDataAcquisition \
        HPVM_EntitlementCapabilities \
        HPVM_GuestCapabilities \
        HPVM_HostCapabilities \
        HPVM_Capabilities \
        HPVM_Guest \
        HPVM_Host \
        HPVM_GuestIODeviceDB \
        HPVM_RestrictedIODeviceDB \
        HPVM_ServerIODeviceDB \
        HPVM_IODeviceDB \
        HPVM_IODevice \
        HPVM_Switch \
        HP_SoftPartitionCS"

    # added_classnames is a list of classes that have been added since the 
    # initial release and thus these classes may not be present in the 
    # repository.
    export added_classnames="\
        HPVM_MSEServer \
        HPVM_HostLanDevice \
        HPVM_DistributedHost"

    # When deleting classes all of a class's children must be deleted before
    # it is deleted.  When adding classes the parent class must be added first.
    # In other words, the order of the class names in $classnames is important
    # and the order of the mof files in $mof_files is important.
    #
    # Note: the cimmof command does not update the class if that class is
    # already in the repository.  To make sure the class gets updated this 
    # first deletes the classes and then adds them.
    update_mof ()
    {
        for classname in $added_classnames
        do
            ls "/var/opt/wbem/repository/root#cimv2#hpvm/classes" 2>&1 | grep "^$classname" 2>/dev/null > /dev/null
            if [ $? -eq 0 ]; then
                message=$(sed "s/-CLASSNAME-/$classname/" $hpvm_home/mof/DeleteClass.xml | sed "s/-PENDING-//" | $wbem_home/bin/wbemexec 2>&1)
                hpvm_print_message $? "$message"
            fi

            ls "/var/opt/wbem/repository/root#cimv2#hpvm#pending/classes" 2>/dev/null | grep "^$classname" 2>/dev/null > /dev/null
            if [ $? -eq 0 ]; then
                message=$(sed "s/-CLASSNAME-/$classname/" $hpvm_home/mof/DeleteClass.xml | sed "s/-PENDING-/$namespace_pending_xml/" | $wbem_home/bin/wbemexec 2>&1)
                hpvm_print_message $? "$message"
            fi
        done

        for classname in $classnames
        do
            message=$(sed "s/-CLASSNAME-/$classname/" $hpvm_home/mof/DeleteClass.xml | sed "s/-PENDING-//" | $wbem_home/bin/wbemexec 2>&1)
            hpvm_print_message $? "$message"

            message=$(sed "s/-CLASSNAME-/$classname/" $hpvm_home/mof/DeleteClass.xml | sed "s/-PENDING-/$namespace_pending_xml/" | $wbem_home/bin/wbemexec 2>&1)
            hpvm_print_message $? "$message"
        done

        for mof_file in $mof_files
        do
            message=$($wbem_home/bin/cimmof -w -n $namespace $mof_file 2>&1)
            hpvm_print_message $? "$message"

            message=$($wbem_home/bin/cimmof -w -n $namespace_pending $mof_file 2>&1)
            hpvm_print_message $? "$message"
        done
    }

#------------------------------------------------------------------------
  start_hpvmprovider_cimserver ()
    {
        if [[ ${SW_SESSION_IS_DRD} != 1 ]]; then
            junk=$( ps -e | grep "cimserver$" 2>&1 )
            if [ $? -ne 0 ]; then
                msg NOTE "Starting cimserver"
                $wbem_home/sbin/cimserver > /dev/null 2>&1
                if [ $? -ne 0 ]; then
                    exitval=$FAILURE
                    msg ERROR "Failed to start cimserver."
                fi
            fi
        fi
    }

#------------------------------------------------------------------------
    unregister_module ()
    {
        out=$( $wbem_home/bin/cimprovider -l 2>&1 )
        if [ $? -ne 0 ]; then
            msg ERROR "$wbem_home/bin/cimprovider failed: $out"
        else
            module=$( echo $out | grep $mod_name 2> /dev/null )
            if [ -n "$module" ]; then
                out=$( $wbem_home/bin/cimprovider -d -m $mod_name 2>&1 )
                hpvm_print_message $? "$out"

                out=$( $wbem_home/bin/cimprovider -r -m $mod_name 2>&1 )
                hpvm_print_message $? "$out"
            fi
        fi
    }

#------------------------------------------------------------------------
# Running cimprovider -d -m HPVMProviderModule should unload the vm provider's
# libraries.  However, in testing it sometimes does not.  This method is here
# to work around the problem.
#
# Note that when SD finds a library that cannot be removed and is in use it 
# moves the library and pre-appends it with a # sign.  If there is already a 
# file with a # sign it pre-appends it with ##.

    restart_hpvmprovider_cimserver_if_needed ()
    {
        oldlibs=$( ls /opt/hpvmprovider/lib/* | grep "#" | grep libHPVMProvider 2>/dev/null )

        restart_cimserver=0
        for oldlib in $oldlibs
        do
            rm -f $oldlib >/dev/null 2>&1
            if [ $? -ne 0 ]; then
                restart_cimserver=1
                msg NOTE "The library, $oldlib, is still in use by the cimserver."
            fi
        done

        if [ $restart_cimserver -ne 0 ]; then
            msg NOTE "Restarting the cimserver."
            $wbem_home/sbin/cimserver -s > /dev/null 2>&1
            hpvm_print_message $? "Failed to stop the cimserver."

            $wbem_home/sbin/cimserver > /dev/null 2>&1
            hpvm_print_message $? "Failed to start cimserver."
        fi

        oldlibs=$( ls /opt/hpvmprovider/lib/* | grep "#" | grep libHPVMProvider 2>/dev/null )
        for oldlib in $oldlibs
        do
            rm -f $oldlib >/dev/null 2>&1
            hpvm_print_message $? "The library, $oldlib, is still in use by the cimserver after trying to restart the cimserver."
        done
    }

#------------------------------------------------------------------------
    setup_symbolic_link ()
    {
        rm -f $lib_link > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            exitval=$FAILURE
            msg ERROR "Failed to remove $lib_link"
        fi

        ln -s $hpvm_lib $lib_link > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            exitval=$FAILURE
            msg ERROR "Could not create the symbolic link $lib_link"
        fi
    }

#------------------------------------------------------------------------
    create_namespace ()
    {
        message=$( $wbem_home/bin/cimmof -w -n $namespace \
                   $hpvm_home/mof/Core27_Qualifiers.mof 2>&1 )
        hpvm_print_message $? "$message"

        message=$( $wbem_home/bin/cimmof -w -n $namespace_pending \
                   $hpvm_home/mof/Core27_Qualifiers.mof 2>&1 )
        hpvm_print_message $? "$message"
    }

#------------------------------------------------------------------------
    register_module ()
    {
        message=$( $wbem_home/bin/cimmof -w -n root/PG_InterOp \
                   $hpvm_home/mof/HPVMReg.mof 2>&1 )
        hpvm_print_message $? "$message"
    }

#------------------------------------------------------------------------
# main

    start_hpvmprovider_cimserver

    unregister_module

    restart_hpvmprovider_cimserver_if_needed

    setup_symbolic_link
    
    create_namespace

    update_mof
     
    register_module

exit $exitval
