#!/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_r720 src/43haes/usr/sbin/cluster/sa/tds/sbin/stopTDS.sh 1.2.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/stopTDS.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
###############################################################################
#
# This is a wrapper script for stopping TDS
#
###############################################################################

TDS_INSTALL_DIR=""
TDS_VG=""
TDS_DB_DIR=""
TDS_DB_VG=""

TDS_ADMIN=""
TDS_PASSWORD=""
TDS_PORT=""
TDS_DB_USER=""

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() {
    [[ $VERBOSE_LOGGING == "high" ]] && set -x

    # 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 [[ -z $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 [[ ! -s  $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: stop_tds
###############################################################################
#
# Stop TDS
###############################################################################
stop_tds() {
    [[ $VERBOSE_LOGGING == "high" ]] && set -x

    if [[ -f  "$TMP_FAILED_ADM_INSTANCES_FILE" ]]; then
        return
    fi

    if [[ -f  "$TMP_FAILED_DS_INSTANCES_FILE" ]]; then
	while read line
	do
	    cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$line".cfg"
	    . $cfg_script

            /usr/bin/su - $TDS_DB_USER -c "db2 force application all"
            /usr/bin/su - $TDS_DB_USER -c "db2stop"
            /usr/bin/su - $TDS_DB_USER -c "db2 terminate"
        done<"$TMP_FAILED_DS_INSTANCES_FILE"
        return
    fi

    instance=$1
    if [[ -z $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 < $maxnum ))
	do
	    INSTANCE="INSTANCE"$i
	    clquerysaapp -a $APPLICATION_ID $INSTANCE | cut -d'=' -f2 | read instance
	    cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$instance".cfg"
	    logmsg HAWS_TRACE $MSG_READING_CONFIG "Reading configuration file: %s\n" $cfg_script
	    . $cfg_script
	
	    logmsg HAWS_TRACE $MSG_TDS_STOP "Stopping TDS...\n"

	    $TDS_STOP_DIR_SERVER -k -I "$instance"
	    $TDS_STOP_ADM_SERVER -k -I "$instance"

	    logmsg HAWS_TRACE $MSG_TDS_DB_STOP "Stopping TDS...\n"
	    /usr/bin/su - $TDS_DB_USER -c "db2 force application all"
	    /usr/bin/su - $TDS_DB_USER -c "db2stop"
	    /usr/bin/su - $TDS_DB_USER -c "db2 terminate"
	    (( i++ ))
	done
    else
	cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$instance".cfg"
	logmsg HAWS_TRACE $MSG_READING_CONFIG "Reading configuration file: %s\n" $cfg_script
	. $cfg_script

	$TDS_STOP_DIR_SERVER -k -I "$instance"
	$TDS_STOP_ADM_SERVER -k -I "$instance"

	logmsg HAWS_TRACE $MSG_TDS_DB_STOP "Stopping TDS...\n"
	/usr/bin/su - $TDS_DB_USER -c "db2 force application all"
	/usr/bin/su - $TDS_DB_USER -c "db2stop"
	/usr/bin/su - $TDS_DB_USER -c "db2 terminate"
    fi

    if [[ $TDS_VERSION < 6.4 ]];then
        logmsg HAWS_TRACE $MSG_TDS_WEB_STOP "Stopping TDS...\n"
        $TDS_STOP_WEB_SERVER
    fi
}


###############################################################################
# Function: read_config
###############################################################################
#
# Read our configuration file. Set up the command lines to stop TDS.
#
# 
###############################################################################
read_config() {
    [[ $VERBOSE_LOGGING == "high" ]] && set -x

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

    TDS_STOP_DIR_SERVER="$TDS_INSTALL_DIR/sbin/ibmslapd" 
    TDS_STOP_ADM_SERVER="$TDS_INSTALL_DIR/sbin/ibmdiradm"
    if [[ $TDS_VERSION < 6.4 ]];then
        TDS_STOP_WEB_SERVER="$WAS_PROFILE_DIR/../bin/stopServer.sh $WAS_SERVER_NAME"
    fi

#    TDS_STOP_SERVER="$TDS_INSTALL_DIR/bin/ibmdirctl -D cn=$TDS_ADMIN -w $TDS_PASSWORD -p $TDS_PORT stop"
#    TDS_STOP_WEB_SERVER="$WAS_INSTALL_DIR/bin/stopServer.sh $WAS_SERVER_NAME"
}

#
###############################################################################
#
# 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=${0##*/}
PATH=$PATH:/usr/es/sbin/cluster/sa/sbin
PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)
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)
if [[ $VERBOSE_LOGGING == "high" ]]
then
    set -x
    version='1.2.1.1'
fi
init
read_config 
stop_tds $*
exit $HAWS_EXIT_SUCCESS
