#!/opt/perl/5.8.0/bin/perl

##########################################################################
#
# script resume_async
#
# C.A. Clayton 	4th July 2005 	original version V1.0
#		
# Invocation:	resume_async <DGName> <RDF group> 
#
# Arguments: 	<DGName>	- Device Group Name
#               <RDF group> 	- RDF group number of async link
#
# Example:	resume_async App1 4
#
#
# Return codes:
#		0 	Success
#		1	Wrong number of arguments supplied
#		2 	A symmir verify command showed system not in right state
#		3 	A symmir command failed to run correctly
#
# Assumptions:
#
# 1) PIT BCVs are defined in the device group
#
# 2) The PIT BCV is established. Note this script will not run
#    if BCV is not established or establishing as this may 
#    may indicate that the SRDF/A outage occurred during a 
#    previously incomplete resunc operation and hence the BCV
#    should NOT be established against a potentially inconsistent
#    R2. 
#
# 
# 1) Verify remote BCV is fully established
# 2) Split remote BCV
# 3) Wait until finished splitting in the backgroup
# 4) Set mode to adaptive copy 
# 5) Establish RDF
# 6) Wait until RDF is full synced up
# 7) Set mode to async mode 
# 8) Enable consistency
# 9) Establish remote BCV
# 10) Wait until remote BCV fully established before exiting

##########################################################################
#
# Variables used:
#
# $dgname		Device Group Name
#
# $rdf_group            Current RDF group being processed
#
# $output		Output returned from external commands
#
# $returncode		Return code from external commands
#
##########################################################################
#
# Parse input parameters. Exactly 2 arguments expected i.e. $#ARGV = 1
#

if ( $#ARGV != 1 ) {
   print "Incorrect number of arguments for script resume_async\n";
   print "USAGE: resume_async <DGName> <RDF group>\n";
   exit(1);

} else {

   $dgname = $ARGV[0];
   $rdf_group = $ARGV[1];
}

##########################################################################
#
# 1) Check remote BCV is synced up first
#
print "\n*** Starting SRDF/A resynchronization exercise\n";
print "*** Step 1: Verifying remote BCVs already synced up\n";
$output=`pbrun /usr/symcli/bin/symmir -g $dgname verify -synched -rdf`;
$returncode = ($? >> 8);

if ( $returncode != 0 ) {
   print "PIT BCV(s) not already fully synced up. Script exiting.\n";
   exit (2)
}


##########################################################################
#
# 2) Split remote BCV
#

print "*** Step 2: Splitting remote BCVs\n";
$output=`pbrun /usr/symcli/bin/symmir -g $dgname -consistent -rdf split -noprompt`;
$returncode = ($? >> 8);

if ( $returncode != 0 ) {
   print "PIT BCV(s) failed to split. Script exiting.\n";
   exit (3)
}

##########################################################################
#
# 3) Wait until finished splitting in the background
#

print "*** Step 3: Waiting for background BCV split for $dgname to finish\n";
$output=`pbrun /usr/symcli/bin/symmir -g $dgname verify -rdf -split -bg -i 5`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0 
#
if ( $returncode != 0 ) {
   print "Split failed to complete. Script exiting.\n";
   exit(2);
}

##########################################################################
#
# 4) Set mode to adaptive copy
#

print "*** Step 4: Setting mode for $dgname to adaptive copy\n";
$output=`pbrun /usr/symcli/bin/symrdf -g $dgname -rdfg $rdf_group disable -nop`;
$output=`pbrun /usr/symcli/bin/symrdf -g $dgname -rdfg $rdf_group set mode acp_disk -nop`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0
#
if ( $returncode != 0 ) {
   print "Mode change to adapative copy failed. Script exiting.\n";
   exit(2);
}

##########################################################################
#
# 5) Establish RDF
#

print "*** Step 5: Establish RDF link for $dgname\n";
$output=`pbrun /usr/symcli/bin/symrdf -g $dgname -rdfg $rdf_group establish -nop`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0
#
if ( $returncode != 0 ) {
   print "Incremental establish operation failed. Script exiting.\n";
   exit(2);
}

##########################################################################
#
# 6) Wait until RDF is full synced up
#

print "*** Step 6: Waiting until $dgname RDF is fully synced up\n";
$output=`pbrun /usr/symcli/bin/symrdf -g $dgname -rdfg $rdf_group -i 10 verify -synchronized -nop`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0
#
if ( $returncode != 0 ) {
   print "symrdf verify operation failed. Script exiting.\n";
   exit(3);
}

##########################################################################
#
# 7) Set mode to async mode
#

print "*** Step 7: Change mode back to async for $dgname\n";
$output=`pbrun /usr/symcli/bin/symrdf -g $dgname -rdfg $rdf_group set mode async -force -nop`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0
#
if ( $returncode != 0 ) {
   print "Mode change to async failed. Script exiting.\n";
   exit(2);
}

##########################################################################
#
# 8) Enable consistency
#

print "*** Step 8: Enabling consistency for $dgname\n";
$output=`pbrun /usr/symcli/bin/symrdf -g $dgname -rdfg $rdf_group enable -nop`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0
#
if ( $returncode != 0 ) {
   print "Mode change to async failed. Script exiting.\n";
   exit(2);
}

##########################################################################
#
# 9) Establish remote BCV
#

print "*** Step 9: Incrementally establish remote BCV for $dgname\n";
$output=`pbrun /usr/symcli/bin/symmir -g $dgname establish -rdf -force -nop`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0
#
if ( $returncode != 0 ) {
   print "Incremental etablish operation failed. Script exiting.\n";
   exit(2);
}

##########################################################################
#
# 10) Wait until remote BCV fully established before exiting
#

print "***  Step 10: Wait until remote BCV fully established for $dgname\n";
$output=`pbrun /usr/symcli/bin/symmir -g $dgname verify -synched -rdf -i 5`;
$returncode = ($? >> 8);

#
# Return code from verify should be 0
#
if ( $returncode != 0 ) {
   print "Verification of BCV synced state failed. Script exiting.\n";
   exit(2);
}


print "\n***  SRDF/A Resynchronization exercise for device group $dgname completed \n";
exit (0);
