#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_add_tape.sh 1.5 
#  
# 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 
# @(#)03	1.5  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_add_tape.sh, hacmp.assist, 61haes_r714 8/6/13 16:54:59

#================================================
# 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_add_tape

=head1 VERSION

 Version Number:  1.5
 Last Extracted:  1/31/14 04:41:46
 Last Changed:    8/6/13 16:54:59

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_add_tape.sh, hacmp.assist, 61haes_r714

=head1 SYNOPSIS

 clmgr add tape <tape> \
             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 add a new tape device to the cluster configuration.

=head1 ARGUMENTS

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

 2. tape [REQUIRED] [string]
    The label that is to be applied to this tape device.

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

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

 5. start_script [OPTIONAL] [string]
    The start script for the tape device.

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

 7. stop_script [OPTIONAL] [string]
    The stop script for the tape device.

 8. 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_add_tape {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=1.5, src/43haes/lib/ksh93/hacmp/KLIB_HACMP_add_tape.sh, hacmp.assist, 61haes_r714
    : INPUTS: $*

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

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

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset start_script_opt= start_sync_opt= stop_script_opt= stop_sync_opt=

    typeset existing
    CL=$LINENO KLIB_HACMP_list_tapes existing 2>>$CLMGR_TMPLOG

    #=================
    : 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 [[ " ${existing[*]} " == *\ $tape\ * ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 229 "\nERROR: the specified object already exists: \"%1\$s\"\n\n" "$tape" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -z $device ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 101 "\nERROR: this operation requires the \"%1\$s\" attribute.\n\n" DEVICE 1>&2
        rc=$RC_MISSING_INPUT
    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 "$start_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 $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" 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

    #========================================================
    : Create the tape if no input errors have been detected
    #========================================================
    if (( $rc == RC_UNKNOWN )); then
        [[ -z $description ]] && description="$tape at $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/claddtape -n \"$tape\" -D \"$description\" -d \"$device\" $start_script_opt $start_sync_opt $stop_script_opt $stop_sync_opt" >>$CLMGR_TMPLOG  # Always log commands
        $HAUTILS/claddtape -n "$tape" \
                           -D "$description" \
                           -d "$device" \
                           $start_script_opt \
                           $start_sync_opt \
                           $stop_script_opt \
                           $stop_sync_opt
        rc=$?
        print "claddtape RC: $rc" >>$CLMGR_TMPLOG  # Always log command result

        if (( $rc != RC_SUCCESS )); then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 201 "\nERROR: failed to create \"%1\$s\".\n\n" "$tape" 1>&2
            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" \
        "add tape -h" "TAPE:" "$CLMGR_PROGNAME" 1>&2
    fi

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