#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/libHSS/HSS2107/rsQueryComplete.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2011 
# 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 
# @(#)34	1.1  src/43haes/lib/libHSS/HSS2107/rsQueryComplete.sh, hacmp.sap, 61haes_r714 11/28/11 15:17:26

##-------------------------------------------------------------------#
## %PS
##    
## Licensed Internal Code - Property of IBM
##    
## 2107 Licensed Internal Code
##    
## (C) Copyright IBM Corp. 1999, 2004 All Rights Reserved.
##    
## US Government Users Restricted Rights - Use, duplication or
## disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
## %EPS
##
## COMPONENT_NAME (RAS WUI)
##
##-------------------------------------------------------------------#
##
## Filename: rsQueryComplete.sh
##
## Subsystem: RAS WUI
##
## Input:	rsQueryComplete.sh -v -P DSclipath -u username -p password -m minutes -s serverName -b backup_server -d DS_dev_ID -t tracks_out_off_sync -S sequencenumber volumepair
##
##		-v = verbose
##		-P = DSclipath
##		-u = username
##		-p = password
##		-b = backup server
##		-d = DS 8000 storage image ID
##		-m = minutes to wait between checks of whether PPRC
##		     copy initialization has completed
##		-s = name of copy services server
##		-t = tracks out of sync
##		-S = sequencenumber
##		volumepair = pair|pairs of volumes with fc relation
##
## Description: Wait on the completion of a copy initialization.
##
##
##-------------------------------------------------------------------#

OS=`uname | cut -c 1-3`
##--------------------------------------------------------------------#
## Set INSTALL system variable
##--------------------------------------------------------------------#
##
##
export INSTALL=/usr/opt/ibm/ibmsap/connectors/HSS2107  

##-------------------------------------------------------------------#
## Set defaults
##-------------------------------------------------------------------#
VERBOSE=
DSCLIPATH=
SERVERNAME=
PRIMARY_SERVER=
BACKUP_SERVER=
USERNAME=
PASSWORD=
DS_DEV_ID=
MINUTES=
TRACKS_OUT_OFF_SYNC=
SEQNUMBER=
VOLUMEPAIR=

RC=
OSVERSION=


##-------------------------------------------------------------------#
## define functions
##-------------------------------------------------------------------#

function GetProgress {
  #set -A sources $(ssh $OPT_USERNAME admin@$SERVERNAME svcinfo lsfcmap -nohdr | grep -w $TASKNAMES | awk '{print $12}')
  set -A sources $($DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SERVERNAME lsflash -dev $DS_DEV_ID -hdr off -l $VOLUMEPAIR 2>/dev/null | grep -wi $SEQNUMBER | awk '{print $12}')
  let i=0
  let OUT_OFF_SYNC=0
  while (( i < ${#sources[*]} )); do
    let OUT_OFF_SYNC=$OUT_OFF_SYNC+${sources[$i]}
    let i=$i+1
  done
}

##-------------------------------------------------------------------#
## Set up error message codes
##-------------------------------------------------------------------#
NO_INSTALL_CODE=62
NO_CFG_FILE_CODE=63
NO_EXISTING_TASKNAME=64

##-------------------------------------------------------------------#
## Assign COMMAND for messaging
##-------------------------------------------------------------------#
RSCOMMAND=rsQueryComplete

##-------------------------------------------------------------------#
## Set default BLANK values
##-------------------------------------------------------------------#

##-------------------------------------------------------------------#
## Test for $INSTALL variable
##-------------------------------------------------------------------#
if [[ -z $INSTALL ]];then
     echo "$RSCOMMAND: No value is specified for the <INSTALL> system variable."
     exit $NO_INSTALL_CODE
fi

##-------------------------------------------------------------------#
## Process arguments
##-------------------------------------------------------------------#
USAGE='usage: rsQueryComplete.sh [-v] -P DSclipath -u username -p password -s primaryserver -b backupserver -d DS_dev_ID -m minutes -t tracks_out_off_sync -S sequencenumber VolumePair'

if [[ -z $1 ]]; then
    echo "rsQueryComplete: No parameters specified"
    echo $USAGE
    exit 21
fi

while getopts ":vm:s:u:p:b:P:d:t:S:" opt; do
        if [[ $OPTARG = -* ]]; then
           echo "rsQueryComplete: Missing argument for -$opt"
           echo $USAGE
           exit 20
        fi
        case $opt in
          P  ) DSCLIPATH=$OPTARG;;
          u  ) USERNAME=$OPTARG;;
          p  ) PASSWORD=$OPTARG;;
          s  ) PRIMARY_SERVER=$OPTARG;;
          b  ) BACKUP_SERVER=$OPTARG;;
          d  ) DS_DEV_ID=$OPTARG;;
          m  ) MINUTES=$OPTARG;;
          t  ) TRACKS_OUT_OFF_SYNC=$OPTARG;;
          S  ) SEQNUMBER=$OPTARG;;
          v  ) VERBOSE=-v;;
          \? ) echo "rsQueryComplete: Invalid parameter specified"
               echo $USAGE
               exit 19
        esac
done
shift $(($OPTIND - 1))

while [[ ! -z $1 ]]; do
        if [[ $1 = - ||  $1 = v || $1 = P || $1 = u || $1 = p || $1 = a || $1 = m || $1 = t || $1 = s || $1 = b || $1 = d ]];then
