#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2006,2019 
# 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 
#
#* CPRY
#
# sccsid = "@(#)28   1.7   src/rsct/pgs/cmds/hagsmg.sh, gsctrl, rsct_rady, rady2035a 11/12/15 16:45:07"
#*======================================================================
#* Module Name:  hagsmg
#*======================================================================

#Fix for Defect 181688
unalias -a

basecmd="hagsmg"
basepath="/opt/rsct/bin"

RSCTBIN=/opt/rsct/bin
SUBSYS=cthags
set -A clinfo $(${RSCTBIN}/ct_clusterinfo -c -n)        
CLUSTER_NAME=${clinfo[0]}       # cluster-name          

SPMSG=${basepath}/hadspmsg
MSGMAPPATH=/opt/rsct/msgmaps
export MSGMAPPATH

# process the command lines                                                     
if [[ -z $1 ]]; then                                                            
   # no arguments                                                               
   $SPMSG hagsctrl ha_gs.cat IMsg_hagsmg_Usage $basecmd $basecmd $basecmd 
                                                                           
   exit                                                                         
fi                                                                              

args=""
c_opt=""
h_opt=""

# for a,l,c option coded here they are for future extention
# but it won't affect output, just put the codes here now for future use.

while getopts ":h:g:s:p:k" opt; do
   case $opt in
	h )
                h_opt=$OPTARG
	        args="$args -${opt} $OPTARG"
	        ;;
        p )
                args="$args -${opt} $OPTARG"
                ;;
        g )
                opt=s
                args="$args -${opt} $OPTARG"
                subsys=$OPTARG
                ;;
	s )
                args="$args -${opt} $OPTARG"
                subsys=$OPTARG
                ;;
        k )     savefile=1
                ;;
        * )
                #echo ${basecmd}: Not a recognized flag: -${OPTARG}
                $SPMSG hagsctrl ha_gs.cat EMsg_Incorrect_Flag ${basecmd} ${OPTARG}
                $SPMSG hagsctrl ha_gs.cat IMsg_hagsmg_Usage $basecmd $basecmd $basecmd
                exit 1
                ;;
    esac
done

shift $(($OPTIND - 1))

if [[ $subsys = "cthags" ]]
then
    domain=CLUSTER
elif [[ $subsys = "hags" ]]
then
    domain=PSSP
elif [[ $subsys = "grpsvcs" ]]
then
    domain=HACMP
fi

# canonical form
nextcmd=`${basepath}/${basecmd}_c $args`

# execute the srccmd & get the output (which is the filename)

if [[ $domain = "CLUSTER" ]]
then
    if [[ ! -z $CLUSTER_NAME ]]
    then
        if [[ $CLUSTER_NAME = "IW" ]]
        then
            exec ${basepath}/${basecmd}_c $args
            exit
        else
            logdir=/var/ct/${CLUSTER_NAME}/log/${SUBSYS}
        fi
    fi
elif [[ $domain = "PSSP" || $domain = "HACMP" ]]
then
    if [[ -e /var/ha/log ]]
    then
        logdir=/var/ha/log
    fi
else
    exec ${basepath}/${basecmd}_c $args
    exit
fi

outputfile="$logdir/$basecmd.out"

rc=$?
if [[ ! -z $h_opt ]]
then
    # copy remote file
#    rcp $h_opt:$outputfile /tmp/$basecmd.out
#    cat /tmp/$basecmd.out
#    rm -f  /tmp/$basecmd.out
    rsh $h_opt cat $outputfile
    exit 0
fi

if [[ $rc -eq 0 && ! -z $outputfile ]]; then
	# successful....and then copy the output to stdout
        if [[ -e $outputfile ]]
        then
	    cat $outputfile
            if [[ -z $savefile ]]
            then
	        rm -f $outputfile		# don't save the output 
            fi
        else
            # This will print whatever SRC error it encounterred
            exec ${basepath}/${basecmd}_c $args
        fi
fi
exit $rc