#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/diag/cld_error.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,1999 
# 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 
# @(#)68  1.6  src/43haes/usr/sbin/cluster/diag/cld_error.sh, hacmp.diags, 61haes_r714 1/21/99 21:21:55

####################################################################
#   COMPONENT_NAME: DIAG
#
#   FUNCTIONS: usage_message
# The "cld_error" script will provide the interface to execute the
# different error reports: short, long, cluster.
#
# The following is the syntax to request an error report:
#       short [-h node] [-R file]
#       long [-h node] [-R file]
#       cluster [-h node] [-R file]
# All the arguments inside brackets are optional. This
# flags, as well as the type of error report, are
# then passed to "cld_error" as positional arguments.
###################################################################


###############################################################
#               Function USAGE_MESSAGE
#  This function displays an error message every time a
#  usage error occurs.
###############################################################

usage_message () {

    dspmsg scripts.cat 553 "Usage: error type [-h host] [-R file]\n" >&2
    dspmsg scripts.cat 554 "Where type is:\n" >&2
    dspmsg scripts.cat 555 "          short   - short error report\n" >&2
    dspmsg scripts.cat 556 "          long    - long error report\n" >&2
    dspmsg scripts.cat 557 "          cluster - clstrmgr and hacmp/6000 error report\n" >&2
    exit 1
}

################################################################
#                    MAIN ROUTINE
#  The main routine will take as arguments the type of report
#  (short, long, and cluster), the node name for which to
#  request the report, and the file name in which to save the
#  report.
#  Then according to the parameters passed, it generates
#  the report.
################################################################

[ "$VERBOSE_LOGGING" = "high" ] && set -x
PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../utilities/cl_get_path all)"
HA_DIR="$(cl_get_path)"

NUM_ARGS=$#                # gets the number of arguments passed

# first it checks for the right number of parameters passed.

if [ $NUM_ARGS -lt 1 ]; then usage_message;
elif [ $NUM_ARGS -gt 5 ]; then usage_message;
fi

case $1 in                      # variable TYPE gets the type of report:
                                #       short, long, or cluster.
        short ) TYPE="" ;;
        long ) TYPE="-a" ;;
        cluster ) TYPE="-N clstrmgr,hacmp/6000" ;;
        * ) usage_message ;;
esac

shift       # moves the first flag to position $1


set -- `getopt h:R: $*` 

if [ $? -ne 0 ]			# checks if the flags are valid.
then
	usage_message	
	exit 1
fi

HOSTNAME=""
FILENAME=""
RSH=""

while [ $1 != -- ]
do
	case $1 in		     # This case statement gets the
				     # values for RSH and FILENAME
				     # if these are entered.
		-h ) RSH="/usr/${HA_DIR}/sbin/cluster/utilities/cl_rsh $2" 
		     shift 2 ;;

		-R ) FILENAME=$2
		     shift 2 ;;

	esac
done

shift     # loose --


if [ $@ ]		# After parsing through the flags, it
			# checks if there are any left over  
then			# parameters.
	usage_message
fi



if [ -z "$FILENAME" ]		# generates the reports according to
				# the parameters passed.
then
	# Copy the file locally to prevent buffering problems
        $RSH errpt $TYPE 2>&1 > /tmp/cld_err
        cat /tmp/cld_err | more
else
	$RSH errpt $TYPE 2>&1 > $FILENAME
fi