#!/usr/bin/ksh
#####################################################################################################
# This script does the R2--->RBCV establish for SRDF/A DG with the input file name/ or DGname as an argument
#####################################################################################################
#set -x
DIR=/gsmsan04/cob_scripts/input_file
LOG=/gsmsan04/cob_scripts/output_logs
DG_list=/gsmsan04/cob_scripts/final_master_dg_list
TMP1=/tmp/tmp1.$$

Usage()
{
#set -x
#tput clear
[[ -n "$1" ]] && print "\n\n$(basename $0): $@ \n "
print "\n\n $(basename $0) -f <file_name > -g <devicegroup_name>   \n"
print " arguments: "
print "            -f <file_name>           : Indicates  the input file name with Device Group list located in $DIR \n "
print "            -g <devicegroup_name>    : Indicates  the Device_Group name \n "
print "  if using -f  <filename> type -g null and if using -g <group> type -f null   \n "
print " \n\n  sample usage is $(basename $0) -f file_sp65064 -g null       OR \n "
print " \n  sample usage is $(basename $0) -f null  -g dg_test_SRDF        \n "
print "\t The Output file is  ${LOG}/dg_state_<host>_<filename or dgname>_<userid>_<mmddyy> \n"
print "\t This script does the R2---->RBCV establish for SRDF/A DG with the Device Group information  in the input file or taking device group as an a
rg
ument \n"
exit 1
}

Arg()
{
#set -x
        [[ $# = 0 ]] && Usage
        while getopts :f:g: OPT; do
                case $OPT in
                         f) file=$OPTARG ;;
                         g) group=$OPTARG ;;
                        \?) Usage $@ ;;
                esac
        done
shift OPTIND-1

tag=`id |awk -F'(' '{print $2}' |awk -F')' '{print $1}'`
terminal=`tty`
if [[ "$file" = "null" ]] ;then
     if [[ "$group" != "null" ]];then
         Group
      else
         Usage "Incorrect option, either group or file name should be specfied but not both type one field as null "
    fi
 else
     if [[ "$group" != "null" ]];then
         Usage "Incorrect option, either group or file name should be specfied but not both type one field as null "
     fi
       File
fi


}

Email(){
#set -x
cat $OUT|mailx -s " COB activity for $new_var as of `date +"%m%d%y_%H%M"` " $tag
}

Group()
{
#set -x
  print "\n\nUsing the group option\n\n"
  pbrun symdg show $group > /dev/null
    if [[ $? -ne 0 ]];then
        Usage "Error: The DG \" $group \" doesn't exists on `hostname` : Please check  $DG_list"
     fi
    OUT=${LOG}/dg_state_`hostname`_${group}_${tag}_`date +"%m%d%y"`.out
 print "\n The output is in file \" $OUT \" \n"

Check_rdf

 exec  >>$OUT 2>&1
Main_DG

}

File()
{
#set -x
     print "\n\nUsing the file option\n\n"
     ls ${DIR}/${file}
       if ( [[ $? -eq  0 ]] && [[ -s ${DIR}/${file} ]] ); then
            INPUT=${DIR}/${file}
            OUT=${LOG}/dg_state_`hostname`_${file}_${tag}_`date +"%m%d%y"`.out
           print "\n The output is in file \" $OUT \" \n"
        else
           Usage "Incorrect DG filename mentioned, $DIR/$file doesn't exists or has zero file size \n"
      fi

 exec  >>$OUT 2>&1
Main_file


}

Date()
{
#set -x
print "\n*******************************************************************************************"
print " `hostname`: $1  : `date +"%D,%T"` "
print "*******************************************************************************************\n"
}

Check_rdf()
{
#set -x
pbrun symdg show  $group >$TMP1
dos2unix $TMP1 $TMP1 >/dev/null 2>&1

rdf_type=`cat $TMP1 |egrep "RDF Type" |awk -F':' '{print $2}' |sed 's/^ //g'`
if [[ "$rdf_type" = "R2" ]]; then
  sw_rdf=""
else
  sw_rdf="-rdf"
fi
#print ":$sw_rdf:"
}


DG_rdfa_verify()
{
#set -x
Date " The Current state of R1-->R2 for the $group Before R2--RBCV establish as of "
pbrun symrdf -g $group verify -consistent -i 15
pbrun symrdf -g $group  query -rdfa
}



DG_bcv_preest()
{
Date " The Current state of R2-->BCV for the $group Before the establish as of "
pbrun symmir -g $group $sw_rdf  verify -split -i 5
pbrun symmir -g $group $sw_rdf query  -bg -percent
}


DG_bcv_postest()
{
Date " The Current state of R2-->BCV for the $group After the establish as of"
pbrun symmir -g $group $sw_rdf verify -synched -i 5
pbrun symmir -g $group $sw_rdf query  -bg -percent
 if [[ $? -eq 0 ]] ;then
     Date " The R2---->BCV establish for the $group finished in the background"
 else
     Date " Error: The R2---->BCV establish for the $group failed to finish in the background"
 fi
}


DG_bcv_est()
{
Date " Starting the establish of R2-->BCV for the $group  "
pbrun symmir -g $group  $sw_rdf establish -i 5 -noprompt
 if [[ $? -eq 0 ]] ;then
     Date " The R2---->BCV establish for the $group was initiated successfully"
 else
     Date " Error: The R2---->BCV establish for the $group failed to initiate"
 fi
}


Main_DG()
{
#set -x
  pbrun symdg show $group >/dev/null
    if [[ $? -eq 0 ]];then
       Date "\n Command execution for DG=$group" >$terminal
       DG_rdfa_verify
       DG_bcv_preest
       DG_bcv_est
       DG_bcv_postest
  else
        print "\n\nError: The DG \" $group \" doesn't exists on `hostname` : Please check  $DG_list"
        exit 1
     fi
dos2unix $OUT $OUT >/dev/null 2>&1

# print "\n The output is in file \" $OUT \" \n"
new_var=`print "DG: $group"`
Email
}

Main_file()
{
#set -x
 for group in `cat $INPUT`
do
  pbrun symdg show $group > /dev/null
    if [[ $? -eq 0 ]];then
       Date "\n Command execution for DG=$group" >$terminal
       Check_rdf
       DG_rdfa_verify
       DG_bcv_preest
       DG_bcv_est
       DG_bcv_postest
  else
        print "\n\nError: The DG \" $group \" doesn't exists on `hostname` : Please check  $DG_list"
        exit 1
   fi
done
dos2unix $OUT $OUT >/dev/null 2>&1
# print "\n The output is in file \" $OUT \" \n"
new_var=`print "DGs in file: $INPUT "`
Email
}



Arg $@
