#!/usr/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/ihs/sbin/cl_ihsimport.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)23	1.2  src/43haes/usr/sbin/cluster/sa/ihs/sbin/cl_ihsimport.sh, hacmp, 61haes_r714 12/6/11 13:25:06

###################################################################
##
## NAME:    cl_ihsimport
##
## DESCRIPTION:    
##    Makes the necessary configuration changes to HACMP 
##    to create or remove highly available IBM Http Server.  
##    
##    When adding, an HACMP resource group will be created along 
##    with a the following HACMP resources
##        Service Label
##        Application Monitor
##        Application Server
##    After the resources are created, the IBM Http Server components will 
##    be updated to listen on the new service IP label.
##
##    When removing, the above changes will be undone.
##
## SYNTAX:    
##    cl_ihsimport -a -t 'HTTPSERVER' -h HttpServerName -p PrimaryNode - n SeondaryNodes -s ServiceIP \n\
##    cl_ihsimport -r -t 'HTTPSERVER' -h HttpServerName \n\
##
## EXIT CODES:    
##    0 - success
##    1 - failure
##
## KLIB Functions:
##    KLIB_AIX_ODM_get_odm_fields
##    KLIB_SA_get_metadata
##    KLIB_SA_delete_by_key_type_metadata
##    KLIB_HACMP_generate_name
##    KLIB_SA_add_metadata
##    KLIB_SA_add_ServiceLabel
##
###################################################################

###############################################################################
#
# Name: usage
#
# Prints usage message and exits the program.
#
# Arguments:  none
#
# Returns:    does not return
#
################################################################################
function usage {
    set +u
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    set -u

    print -u2 "
cl_ihsimport -a -t HTTPSERVER -h <HTTPServerName> \\
             -o <PrimaryNode> -n <SecondaryNode(s)> \\
             -s <ServiceIP> -P <PrefixOrNetmaskForServiceIP> \\
             [ -A <ApplicationName> ] [ -f <ConfigFile> ]

cl_ihsimport -u -t HTTPSERVER -h <HTTPServerName> \\
             -s <ServiceIP> -P <PrefixOrNetmaskForServiceIP> \\
             -A <ApplicationName>

cl_ihsimport -r -t HTTPSERVER -h <HTTPServerName>
"

    exit 1
} # End of "usage()"


