#!/bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by COMPUTER
#* ASSOCIATES INTERNATIONAL, INC.  The program, including this material,
#* may not be duplicated, disclosed or reproduced in whole or in part for
#* any purpose without the express written authorization of COMPUTER
#* ASSOCIATES. All authorized reproductions must be marked with this
#* legend.
#*
#*                          Copyright (c) 1992-2005
#*             Computer Associates International, Incorporated
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#*  SCCS Module Control Stamp
#* ----------------------------------------------------------------------
#
#* "@(#)displayMsg	1.3  10/06/05 09:44:25"
#* "Copyright (C) 1992-2005 Computer Associates International, Inc.  All rights reserved."
#
#
# Name
#   displayMsg
#
# Description
#   Provides the displayMsg function only (see function header comment for
#   details.
#
#   NB: this script sources in the lcl_echo_func script.
#
#

###############################################################################
#                                                                             #
#                               V A R I A B L E S                             #
#                                                                             #
###############################################################################

DISPLAYMSG_GOT_LCL_ECHO=0

###############################################################################
#                                                                             #
#                               F U N C T I O N S                             #
#                                                                             #
###############################################################################

#
# Name
#   displayMsg
#
# Description
#   Displays a configured message on standard output. The message is
#   identifed by a file and id string.
#
# Arguments
#   $1                      Message id.
#   $2                      Message file name. Defaults to ccsinstall if not
#                           provided.
#
# Environment Variables
#   CASHCOMP                IN      Used to locate lcl_echo_func.
#   ECHO                    IN      Echo command to use to output the message.
#   DISPLAYMSG_*            OUT     Internally used variables and defines.
#
# Returns
#   Nothing.
#

displayMsg()
{
    DISPLAYMSG_ID="$1"
    DISPLAYMSG_FILE="$2"
    DISPLAYMSG_MSG=""
    
#
#   Basic argument check.
#

    if [ $# -lt 1 ]
    then
        $ECHO "displayMsg: Message id must be specified as a minimum."
        exit 22                         # EINVAL.
    fi

#
#   Source in the lcl_echo_func script if not already done.
#

    if [ $DISPLAYMSG_GOT_LCL_ECHO -eq 0 ]
    then

#
#       Try PIF pre-install location first. If not there try installed location.
#

        DISPLAYMSG_SRCLCL_ECHO_PATH="${PIF_PREINSTALL_DIR}/ca-cs-utils-cmp.d/\$CASHCOMP/lcl_echo_func"
        if [ -z "$PIF_PREINSTALL_DIR" -o ! -f "$DISPLAYMSG_SRCLCL_ECHO_PATH" ]
        then
            DISPLAYMSG_SRCLCL_ECHO_PATH="$CASHCOMP/csutils/scripts/lcl_echo_func"
        fi

        if [ -z "$PIF_PREINSTALL_DIR" ];then
             if [ -z "${CASHCOMP}" -o ! -d "${CASHCOMP}" ]
             then
                  $ECHO "displayMsg: CASHCOMP not set or not a directory."
             fi
        fi

        if [ ! -r "${DISPLAYMSG_SRCLCL_ECHO_PATH}" ]
        then
            $ECHO "displayMsg: ${DISPLAYMSG_SRCLCL_ECHO_PATH} not readable."
        else
            . "${DISPLAYMSG_SRCLCL_ECHO_PATH}"

#
#           Set the all important flag to say it's been done.
#
            
            DISPLAYMSG_GOT_LCL_ECHO=1
        fi  
    fi

#
#   If unable to source in lcl_echo_func then unable to display the message.
#

    if [ $DISPLAYMSG_GOT_LCL_ECHO -eq 1 ]
    then

#
#       Determine the message file to use.
#

        if [ -z "$DISPLAYMSG_FILE" ]
        then
            DISPLAYMSG_FILE="ccsinstall"
        fi

#
#       Get the basic message for output, escaping any double-quotes.
#

        DISPLAYMSG_MSG=`lcl_echo "$DISPLAYMSG_ID" "$DISPLAYMSG_FILE" | sed 's/"/\\\"/g'`

        if [ -z "$DISPLAYMSG_MSG" ]
        then
            $ECHO "displayMsg: lcl_echo returned nothing for message id '$DISPLAYMSG_ID' and message file '$DISPLAYMSG_FILE'."
        else

#
#           Expand any environment variables.
#

            eval DISPLAYMSG_MSG=\""$DISPLAYMSG_MSG"\"

#
#           Output the message using the specified echo command.
#

            $ECHO "$DISPLAYMSG_MSG"
        fi
    fi
}

