#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,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_tsmservmanual.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_tsmservmanual.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################################
#
# NAME:
#       cl_tsmservmanual
#
# PURPOSE:
#       Manual configuration for TSM server instance. This script is used when manual option
#	is selected in SMIT
#
# ARGUMENTS:
#	n/a
#
# RETURNS
#  0 on success
#  1 on Failure
#
#################################################################################################

. /usr/es/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverGlobals
. /usr/es/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverUtilities
KLIB_HACMPLOG_ENTRY=$KLIB_TSM_SERVER_HACMPLOG_ENTRY

typeset APPLICATION_ID     # Smart Assist Application Name of the TSM server instance.
typeset PRIMNODE TAKEOVNODES # primary and takeover nodes.
typeset INSTANCE_ID          # TSM server instance name.
typeset INSTANCE_DIR         # TSM server instance Directory.
typeset SHARED_VGS           # Shared VGs between the nodes in the cluster.
typeset SERVICE_IP           # Service IP for the TSM instance.
typeset NETMASK              # Netmask/ prefix Length of service IP address.
typeset USER                 # TSM user id.
typeset XML_PATH=""

while getopts f: opt
do
        case $opt in

                f) # xml file path
                        XML_PATH=$OPTARG
                        ;;
        esac
done

dspmsg -s 1 tsmadminsa.cat 57 "configuring TSM server Instance using manual configuration\n"
KLIB_SA_logmsg INFO 1 57 tsmserversa.cat "configuring TSM server Instance using manual configuration\n"

INSTANCE_ID=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t Instance|awk -F= '{ print $2 }')
if [[ $? == 0 && -n $INSTANCE_ID ]]
then
	APPLICATION_ID=$($TSM_SERVER_HA_SBIN/cl_tsm_server_query -i $INSTANCE_ID|grep -v application_name|awk -F: '{ print $1 }')
	validateAppName "$APPLICATION_ID"
	[[ "$?" == "1" ]] && {
                KLIB_SA_logmsg ERROR 1 11 tsmserversa.cat "TSM server smart assist Application id=%1\$s is not in the rage [a-z][A-z][0-9]_.\n" "$APPLICATION_ID"
                exit 1
                }
else

	KLIB_SA_logmsg ERROR 1 58 tsmserversa.cat "No value is specified for Instance property in TSM server xml file\n" 
	exit 1
fi

PRIMNODE=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t PrimaryNode|awk -F= '{ print $2 }')
[[ $? != 0 || -z $PRIMNODE ]] && {
	KLIB_SA_logmsg ERROR 1 59 tsmserversa.cat "No value is specified for PrimaryNode property in TSM server xml file\n"
	exit 1
	}
 
TAKEOVNODES=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t TakeoverNodes|awk -F= '{ print $2 }')
if [[ $? == 0 && -n $TAKEOVNODES ]]
then
	TAKEOVNODES=$(echo $TAKEOVNODES|tr "," " ")
else
	KLIB_SA_logmsg ERROR 1 60 tsmserversa.cat "No value is specified for TakeoverNodes property in TSM server xml file\n"	
	exit 1
fi

INSTANCE_DIR=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t InstanceDirectory|awk -F= '{ print $2 }')
[[ $? != 0 || -z $INSTANCE_DIR ]] && {
	KLIB_SA_logmsg ERROR 1 61 tsmserversa.cat "No value is specified for InstanceDirectory property in TSM server xml file\n"
	exit 1
	}

SERVICE_IP=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t IPAddress_or_name|awk -F= '{ print $2 }')
[[ $? != 0 || -z $SERVICE_IP ]] && {
	KLIB_SA_logmsg ERROR 1 62 tsmserversa.cat "No value is specified for ServiceIPs property in TSM server xml file\n"
	exit 1
	}

SERVICE_IP=$(/usr/bin/host $SERVICE_IP|awk -F" " '{print $1}'|awk -F. '{print $1}')
[[ $? != 0 || -z $SERVICE_IP ]] && {
	KLIB_SA_logmsg ERROR 1 66 tsmserversa.cat "No entry for service IP in /etc/host file\n"
	exit 1
	}

NETMASK=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t Prefix_or_Netmask|awk -F= '{ print $2 }')

USER=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t User|awk -F= '{ print $2 }')
[[ $? != 0 || -z $USER ]] && {
	KLIB_SA_logmsg ERROR 1 63 tsmserversa.cat "No value is specified for User property in TSM server xml file\n"
	exit 1
	}

SHARED_VGS=$($SA_SBIN/clsaxmlutil -s -x $XML_PATH -m $TSM_SERVER_HA_CONFIG/cl_tsmserver_config.xsd -t VolumeGroups|awk -F= '{ print $2 }')
if [[ $? == 0 && -n $SHARED_VGS ]]
then
	SHARED_VGS=$(echo $SHARED_VGS)
else
	KLIB_SA_logmsg ERROR 1 65 tsmserversa.cat "No value is specified for VolumeGroups property in TSM server xml file\n"
	exit 1
fi

validateNodes $PRIMNODE "$TAKEOVNODES"
(( $? != 0 )) && {
KLIB_SA_logmsg ERROR 1 14 tsmserversa.cat "TSM server smart assist has primary=%1\$s and takeover nodes=%2\$s as repetitive\n" $PRIMNODE "$TAKEOVNODES"
exit 1
}

if [[ -z $NETMASK ]];then
	$TSM_SERVER_HA_SBIN/cl_tsm_serv_config -a $APPLICATION_ID -p $PRIMNODE -t "$TAKEOVNODES" -i $INSTANCE_ID -d $INSTANCE_DIR -v "$SHARED_VGS" -s $SERVICE_IP -u $USER
	exit $?
else
	$TSM_SERVER_HA_SBIN/cl_tsm_serv_config -a $APPLICATION_ID -p $PRIMNODE -t "$TAKEOVNODES" -i $INSTANCE_ID -d $INSTANCE_DIR -v "$SHARED_VGS" -s $SERVICE_IP -n $NETMASK -u $USER
	exit $?
fi
