#!/bin/ksh

function USAGE
{

echo "\n
Usage: $(basename $0) [-h] [-f] "filename" [-g] "Device Group" [-m] "list of DGs"
  This script processes a device group and performs pre checks for COB tests. Input can be either a file with 
  list of device groups in individual lines or a single device group
Options:
        -f filename
                where filename is name or path to input file from symdg list -v output
	-g Device Group Name
	-m Device group names seperated by a space
        -h
                Help \n"

        return 0
}



function TEST_DG
{
#set -x
BCV_DG=$1
DG_DATA="/gsmsan04/cob_scripts/output_logs/final_device_group_list_allframes_cob_042109"

D_COUNT=`grep $BCV_DG $DG_DATA | cut -d : -f 1 | wc -l | awk '{print $1}'`
PSERVER=`grep $BCV_DG $DG_DATA | cut -d : -f 1 | sort -u` 
PHOST=`grep $BCV_DG $DG_DATA | cut -d : -f 2 | sort -u`
PFRAME=`grep $BCV_DG $DG_DATA | cut -d : -f 3 | sort -u`
PDG=`grep $BCV_DG $DG_DATA | cut -d : -f 4 | sort -u`
PTYPE=`grep $BCV_DG $DG_DATA | cut -d : -f 6 | sort -u`
PRDFG=`grep $BCV_DG $DG_DATA | cut -d : -f 8 | sort -u`

RSERVER=`grep $BCV_DG $DG_DATA | cut -d : -f 9 | sort -u` 
RHOST=`grep $BCV_DG $DG_DATA | cut -d : -f 10 | sort -u`
RFRAME=`grep $BCV_DG $DG_DATA | cut -d : -f 11 | sort -u`
RDG=`grep $BCV_DG $DG_DATA | cut -d : -f 12 | sort -u`
RTYPE=`grep $BCV_DG $DG_DATA | cut -d : -f 14 | sort -u`
RRDFG=`grep $BCV_DG $DG_DATA | cut -d : -f 16 | sort -u`

echo "Checking Prod Group $PDG for production server $PHOST on $PFRAME using $PSERVER"
pbrun -h $PSERVER /usr/symcli/bin/symrdf -g $PDG query

echo " Hit any key to continue ----------------------"
read c

pbrun -h $PSERVER /usr/symcli/bin/symdg show $PDG 


echo " Hit any key to continue ----------------------"
read c

# Check R2 and BCV
echo "Checking Remote Group $RDG for COB server $RHOST on $RFRAME using $RSERVER"
pbrun -h $RSERVER /usr/symcli/bin/symrdf -g $RDG query

echo " Hit any key to continue ----------------------"
read c

pbrun -h $RSERVER /usr/symcli/bin/symdg show $RDG 

echo " Hit any key to continue ----------------------"
read c
echo "Checking if RBCV is synchronised ..........."
pbrun -h $RSERVER /usr/symcli/bin/symmir -g $RDG query


echo " Hit any key to continue ----------------------"
read c

}

function MAIN
{
#set -x
        while getopts hf:g:m: opt
        do
                case $opt in
                h)
                        USAGE
                        exit 0
                        ;;
                f)
                        INFILE=${OPTARG}
			for BCV_DG in `cat $INFILE`
			do
			TEST_DG $BCV_DG
			done
                        ;;
                g)
                        DGNAME=${OPTARG}
			TEST_DG $DGNAME
                        ;;
                m)
                        DGLIST=${OPTARG}
			for BCV_DG in $DGLIST
			do
			TEST_DG $BCV_DG
			done
                        ;;
                ?)
                        USAGE
                        exit 2
                        ;;
                esac
        done
        if [[ "$INFILE" == "" && "$DGNAME" == "" && "$DGLIST" == "" ]]
        then
                USAGE
                exit 2
        fi
        return 0

}
## MAIN Routine

MAIN $*