###############################################################################
#
# Name: UpdateResources
#
# Updates (-m) HACMP resources for IBM HTTP Server Smart Assist added by
# AddResources.
#
# Syntax:
#       UpdateResources
# 
# Arguments: none
# 
# Global Variables:
#
# Returns:
#    0 on SUCCESS
#    1 on FAILURE
#
################################################################################
function UpdateResources {

    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    if [[ -z $APPLICATION_NAME ]] ; then
        return 1
    fi
    
    typeset SERVICE_IP_LABEL_CHANGE=false
    typeset serviceip=""
    
    #############################################
    # Check the resource group exists or not, is offline, then remove it.
    #############################################
    typeset RESOURCE_GROUP
	set -a
	    eval $(clquerysaapp -a $APPLICATION_NAME RESOURCE_GROUP)
	set +a
	
    if [[ -z $RESOURCE_GROUP ]]; then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 80 "WARNING: The Resource Group does not exist in PowerHA SystemMirror.\n"
        RESOURCE_GROUP=$(KLIB_HACMP_generate_name "$APPLICATION_NAME" 63 "_rg" "HACMPgroup" "group")
    else
        serviceip=$(KLIB_AIX_ODM_get_odm_fields "group=$RESOURCE_GROUP and name=SERVICE_LABEL" "HACMPresource" "value")

        if [[ $SERVICE_IP_LABEL != $serviceip ]] ; then
            SERVICE_IP_LABEL_CHANGE=true;
        fi
        
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 120 "Removing the resource group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP"
        
        # Is resource group online?
        typeset STATE=""
        set -a
            eval $(clvt query resource_group $RESOURCE_GROUP | grep "STATE=")
        set +a
        
        if [[ -n $STATE && $STATE != "OFFLINE" && $STATE != "ERROR" && $STATE != "UNKNOWN" ]] ; then
            # Resource group is not offline, error, or in unknown so exit
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 590 "Resource Group $RESOURCE_GROUP is still online.  It must me offline\n       before removing the WebSphere Component.\n" "$RESOURCE_GROUP"
            return 1
        fi
        
        clrmgrp -g $RESOURCE_GROUP
        if (( $? != 0 )); then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 130 "ERROR: Failed removing resource group $RESOURCE_GROUP from PowerHA SystemMirror.\n" "$RESOURCE_GROUP" 1>&2
            return 1
        fi
        
        clrmsaapp -a $APPLICATION_NAME RESOURCE_GROUP
    fi
    
    #############################################
    # Check for new service label
    #############################################
    if [[ $SERVICE_IP_LABEL_CHANGE == true ]] ; then
        # create new one
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 140 "Creating service label $SERVICE_IP_LABEL.\n" "$SERVICE_IP_LABEL"
        KLIB_SA_add_ServiceLabel $SERVICE_IP_LABEL $SERVICE_IP_LABEL_PREFIX || {
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 160 "ERROR: Failed creating PowerHA SystemMirror Service IP Label $SERVICE_IP_LABEL.\n" "$SERVICE_IP_LABEL" 1>&2
            return 1
        }
    
        # delete old one
        for serviceIP in $serviceip; do
            clvt delete service_ip $serviceIP >/dev/null 2>&1
        done
    fi
    
    #############################################
    # Re-create resource group
    #############################################
    dspmsg -s $IMPORT_MSGSET $IHSSACAT 180 "Creating PowerHA SystemMirror Resource Group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP"
    
    typeset INSTALL_VGS=""
    typeset INSTALL_PATH=""
    typeset INSTALL_NODE_NAME=""
    typeset COMPONENT_ID=""
    typeset HACMP_SERVER_NAME=""
    typeset INSTALL_VGS=""
    set -a
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_VGS)
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_PATH)
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_NODE_NAME)
        eval $(clquerysaapp -a $APPLICATION_NAME COMPONENT_ID)
        eval $(clquerysaapp -a $APPLICATION_NAME HACMP_SERVER_NAME)
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_VGS)
    set +a
    
    claddsaapp -a $APPLICATION_NAME RESOURCE_GROUP="$RESOURCE_GROUP"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" RESOURCE_GROUP "$RESOURCE_GROUP" 1>&2
        return 1
    fi
    
    claddgrp -g "$RESOURCE_GROUP" -s "ignore" -n "$PRIMARY $SECONDARY" -S 'OHN' -O 'FNPN' -B 'NFB' -A $COMPONENT_ID -K $APPLICATION_NAME
    if (( $? != 0 )); then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 190 "ERROR: Failed creating PowerHA SystemMirror Resource Group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP" 1>&2
        return 1
    fi

    #claddres get into trouble if the Service Label is in dotted fashion
    ip_addr=$(/usr/bin/host $SERVICE_IP_LABEL | cut -d ' ' -f3)
    service_label=$(/usr/es/sbin/cluster/utilities/cllsif -c |  awk -F':' -v ipaddr=$ip_addr '$2 == "service" && $7 == ipaddr { print $1}' | uniq)

    claddres -g "$RESOURCE_GROUP" SERVICE_LABEL="$service_label" APPLICATIONS="$HACMP_SERVER_NAME" \
    VOLUME_GROUP="$INSTALL_VGS" FORCED_VARYON='false' \
    VG_AUTO_IMPORT='true' FILESYSTEM= FSCHECK_TOOL='fsck' \
    RECOVERY_METHOD='parallel' FS_BEFORE_IPADDR='false' \
    EXPORT_FILESYSTEM=  \
    MOUNT_FILESYSTEM= NFS_NETWORK= \
    SHARED_TAPE_RESOURCES= DISK= AIX_FAST_CONNECT_SERVICES= \
    COMMUNICATION_LINKS= MISC_DATA=

    if (( $? != 0 )); then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 200 "ERROR: Failed updating PowerHA SystemMirror Resource Group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP" 1>&2
        return 1
    fi

    # If Service IP Label changed 
    if [[ $SERVICE_IP_LABEL_CHANGE == true ]] ; then
    
        # Update IHS Config
        if [[ "$COMPONENT_TYPE" == "HTTPSERVER" ]]; then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 660 "Setting Listen Directive for IBM Http Server on node $INSTALL_NODE_NAME to the value \"$SERVICE_IP_LABEL\".\n" "$INSTALL_NODE_NAME" "$SERVICE_IP_LABEL"
            cl_rsh $INSTALL_NODE_NAME "$IHSSA_SBIN_DIR/cl_configihs $INSTALL_VGS $INSTALL_PATH $SERVICE_IP_LABEL" >/dev/null
            if (( $? != 0 )) ; then
                dspmsg -s $IMPORT_MSGSET $IHSSACAT 680 "ERROR: Failed setting Listen Directive for IBM Http Server on node %s." "$INSTALL_NODE_NAME" 1>&2
                return 1
            fi
        fi

        # Remove old HACMPsa_metadata entry for service label
        clrmsaapp -a $APPLICATION_NAME SERVICE_IP_LABEL
        
        # Add new HACMPsa_metadata entry for service label
        claddsaapp -a $APPLICATION_NAME SERVICE_IP_LABEL="$SERVICE_IP_LABEL"
        if (( $? != 0 )); then
            dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" SERVICE_IP_LABEL "$SERVICE_IP_LABEL" 1>&2
            return 1
        fi
    fi

    return 0
} # End of "UpdateResources()"


