#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/libHSS/HSS2107/rsExecuteTask.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 
# @(#)94	1.1  src/43haes/lib/libHSS/HSS2107/rsExecuteTask.sh, hacmp.sap, 61haes_r714 11/28/11 15:15:47

##-------------------------------------------------------------------#
## %PS
##    
## Licensed Internal Code - Property of IBM
##    
## 2145 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: rsExecuteTask.sh
##
## Subsystem: RAS WUI
##
## Input:	rsExecuteTask.sh -v -P DSclipath -u username -p password -s serverName -b backup_server -d DS_dev_ID -S sequencenumber volumepair [volumepair]
##
##		-v = verbose
##		-P = DSclipath
##		-u = username
##		-p = password
##		-s = name of copy services server
##		-b = name of backup copy services server
##		-d = DS 8000 storage image ID
##		-S = sequence number = a unique 4 digit hexadezimal number which should be used as sequence number for the task
##
## Description: Execute one Copy Services tasks on DS8000
##
##-------------------------------------------------------------------#

OS=`uname | cut -c 1-3`

##-------------------------------------------------------------------#
## Set defaults
##-------------------------------------------------------------------#
VERBOSE=
DSCLIPATH=
SERVERNAME=
PRIMARY_SERVER=
BACKUP_SERVER=
USERNAME=
PASSWORD=
DS_DEV_ID=
SEQNUMBER=
VOLUMEPAIR=
RC=
OSVERSION=

##-------------------------------------------------------------------#
## Set up error message codes
##-------------------------------------------------------------------#
NO_INSTALL_CODE=62
NO_CFG_FILE_CODE=63
##-------------------------------------------------------------------#
## Assign COMMAND for messaging
##-------------------------------------------------------------------#
RSCOMMAND=rsExecuteTask

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

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

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

while getopts ":s:d:P:u:p:b:S:v" opt; do
        if [[ $OPTARG = -* ]]; then 
           echo "rsExecuteTask: 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;;
          S  ) SEQNUMBER=$OPTARG;;
          v  ) VERBOSE=-v;;
          \? ) echo "rsExecuteTask: 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 = s || $1 = b || $1 = d || $1 = S ]]; then
             echo "rsExecuteTask: 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 "rsExecuteTask: No primary or backup server name specified"
        exit 3
fi

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

if [[ -z $SEQNUMBER ]]; then
	echo "rsExecuteTask: No sequence number specified"
	exit 4
fi

if [[ -z $USER ]]; then
	echo "rsExecuteTask: No user specified"
	exit 4
fi

if [[ -z $DS_DEV_ID ]]; then
	echo "rsExecuteTask: No DS device ID specified"
	exit 4
fi

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


##-------------------------------------------------------------------#
## 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 "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"
   $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SNAME lssi 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"
   $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SNAME lssi 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"
        $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SNAME lssi 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

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

if [[ ! -z  $ACTION_TO_CONSISTENCY_GROUP ]];then
  if [[ $ACTION_TO_CONSISTENCY_GROUP = "make" ]]; then
    echo "$RSCOMMAND: ssh $USERNAME admin@$SERVERNAME svctask mkfcconsistgrp -name $TASKNAME"
#    ssh $USERNAME admin@$SERVERNAME svctask mkfcconsistgrp -name $TASKNAME
  elif [[ $ACTION_TO_CONSISTENCY_GROUP = "start" ]]; then
    echo "$RSCOMMAND: ssh $USERNAME admin@$SERVERNAME svctask startfcconsistgrp -prep $TASKNAME"
    ssh $USERNAME admin@$SERVERNAME svctask startfcconsistgrp -prep $TASKNAME
  fi
else
  echo "$RSCOMMAND: $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SERVERNAME mkflash -dev $DS_DEV_ID -seqnum $SEQNUMBER $VOLUMEPAIR"
  $DSCLIPATH/dscli -user $USERNAME -passwd $PASSWORD -hmc1 $SERVERNAME mkflash -dev $DS_DEV_ID -persist -seqnum $SEQNUMBER $VOLUMEPAIR
fi 

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