#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 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/tds/sbin/startTDS.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,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/tds/sbin/startTDS.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
###############################################################################
#
# This is a wrapper script for starting up TDS
#
###############################################################################

HAWS_HOME=/usr/es/sbin/cluster/sa/tds
TDS_INSTALL_DIR=""
TDS_VG=""
TDS_DB_DIR=""
TDS_DB_VG=""

HAWS_HOME="/usr/es/sbin/cluster/sa/tds"
TMPFILE=$HAWS_HOME"/tds_config_type"

TDS_CONFIG_TYPE=`cat $TMPFILE`

TMP_FAILED_DS_INSTANCES_FILE=$HAWS_HOME"/failedDSinsances"
TMP_FAILED_ADM_INSTANCES_FILE=$HAWS_HOME"/failedADMinsances"

###############################################################################
# Function: init
###############################################################################
#
# Initialize this script
###############################################################################
init() {
    # The standard directory for the HAWS software is in
    # /usr/es/sbin/cluster/sa/tds. But this can be changed by setting the
    # environment string HAWS_HOME. 
    if [[ "$HAWS_HOME" = "" ]]; then
        HAWS_HOME=/usr/es/sbin/cluster/sa/tds
    fi

    # Source function library. This is standard for all scripts
    clhaws_functions=$HAWS_HOME/sbin/clhaws_functions
    if [[ ! -f  "$clhaws_functions" ]]; then
        echo "The file '$clhaws_functions' is missing! Unable to continue. Bye"
        exit 1
    fi
    . $clhaws_functions

    # We now call into the generic initialization routine. This will
    # complete the initialization process
    generic_init

    # once the above clhaws_functions complete, we have all our variables and 
    # functions defined. We can now safely log messages and begin processing.
    logmsg HAWS_TRACE "$MSG_BEGIN" "Begin\n"

}

###############################################################################
# Function: start_tds
###############################################################################
#
# Start TDS
###############################################################################
start_tds() {
	logmsg HAWS_TRACE $MSG_TDS_START "Starting TDS...\n"

	instance=$1
	if [[ ! -f  "$TMP_FAILED_ADM_INSTANCES_FILE" && ! -f  "$TMP_FAILED_DS_INSTANCES_FILE" ]]; then
		if [[ -z $instance || "$instance" = "" ]]; then
			typeset NUM_INSTANCES
	        eval $(clquerysaapp -a $APPLICATION_ID NUM_INSTANCES)
			let NUM_INSTANCES=$NUM_INSTANCES+1
   		    i=1
			maxnum=$NUM_INSTANCES
	   		while [[ $i -lt $maxnum ]];
	 		do
        		INSTANCE="INSTANCE"$i
        		echo $INSTANCE
        		echo $APPLICATION_ID
        		echo `clquerysaapp -a $APPLICATION_ID $INSTANCE` | cut -d'=' -f2 | read instance
        		echo $instance
        		cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$instance".cfg"
	       		. $cfg_script
			cat $TDS_DB_DIR/sqllib/db2nodes.cfg |grep `hostname`
			if (( $? != 0 )); then
				echo "0 `hostname` 0" > $TDS_DB_DIR/sqllib/db2nodes.cfg
			fi

				$TDS_START_DIR_SERVER -I $instance
				$TDS_START_ADM_SERVER -I $instance
				let i=$i+1
			done
		else
		cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$instance".cfg"
			. $cfg_script
			cat $TDS_DB_DIR/sqllib/db2nodes.cfg |grep `hostname`
			if (( $? != 0 )); then
				echo "0 `hostname` 0" > $TDS_DB_DIR/sqllib/db2nodes.cfg
			fi
			$TDS_START_DIR_SERVER -I $instance
			$TDS_START_ADM_SERVER -I $instance
		fi

	else
    	while read line
	cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$line".cfg"
	. $cfg_script
    	do
		cat $TDS_DB_DIR/sqllib/db2nodes.cfg |grep `hostname`
		if (( $? != 0 )); then
			echo "0 `hostname` 0" > $TDS_DB_DIR/sqllib/db2nodes.cfg
		fi

			$TDS_START_DIR_SERVER -I $line
		done<"$TMP_FAILED_DS_INSTANCES_FILE"
		rm -f $TMP_FAILED_DS_INSTANCES_FILE

		while read line
   		do
			$TDS_START_ADM_SERVER -I $line
    	done<"$TMP_FAILED_ADM_INSTANCES_FILE"
		rm -f $TMP_FAILED_ADM_INSTANCES_FILE
	fi

    if [[ $TDS_VERSION < 6.4 ]];then
        logmsg HAWS_TRACE $MSG_TDS_WEB_START "Starting TDS WebSphere...\n"
        $TDS_START_WEB_SERVER
    fi

}


###############################################################################
# Function: read_config
###############################################################################
#
# Read our configuration file.  Set up the command lines to start TDS
#
# 
###############################################################################
read_config() {

	cfgfile=$HAWS_HOME"/config/HAWS_CFG_tds.cfg"
    logmsg HAWS_TRACE $MSG_READING_CONFIG "Reading configuration file: %s\n" $cfgfile
   	. $cfgfile

    TDS_START_DIR_SERVER="$TDS_INSTALL_DIR/sbin/ibmslapd"
	TDS_START_ADM_SERVER="$TDS_INSTALL_DIR/sbin/ibmdiradm"
    if [[ $TDS_VERSION < 6.4 ]];then
        TDS_START_WEB_SERVER="$WAS_PROFILE_DIR/../bin/startServer.sh $WAS_SERVER_NAME"
    fi
}

#
###############################################################################
#
# SCRIPT EXECUTION SECTION
#
###############################################################################
# This section of the script is used to call into the various predefined
# functions composed of the common code, and the script-specific functions.
#
# The intent of this section is to provide a high-level view of how this
# script operates.
###############################################################################
umask -S u=rw,g=,o=

PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../../utilities/cl_get_path all)"
PATH=/usr/bin:$PATH:/etc:/usr/sbin:/usr/ucb:/sbin:/usr/es/sbin/cluster/sa/sbin
FPATH_BASE=/usr/es/lib/ksh93
FPATH=$FPATH_BASE/hacmp:$FPATH_BASE/sa:$FPATH_BASE/aix:$FPATH_BASE/aix/odm
export PATH FPATH

HACMP_HOME=/usr/es/sbin/cluster
PRIMARYNODE=$($HACMP_HOME/utilities/get_local_nodename)

init
read_config
start_tds $*
exit $HAWS_EXIT_SUCCESS