###############################################################################
#
# Name: ResetIHSConfig
#
# Removes (-r) HACMP resources for IBM HTTP Server Smart Assist added by
# AddResources.
#
# Syntax:
#       ResetIHSConfig
# 
# Arguments: none
# 
# Global Variables:
#
# Returns:
#    0 on SUCCESS
#    1 on FAILURE
#
################################################################################
function ResetIHSConfig {

    set +u
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    set -u

    typeset INSTALL_VGS=""
    typeset INSTALL_PATH=""
    typeset INSTALL_NODE_NAME
    set -a
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_VGS)
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_PATH)
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_NODE_NAME)
    set +a

    if [[ "$COMPONENT_TYPE" == "HTTPSERVER" ]]; then
        typeset LISTEN_HOST=""
        set -a
            eval $(clquerysaapp -a $APPLICATION_NAME LISTEN_HOST)
        set +a


        dspmsg -s $IMPORT_MSGSET $IHSSACAT 660 "Setting Listen Directive for IBM Http Server on node $INSTALL_NODE_NAME to the value \"$LISTEN_HOST\".\n" "$INSTALL_NODE_NAME" "$LISTEN_HOST"
        cl_rsh $INSTALL_NODE_NAME "$IHSSA_SBIN_DIR/cl_configihs $INSTALL_VGS $INSTALL_PATH $LISTEN_HOST" >/dev/null
        if (( $? != 0 )) ; then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 680 "ERROR: Failed setting Listen Directive for IBM Http Server on node %s." "$INSTALL_NODE_NAME" 1>&2
            return 1
        fi
    fi

    return 0
} # End of "ResetIHSConfig()"


###############################################################################
#
# Name: RemoveHACMPResources
#
# Removes (-r) HACMP resources for Websphere Smart Assist added by
# AddResources.
#
# Syntax:
#       RemoveHACMPResources
# 
# Arguments: none
# 
# Global Variables:
#
# Returns:
#    0 on SUCCESS
#    1 on FAILURE
#
################################################################################
function RemoveHACMPResources {

    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    
    # If we are here, then something happened during import, so 
    # all the values that are in HACMPsa_metadata should be correct

    ERROR=0

    # Check the resource group exists or not.
    typeset RESOURCE_GROUP
	set -a
	    eval $(clquerysaapp -a $APPLICATION_NAME RESOURCE_GROUP)
	set +a
    if [[ -z $RESOURCE_GROUP ]]; then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 80 "WARNING: The Resource Group does not exist in PowerHA SystemMirror.\n"
        return 0
    fi

    # Remove Application Server from the HACMP.
    typeset HACMP_SERVER_NAME
	set -a
	    eval $(clquerysaapp -a $APPLICATION_NAME HACMP_SERVER_NAME)
	set +a

    if [[ -n $HACMP_SERVER_NAME ]]; then

        cllsserv -n $HACMP_SERVER_NAME > /dev/null 2>&1
        if (( $? == 0 )); then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 440 "Removing PowerHA SystemMirror Application Server $PowerHA SystemMirror_SERVER_NAME from PowerHA SystemMirror.\n" "$PowerHA" SystemMirror_SERVER_NAME
            clrmserv "$HACMP_SERVER_NAME" 
            if (( $? != 0 )); then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 450 "ERROR: Failed removing PowerHA SystemMirror Application Server $PowerHA SystemMirror_SERVER_NAME from PowerHA SystemMirror.\n" "$PowerHA" SystemMirror_SERVER_NAME 1>&2
            ERROR=1
            fi
        else
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 460 "WARNING: The PowerHA SystemMirror Application Server $PowerHA SystemMirror_SERVER_NAME does not exist in PowerHA SystemMirror.\n" "$PowerHA" SystemMirror_SERVER_NAME
        fi
    else
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 470 "WARNING: The PowerHA SystemMirror Application Server does not exist in PowerHA SystemMirrorsa_metadata.\n"
    fi
   
    # Remove Application Monitor
    typeset HACMP_MONITOR_NAME
	set -a
	    eval $(clquerysaapp -a $APPLICATION_NAME HACMP_MONITOR_NAME)
	set +a
    if [[ -n $HACMP_MONITOR_NAME ]] ; then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 480 "Removing PowerHA SystemMirror Application Monitor $PowerHA SystemMirror_MONITOR_NAME from PowerHA SystemMirror.\n" "$PowerHA" SystemMirror_MONITOR_NAME
        clrmappmon $HACMP_MONITOR_NAME > /dev/null 2>&1
        if (( $? != 0 )); then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 490 "ERROR: Failed removing PowerHA SystemMirror Application Monitor $PowerHA SystemMirror_MONITOR_NAME from PowerHA SystemMirror.\n" "$PowerHA" SystemMirror_MONITOR_NAME 1>&2
            ERROR=1
        fi
    fi

    # Remove Service IP Label from the HACMP.
    typeset SERVICE_IP_LABEL
	set -a
	    eval $(clquerysaapp -a $APPLICATION_NAME SERVICE_IP_LABEL)
	set +a
    if [[ -n $SERVICE_IP_LABEL ]]; then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 90 "Removing Service IP Label $SERVICE_IP_LABEL from PowerHA SystemMirror.\n" "$SERVICE_IP_LABEL"
        clrmnode -a "$SERVICE_IP_LABEL"
        if (( $? != 0 )); then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 100 "ERROR: Failed removing Service IP Label $SERVICE_IP_LABEL from PowerHA SystemMirror.\n" "$SERVICE_IP_LABEL" 1>&2
            ERROR=1
        fi
    else
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 110 "WARNING: The Service IP Label does not exist in HACMPsa_metadata ODM.\n"
    fi

   # Remove Resource Group from the HACMP.
    if [[ -n $RESOURCE_GROUP ]]; then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 120 "Removing the resource group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP"
        clrmgrp -g $RESOURCE_GROUP
        if (( $? != 0 )); then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 130 "ERROR: Failed removing resource group $RESOURCE_GROUP from PowerHA SystemMirror.\n" "$RESOURCE_GROUP" 1>&2
            ERROR=1
        fi
    fi

    return $ERROR
} # End of "RemoveHACMPResources()"


