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

##-------------------------------------------------------------------#
## %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: rsExecuteTask.sh
##
## Subsystem: RAS WUI
##
## Input:	rsExecuteTask.sh -v -u username -p password -s serverName -b backup_server [-f source_volume -t target_volume taskName] | [-c consitency_group]
##
##		-v = verbose
##		-u = username
##		-p = password
##              -a = filepath
##		-s = name of copy services server
##              -b = name of backup copy services server
##              -f = source volume (from)
##              -t = target volume (to)
##		-c = consistency group name
##		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   
export PATH=/usr/sbin:$PATH
##-------------------------------------------------------------------#
## Set defaults
##-------------------------------------------------------------------#
SERVERNAME=
PRIMARY_SERVER=
BACKUP_SERVER=
USERNAME=
PASSWORD=
FILEPATH=
VERBOSE=
SOURCE_VOLUME=
TARGET_VOLUME=
TASKNAME=
RC=
OSVERSION=
FCCONSISTGRP=

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

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

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

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

##-------------------------------------------------------------------#
## Process arguments
##-------------------------------------------------------------------#
USAGE='usage: rsExecuteTask.sh [-v] -u username & -p password -s primaryserver -b backupserver [-f source -t target taskNames]| [-c [make|start] consistency_group]'

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

while getopts ":s:u:p:b:f:t:c:v" opt; do
        if [[ $OPTARG = -* ]]; then 
           echo "rsExecuteTask: 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;;
          v  ) VERBOSE=-v;;
	  f  ) SOURCE_VOLUME=$OPTARG;;
	  t  ) TARGET_VOLUME=$OPTARG;;
	  c  ) ACTION_TO_CONSISTENCY_GROUP=$OPTARG;;
          \? ) echo "rsExecuteTask: 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 = s || $1 = b  || $1 = f || $1 = t || $1 = -c ]]; then
             echo "rsExecuteTask: Invalid parameter specified"
             echo $USAGE
             exit 19
        fi
	if [[ -z $TASKNAME ]]; then
		TASKNAME=$1
	else
		TASKNAME="$TASKNAME $1"
	fi
	shift
done

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

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

if [[ ! -z $SOURCE_VOLUME && -z $TARGET_VOLUME && -z $ACTION_TO_CONSISTENCY_GROUP ]];then
	echo "$RSCOMMAND: no Volumes or consistency group specified"
	exit 5
fi

if [[ ! -z $ACTION_TO_CONSISTENCY_GROUP ]]; then 
  if [[ ($ACTION_TO_CONSISTENCY_GROUP != "make") && ($ACTION_TO_CONSISTENCY_GROUP != "start") ]]; then 
    echo "$RSCOMMAND: use 'make' or 'start' for option argument '-c'."
    exit 6
  fi
fi

if [[ ! -z  $FILEPATH ]];then
  if [[ ! -a $FILEPATH ]];then
     echo "rsExecuteTask: security file $FILEPATH could not be found"
     exit 23
  fi
  if [[ ! -s $FILEPATH ]];then
     echo "rsExecuteTask: format of security file $FILEPATH is incorrect"
     exit 24
  fi
  if [[ ! -r $FILEPATH ]];then
     echo "rsExecuteTask: 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 "rsExecuteTask: 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 "rsExecuteTask: 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 "rsExecuteTask: 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 "rsExecuteTask: The primary and backup server are down.  The task is not completed."
           exit 42 
        elif [[ $RC -eq 0 ]]; then
           if [[ ! -z $VERBOSE ]]; then
             echo "rsExecuteTask: The task will be run off the backup server."
           fi
           SERVERNAME=$BACKUP_SERVER
        fi
    elif [[ $RC -eq 0 ]]; then
      SERVERNAME=$PRIMARY_SERVER
    fi 
fi


# for testing comment the three lines starting with 'ssh.....'
if [[ ! -z  $ACTION_TO_CONSISTENCY_GROUP ]];then
  if [[ $ACTION_TO_CONSISTENCY_GROUP = "make" ]]; then
    #echo "$RSCOMMAND: ssh $OPT_USERNAME $SERVERNAME svctask mkfcconsistgrp -name $TASKNAME"
    ssh $OPT_USERNAME $SERVERNAME svctask mkfcconsistgrp -name $TASKNAME
  elif [[ $ACTION_TO_CONSISTENCY_GROUP = "start" ]]; then
    #echo "$RSCOMMAND: ssh $OPT_USERNAME $SERVERNAME svctask startfcconsistgrp -prep $TASKNAME"
    ssh $OPT_USERNAME $SERVERNAME svctask startfcconsistgrp -prep $TASKNAME
  fi
else
  ssh $OPT_USERNAME $SERVERNAME svctask mkfcmap -source $SOURCE_VOLUME -target $TARGET_VOLUME -copyrate 100 -consistgrp $TASKNAME
fi 


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