echo "rsQueryComplete: Invalid parameter specified"
            echo $USAGE
            exit 19
        fi
        if [[ -z $VOLUMEPAIR ]]; then
                VOLUMEPAIR=$1
        else
                VOLUMEPAIR="$VOLUMEPAIR $1"
        fi
        shift
done

if [[ -z $PRIMARY_SERVER && -z $BACKUP_SERVER ]]; then
        echo "rsQueryComplete: No primary or backup server name specified"
        exit 3
fi

if [[ -z $VOLUMEPAIR ]]; then
	echo "rsQueryComplete: No volume pair specified"
	exit 4 
fi

##-------------------------------------------------------------------#
## Set-up environmental variables
##-------------------------------------------------------------------#

##-------------------------------------------------------------------#
## Test Connections if only Primary Server is given
##-------------------------------------------------------------------#
if [[ ! -z $PRIMARY_SERVER && -z $BACKUP_SERVER ]];then
   SNAME="$PRIMARY_SERVER"
   $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SNAME lssi 2>&1 > /dev/null
   RC=$?
   if [[ $RC -ne 0 ]];then
      echo "rsQueryComplete: The primary server is down and there is no backup server specified."
      echo "                 The task is not completed."
      exit 41
   elif [[ $RC -eq 0 ]]; then
      SERVERNAME=$PRIMARY_SERVER
   fi
fi

##-------------------------------------------------------------------#
## Test Connections if only Backup Server is given
##-------------------------------------------------------------------#
if [[ -z $PRIMARY_SERVER && ! -z $BACKUP_SERVER ]];then
   SNAME="$BACKUP_SERVER"
   $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SNAME lssi 2>&1 > /dev/null
   RC=$?
   if [[ $RC -ne 0 ]];then
      echo "rsQueryComplete: The backup server is down and there is no primary server specified."
      echo "                 The task is not completed."
      exit 45
   elif [[ $RC -eq 0 ]]; then
      SERVERNAME=$BACKUP_SERVER
   fi
fi

##-------------------------------------------------------------------#
## Test Connections if both of Primary  and Backup Server is given
##-------------------------------------------------------------------#
if [[ ! -z $PRIMARY_SERVER && ! -z $BACKUP_SERVER ]];then
   SNAME="$PRIMARY_SERVER"
   $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SNAME lssi 2>&1 > /dev/null
   RC=$?
   if [[ $RC -ne 0 ]]; then
        if [[ ! -z $VERBOSE ]]; then
           echo "rsQueryComplete: The primary server is down, trying the specified backup server ..."
        fi
        SNAME="$BACKUP_SERVER"
        $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SNAME lssi 2>&1 > /dev/null
        RC=$?
        if [[ $RC -ne 0 ]]; then
           echo "rsQueryComplete: The primary and backup server are down.  The task is not completed."
           exit 42
        elif [[ $RC -eq 0 ]]; then
           if [[ ! -z $VERBOSE ]]; then
             echo "rsQueryComplete: The task will be run off the backup server."
           fi
           SERVERNAME=$BACKUP_SERVER
        fi
    elif [[ $RC -eq 0 ]]; then
      SERVERNAME=$PRIMARY_SERVER
    fi
fi

##-------------------------------------------------------------------#
## Setting up the variables
##-------------------------------------------------------------------#

# missleading! MINUTES are SECONDS used for sleep command!
if [[ -z $MINUTES ]]; then
	((MINUTES = 10))
fi

##-------------------------------------------------------------------#
## test if TASKNAME is existing on DS
##-------------------------------------------------------------------#
$DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SERVERNAME lsflash -dev $DS_DEV_ID -hdr off $VOLUMEPAIR 2>/dev/null | grep -qi $SEQNUMBER
RC=$?
if [[ $RC -ne 0 ]];then
  echo $RSCOMMAND: FlashCopy relation for volume \'$VOLUMEPAIR\' does not exist!
  exit $NO_EXISTING_TASKNAME
fi

##-------------------------------------------------------------------#
## Call command 
##-------------------------------------------------------------------#

echo $RSCOMMAND: request for $TRACKS_OUT_OFF_SYNC out off sync tracks of mapping \'$VOLUMEPAIR\' starts.

## get our off sync track of existing volume pairs from DS
GetProgress

if [[ $OUT_OFF_SYNC -le $TRACKS_OUT_OFF_SYNC ]]; then
  echo $RSCOMMAND: CopyTask of volume pair \'$VOLUMEPAIR\' has $OUT_OFF_SYNC tracks out off sync.
  exit 0
fi

echo $RSCOMMAND: CopyTask of volume pair \'$VOLUMEPAIR\' has currently $OUT_OFF_SYNC tracks out off sync, will wait until $TRACKS_OUT_OFF_SYNC is reached.

until [[ $OUT_OFF_SYNC -le $TRACKS_OUT_OFF_SYNC ]]
  do
    sleep $MINUTES
    GetProgress
    if [[ $VERBOSE = "-v" ]]; then
      echo $RSCOMMAND: CopyTask of volume pair \'$VOLUMEPAIR\' is still in progress. Out off sync tracks....................$OUT_OFF_SYNC
    fi
  done

echo $RSCOMMAND: CopyTask of volume pair \'$VOLUMEPAIR\' reached out off sync tracks of $OUT_OFF_SYNC.

##-------------------------------------------------------------------#
## Quit
##-------------------------------------------------------------------#
RC=$?
exit $RC 