###############################################################################
#
# Name: AddResources
#
# Adds (-a) HACMP resources for IBM HTTP Server Smart Assist.  If the resources already exist,
# they will be removed and then added.  Entries will also be added to the
# HACMPsa_metadata ODM.  
#
# Syntax:
#       AddResources
# 
# Arguments: none
# 
# Global Variables:
#
# Returns:
#    0 on SUCCESS
#    1 on FAILURE
#
################################################################################
function AddResources {

    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    # find the information related to the selected HTTP Server
    if [[ "$COMPONENT_TYPE" == "HTTPSERVER" && "$MANUAL_CONFIG" == false ]]; then 
        cl_rsh $HTTPSERVER $IHSSA_SBIN_DIR/cl_discihs 2>/dev/null | while read line; do
            echo $line | IFS=: read component install_count INSTALL_PATH version INSTALL_VGS install_pvids LISTEN_HOST
            
            # Only read one line because that's all there should be
            # Perform no checks because those have already been done
            break;
        done
        
    fi
    INSTALL_NODE_NAME=$HTTPSERVER
        
    STARTSCRIPT="cl_ihsstart"
    STOPSCRIPT="cl_ihsstop"
    MONSCRIPT="cl_ihsmon"
    
    # Add an entry into the HACMPsa_metadata ODM for the application id
    # and SMARTASSIST_ID and COMPONENT_ID
    claddsaapp -a "$APPLICATION_NAME" SMARTASSIST_ID='IHS_6.0' COMPONENT_ID="$COMPONENT_ID" APPLICATION_NAME="$APPLICATION_NAME" INSTANCE_NAME="$INSTANCE_KEY"

    # Create the Service IP Label
    # Find a useable network for this service IP label, this
    # is the same method used by the 2 node configuration assistant

    dspmsg -s $IMPORT_MSGSET $IHSSACAT 140 "Creating service label $SERVICE_IP_LABEL.\n" "$SERVICE_IP_LABEL"
    claddsaapp -a $APPLICATION_NAME SERVICE_IP_LABEL="$SERVICE_IP_LABEL" SERVICE_IP_LABEL_PREFIX="$SERVICE_IP_LABEL_PREFIX"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" SERVICE_IP_LABEL "$SERVICE_IP_LABEL" 1>&2
        return 1
    fi

    KLIB_SA_add_ServiceLabel $SERVICE_IP_LABEL $SERVICE_IP_LABEL_PREFIX || {
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 160 "ERROR: Failed creating PowerHA SystemMirror Service IP Label $SERVICE_IP_LABEL.\n" "$SERVICE_IP_LABEL" 1>&2
        return 1
    }

    # Create a HACMP application server   
    HACMP_SERVER_NAME=$(KLIB_HACMP_generate_name "$APPLICATION_NAME" 63 "_as" "HACMPserver" "name")
    dspmsg -s $IMPORT_MSGSET $IHSSACAT 540 "Creating PowerHA SystemMirror Application Server $PowerHA SystemMirror_SERVER_NAME.\n" "$PowerHA" SystemMirror_SERVER_NAME

    claddsaapp -a $APPLICATION_NAME HACMP_SERVER_NAME="$HACMP_SERVER_NAME"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" HACMP_SERVER_NAME "$HACMP_SERVER_NAME" 1>&2
        return 1
    fi

    claddserv -s "$HACMP_SERVER_NAME" \
    -b "$IHSSA_SBIN_DIR/$STARTSCRIPT $APPLICATION_NAME" \
    -e "$IHSSA_SBIN_DIR/$STOPSCRIPT $APPLICATION_NAME"

    if (( $? != 0 )); then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 550 "ERROR: Failed creating PowerHA SystemMirror Application Server $PowerHA SystemMirror_SERVER_NAME.\n" "$PowerHA" SystemMirror_SERVER_NAME 1>&2
        return 1
    fi

    # Create the Application Monitor
    HACMP_MONITOR_NAME=$(KLIB_HACMP_generate_name "$APPLICATION_NAME" 63 "_monitor" "HACMPmonitor" "monitor")
    claddsaapp -a $APPLICATION_NAME HACMP_MONITOR_NAME="$HACMP_MONITOR_NAME"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" HACMP_MONITOR_NAME "$HACMP_MONITOR_NAME" 1>&2
        return 1
    fi

    dspmsg -s $IMPORT_MSGSET $IHSSACAT 570 "Creating PowerHA SystemMirror Custom Application Monitor $PowerHA SystemMirror_MONITOR_NAME.\n" "$PowerHA" SystemMirror_MONITOR_NAME
    claddappmon MONITOR_TYPE=user name=$HACMP_MONITOR_NAME \
                RESOURCE_TO_MONITOR=$HACMP_SERVER_NAME \
                MONITOR_METHOD="$IHSSA_SBIN_DIR/$MONSCRIPT $APPLICATION_NAME" \
                INVOCATION='longrunning' \
                MONITOR_INTERVAL='120' STABILIZATION_INTERVAL='420' \
                RESTART_COUNT='3' RESTART_INTERVAL='1800' FAILURE_ACTION='fallover' \
                CLEANUP_METHOD="$IHSSA_SBIN_DIR/$STOPSCRIPT $APPLICATION_NAME" \
                RESTART_METHOD="$IHSSA_SBIN_DIR/$STARTSCRIPT $APPLICATION_NAME" \
                HUNG_MONITOR_SIGNAL='9' > /dev/null 2>&1
    if (( $? != 0 )); then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 580 "ERROR: Failed creating PowerHA SystemMirror Custom Application Monitor $PowerHA SystemMirror_MONITOR_NAME.\n" "$PowerHA" SystemMirror_MONITOR_NAME 1>&2
        return 1
    fi

    # Create the Resource Group
    RESOURCE_GROUP=$(KLIB_HACMP_generate_name "$APPLICATION_NAME" 63 "_rg" "HACMPgroup" "group")
    dspmsg -s $IMPORT_MSGSET $IHSSACAT 180 "Creating PowerHA SystemMirror Resource Group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP"
    claddsaapp -a $APPLICATION_NAME RESOURCE_GROUP="$RESOURCE_GROUP"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" RESOURCE_GROUP "$RESOURCE_GROUP" 1>&2
        return 1
    fi
    
    claddgrp -g "$RESOURCE_GROUP" -s "ignore" -n "$PRIMARY $SECONDARY" -S 'OHN' -O 'FNPN' -B 'NFB' -A $COMPONENT_ID -K $APPLICATION_NAME
    if (( $? != 0 )); then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 190 "ERROR: Failed creating PowerHA SystemMirror Resource Group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP" 1>&2
        return 1
    fi

    #claddres get into trouble if the Service Label is in dotted fashion
    ip_addr=$(/usr/bin/host $SERVICE_IP_LABEL | cut -d ' ' -f3)
    service_label=$(/usr/es/sbin/cluster/utilities/cllsif -c |  awk -F':' -v ipaddr=$ip_addr '$2 == "service" && $7 == ipaddr { print $1}' | uniq)

    claddres -g "$RESOURCE_GROUP" SERVICE_LABEL="$service_label" APPLICATIONS="$HACMP_SERVER_NAME" \
    VOLUME_GROUP="$INSTALL_VGS" FORCED_VARYON='false' \
    VG_AUTO_IMPORT='true' FILESYSTEM= FSCHECK_TOOL='fsck' \
    RECOVERY_METHOD='parallel' FS_BEFORE_IPADDR='false' \
    EXPORT_FILESYSTEM=  \
    MOUNT_FILESYSTEM= NFS_NETWORK= \
    SHARED_TAPE_RESOURCES= DISK= AIX_FAST_CONNECT_SERVICES= \
    COMMUNICATION_LINKS= MISC_DATA=

    if (( $? != 0 )); then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 200 "ERROR: Failed updating PowerHA SystemMirror Resource Group $RESOURCE_GROUP.\n" "$RESOURCE_GROUP" 1>&2
        return 1
    fi

    if [[ "$COMPONENT_TYPE" == "HTTPSERVER" ]]; then
        dspmsg -s $IMPORT_MSGSET $IHSSACAT 660 "Setting Listen Directive for IBM Http Server on node $INSTALL_NODE_NAME to the value \"$SERVICE_IP_LABEL\".\n" "$INSTALL_NODE_NAME" "$SERVICE_IP_LABEL"
        cl_rsh $INSTALL_NODE_NAME "$IHSSA_SBIN_DIR/cl_configihs $INSTALL_VGS $INSTALL_PATH $SERVICE_IP_LABEL" >/dev/null
        if (( $? != 0 )) ; then
            dspmsg -s $IMPORT_MSGSET $IHSSACAT 680 "ERROR: Failed setting Listen Directive for IBM Http Server on node %s." "$INSTALL_NODE_NAME" 1>&2
            return 1
        fi
    fi

    # add the metadata common elements
    AddCommonresources
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 410 "ERROR: Failed updating HACMPsa_metadata ODM.\n" 1>&2
        return 1
    fi
    if [[ "$COMPONENT_TYPE" == "HTTPSERVER" ]]; then
        # add the metadata elments specific to HTTP resources
        AddHTTPresources
    fi
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 410 "ERROR: Failed updating HACMPsa_metadata odm" 1>&2
        return 1
    fi

    return 0
} # End of "AddResources()"


