#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2006,2007 
# 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
#
# @(#)29   1.4         src/rsct/pgs/cmds/hagsgr.sh, gsctrl, rsct_relgh, relghs001a 2/27/06 22:13:33
#*======================================================================
#* Module Name:  hagsgr
#*======================================================================

basecmd="hagsgr"
basepath="/usr/sbin/rsct/bin"

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

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

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

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

while getopts ":h:g:s:p:a:lck" opt; do
   case $opt in
	h )
                 h_opt=$OPTARG
 		 args="$args -${opt} $OPTARG"
                 ;;
        p | a )
                args="$args -${opt} $OPTARG"
                ;;
        g )
                opt=s
                args="$args -${opt} $OPTARG"
                subsys=$OPTARG
                ;;
	s )
                args="$args -${opt} $OPTARG"
                subsys=$OPTARG
                ;;
	l )
		args="$args -l"
		;;
        c )
                c_opt=1
                ;;
        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_hagsgr_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                                            
            # this will print out 0513-085 The $basecmd Subsystem is not on file.
            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                                                    
    # this will print out 0513-085 The $basecmd Subsystem is not on file.
    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 out whatever error SRC encountered
            exec ${basepath}/${basecmd}_c $args
        fi                                                                     
fi
exit $rc

