#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,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_add_fallback_timer.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 
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_add_fallback_timer.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

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

=head1 VERSION

 Version Number:  1.6
 Last Extracted:  1/31/14 04:41:43
 Last Changed:    8/6/13 16:54:49

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

=head1 SYNOPSIS

 clmgr add fallback_timer <timer> \
             [ YEAR=<####> ] \
             [ MONTH=<{1..12 | Jan..Dec}> ] \
             [ DAY_OF_MONTH=<{1..31}> ] \
             [ DAY_OF_WEEK=<{0..6 | Sun..Sat}> ] \
             HOUR=<{0..23}> \
             MINUTE=<{0..59}>

 NOTE: aliases for "fallback_timer" are "fa" and "timer".

=head1 DESCRIPTION

Attempts to create a fallback timer within the cluster that conforms
to the provided specifications.

=head1 ARGUMENTS

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

 2. timer [REQUIRED] [string]
    The label to apply to the new fallback timer.

 3. year [OPTIONAL] [integer]
    The year when this timer should be invoked.
    NOTE: the presence of this value implies a non-repeating timer.

 4. month [OPTIONAL] [integer]
    The month when this timer should be invoked.
    NOTE: the presence of this value implies a timer that repeats yearly.

 5. day_of_month [OPTIONAL] [integer]
    The day of the month when this timer should be invoked.
    NOTE: the presence of this value implies a timer that repeats monthly.

 6. day_of_week [OPTIONAL] [integer]
    The day of the week when this timer should be invoked.
    NOTE: the presence of this value implies a timer that repeats weekly.

 7. hour [REQUIRED] [integer]
    The hour when this timer should be invoked.

 8. minute [REQUIRED] [integer]
    The minute when this timer should be invoked.

=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_fallback_timer {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_add_fallback_timer.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
    : INPUTS: $*

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

    typeset year=${1//\"/}
    typeset monthStr=${2//\"/}
    typeset day_of_month=${3//\"/}
    typeset day_of_weekStr=${4//\"/}
    typeset hour=${5//\"/}
    typeset minute=${6//\"/}

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset JAN= FEB= MAR= APR= MAY= JUN= JUL= AUG= SEP= OCT= NOV= DEC=
    typeset MONTHS=$(/usr/bin/dspmsg -s 36 cluster.cat 15 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec')
    print -- "$MONTHS" | IFS=, read JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC REM
    typeset -l JAN_LC=$JAN
    typeset -l FEB_LC=$FEB
    typeset -l MAR_LC=$MAR
    typeset -l APR_LC=$APR
    typeset -l MAY_LC=$MAY
    typeset -l JUN_LC=$JUN
    typeset -l JUL_LC=$JUL
    typeset -l AUG_LC=$AUG
    typeset -l SEP_LC=$SEP
    typeset -l OCT_LC=$OCT
    typeset -l NOV_LC=$NOV
    typeset -l DEC_LC=$DEC
    typeset SUNDAY= MONDAY= TUESDAY= WEDNESDAY= THURSDAY= FRIDAY= SATURDAY=
    typeset DAYS=$(/usr/bin/dspmsg -s 36 cluster.cat 16 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday')
    print -- "$DAYS" | IFS=, read SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY REM
    typeset -l SUNDAY_LC=$SUNDAY
    typeset -l MONDAY_LC=$MONDAY
    typeset -l TUESDAY_LC=$TUESDAY
    typeset -l WEDNESDAY_LC=$WEDNESDAY
    typeset -l THURSDAY_LC=$THURSDAY
    typeset -l FRIDAY_LC=$FRIDAY
    typeset -l SATURDAY_LC=$SATURDAY
    typeset -i month=-1
    if [[ -n $monthStr ]]; then
        if [[ $monthStr == +([[:digit:]]) ]]; then
            if (( monthStr >= 1 && monthStr <= 12 )); then
                (( month = $monthStr - 1 ))
            else
                CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\"\n\n" MONTH "$monthStr" 1>&2
                CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12" 1>&2
                rc=$RC_INCORRECT_INPUT
            fi
        else
            typeset -l monthStr_lc=$monthStr
            case $monthStr_lc in
                $JAN_LC) month=0  ;;
                $FEB_LC) month=1  ;;
                $MAR_LC) month=2  ;;
                $APR_LC) month=3  ;;
                $MAY_LC) month=4  ;;
                $JUN_LC) month=5  ;;
                $JUL_LC) month=6  ;;
                $AUG_LC) month=7  ;;
                $SEP_LC) month=8  ;;
                $OCT_LC) month=9  ;;
                $NOV_LC) month=10 ;;
                $DEC_LC) month=11 ;;
                *) CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\"\n\n" MONTH "$monthStr" 1>&2
                   CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "$JAN, $FEB, $MAR, $APR, $MAY, $JUN, $JUL, $AUG, $SEP, $OCT, $NOV, $DEC" 1>&2
                   rc=$RC_INCORRECT_INPUT
                ;;
            esac
        fi
    fi
    typeset -i day_of_week=-1
    if [[ -n $day_of_weekStr ]]; then
        if [[ $day_of_weekStr == +([[:digit:]]) ]]; then
            if (( day_of_weekStr >= 0 && day_of_weekStr <= 6 )); then
                day_of_week=$day_of_weekStr
            else
                CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\"\n\n" DAY_OF_WEEK "$day_of_weekStr" 1>&2
                CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "0, 1, 2, 3, 4, 5, 6" 1>&2
                rc=$RC_INCORRECT_INPUT
            fi
        else
            typeset -l dow_lc=$day_of_weekStr
            case $dow_lc in
                $SUNDAY_LC)    day_of_week=0 ;;
                $MONDAY_LC)    day_of_week=1 ;;
                $TUESDAY_LC)   day_of_week=2 ;;
                $WEDNESDAY_LC) day_of_week=3 ;;
                $THURSDAY_LC)  day_of_week=4 ;;
                $FRIDAY_LC)    day_of_week=5 ;;
                $SATURDAY_LC)  day_of_week=6 ;;
                *) CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\"\n\n" DAY_OF_WEEK "$day_of_weekStr" 1>&2
                   CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "$SUNDAY, $MONDAY, $TUESDAY, $WEDNESDAY, $THURSDAY, $FRIDAY, $SATURDAY" 1>&2
                   rc=$RC_INCORRECT_INPUT
                ;;
            esac
        fi
    fi

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

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

    elif [[ -n $timer && -n "${timer//[a-zA-Z0-9_]/}" ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 105 "\nERROR: one or more invalid characters were detected in \"%1\$s\" (\"%2\$s\").\n\nValid characters include letters, numbers, and underscores only.\n\n" "$timer" "${timer//[a-zA-Z0-9_]/}" 1>&2
        rc=$RC_INCORRECT_INPUT

    elif (( ${#timer} > $MAX_NAME_LENGTH )); then
        dspmsg -s $CLMGR_SET $CLMGR_MSGS 1465 '\nERROR: Fallback Timer name %1$s should not be more than %2$d characters.\n' "$timer" "$MAX_NAME_LENGTH" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -z $hour ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 101 "\nERROR: this operation requires the \"%1\$s\" attribute.\n\n" HOUR 1>&2
        rc=$RC_MISSING_INPUT

    elif [[ $hour != +([[:digit:]]) ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n\n" HOUR "$hour" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -z $minute ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 101 "\nERROR: this operation requires the \"%1\$s\" attribute.\n\n" MINUTE 1>&2
        rc=$RC_MISSING_INPUT

    elif [[ $minute != +([[:digit:]]) ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n\n" MINUTE "$minute" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $year && $year != +([[:digit:]]) ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n\n" YEAR "$year" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $year ]] && [[ -z $monthStr || -z $day_of_month ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 180 "\nERROR: \"%1\$s\" requires the following option(s): %2\$s.\n\n" YEAR "MONTH, DAY_OF_MONTH" 1>&2
        rc=$RC_MISSING_INPUT
    fi

    if (( month >= 0 )) && [[ -z $day_of_month ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 180 "\nERROR: \"%1\$s\" requires the following option(s): %2\$s.\n\n" MONTH DAY_OF_MONTH 1>&2
        rc=$RC_MISSING_INPUT
    fi

    if [[ -n $day_of_weekStr ]] && (( day_of_week < 0 )); then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n\n" DAY_OF_WEEK "$day_of_weekStr" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    #==================================================================
    : Create the fallback timer if no input errors have been detected
    #==================================================================
    if (( $rc == RC_UNKNOWN )); then
        if [[ -n $year ]]; then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 224 "Creating a fallback timer that executes only once...\n"
            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/cltimerpolicy -a add -r once -n $timer -y $year -m $month -d $day_of_month -h $hour -M $minute" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/cltimerpolicy -a add \
                                   -r once \
                                   -n $timer \
                                   -y $year \
                                   -m $month \
                                   -d $day_of_month \
                                   -h $hour \
                                   -M $minute
        elif (( month >= 0 )); then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 225 "Creating a fallback timer that executes yearly...\n"
            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/cltimerpolicy -a add -r yearly -n $timer -m $month -d $day_of_month -h $hour -M $minute" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/cltimerpolicy -a add \
                                   -r yearly \
                                   -n $timer \
                                   -m $month \
                                   -d $day_of_month \
                                   -h $hour \
                                   -M $minute
        elif [[ -n $day_of_month ]]; then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 226 "Creating a fallback timer that executes monthly...\n"
            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/cltimerpolicy -a add -r monthly -n $timer -d $day_of_month -h $hour -M $minute" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/cltimerpolicy -a add \
                                   -r monthly \
                                   -n $timer \
                                   -d $day_of_month \
                                   -h $hour \
                                   -M $minute
        elif (( day_of_week  >= 0 )); then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 227 "Creating a fallback timer that executes weekly...\n"
            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/cltimerpolicy -a add -r weekly -n $timer -w $day_of_week -h $hour -M $minute" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/cltimerpolicy -a add \
                                   -r weekly \
                                   -n $timer \
                                   -w $day_of_week \
                                   -h $hour \
                                   -M $minute
        else
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 228 "Creating a fallback timer that executes daily...\n"
            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/cltimerpolicy -a add -r daily -n $timer -h $hour -M $minute" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/cltimerpolicy -a add \
                                   -r daily \
                                   -n $timer \
                                   -h $hour \
                                   -M $minute
        fi
        rc=$?
        print "cltimerpolicy RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        if (( $rc != RC_SUCCESS )); then
            rc=$RC_ERROR
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 201 "\nERROR: failed to create \"%1\$s\".\n\n" "$timer" 1>&2

        #===========================================================
        : If output from this operation was requested, retrieve it
        #===========================================================
        else
            if (( CLMGR_VERBOSE )) || [[ -n $CLMGR_ATTRS ]]; then
                CL=$LINENO KLIB_HACMP_get_fallback_timer_attributes "$timer" 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 fallback_timer -h" "FALLBACK TIMER:" "$CLMGR_PROGNAME" 1>&2
    fi

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