###############################################################################
#
# Name: AddHTTPResources
#
# Entries related to HTTP Server will be added to the HACMPsa_metadata ODM.  
#
# Syntax:
#       AddHTTPResources
# 
# Arguments: none
# 
# Global Variables:
#
# Returns:
#    0 on SUCCESS
#    1 on FAILURE
#
################################################################################
function AddHTTPresources {
    
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    claddsaapp -a "$APPLICATION_NAME" LISTEN_HOST="$LISTEN_HOST"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" LISTEN_HOST "$LISTEN_HOST" 1>&2
        return 1
    fi

    return 0
} # End of "AddHTTPresources()"


###############################################################################
#
# Name: AddCommonResources
#
# Entries related to all the components (HTTP Server /Websphere Application 
# Server Node) will be added to the HACMPsa_metadata ODM.  
#
# Syntax:
#       AddCommonResources
# 
# Arguments: none
# 
# Global Variables:
#
# Returns:
#    0 on SUCCESS
#    1 on FAILURE
#
################################################################################
function AddCommonresources {
    
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    claddsaapp -a "$APPLICATION_NAME" INSTALL_NODE_NAME="$INSTALL_NODE_NAME"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" INSTALL_NODE_NAME "$INSTALL_NODE_NAME" 1>&2
        return 1
    fi

    claddsaapp -a "$APPLICATION_NAME" INSTALL_PATH="$INSTALL_PATH"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" INSTALL_PATH "$INSTALL_PATH" 1>&2
        return 1
    fi

    claddsaapp -a "$APPLICATION_NAME" INSTALL_VGS="$INSTALL_VGS"
    if (( $? != 0 )); then
        dspmsg -s $CONF_MSGSET $IHSSACAT 390 "ERROR: Failed adding %1\$s %2\$s to the HACMPsa_metadata ODM.\n" INSTALL_VGS "$INSTALL_VGS" 1>&2
        return 1
    fi

    return 0
} # End of "AddCommonresources()"


