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

#Fix for Defect 181688
unalias -a

basecmd="hagsvote"
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_hagsvote_Usage $basecmd $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
                ;;
        * )
                #${basecmd}: Not a recognized flag: -${OPTARG}
                $SPMSG hagsctrl ha_gs.cat EMsg_Incorrect_Flag ${basecmd} ${OPTARG}
                $SPMSG hagsctrl ha_gs.cat IMsg_hagsvote_Usage $basecmd $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                                        

if [[ ! -z $c_opt ]]; then
	# canonical form
	nextcmd=`${basepath}/${basecmd}_c $args`
else
	# locale dependent
        nextcmd=`${basepath}/${basecmd}_nls $args`
fi

# 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 the error the SRC encounterred
            exec ${basepath}/${basecmd}_c $args                       
        fi                                                               
fi
exit $rc