#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_site.sh 1.8 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,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 
# @(#)88	1.8  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_site.sh, hacmp.assist, 61haes_r720, 1520B_hacmp720 5/8/15 10:48:44

# Start of POD-formatted documentation. Viewing suggestions:
#      perldoc <FILENAME>
#      pod2text -c <FILENAME>
#      pod2text -c --code <FILENAME>
#      pod2html <FILENAME>
function devDoc {
    : <<'=cut' >/dev/null 2>&1

=head1 NAME

KLIB_HACMP_delete_site

=head1 VERSION

 Version Number:  1.8
 Last Extracted:  5/14/15 17:39:14
 Last Changed:    5/8/15 10:48:44

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_site.sh, hacmp.assist, 61haes_r720, 1520B_hacmp720

=head1 SYNOPSIS

clmgr delete site {<sitename>[,<sitename#2>,<sitename#n>,...] | ALL}

=head1 DESCRIPTION

Deletes one or more sites from the cluster configuration.

=head1 ARGUMENTS

 1. sites [REQUIRED] [string]
    One or more (comma or space separated) sites to attempt to delete.

=head1 RETURN

 0: no errors were detected; the operation appears to have been successful
 1: a general error has occurred
 2: a specified resource does not exist, or could not be found
 3: some required input was missing
 4: some detected input was incorrect in some way
 5: a required dependency does not exist
 6: a specified search failed to match any data

=head1 COPYRIGHT

COPYRIGHT International Business Machines Corp. 2005,2010
All Rights Reserved

=cut
} # End of POD-formatted documentation.


#============================================================================
#
# Name:        KLIB_HACMP_delete_site
#
# Description: This is the main, FPATH function that is invoked by clmgr
#              to delete a cluster site. If the last site is removed from
#              a stretched cluster, the cluster is automatically changed
#              to a standard cluster.
#
# Inputs:      See the "devDoc()" function, above.
#
# Outputs:     Warning messages are emitted if a cluster type change is
#              performed.
#
# Returns:     Zero if no errors are detected. Otherwise, an appropriate
#              non-zero value is returned. Refer to the "RETURN" section
#              of the "devDoc()" function, above, for the standard return
#              code values/meanings for clmgr.
#
#============================================================================
function KLIB_HACMP_delete_site {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=1.8, src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_site.sh, hacmp.assist, 61haes_r720, 1520B_hacmp720
    : INPUTS: $*

    typeset sites=${1//\"/}
            sites=${sites//,/ }

    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset -i i=0
    typeset site=

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        rc=$RC_MISSING_DEPENDENCY

    #=================
    : Validate input
    #=================
    elif [[ -z $sites ]]; then
        dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
        rc=$RC_MISSING_INPUT

    else
        typeset -u uctext=$sites
        if [[ $uctext == "ALL" ]]; then
            sites=
            typeset available
            CL=$LINENO KLIB_HACMP_list_sites available
            for (( i=0; i<${#available[*]}; i++ )); do
                sites="$sites ${available[$i]}"
            done

            if [[ $sites == *([[:space:]]) ]]; then
                dspmsg -s $CLMGR_SET $CLMGR_MSGS 332 "Warning: there are no sites to delete.\n"
                rc=$RC_SUCCESS
            fi
        fi
    fi

    #=======================================================================
    : Validates if any repos mg or storage system exists with this site ID, 
    : if yes then stop here
    #=======================================================================
    if (( $rc == RC_UNKNOWN )); then
        for site in $sites; do
            is_entry=$(clodmget -n -q "field_type=site and value=$site" -f identifier HACMPxd_ext_attr 2>/dev/null)
            if [[ -n $is_entry ]]; then
                rc=$RC_ERROR
                dspmsg -s $CLVT_SET $CLVT_MSGS 770 "\nA repository mirror group with name \"%1\$s\" exists for site \"%2\$s\". Please remove it first.\n\n" "$is_entry" "$site" 1>&2
                break
            fi
            is_entry=$(clodmget -n -q "xd_storage_system_site_affiliation=$site" -f xd_storage_system_name HACMPxd_storage_system 2>/dev/null)
            if [[ -n $is_entry ]]; then
                rc=$RC_ERROR
                dspmsg -s $CLVT_SET $CLVT_MSGS 771 "\nA storage system with name \"%1\$s\" exists for site \"%2\$s\". Please remove it first.\n\n" "$is_entry" "$site" 1>&2
                break
            fi
        done
        (( $rc != RC_SUCCESS )) && print -u2
    fi

    #========================================================
    : Delete the site if no input errors have been detected
    #========================================================
    if (( $rc == RC_UNKNOWN )); then
        for site in $sites; do
            CL=$LINENO KLIB_HACMP_is_known_site "$site" >/dev/null 2>&1
            if (( $? != RC_SUCCESS )); then
                dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$site" 1>&2
                dspmsg -s $CLMGR_SET $CLMGR_MSGS 157 "Available Sites:\n\n" 1>&2

                typeset available
                CL=$LINENO KLIB_HACMP_list_sites available
                for (( i=0; i<${#available[*]}; i++ )); do
                    if [[ ${available[$i]} != *([[:space:]]) ]]; then
                        print -u2 "\t${available[$i]}"
                    fi
                done
                rc=$RC_NOT_FOUND

            else
                #=====================================================
                : If removing sites from a linked cluster, then warn
                : that cluster synchronization will not be possible.
                #=====================================================
                if [[ $CLUSTER_TYPE == "LC" ]]
                then
                    print
                    cl_dspmsg -s 63 cluster.cat 55 "\
Warning: you are removing sites from a Linked Cluster.
You will not be able to verify and synchronize these
changes until you add site(s) for a total of 2 sites.\n"
                    sleep 5  # Provide a little Ctrl+C time
                else
                    #======================================================
                    : If this removal will leave just one site, then warn
                    : that cluster synchronization will not be possible.
                    #======================================================
                    if (( $(clodmget -n -f name HACMPsite | wc -l) == 2 ))
                    then
                        print
                        cl_dspmsg -s 63 cluster.cat 54 "\
Warning: you are removing a site which will leave
only 1 site configured.
You will not be able to verify and synchronize these
changes until you add site(s) for a total of 2 sites
or delete all sites.\n"
                        sleep 5  # Provide a little Ctrl+C time

                    #========================================================
                    : If removing the last site, and this is a stretched
                    : cluster, then warn that the cluster will be converted
                    : to a standard cluster.
                    #========================================================
                    elif (( $(clodmget -n -f name HACMPsite | wc -l) == 1 )) &&
                         [[ $CLUSTER_TYPE == "SC" ]]
                    then
                        cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1016 "
Warning: you are removing the last site (\"%1\$s\") from a stretched cluster.
This will result in the cluster automatically being converted to a
standard cluster.\n" "$site"
                        sleep 5  # Provide a little Ctrl+C time
                    fi
                fi

                print "$0()[$LINENO]($SECONDS): claddsite -d -s $site" >>$CLMGR_TMPLOG  # Always log commands
                claddsite -d -s $site
                rc=$?
                print "$0()[$LINENO]($SECONDS): claddsite RC: $rc" >>$CLMGR_TMPLOG # Log the result
                if (( $rc != RC_SUCCESS )); then
                    rc=$RC_ERROR
                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 300 "\nERROR: failed to delete \"%1\$s\".\n\n" "$site" 1>&2
                    break

                else
                    #==========================================================
                    : The site was successfully removed. If there are no more
                    : sites, and this is a stretched cluster, then change the
                    : cluster type to standard.
                    #==========================================================
                    if [[ -z $(clodmget -n -f name HACMPsite) &&
                          $CLUSTER_TYPE == "SC" ]]
                    then
                        print "$0()[$LINENO]($SECONDS): multi_site_lc == $(clodmget -f multi_site_lc HACMPcluster)" >>$CLMGR_TMPLOG
                        print "$0()[$LINENO]($SECONDS): echo \"HACMPcluster: multi_site_lc=-1\" | odmchange -o HACMPcluster" >>$CLMGR_TMPLOG
                        print "HACMPcluster: multi_site_lc=-1" | odmchange -o HACMPcluster
                        print "$0()[$LINENO]($SECONDS): multi_site_lc == $(clodmget -f multi_site_lc HACMPcluster)" >>$CLMGR_TMPLOG


                        cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1015 "\nNote: now that the last site has been removed from the cluster, the cluster type has been automatically changed from stretched to standard.\n"
                    fi
                fi
            fi
        done
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
       [[ $CLMGR_GUI == *([[:space:]]) ]]
    then
        CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 104 "For more information about available options and syntax, try\n\"$HAUTILS/clmgr %1\$s\". As an\nalternative, if the PowerHA SystemMirror man pages have been installed, invoke\n\"$HAUTILS/clmgr -hv\" (or \"/usr/bin/man clmgr\"),\nsearching for \"%2\$s\" in the displayed text.\n\n" \
        "delete site -h" "SITE:" "$CLMGR_PROGNAME" 1>&2
    fi

    log_return_msg "$rc" "$0()" "$LINENO"
    return $?
} # End of "KLIB_HACMP_delete_site()"