################################################################################
#
# Name:         importConfigFromFile
#
# Description:
#               This function will read the supplid config file and create
#               SystemMirror resources to configure DB2 Database for HA.
#
# Arguments:
#               None
#
# Returns:
#               0 - on success
#               1 - on failure
#
################################################################################
function importConfigFromFile {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    ACTION="add"
    COMPONENT_TYPE="HTTPSERVER"

    ver=$(clsaxmlutil -s -x $CONFIG_FILE -m $IHS_MANUAL_CONFIG_SCHEMA -t VERSION)
    (( $? != 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 4 "Problem in parsing %1$s tag in function %2$s\n" VERSION importConfigFromFile 1>&2
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 1 "Problem with XML configuration file. Ensure a valid XML file supplied.\n" 1>&2
        exit 1
    }
    VERSION=$(echo $ver | cut -d"=" -f2)

    path=$(clsaxmlutil -s -x $CONFIG_FILE -m $IHS_MANUAL_CONFIG_SCHEMA -t PATH_TO_HTDOCS)
    (( $? != 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 4 "Problem in parsing %1$s tag in function %2$s\n" PATH_TO_HTDOCS importConfigFromFile 1>&2
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 1 "Problem with XML configuration file. Ensure a valid XML file supplied.\n" 1>&2
        exit 1
    }

    path=$(echo $path | cut -d"=" -f2)

    INSTALL_PATH=$(print ${path/%htdocs\//})
   
    KLIB_AIX_add_vg_by_path INSTALL_VGS $INSTALL_PATH "$INSTALL_VGS"


    primary_node=$(clsaxmlutil -s -x $CONFIG_FILE -m $IHS_MANUAL_CONFIG_SCHEMA -t PrimaryNode)
    (( $? != 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 4 "Problem in parsing %1$s tag in function %2$s\n" PrimaryNode importConfigFromFile 1>&2
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 1 "Problem with XML configuration file. Ensure a valid XML file supplied.\n" 1>&2
        exit 1
    }
    primary_node=$(echo $primary_node | cut -d"=" -f2)
    found=0
    for node in $(/usr/es/sbin/cluster/utilities/clnodename)
    do
        [[ $node == $primary_node ]] && {
            found=1
        }
    done
    (( $found == 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 2 "Primary Node %s is not valid in the cluster.\n" "$primary_node" 1>&2
        exit 1
    }
    PRIMARY=$primary_node
    takover_nodes=$(clsaxmlutil -s -x $CONFIG_FILE -m $IHS_MANUAL_CONFIG_SCHEMA -t TakeoverNodes)
    (( $? != 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 4 "Problem in parsing %1$s tag in function %2$s\n" TakeoverNodes importConfigFromFile 1>&2
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 1 "Problem with XML configuration file. Ensure a valid XML file supplied.\n" 1>&2
        exit 1
    }

    takover_nodes=$(echo $takover_nodes | cut -d"=" -f2)
    takeover_nodes=$(echo $takover_nodes  | tr ',' ' ')
    found=1
    for tnode in $takeover_nodes
    do
        flag=0
        for node in $(/usr/es/sbin/cluster/utilities/clnodename)
        do
            [[ $node == $tnode ]] && {
                flag=1
            }
        done
        (( $flag == 0 )) && {
            found=0
        }
    done

    (( $found == 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 3 "One of the Takeover node from %s is not valid in the cluster.\n" "$takeover_nodes" 1>&2
        exit 1
    }

    SECONDARY=$takeover_nodes

    ipaddr=$(clsaxmlutil -s -x $CONFIG_FILE -m $IHS_MANUAL_CONFIG_SCHEMA -t Listen_Host | grep IPAddress_or_name)
    (( $? != 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 4 "Problem in parsing %1$s tag in function %2$s\n" IPAddress_or_name importConfigFromFile 1>&2
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 1 "Problem with XML configuration file. Ensure a valid XML file supplied.\n" 1>&2
        exit 1
    }
    SERVICE_IP_LABEL=$(echo $ipaddr | cut -d"=" -f2)

    netmask=$(clsaxmlutil -s -x $CONFIG_FILE -m $IHS_MANUAL_CONFIG_SCHEMA -t Listen_Host | grep Prefix_or_Netmask)
    (( $? != 0 )) && {
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 4 "Problem in parsing %1$s tag in function %2$s\n" Prefix_or_Netmask importConfigFromFile 1>&2
        dspmsg -s $MANUAL_CONFIG_SET $IHSSACAT 1 "Problem with XML configuration file. Ensure a valid XML file supplied.\n" 1>&2
        exit 1
    }
    SERVICE_IP_LABEL_PREFIX=$(echo $netmask | cut -d"=" -f2)

    LISTEN_HOST=$SERVICE_IP_LABEL

    HTTPSERVER=$PRIMARY
    INSTALL_NODE_NAME=$PRIMARY
    APPLICATION_NAME="ihs_$PRIMARY"
} # End of "importConfigFromFile()"


##########
# MAIN
##########

# Read in the message catalog entries

# Load the common functions, logmsg, dbgmsg, errmsg, abort
. /usr/es/lib/ksh93/common_functions.ksh93

umask -S u=rw,g=,o=

PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../../utilities/cl_get_path all)"
PATH=/usr/bin:$PATH:/etc:/usr/sbin:/usr/ucb:/sbin:/usr/es/sbin/cluster/sa/sbin
FPATH_BASE=/usr/es/lib/ksh93
FPATH=$FPATH_BASE/hacmp:$FPATH_BASE/sa:$FPATH_BASE/aix:$FPATH_BASE/aix/odm
export PATH FPATH

[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

IHSSACAT="ihssa.cat"
CONF_MSGSET=7
MANUAL_CONFIG_SET=20
IMPORT_MSGSET=8

IHSSA_HOME="/usr/es/sbin/cluster/sa/ihs"
IHSSA_ETC_PATH="$IHSSA_HOME/etc"
IHSSA_SBIN_DIR="$IHSSA_HOME/sbin"

typeset IHS_MANUAL_CONFIG_SCHEMA="$IHSSA_HOME/config/ihs_config.xsd"
typeset MANUAL_CONFIG=false #Manual Configuration mode
typeset CONFIG_FILE=""
typeset VERSION
typeset INSTALL_VGS
typeset INSTALL_PATH


APPLICATION_NAME=""

# Get the arguments.
while getopts ":A:n:h:s:P:o:N:c:t:m:f:aru" opt; do
    case $opt in
        A) APPLICATION_NAME=$OPTARG ;;
        a) ACTION="add" ;;
        f) MANUAL_CONFIG=true
           CONFIG_FILE=$OPTARG
        ;;
        h) HTTPSERVER=$OPTARG ;;
        n) [[ -n $SECONDARY ]] && SECONDARY="$SECONDARY "
           SECONDARY="$SECONDARY$OPTARG"
        ;;
        o) PRIMARY=$OPTARG ;;
        P) SERVICE_IP_LABEL_PREFIX=$OPTARG ;;
        r) ACTION="remove" ;;
        s) SERVICE_IP_LABEL=$OPTARG ;;
        t) COMPONENT_TYPE=$OPTARG ;;
        u) ACTION="update" ;;
        *) usage
           exit 1
        ;;
    esac
