#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/libHSS/HSS2145/rsQueryComplete.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2011,2012 
# 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 
# @(#)73	1.3  src/43haes/lib/libHSS/HSS2145/rsQueryComplete.sh, hacmp.sap, 61haes_r714 4/2/12 14:20:32

##-------------------------------------------------------------------#
## %PS
##    
## Licensed Internal Code - Property of IBM
##    
## 2145 Licensed Internal Code
##    
## (C) Copyright IBM Corp. 1999, 2003 All Rights Reserved.
##    
## US Government Users Restricted Rights - Use, duplication or
## disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
## %EPS
##
## COMPONENT_NAME (IBM LIB HSS)
##
##-------------------------------------------------------------------#
##
## Filename: rsQueryComplete.sh
##
## Subsystem: IBM LIB HSS
##
## Input:	rsQueryComplete.sh -v -u username -p password -a securityfilepath -m minutes -t threshold -s serverName -b backup_server taskName
##
##		-v = verbose
##		-u = username
##		-p = password
##              -a = securityfilepath
##              -b = backup server
##		-m = minutes to wait between checks of whether PPRC
##		     copy initialization has completed
##		-t = threshold percentage which determines 
##		     completion. Default=2%
##		-s = name of copy services server
##		taskName = name of pre-defined Copy Services Server
##			   PPRC task.
##
## Description: Wait on the completion of PPRC copy initialization.
##
##
##-------------------------------------------------------------------#

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

##-------------------------------------------------------------------#
## Set defaults
##-------------------------------------------------------------------#
SERVERNAME=
PRIMARY_SERVER=
BACKUP_SERVER=
USERNAME=
PASSWORD=
FILEPATH=
VOLUME=
MINUTES=
THRESHOLD=
VERBOSE=
TASKNAMES=
RC=
OSVERSION=
PROGRESS=


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

function GetProgress {
  set -A sources $(ssh $OPT_USERNAME $SERVERNAME svcinfo lsfcmap -nohdr | grep -w $TASKNAMES | awk '{print $10}')
  let i=0
  let SUMME=0
  while (( i< ${#sources[*]} )); do
    let SUMME=$SUMME+${sources[$i]}
    let i=$i+1
  done
  let PROGRESS=${SUMME}/${#sources[*]}
}

##-------------------------------------------------------------------#
## 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
##-------------------------------------------------------------------#
CFGFILE=CLI.CFG
CFGFILE=$INSTALL/$CFGFILE

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

##-------------------------------------------------------------------#
## Test to see if Configuration File exists
##-------------------------------------------------------------------#
#if [[ ! -r $CFGFILE || ! -s $CFGFILE ]];then
#      echo "$RSCOMMAND:  The CLI.CFG file is not found or accessible."
#      exit $NO_CFG_FILE_CODE
#fi

##-------------------------------------------------------------------#
## Process arguments
##-------------------------------------------------------------------#
USAGE='usage: rsQueryComplete.sh [-v] -u username [-m minutes] [-t threshold] -s primaryserver -b backupserver taskName'

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

while getopts ":vm:t:s:u:b:" opt; do
        if [[ $OPTARG = -* ]]; then
           echo "rsQueryComplete: Missing argument for -$opt"
           echo $USAGE
           exit 20
        fi
        case $opt in
          m  ) MINUTES=$OPTARG;;
          t  ) THRESHOLD=$OPTARG;;
          u  ) USERNAME=$OPTARG;;
	  s  ) PRIMARY_SERVER=$OPTARG;;
          b  ) BACKUP_SERVER=$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 = u || $1 = a || $1 = m || $1 = t || $1 = s || $1 = b ]];then
echo "rsQueryComplete: Invalid parameter specified"
            echo $USAGE
            exit 19
        fi
        if [[ -z $TASKNAMES ]]; then
                TASKNAMES=$1
        else
                TASKNAMES="$TASKNAMES $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 $TASKNAMES ]]; then
	echo "rsQueryComplete: No task names specified"
	exit 4 
fi

if [[ ! -z $FILEPATH ]];then
  if  [[ ! -a $FILEPATH ]];then
     echo "rsQueryComplete: security file $FILEPATH could not be found"
     exit 23
  fi
  if [[ ! -s $FILEPATH ]];then
    echo "rsQueryComplete: format of security file $FILEPATH is incorrect"
    exit 24
  fi
  if [[ ! -r $FILEPATH ]];then
    echo "rsQueryComplete: read access to $FILEPATH not provided"
    exit 61
  fi  
fi

##-------------------------------------------------------------------#
## Set-up environmental variables
##-------------------------------------------------------------------#
export PATH=$INSTALL:/usr/sbin:$PATH
export LIBPATH=$INSTALL:/usr/lib

if [[ $OS = "HP-" ]]; then
    export SHLIB_PATH=$INSTALL:/usr/lib
fi

##-------------------------------------------------------------------#
## Test Connections if only Primary Server is given
##-------------------------------------------------------------------#
if [[ ! -z $PRIMARY_SERVER && -z $BACKUP_SERVER ]];then
   SNAME="$PRIMARY_SERVER"
   ping -c1 $SNAME 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"
   ping -c1 $SNAME 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"
   ping -c1 $SNAME 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"
        ping -c1 $SNAME 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
##-------------------------------------------------------------------#

if [[ -z $THRESHOLD ]]; then
	((THRESHOLD = 2))
fi

if [[ -z $MINUTES ]]; then
	((MINUTES = 10))
fi

OPT_USERNAME=${USERNAME:+"-l $USERNAME"}

##-------------------------------------------------------------------#
## test if TASKNAME is existing on SVC
##-------------------------------------------------------------------#

ssh $OPT_USERNAME $SERVERNAME svcinfo lsfcmap 2>/dev/null | grep -q $TASKNAMES
RC=$?
if [[ $RC -ne 0 ]];then
  echo $RSCOMMAND: Mapping \'$TASKNAMES\' does not exist!
  exit $NO_EXISTING_TASKNAME
fi


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

echo $RSCOMMAND: request for \'$THRESHOLD\%\' threshold of mapping \'$TASKNAMES\' starts.

## get progress of existing mapping from SVC
GetProgress

if [[ $PROGRESS -ge $THRESHOLD ]]; then
  echo $RSCOMMAND: Task \'$TASKNAMES\' reached $PROGRESS\% completion.
  exit 0
fi

echo $RSCOMMAND: Task \'$TASKNAMES\' has current progress of $PROGRESS\% will wait until \'$THRESHOLD\%\' is reached.

until [[ $PROGRESS -ge $THRESHOLD ]]
  do
    sleep $MINUTES
    GetProgress
    if [[ $VERBOSE = "-v" ]]; then
      echo $RSCOMMAND: Task \'$TASKNAMES\' is still in progress. Percent complete....................$PROGRESS\%
    fi
  done

echo $RSCOMMAND: Task \'$TASKNAMES\' reached threshold of $THRESHOLD\%.

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