#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/lib/libcspoc/nls_msg.sh 1.7.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1996,2016 
# 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/libcspoc/nls_msg.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
#   COMPONENT_NAME: CSPOC
#
#   FUNCTIONS:
#
#   ORIGINS:
#
#
################################################################################
#
# Name:
#       nls_msg
#
# Description:
#	Retrieves a message from a message catalog and prints it to the
#	users tty on either stdout or stderr.  Will also print the message
#       to a logfile if one is specified.
#
# Usage:
#   nls_msg [-0 | -1 | -2] [-l logfile] [-c catalog] [-p prefix] \
#		<msg_set> <msg_id> <def_msg> <args>
#
#	-0		Supress printing of messages to users tty.
#	-1		Print to users tty on stdout (default).
#	-2		Print to users tty on stderr.
#	-l logfile	Print message to the logfile specfied.
#	-c catalog	Obtain messages from the message catalog specfied.
#	-p prefix	Use prefix when printing logfile entries. See below.
#	<msg_set>	Message Set 
#	<msg_id>	Message ID
#	<def_msg>	Default Message String 
#	<args>		Positional parameters corresponding to each '%s' in
#			the message.
#
# Logfile entry format:
#
#	MM/DD/YY HH:MM:SS <prefix><message>
#
#  Environment Variables:
#    NLSPATH	Sets path to NLS message catalogs.
#    LANG	Sets language for NLS message catalogs.
#
#    _MSG_CAT	Sets message catalog name in execution plan.
#    _DEBUG	Sets debug level (1-9).
#
# Return Values:
#       0 - success    
#       2 - bad number of arguments
#
# Notes:
#
#   Message catalogs can be specified in one of three ways which
#   take precedence in the following order:
#	1 - On the command line with the option '-c <msg_cat>'
#	2 - In an execution plan by setting $_MSG_CAT
#	3 - Use the default message catalog hard coded below as: $MSG_CAT
#
#   This script requires that newlines be imbedded into all lines of a
#   message in the message catalog as well as in any default strings.
#   Any lines not ended with a newline will not appear in the logfile
#   and will be printed to the users tty without a newline.
#   The reason the lines will not appear in the logfile is that they
#   are post-processed with sed which requires the newline to flush
#   its buffers.
#
#
################################################################################

function nls_msg
{

  [[ -n $_DEBUG ]] && {
    print "Entering nls_msg version $Source: 61haes_r711 43haes/lib/libcspoc/nls_msg.sh"
    (( $_DEBUG >= 8 )) && {
	typeset PROGNAME="nls_msg"
	set -x
	version='1.7.1.2'
    }
  }

  #
  : local variables
  #
  typeset PREFIX LOGFILE MSG_SET MSG_ID
  typeset MSG_CAT=${_MSG_CAT:-cspoc.cat}

  #
  : parse option flags
  #
  while getopts ":c:l:p:210" option 
  do
    case $option in
	c ) :	Set name of message catalog ;	MSG_CAT=$OPTARG 
	   ;;
	l ) :	Write message to log file   ;	LOGFILE=$OPTARG
	   ;;
	p ) :  Set prefix used after date/time stamp in logfile	    ;	PREFIX=$OPTARG
	   ;;
	2 ) :  Write message to stderr	    ;	exec >&2
	   ;;
	1 ) :  Write message to stdout	    ;	exec 2>&1
	   ;;
	0 ) :   Suppress output		    ;	exec > /dev/null 2>&1
	   ;;
    esac
  done
  shift $(($OPTIND - 1))

  #
  : check usage - must have message set, message ID and default message
  #
  (( $# < 3 )) && {
    dspmsg $MSG_CAT -s 30 10 "USAGE: nls_msg [-0 | -1 | -2] [-l logfile] <msg_set> <msg_id> <def_msg> <args>\n" >&2
    return 2
  }

  PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)

  #
  : Pick up the message identifiers, leaving the message text 
  #
  MSG_SET=$1
  MSG_ID=$2
  shift 2

  #
  : Write the message to the users TTY
  #
  if [[ -n $_DEBUG ]] && (( $_DEBUG < 8 && $_DEBUG > 0 ))
  then
      print dspmsg -s $MSG_SET $MSG_CAT $MSG_ID "$@"
  fi
  #
  : Get the message
  #
  LOG_MSG=$(dspmsg -s $MSG_SET $MSG_CAT $MSG_ID "$@" )
  print "$LOG_MSG"

  #
  : Write the message to the logfile
  #
  if [[ -n $LOGFILE ]]
  then
    if [[ -n $_CMD && $LOG_MSG != @($_CMD:*) ]]
    then
	#
	:   If the message does not begin with the name of 
	:   the current command, tag the message with the 
	:   command name
	#
	LOG_MSG="$_CMD: $LOG_MSG"
    fi
    if [[ -n $PREFIX && $PREFIX != $_CMD: ]]
    then
	#
	:   If there is a prefix other than the command
	:   name, prepend it to the message
	#
	LOG_MSG="$PREFIX: $LOG_MSG"
    fi
    #
    :	Log the message in the form used for other CSPOC messages
    #
    LOCALNODENAME=${LOCALNODENAME:-$LOCAL_NODE}
    LOCALNODENAME=${LOCALNODENAME:-$(get_local_nodename 2> /dev/null)}
    print $LOG_MSG | sed -e "s?^?$(LC_ALL=C date '+%Y-%m-%dT%H:%M:%S')|${LOCALNODENAME}: ?" >> ${LOGFILE}
  fi

  return 0
}

if [[ -n $@ ]]
then
    nls_msg "$@"
fi
