#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2019,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_restore_snapshot.sh 1.4 
#  
# 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 
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_restore_snapshot.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

# 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_restore_snapshot

=head1 SYNOPSIS

 clmgr manage snapshot restore <snapshot> \
             [ CONFIGURE={yes|no} ] \
             [ FORCE={no|yes} ]

 clmgr manage snapshot restore <snapshot> \
             NODES=<HOST>,<HOST#2> \
             REPOSITORIES=<DISK>[,<BACKUP>][:<SITE2_DISK>[,<BACKUP>]] \
             [ CLUSTER_NAME=<NEW_CLUSTER_LABEL> ] \
             [ CONFIGURE={yes|no} ] \
             [ FORCE={no|yes} ]

 NOTE: for "REPOSITORIES", any disks specified after the colon
       will be applied to the second site. This only applies to
       linked cluster.
 NOTE: the alias for "snapshot" is "sn".

=head1 DESCRIPTION

Apply the specified snapshot to the local cluster, blowing away the
current configuration in the process (i.e. replacing it with the
contents of the snapshot).

=head1 ARGUMENTS

 1. snapshot [REQUIRED] [string]
    The name of the snapshot that will be restored.

 2. configure [OPTIONAL] [string]
    A Boolean-like indicator that indicates (yes*/no) whether or
    not to configure resources during the restore, as needed.

 3. force [OPTIONAL] [string]
    A Boolean-like indicator that indicates (yes/no*) whether or
    not to force the restoration to continue even if a verification
    error is detected.

 4. nodes [REQUIRED] [string]
    This option should only be used when a cluster copy operation
    is intended (i.e. restoring a snapshot from a different cluster
    to the local cluster). This is the list of nodes that will be
    used to replace the nodes defined within the snapshot.

 5. repositories [REQUIRED] [string]
    This option should only be used when a cluster copy operation
    is intended (i.e. restoring a snapshot from a different cluster
    to the local cluster). This is the repository disk list that
    will replace the disk(s) defined within the snapshot.

 6. cluster_name [OPTIONAL] [string]
    This option should only be used when a cluster copy operation
    is intended (i.e. restoring a snapshot from a different cluster
    to the local cluster). This value will replace the cluster name
    defined within the snapshot.

=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

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_restore_snapshot {
    . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_restore_snapshot.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
    : INPUTS: $*

    typeset    snapshot=${1//\"/}
    typeset -l configure=${2//\"/}
    typeset -l force=${3//\"/}
    typeset    nodes=${4//\"/}
    typeset    repositories=${5//\"/}
    typeset    cluster_name=${6//\"/}

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset Copt= fopt= HOpt= DOpt= LOpt=
    typeset -i rc=$RC_UNKNOWN

    #=====================
    : Validate the input
    #=====================
    if [[ -z $snapshot ]]; then
        dspmsg -s $CLVT_SET $CLVT_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2

        dspmsg -s $CLMGR_SET $CLMGR_MSGS 158 "Available Snapshots:\n\n" 1>&2
        typeset available
        CL=$LINENO KLIB_HACMP_list_snapshots available
        for (( i=0; i<${#available[*]}; i++ )); do
            if [[ ${available[$i]} != *([[:space:]]) ]]; then
                print -u2 "\t${available[$i]}"
            fi
        done
        print -u2 ""

        rc=$RC_MISSING_INPUT

    elif [[ -n $configure && $configure != @(y|t|n|f)* ]]; then
        dspmsg -s $CLVT_SET $CLVT_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" CONFIGURE "$configure" 1>&2
        dspmsg -s $CLVT_SET $CLVT_MSGS 3 "Valid values: %1\$s\n\n" "yes, no" 1>&2
        rc=$RC_INCORRECT_INPUT

    elif [[ -n $force && $force != @(y|t|n|f)* ]]; then
        dspmsg -s $CLVT_SET $CLVT_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" FORCE "$force" 1>&2
        dspmsg -s $CLVT_SET $CLVT_MSGS 3 "Valid values: %1\$s\n\n" "no, yes" 1>&2
        rc=$RC_INCORRECT_INPUT

    else
        [[ $configure == @(n|f)* ]] && Copt=" -C"
        [[ $force     == @(y|t)* ]] && fopt=" -f true" || fopt=" -f false"

        [[ $nodes        != *([[:space:]]) ]] && HOpt=" -H ${nodes//+( )/,}"
        [[ $repositories != *([[:space:]]) ]] && DOpt=" -D ${repositories//+( )/,}"
        [[ $cluster_name != *([[:space:]]) ]] && LOpt=" -L $cluster_name"

        print "$0()[$LINENO]($SECONDS): clsnapshot -a -n \"$snapshot\"$fopt$Copt$HOpt$DOpt$LOpt" >>$CLMGR_TMPLOG  # Always log commands
        clsnapshot -a -n "$snapshot"$fopt$Copt$HOpt$DOpt$LOpt
        rc=$?
        print "clsnapshot RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        (( $rc != RC_SUCCESS )) && rc=$RC_ERROR
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
       [[ $CLVT_GUI == *([[:space:]]) ]]
    then
        CL=$LINENO cl_dspmsg -s $CLVT_SET $CLVT_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 \"man clmgr\"),\nsearching for \"%2\$s\" in the displayed text.\n\n" \
        "restore snapshot -h" "SNAPSHOT:" "$CLVT_PROGNAME" 1>&2
    fi

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