#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverstop.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# 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
# @(#)  7d4c34b 43haes/usr/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverstop.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################################
#
# NAME:
#       cl_tsmserverstop
#
# PURPOSE:
#       stop script of Smart Assist for TSM server instance
#
# ARGUMENTS:
#       -i TSM instance name
#
# RETURNS
#  0 on success
#  1 on Failure
#
#################################################################################################

. /usr/es/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverGlobals
[[ "VERBOSE_LOGGING" == "high" ]] && set -x
KLIB_HACMPLOG_ENTRY=$KLIB_TSM_SERVER_HACMPLOG_ENTRY

typeset inst_name

while getopts i:d: opt
do
        case $opt in

                i) # instance name, which will be highly available
                        inst_name=$OPTARG
                        ;;
	esac
done
dat=$(/usr/bin/date)
app_id=$(clodmget -q "name=INSTANCE_NAME and value=$inst_name" -f application_id -n HACMPsa_metadata)
inst_dir=$(clodmget -q "application_id=$app_id and name=INSTANCE_DIRECTORY" -f value -n HACMPsa_metadata)
KLIB_SA_logmsg INFO 2 20 tsmserversa.cat "Stoping TSM server instance %1\$s at %2\$s\n" $inst_name "$dat"

[[ -f $inst_dir/dsmserv.v6lock ]] && {

	read R1 R2 R3 PID R4 < $inst_dir/dsmserv.v6lock
}
#for IPv4 communication
if [[ -n $PID ]];then
        [[ -f $inst_dir/dsmserv.lock ]] && {

               read R1 R2 R3 PID R4 < $inst_dir/dsmserv.lock
        }
fi
user=$(clodmget -q "application_id=$app_id and name=INSTANCE_USER" -f value -n HACMPsa_metadata)
set +x
password=$(clodmget -q "application_id=$app_id and name=INSTANCE_PASSWORD" -f value -n HACMPsa_metadata | /usr/es/sbin/cluster/cspoc/cldecodearg)
set -x
# The q sess will halt TSM server connection
$DSM_DIR/dsmadmc -quiet -se=$inst_name -id=$user -pass=$password halt > /dev/null 2>&1
[[ $? == 0 ]] && {
                        dat=$(/usr/bin/date)
                        KLIB_SA_logmsg INFO 2 54 tsmserversa.cat "TSM server instance %1\$s is stopped  at %2\$s\n" $inst_name "$dat"
                        sleep 6
                        exit 0
                 }

KLIB_SA_logmsg DEBUG 2 21 tsmserversa.cat "The PID %1\$s of the stopped dsmserv for TSM server instance %2\$s\n" $PID $inst_name
[[ -n $PID ]] && {
		ps -ef|grep $PID|grep -v grep
		[[ $? == 1 ]] && exit 0 
		}
		
[[ -n $PID ]] && {
	kill -9 $PID
	res=$(echo $?)
	[[ $res == "0" ]] && {
				ps -ef|grep -w db2acd|awk '{print $2}'|xargs kill -9 > /dev/null 2>&1
                                ps -ef|grep -w db2sysc|awk '{print $2}'|xargs kill -9 > /dev/null 2>&1
				dat=$(/usr/bin/date)
				KLIB_SA_logmsg INFO 2 54 tsmserversa.cat "TSM server instance %1\$s is stopped  at %2\$s\n" $inst_name "$dat"
			     }	
	exit $res
}
