#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_tape.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 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 
# @(#)74	1.6  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_tape.sh, hacmp.assist, 61haes_r721, 1618A_hacmp721 4/29/16 06:30:11

#================================================
# The following, commented line enforces coding
# standards when this file is edited via vim.
#================================================
# vim:tabstop=4:shiftwidth=4:expandtab:smarttab
#================================================

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

=head1 VERSION

 Version Number:  1.6
 Last Extracted:  5/3/16 18:54:04
 Last Changed:    4/29/16 06:30:11

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_tape.sh, hacmp.assist, 61haes_r721, 1618A_hacmp721

=head1 SYNOPSIS

 clmgr modify tape <tape> \
             [ NAME=<new_tape_label> ] \
             [ DEVICE=<tape_device_name> ] \
             [ DESCRIPTION=<tape_device_description> ] \
             [ STARTSCRIPT="</script/to/start/tape/device>" ] \
             [ START_SYNCHRONOUSLY={yes|no} ] \
             [ STOPSCRIPT="</script/to/stop/tape/device>" ] \
             [ STOP_SYNCHRONOUSLY={yes|no} ]

 NOTE: the alias for "tape" is "tp".

=head1 DESCRIPTION

Attempts to modify the specified PowerHA tape device
to conform to the provided specifications.

=head1 ARGUMENTS

 1. properties [REQUIRED] [hash ref]
    An associative array within which data about the
    modified object can be returned to the caller.

 2. tape [REQUIRED] [string]
    The label of the tape that is to be modified.

 3. new_name [OPTIONAL] [string]
    A new label to attempt to apply to the specified tape.

 4. device [REQUIRED] [string]
    The name for this tape device.

 5. description [OPTIONAL] [string]
    A description of the tape device. If no description is provided,
    then one will be automatically generated.

 6. start_script [OPTIONAL] [abspath]
    The start script for the tape device.

 7. start_sync [OPTIONAL] [string]
    A Boolean-like indicator of whether or not that tape device
    is to be started synchronously.

 8. stop_script [OPTIONAL] [abspath]
    The stop script for the tape device.

 9. stop_sync [OPTIONAL] [string]
    A Boolean-like indicator of whether or not that tape device
    is to be stopped synchronously.

=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.


