#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/db2/KLIB_DB2_print_message.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2011 
# 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 
# @(#)93	1.3 src/43haes/lib/ksh93/db2/KLIB_DB2_print_message.sh, hacmp, 61haes_r714 11/28/11 14:55:00
#
#=head1 NAME
#
# KLIB_DB2_print_message - print a message to the console and to db2sa.log
#
#=head1 SYNOPSIS
#
# KLIB_DB2_print_message SetNumber MessageNumber "Message" [Arguments]
#
#=head1 DESCRIPTION
#
# Print a message to the console and to db2sa.log
#
# Global Variables:
#       DB2SACAT (required)
#       DB2SALOG (required if DEBUG=1)
#       VERBOSE
#       DEBUG
#       VERBOSE_LOGGING
#
#=head1 ARGUMENTS
#
#       1: [scalar] SetNumber: dspmsg SetNumber (-s)
#       2: [scalar] MessageNumber: dspmsg MessageNumber
#       3: [scalar] Message: The message to be printed/logged.
#       4+ [scalar] Arguments: dspmsg Arguments
#
#
#=head1 RETURN
#
#       0: on SUCCESS
#       1: on FAILURE
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_print_message
{
    set +u
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
    set -u

    # Checking for required environments.
    env | egrep DB2SACAT > /dev/null 2>&1 || return 1
    if [[ $DEBUG -eq 1 ]]; then
        env | egrep DB2SALOG > /dev/null 2>&1 || return 1
    fi

    [[ $# -lt 3 ]] && return 1

    # See if we have any Arguments passed to dspmsg
    ARGS=0
    [[ $# -gt 3 ]] && ARGS=1

    SET_NUMBER=$1
    MESSAGE_NUMBER=$2
    MESSAGE="$3"
    shift; shift; shift	# Skip to Arguments

    if [[ $DEBUG -eq 1 ]]; then
        DATE=`date '+m%d12/15/09M%S'`
        print -n "$DATE: DEBUG: " | tee -a $DB2SALOG
        if [[ $ARGS -eq 1 ]]; then
            dspmsg -s $SET_NUMBER $DB2SACAT $MESSAGE_NUMBER "$MESSAGE" $@ | tee -a $DB2SALOG
        else
            dspmsg -s $SET_NUMBER $DB2SACAT $MESSAGE_NUMBER "$MESSAGE" | tee -a $DB2SALOG
        fi
        [[ $? -ne 0 ]] && print "WARNING: Failed logging to $DB2SALOG!  Filesystem full?"
    elif [[ $VERBOSE -eq 1 ]]; then
        if [[ $ARGS -eq 1 ]]; then
            dspmsg -s $SET_NUMBER $DB2SACAT $MESSAGE_NUMBER "$MESSAGE" $@
        else
            dspmsg -s $SET_NUMBER $DB2SACAT $MESSAGE_NUMBER "$MESSAGE"
        fi
    else
        :
    fi

    return 0
}
