#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/libHSS/HSS2145/rsRemoveTask.sh 1.2 
#  
# 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 
# @(#)45	1.2  src/43haes/lib/libHSS/HSS2145/rsRemoveTask.sh, hacmp.sap, 61haes_r714 1/30/12 09:03:02

##-------------------------------------------------------------------#
## %PS
##    
## Licensed Internal Code - Property of IBM
##    
## 2145 Licensed Internal Code
##    
## (C) Copyright IBM Corp. 1999, 2010 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: rsRemoveTask.sh
##
## Subsystem: IBM LIB HSS
##
## Input:	rsRemoveTask.sh -v -u username -p password -a filepath -s serverName -b backup_server taskName
##
##		-v = verbose
##		-u = username
##		-p = password
##              -a = filepath
##		-s = name of copy services server
##              -b = name of backup copy services server
##		taskName = a unic name which should be used for the task
##
## Description: Execute one Copy Services tasks on SVC
##
##-------------------------------------------------------------------#

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=
VERBOSE=
TASKNAME=
RC=
OSVERSION=

##-------------------------------------------------------------------#
## define functions
##-------------------------------------------------------------------#
function GetFcConsistGrpStatus {
  FC_CONSISTGRP_STATUS=$(ssh $OPT_USERNAME $SERVERNAME svcinfo lsfcmap -nohdr | grep -w $TASKNAMES | awk '{print $9}')
}


NO_EXISTING_TASKNAME=1

##-------------------------------------------------------------------#
## Set up error message codes
##-------------------------------------------------------------------#
NO_INSTALL_CODE=62
NO_CFG_FILE_CODE=63
NO_SOURCE_VOLUME_CODE=64
NO_TARGET_VOLUME_CODE=65
NO_EXISTING_TASKNAME=1

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


#-------------------------------------------------------------------#
## Set default BLANK values
##-------------------------------------------------------------------#
CFGFILE=CLI.CFG

CFGFILE=$INSTALL/$CFGFILE

export PATH=/usr/sbin:$PATH

##-------------------------------------------------------------------#
## 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: rsRemoveTask.sh [-v] -u username & -p password | -a securityfilepath -s primaryserver -b backupserver taskName'

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

while getopts ":s:u:p:b:a:v" opt; do
        if [[ $OPTARG = -* ]]; then 
           echo "$RSCOMMAND: Missing argument for -$opt"
           echo $USAGE 
           exit 20
        fi
        case $opt in
          u  ) USERNAME=$OPTARG;;
          p  ) PASSWORD=$OPTARG;;
	  s  ) PRIMARY_SERVER=$OPTARG;;
          b  ) BACKUP_SERVER=$OPTARG;;
          a  ) FILEPATH=$OPTARG;;
          v  ) VERBOSE=-v;;
          \? ) echo "$RSCOMMAND: Invalid parameter specified"
               echo $USAGE 
               exit 19
        esac
done
shift $(($OPTIND - 1))

while [[ ! -z $1 ]]; do
        if [[ $1 = - || $1 = v || $1 = u || $1 = p || $1 = a || $1 = s || $1 = b ]]; then
             echo "$RSCOMMAND: 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 "$RSCOMMAND: No primary or backup server name specified"
        exit 3 
fi

if [[ -z $TASKNAMES ]]; then
	echo "$RSCOMMAND: No task names specified"
	exit 4 
fi

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

##-------------------------------------------------------------------#
## Set-up command options
##-------------------------------------------------------------------#

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

##-------------------------------------------------------------------#
## 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 "$RSCOMMAND: 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 "$RSCOMMAND: 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 "$RSCOMMAND: 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 "$RSCOMMAND: The primary and backup server are down.  The task is not completed."
           exit 42 
        elif [[ $RC -eq 0 ]]; then
           if [[ ! -z $VERBOSE ]]; then
             echo "$RSCOMMAND: The task will be run off the backup server."
           fi
           SERVERNAME=$BACKUP_SERVER
        fi
    elif [[ $RC -eq 0 ]]; then
      SERVERNAME=$PRIMARY_SERVER
    fi 
fi

##-------------------------------------------------------------------#
## Test and exit if ${TASKNAMES} does not exist 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

##-------------------------------------------------------------------#
## Get all flashCopy maps for consisteny group ${TASKNAMES}
##-------------------------------------------------------------------#

set -A fctasknames $(ssh $OPT_USERNAME $SERVERNAME svcinfo lsfcmap -nohdr | grep -w $TASKNAMES | awk '{print $2}')

if [[ $VERBOSE != "-v" ]]; then
  ssh $OPT_USERNAME $SERVERNAME svctask stopfcconsistgrp $TASKNAMES 2>&1 > /dev/null
else
  echo $RSCOMMAND: executing \'ssh $OPT_USERNAME $SERVERNAME svctask stopfcconsistgrp $TASKNAMES\'
  ssh $OPT_USERNAME $SERVERNAME svctask stopfcconsistgrp $TASKNAMES
fi

##-------------------------------------------------------------------#
## wait until fcconsistgrp is in state 'stopped'
## CMMVC6241E The FlashCopy mapping was not deleted because the mapping
## or consistency group is in the stopping state. The mapping or
## consistency group must be stopped first
##-------------------------------------------------------------------#

GetFcConsistGrpStatus
#until [[ $FC_CONSISTGRP_STATUS = "stopped" ]]
while [[ $FC_CONSISTGRP_STATUS = "stopping" ]]
  do
    if [[ $VERBOSE = "-v" ]]; then
      echo $RSCOMMAND: Task \'$TASKNAMES\' is in state $FC_CONSISTGRP_STATUS, will wait until status changed ...
    fi
    sleep 2
    GetFcConsistGrpStatus
  done

let i=0
while (( i< ${#fctasknames[*]} )); do
  if [[ $VERBOSE != "-v" ]]; then
#    ssh $OPT_USERNAME $SERVERNAME svctask stopfcmap ${fctasknames[$i]} 2>&1 > /dev/null
    ssh $OPT_USERNAME $SERVERNAME svctask rmfcmap -force ${fctasknames[$i]} 2>&1 > /dev/null
  else
#    echo $RSCOMMAND: executing \'ssh $OPT_USERNAME $SERVERNAME svctask stopfcmap ${fctasknames[$i]}\'
#    ssh $OPT_USERNAME $SERVERNAME svctask stopfcmap ${fctasknames[$i]}
    echo $RSCOMMAND: executing \'ssh $OPT_USERNAME $SERVERNAME svctask rmfcmap -force ${fctasknames[$i]}\'
    ssh $OPT_USERNAME $SERVERNAME svctask rmfcmap -force ${fctasknames[$i]}
  fi
  let i=$i+1
done

if [[ $VERBOSE != "-v" ]]; then
  ssh $OPT_USERNAME $SERVERNAME svctask rmfcconsistgrp -force $TASKNAMES 2>&1 > /dev/null
else
  echo $RSCOMMAND: executing \'ssh $OPT_USERNAME $SERVERNAME svctask rmfcconsistgrp -force $TASKNAMES
  ssh $OPT_USERNAME $SERVERNAME svctask rmfcconsistgrp -force $TASKNAMES
fi 

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