function KLIB_HACMP_modify_tape {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=1.6, src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_tape.sh, hacmp.assist, 61haes_r721, 1618A_hacmp721
    : INPUTS: $*

    typeset -n properties=$1
    typeset tape=${2//\"/}
    shift; shift

    typeset new_name=${1//\"/}
    typeset device=${2//\"/}
    typeset description=${3//\"/}
    typeset start_script=${4//\"/}
    typeset -l start_sync=${5//\"/}
    typeset stop_script=${6//\"/}
    typeset -l stop_sync=${7//\"/}

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

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

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

    #================================================================
    : Assuming an object was specified, see if it is a known object
    #================================================================
    if [[ $tape != *([[:space:]]) ]]; then
        CL=$LINENO KLIB_HACMP_is_known_tape "$tape"
        (( $? != RC_SUCCESS )) && rc=$RC_NOT_FOUND
    fi

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

    elif (( $rc == RC_NOT_FOUND )); then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$tape" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 159 "Available Tapes:\n\n" 1>&2

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

    if [[ -n $description && $description == *:* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 103 "\nERROR: one or more invalid characters were detected in \"%1\$s\":  \"%2\$s\".\n\n" DESCRIPTION ":" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $start_script && $start_script != /* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 106 "\nERROR: the specified path does not appear to be in absolute format:\n%1\$s\n\n" "$start_script" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $start_script && ! -e $start_script ]]; then
        CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 107 '\nERROR: the specified path/file does not appear to exist on "%2$s": %1$s\n\n' "$start_script" "$LOCAL_NODE" 1>&2
        rc=$RC_NOT_FOUND
    fi

    if [[ -n $start_script && $start_script == *[[:space:]]* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 108 "\nERROR: the \"%1\$s\" attribute's value contains whitespace,\nwhich is not allowed: \"%2\$s\"\n\n" STARTSCRIPT "$start" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $stop_script && $stop_script != /* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 106 "\nERROR: the specified path does not appear to be in absolute format:\n%1\$s\n\n" "$stop_script" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $stop_script && ! -e $stop_script ]]; then
        CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 107 '\nERROR: the specified path/file does not appear to exist on "%2$s": %1$s\n\n' "$stop_script" "$LOCAL_NODE" 1>&2
        rc=$RC_NOT_FOUND
    fi

    if [[ -n $stop_script && $stop_script == *[[:space:]]* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 108 "\nERROR: the \"%1\$s\" attribute's value contains whitespace,\nwhich is not allowed: \"%2\$s\"\n\n" STOPSCRIPT "$stop" 1>&2
    fi

    if [[ -n $start_sync && $start_sync != @(y|t|n|f)* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" START_SYNCHRONOUSLY "$stop_sync" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "yes, no" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $stop_sync && $stop_sync != @(y|t|n|f)* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" STOP_SYNCHRONOUSLY "$stop_sync" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "yes, no" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ "$*" == *([[:space:]]) ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 401 "\nERROR: no valid modifications were specified for \"%1\$s\".\n\n" "$tape" 1>&2
        rc=$RC_MISSING_INPUT
    fi

    #========================================================
    : Modify the tape if no input errors have been detected
    #========================================================
    if (( $rc == RC_UNKNOWN )); then
        [[ -n $new_name ]]           && new_name_opt=" -N $new_name"
        [[ -n $device ]]             && device_opt=" -d $device"
        [[ -n $start_script ]]       && start_script_opt=" -p $start_script"
        [[ -n $stop_script ]]        && stop_script_opt=" -P $stop_script"
        [[ $start_sync == @(y|t)* ]] && start_sync_opt=" -s1" # Sync
        [[ $start_sync == @(n|f)* ]] && start_sync_opt=" -s0" # Async
        [[ $stop_sync == @(y|t)* ]]  && stop_sync_opt=" -S1"  # Sync
        [[ $stop_sync == @(n|f)* ]]  && stop_sync_opt=" -S0"  # Async

        print "$0()[$LINENO]($SECONDS): $HAUTILS/clchtape -n\"$tape\" $new_name_opt $device_opt $start_script_opt $start_sync_opt $stop_script_opt $stop_sync_opt" >>$CLMGR_TMPLOG  # Always log commands
        $HAUTILS/clchtape -n"$tape" \
                          $new_name_opt \
                          $device_opt \
                          $start_script_opt \
                          $start_sync_opt \
                          $stop_script_opt \
                          $stop_sync_opt
        rc=$?
        print "clchtape RC: $rc" >>$CLMGR_TMPLOG  # Always log command result

        if (( $rc != RC_SUCCESS )); then
            rc=$RC_ERROR
        elif [[ -n $new_name ]]; then
            tape=$new_name
        fi

        if (( $rc == RC_SUCCESS )) && [[ -n $description ]]; then
            print "$0()[$LINENO]($SECONDS): $HAUTILS/clchtape -n\"$tape\" -D\"$description\"" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/clchtape -n"$tape" \
                              -D"$description"
            rc=$?
            print "clchtape RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
            (( $rc != RC_SUCCESS )) && rc=$RC_ERROR
        fi

        #===========================================================
        : If output from this operation was requested, retrieve it
        #===========================================================
        if (( $rc == RC_SUCCESS )); then
            if (( CLMGR_VERBOSE )) || [[ -n $CLMGR_ATTRS ]]; then
                CL=$LINENO KLIB_HACMP_get_tape_attributes "$tape" properties
            fi
        fi
    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" \
        "modify tape -h" "TAPE:" "$CLMGR_PROGNAME" 1>&2
    fi

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