done
shift $(( $OPTIND - 1 ))


#
# Before handling anything else, check if we have to configure from XML
#
if [[ $MANUAL_CONFIG == true ]]; then
    if [[ ! -f $CONFIG_FILE ]]; then
        dspmsg -s 51 cluster.cat 26 "Unable to read the configuration file. Please ensure the correct path.\n" 1>&2
        return 1
    fi
    importConfigFromFile
fi


if [[ -z $COMPONENT_TYPE || -z $ACTION ]] ; then
    usage
    exit 1
fi


if [[ "$ACTION" == "add" || "$ACTION" == "update" ]]; then

    if [[ "$COMPONENT_TYPE" == "HTTPSERVER" ]]; then 
        if [[ -z $HTTPSERVER ]] ; then
            usage
            exit 1
        fi

        COMPONENT_ID="IHS_SERVER"
        INSTANCE_KEY="IHS_$HTTPSERVER"
    fi

    # Do pre smart assist stuff
	clsapre

    if [[ -z $PRIMARY || -z $SECONDARY || -z $SERVICE_IP_LABEL ]] ; then
        usage
        exit 1
    fi

    if [[ "$ACTION" == "add" ]] ; then

        if [[ -z $APPLICATION_NAME ]] ; then
            APPLICATION_NAME="$INSTANCE_KEY"
        fi

        # Add all the required odm objects.
        AddResources
        if (( $? != 0 )); then
            
            # Something went wrong so clean-up as best as possible
            RemoveHACMPResources
            clrmsaapp -a $APPLICATION_NAME
            
            exit 1
        fi
    
    elif [[ "$ACTION" == "update" ]] ; then
        UpdateResources
        
        if (( $? != 0 )); then
            exit 1
        fi
    fi
    
    # Do post smart assist stuff. Verification won't be done.
	clsapost

elif [[ "$ACTION" == "remove" ]]; then
    if [[ -z $APPLICATION_NAME ]] ; then
        usage
        exit 1
    fi
    
    # At this point, the resource group has been removed.

    ResetIHSConfig
    if (( $? != 0 )); then
        exit 1
    fi
fi

exit 0
