#!/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/import.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/import.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
###############################################################################
#
# This is an application-specific import script that reads the
# TDS configuration files and translates it into HACMP specific 
# commands. The output of this script is another script that can be 
# called to perform the actual import into HACMP. 
###############################################################################

# TDS Package name
#TDS_PACKAGE_NAME="IBMDirectoryProduct"
TDS_PACKAGE_NAME="idsldap"

# Version 5.1 TDS
TDS_VERSION_51="5.1"

# Version 5.2 TDS
TDS_VERSION_52="5.2"

# Version 6.0 TDS
TDS_VERSION_60="6.0"

# Version 6.1 TDS
TDS_VERSION_61="6.1"

# Version 6.2 TDS
TDS_VERSION_62="6.2"

# Version 6.3 TDS
TDS_VERSION_63="6.3"

# Version 6.4 TDS
TDS_VERSION_64="6.4"

# Global variable for the TDS Version. We carry it around because
# some of the functions may need to do different things depending
# on the version.
TDS_VERSION=""

# The directory where TDS is installed
TDS_INSTAL_DIR="/opt/IBM/ldap/"
# The volume group where TDS is configured. We'll need to make sure
# this is made highly available.
TDS_VG=""

# Specifies the name of the node where TDS will fallover to
# to.
TDS_FALLOVER_NODE=""

TDS_SERVICE_LABEL=""

# Get TDS config type
TDS_CONFIG_TYPE=""

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

TMP_INSTANCE_LIST_FILE=$HAWS_HOME"/instancelist"
TDS_DB_LOCATION_ID="ibm-slapdDbLocation"
TDS_DB_DIR=""
TDS_DB_VG=""
TDS_CONF_FILE=""

WAS_CELL=""
WAS_NODE=""
WAS_SERVER_NAME=""
WAS_INSTALL_DIR=""
WAS_PROFILE_DIR=""

TDSSACAT="tdssa.cat"
IMPORT_MSGSET=8

HAWS_HOME=/usr/es/sbin/cluster/sa/tds

APPLICATION_ID=""
#
# Path to the XML Configuration file
#
MANUAL_CONFIG_FILE=""

#
###############################################################################
#
# SCRIPT DEFINITION SECTION
#
###############################################################################
# Define Functions
###############################################################################

###############################################################################
# Function: verify_tds_version
###############################################################################
#
# Make sure that tds is at one of the supported versions
###############################################################################
verify_tds_version() {
    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "Checking TDS version...\n"
    version=`lslpp -Lc | grep $TDS_PACKAGE_NAME 2>/dev/null | sed -e '/^#/d' | cut -f3 -d':' | cut -f 1,2 -d '.'`
	TDS_VERSION=`echo $version | awk '{print $1}'`
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "TDS Version = $TDS_VERSION\n"
   
    case $TDS_VERSION in
	$TDS_VERSION_60)
		logmsg HAWS_INFO "$MSG_TDS_VER51_DETECTED" "IBM TDS v6.0 detected\n"
		;;
	$TDS_VERSION_61)
		logmsg HAWS_INFO "$MSG_TDS_VER51_DETECTED" "IBM TDS v6.1 detected\n"
		;;
	$TDS_VERSION_62)
		logmsg HAWS_INFO "$MSG_TDS_VER51_DETECTED" "IBM TDS v6.2 detected\n"
		;;
	$TDS_VERSION_63)
	    logmsg HAWS_INFO "$MSG_TDS_VER51_DETECTED" "IBM TDS v6.3 detected\n"
	    ;;
	$TDS_VERSION_64)
	    logmsg HAWS_INFO "$MSG_TDS_VER51_DETECTED" "IBM TDS v6.4 detected\n"
	    ;;
	"")
	    logmsg HAWS_ERROR "$MSG_TDS_NOT_INSTALLED" "IBM TDS could not be found.  Please install IBM TDS v6.4\n"
		exit $HAWS_EXIT_FAIL
	    ;;
	*)
	    logmsg HAWS_ERROR "$MSG_TDS_UNKNOWN_VERSION" "Unsupported version of IBM TDS: %s. Please install IBM TDS v6.4\n" $TDS_VERSION
		exit $HAWS_EXIT_FAIL

    esac
}

###############################################################################
# Function: get_tds_ewas_install_dir
###############################################################################
#
# Get the location where eWAS is installed.
###############################################################################
get_tds_ewas_install_dir() {
	TDS_INSTALL_DIR="$TDS_INSTAL_DIR""V""$TDS_VERSION"
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Directory = $TDS_INSTALL_DIR"

	WAS_INSTALL_DIR="$TDS_INSTALL_DIR""/appsrv"
    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "WAS Install Directory = $WAS_INSTALL_DIR"
}

###############################################################################
# Function: get_tds_conf_path
###############################################################################
#
# Get the location of TDS instance conf file. 
###############################################################################
get_tds_conf_path() {
#    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting TDS installation directory..."
#    TDS_INSTALL_DIR=`lslpp -Lc $TDS_PACKAGE_NAME 2>/dev/null | sed -e '/^#/d' | cut -f9 -d':'`

	configpath=$1
    TDS_CONF_FILE="$1""/ibmslapd.conf"
    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "TDS config file = $TDS_CONF_FILE"
}

###############################################################################
# Function: parse_tds_config
###############################################################################
#
# Get the TDS Admin and TDS Database Directory
###############################################################################
parse_tds_config() {

    TDS_ADMIN=`grep '^ibm-slapdAdminDN:' $TDS_CONF_FILE | awk '{print $2}' | sed 's/cn=//g'`

	if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then
    	TDS_PORT=`grep 'ibm-slapdPort:' $TDS_CONF_FILE | awk '{print $2}' | sed 's/cn=//g' | head -1`
    	TDS_DB_USER=`grep 'ibm-slapdDbInstance:' $TDS_CONF_FILE | awk '{print $2}' | sed 's/cn=//g'`
	fi

    TDS_DB_DIR=`grep $TDS_DB_LOCATION_ID $TDS_CONF_FILE | cut -f2 -d':' | sed -e 's/ //g'`
    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "DB Directory = $TDS_DB_DIR"

}

###############################################################################
# Function: get_tds_vg
###############################################################################
#
# Gets the volume group where the TDS configuration files exist.
###############################################################################
get_tds_vg() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting the vg for TDS...\n"
    VG=""
	
		TDS_HOME_DIR="$1"
		if [[ -d "$TDS_HOME_DIR" ]]; then
			findvg "$TDS_HOME_DIR"
    		if [[ $? -eq HAWS_EXIT_FAIL ]]; then
        		exit $HAWS_EXIT_FAIL
    		fi
		fi

	TDS_VG=$VG
    if [[ "$TDS_VG" = "" ]] ; then
	logmsg HAWS_ERROR "$MSG_TDS_UKNOWN_VG" "Can't get the volume group for TDS directory: %s\n" $TDS_INSTALL_DIR
    else
	logmsg HAWS_INFO  "$MSG_TDS_VG_FOUND" "TDS Volume group = %s\n" $TDS_VG
    fi
}

###############################################################################
# Function: get_tds_db_vg
###############################################################################
#
# Gets the volume group where the TDS database configuration files exist.
###############################################################################
get_tds_db_vg() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting the vg for TDS...\n"
    VG=""
    if [[ -d "$TDS_DB_DIR" ]] ; then
	findvg $TDS_DB_DIR
	if [[ $? -eq HAWS_EXIT_FAIL ]]; then
	    exit $HAWS_EXIT_FAIL
	fi
	TDS_DB_VG=$VG
    fi
    if [[ "$TDS_DB_VG" = "" ]] ; then
	logmsg HAWS_ERROR "$MSG_TDS_DB_UKNOWN_VG" "Can't get the volume group for TDS database directory: %s\n" $TDS_DB_DIR
	exit $HAWS_EXIT_FAIL
    fi
    logmsg HAWS_INFO  "$MSG_TDS_DB_VG_FOUND" "TDS DB Volume group = %s\n" $TDS_DB_VG
}

###############################################################################
# Function: validate_tds_config
###############################################################################
#
# Validate the TDS configuration information against HACMP. Make
# sure it makes sense to import this stuff.
###############################################################################
validate_tds_config() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Validating TDS configuration..."

    IBMSLAPD_CONF=$TDS_INSTALL_DIR"/etc/ibmslapd.conf"
    local_sum=`sum $IBMSLAPD_CONF 2>/dev/null`
	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
		remote_sum=`cl_rsh $TDS_FALLOVER_NODE "sum $IBMSLAPD_CONF" 2>/dev/null`
	fi
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "$IBMSLAPD_CONF checksum - local: $local_sum    $TDS_FALLOVER_NODE: $remote_sum\n"
    if [[ $local_sum != $remote_sum ]] ; then
	logmsg HAWS_WARN "$MSG_TDS_WARN_IBMSLAPD_CONF_SUM" "The checksum of file %s (%s) does not match the checksum on node %s (%s).\n" $IBMSLAPD_CONF $local_sum $TDS_FALLOVER_NODE $remote_sum 
    fi

    # If not Validating, there is no need to do anything else.
    if [[ $VALIDATE_FLAG -ne 1 ]] ; then
	return
    fi

    # Check for the configuration file
    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking Configuration file $cfg_script"
    if [[ ! -f $cfg_script ]] ; then
	logmsg HAWS_ERROR $MSG_CONFIG_FILE_NOT_FOUND "The configuration file %s was not found.\n" $cfg_script
	return
    fi

    # Check the Configuration script for matching items
    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_INSTALL_DIR $TDS_INSTALL_DIR"
    if [[ -z $(grep -w "TDS_INSTALL_DIR=\"$TDS_INSTALL_DIR\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_INSTALL_DIR "The TDS Installation Directory %s does not match value in %s\n" $TDS_INSTALL_DIR $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_VG $TDS_VG"
    if [[ -z $(grep -w "TDS_VG=\"$TDS_VG\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_VG "The TDS Volume Group %s does not match value in %s\n" $TDS_VG $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_DB_DIR $TDS_DB_DIR"
    if [[ -z $(grep -w "TDS_DB_DIR=\"$TDS_DB_DIR\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_DB_DIR "The TDS Database Directory %s does not match value in %s\n" $TDS_DB_DIR $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_DB_VG $TDS_DB_VG"
    if [[ -z $(grep -w "TDS_DB_VG=\"$TDS_DB_VG\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_DB_VG "The TDS Database Volume Group %s does not match value in %s\n" $TDS_DB_VG $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_ADMIN $TDS_ADMIN"
    if [[ -z $(grep -w "TDS_ADMIN=\"$TDS_ADMIN\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_ADMIN "The TDS Administrator %s does not match value in %s\n" $TDS_ADMIN $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_PASSWORD"
    if [[ -z $(grep -w "TDS_PASSWORD=\"$TDS_PASSWORD\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_PASSWORD "The TDS Password does not match value in %s\n" $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_PORT $TDS_PORT"
    if [[ -z $(grep -w "TDS_PORT=\"$TDS_PORT\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_PORT "The TDS Port %s does not match value in %s\n" $TDS_PORT $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_DB_USER $TDS_DB_USER"
    if [[ -z $(grep -w "TDS_DB_USER=\"$TDS_DB_USER\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_DB_USER "The TDS Database User %s does not match value in %s\n" $TDS_DB_USER $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking TDS_CONF_FILE $TDS_CONF_FILE"
    if [[ -z $(grep -w "TDS_CONF_FILE=\"$TDS_CONF_FILE\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_TDS_NO_MATCH_CONF_FILE "The TDS Configuration File %s does not match value in %s\n" $TDS_CONF_FILE $cfg_script
    fi

    if [[ $TDS_VERSION < $TDS_VERSION_64 ]];then
        logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_CELL $WAS_CELL"
        if [[ -z $(grep -w "WAS_CELL=\"$WAS_CELL\"" $cfg_script) ]] ; then
	    logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_CELL "The WAS Cell  %s does not match value in %s\n" $WAS_CELL $cfg_script
        fi

        logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_NODE $WAS_NODE"
        if [[ -z $(grep -w "WAS_NODE=\"$WAS_NODE\"" $cfg_script) ]] ; then
	    logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_NODE "The WAS Node %s does not match value in %s\n" $WAS_NODE $cfg_script
        fi

        logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_SERVER_NAME $WAS_SERVER_NAME"
        if [[ -z $(grep -w "WAS_SERVER_NAME=\"$WAS_SERVER_NAME\"" $cfg_script) ]] ; then
	    logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_SERVER_NAME "The WAS Server Name  %s does not match value in %s\n" $WAS_SERVER_NAME $cfg_script
        fi

        logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_INSTALL_DIR $WAS_INSTALL_DIR"
        if [[ -z $(grep -w "WAS_INSTALL_DIR=\"$WAS_INSTALL_DIR\"" $cfg_script) ]] ; then
	    logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_INSTALL_DIR "The WAS Installation Directory %s does not match value in %s\n" $WAS_INSTALL_DIR $cfg_script
        fi
    fi
}

###############################################################################
# Function: generate_import_script
###############################################################################
#
# Write the script that will be used to import the TDS configuration
# information into HACMP.
###############################################################################
generate_import_script() {

    # Setup the variable names here
    tds_start_script="$HAWS_HOME/sbin/startTDS"
    tds_stop_script="$HAWS_HOME/sbin/stopTDS"
    tds_monitor_script="$HAWS_HOME/sbin/monitor_tds"

    # This is the script that does the actual import
    logmsg HAWS_TRACE  "$MSG_CREATE_IMPORT" "Generating the import script...\n"

    echo "#!/usr/bin/ksh93" > $create_script
    echo "# Script Automatically created on `date`" >> $create_script
    echo "# Any changes made to this file will be lost next time" >> $create_script
    echo "# the IBM Directory Server configuration information is imported" >> $create_script
    echo " " >> $create_script
    echo ". $clhaws_functions" >> $create_script
    echo " " >> $create_script

    echo "FPATH_BASE=/usr/es/lib/ksh93" >> $create_script
    echo "FPATH=\$FPATH_BASE/hacmp:\$FPATH_BASE/sa:\$FPATH_BASE/aix:\$FPATH_BASE/aix/odm" >> $create_script
    echo "export FPATH" >> $create_script
    echo " " >> $create_script

    echo "get_cluster_state" >> $create_script
    echo "if [[ \$CLUSTER_STATE -ne 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_CLUSTER_NOT_STABLE \"Cluster is not in stable state.\\\\n\"" >> $create_script
    echo "exit 1" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script

	####This section is not required for Distributed/ Peer to peer
	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
    echo "# add service label to PowerHA SystemMirror" >> $create_script
    echo "logmsg HAWS_INFO $MSG_TDS_SERVICE_ADD \"Creating Service label %s.\\\\n\" \"$TDS_SERVICE_LABEL\"" >> $create_script
    echo "KLIB_SA_add_ServiceLabel $TDS_SERVICE_LABEL $TDS_SERVICE_LABEL_PREFIX" >> $create_script
    echo "if (( \$? != 0 )); then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_TDS_SERVICE_ADD_FAIL \"ERROR: Failed creating PowerHA SystemMirror Service IP Label %s.\\\\n\" \"$TDS_SERVICE_LABEL\"" >> $create_script
    echo "return 1" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script
	fi
	
	
	echo "# Check the application server " >> $create_script
    echo "$CLLSSERV -n ${asname} >/dev/null 2>&1" >> $create_script
    echo "if [[ \$? -eq 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR  $MSG_ASNAME_EXISTS \"The Application Server name %s already exists in the cluster.\\\\n\"" $asname >> $create_script
    echo "return 2" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script
	
    echo "# Create the application server " >> $create_script
	if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then
    	echo "$CLADDSERV -s'$asname' -b'$tds_start_script' -e'$tds_stop_script'" >> $create_script
	else
		echo "$CLADDSERV -s'$asname' -b'$tds_start_script $TDS_INSTANCE_NAME' -e'$tds_stop_script $TDS_INSTANCE_NAME'" >> $create_script
	fi
    echo "if [[ \$? -ne 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_AS_CREATE_FAILED \"Application Server Creation Failed.\\\\n\"" >> $create_script
    echo "return 1" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script

    echo "logmsg HAWS_INFO $MSG_APP_SERVER_CREATED \"Application Server %s created.\\\\n\" \"$asname\"" >> $create_script
    echo " " >> $create_script


    echo "# Check the resource group " >> $create_script
    echo "$CLLSGRP 2>/dev/null | grep $rgname > /dev/null 2>&1" >> $create_script
    echo "if [[ \$? -eq 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR  $MSG_RGNAME_EXISTS \"The Resource Group name %s already exists in the cluster.\\\\n\"" $rgname >> $create_script
    echo "return 2" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script
    echo "# Create the resource group " >> $create_script
	if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then
    echo "$CLADDGRP -g '$rgname' -s 'ignore' -n '$PRIMARYNODE' -S 'OAAN' -O 'BO' -B 'NFB' -K $APPLICATION_ID" >> $create_script
	else 
	echo "$CLADDGRP -g '$rgname' -s 'ignore' -n '$PRIMARYNODE $TDS_FALLOVER_NODE' -S 'OHN' -O 'FNPN' -B 'NFB' -K $APPLICATION_ID" >> $create_script
	fi
    echo "if [[ \$? -ne 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_RG_CREATE_FAILED \"Resource Group Creation Failed.\\\\n\"" >> $create_script
    echo "return 1" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script

    echo "logmsg HAWS_INFO $MSG_RESOURCE_GROUP_CREATED \"Resource Group %s created.\\\\n\" \"$rgname\"" >> $create_script
    echo " " >> $create_script


    echo "# Check the Application Monitor " >> $create_script
    echo "$CLLSAPPMON -c ${asname} >/dev/null 2>&1" >> $create_script
    echo "if [[ \$? -eq 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_ASMON_EXISTS \"The Application Monitor %s already exists in the cluster.\\\\n\"" ${asname} >> $create_script
    echo "return 2" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script

    echo "# Create the application monitor" >> $create_script
	
	if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then
		echo "$CLADDAPPMON MONITOR_TYPE=user name='$asname' INVOCATION='$mon_mode' RESOURCE_TO_MONITOR='$asname' MONITOR_METHOD='$app_monitor_script' INVOCATION='longrunning' MONITOR_INTERVAL='120' STABILIZATION_INTERVAL='420' RESTART_COUNT='3' RESTART_INTERVAL='1800' CLEANUP_METHOD='$tds_stop_script' RESTART_METHOD='$tds_start_script' HUNG_MONITOR_SIGNAL='9' INSTANCE_COUNT='$NUM_INSTANCES'" >> $create_script
	else
    	echo "$CLADDAPPMON MONITOR_TYPE=user name='$asname' INVOCATION='$mon_mode' RESOURCE_TO_MONITOR='$asname' MONITOR_METHOD='$app_monitor_script' INVOCATION='longrunning' MONITOR_INTERVAL='120' STABILIZATION_INTERVAL='420' RESTART_COUNT='3' RESTART_INTERVAL='1800' FAILURE_ACTION='fallover' CLEANUP_METHOD='$tds_stop_script $TDS_INSTANCE_NAME' RESTART_METHOD='$tds_start_script $TDS_INSTANCE_NAME' HUNG_MONITOR_SIGNAL='9'" >> $create_script
    fi

	echo "if [[ \$? -ne 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_ASMON_CREATE_FAILED \"Application Monitor Creation Failed.\\\\n\"" >> $create_script
    echo "return 1" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script

    echo "logmsg HAWS_INFO $MSG_APP_MON_CREATED \"Application Monitor for application server %s created.\\\\n\" \"$asname\"" >> $create_script
    echo " " >> $create_script

	echo "# Check the Resource to Monitor" >> $create_script
    echo "$CLLSRES -cg ${rgname} | grep -v \"\\#\" | IFS=: read tmp tmp tmp tmp tmp tmp tmp tmp tmp tmp tmp tmp name tmp" >> $create_script
    echo "if (( \${#name} > 0 )) ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_RGMON_EXISTS \"The Resource Monitor %s already exists in the cluster.\\\\n\"" ${rgname} >> $create_script
    echo "return 2" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script
    
	echo "# Create the resource to monitor now" >> $create_script
	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
    echo "$CLADDRES -g '$rgname' APPLICATIONS='$asname' VOLUME_GROUP='$TDS_DB_VG' VG_AUTO_IMPORT='true' RECOVERY_METHOD='parallel' SERVICE_LABEL='$TDS_SERVICE_LABEL'" >> $create_script
    else
	echo "$CLADDRES -g '$rgname' APPLICATIONS='$asname'" >> $create_script
	fi
	echo "if [[ \$? -ne 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_RGMON_CREATE_FAILED \"Adding resources to resource group failed.\\\\n\"" >> $create_script
    echo "return 1" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script

    echo "logmsg HAWS_INFO $MSG_RESOURCES_CREATED \"Resources added to resource group %s.\\\\n\" \"$rgname\"" >> $create_script
    echo " " >> $create_script

    echo "logmsg HAWS_INFO $MSG_IMPORT_CREATE_COMPLETE \"%s import create complete.\\\\n\" TDS" >> $create_script
    echo " " >> $create_script

    # This is the script that backs out the changes
    logmsg HAWS_TRACE  "$MSG_CREATE_REMOVE" "Generating the remove script...\n"

    echo "#!/bin/sh" > $delete_script
    echo "# Script Automatically created on `date`" >> $delete_script
    echo "# Any changes made to this file will be lost next time" >> $delete_script
    echo "# the IBM Directory Server configuration information is imported" >> $delete_script
    echo " " >> $delete_script
    echo ". $clhaws_functions" >> $delete_script
    echo " " >> $delete_script

    echo "get_cluster_state" >> $delete_script
    echo "if [[ \$CLUSTER_STATE -ne 0 ]] ; then" >> $delete_script
    echo "logmsg HAWS_ERROR $MSG_CLUSTER_NOT_STABLE \"Cluster is not in stable state.\\\\n\"" >> $delete_script
    echo "exit 1" >> $delete_script
    echo "fi" >> $delete_script
    echo " " >> $delete_script

    echo "# Remove the resources " >> $delete_script
    echo "$CLRMRES -g '$rgname'" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_RESOURCES_DELETED \"Resources removed to resource group %s.\\\\n\" \"$rgname\"" >> $delete_script
    echo " " >> $delete_script

	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
    echo "# Delete Service IP Label from PowerHA SystemMirror " >> $delete_script
    echo "logmsg HAWS_INFO $MSG_TDS_SERVICE_REMOVE \"Removing Service IP label %s from PowerHA SystemMirror.\\\\n\" \"$TDS_SERVICE_LABEL\"" >> $delete_script
    echo "clrmnode -a \"$TDS_SERVICE_LABEL\"" >> $delete_script
    echo "if (( \$? != 0 )); then" >> $delete_script
    echo "logmsg HAWS_ERROR $MSG_TDS_SERVICE_REMOVE_FAIL \"ERROR: Failed removing Service IP Label %s from PowerHA SystemMirror.\\\\n\" \"$TDS_SERVICE_LABEL\"" >> $delete_script
    echo "fi" >> $delete_script
    echo " " >> $delete_script
	fi

    echo "# Remove the application monitor " >> $delete_script
    echo "$CLRMAPPMON $asname" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_APP_MON_DELETED \"Application Monitor for application server %s deleted.\\\\n\" \"$asname\"" >> $delete_script
    echo " " >> $delete_script

    echo "# Remove the application server " >> $delete_script
    echo "$CLRMSERV $asname" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_APP_SERVER_DELETED \"Application Server %s deleted.\\\\n\" \"$asname\"" >> $delete_script
    echo " " >> $delete_script

    echo "# Remove the resource group " >> $delete_script
    echo "$CLRMGRP -g $rgname" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_RESOURCE_GROUP_DELETED \"Resource Group %s deleted..\\\\n\" \"$rgname\"" >> $delete_script
    echo " " >> $delete_script

    echo "logmsg HAWS_INFO $MSG_IMPORT_DELETE_COMPLETE \"%s import delete complete.\\\\n\" TDS" >> $delete_script
    echo " " >> $delete_script

    # Create the configuration script where application specific settings are stored
    logmsg HAWS_TRACE  "$MSG_CREATE_CONFIG" "Generating config file...\n"

    echo "#!/bin/sh" > $cfg_script
    echo "# Script Automatically created on `date`" >> $cfg_script
    echo "# Any changes made to this file will be lost next time" >> $cfg_script
    echo "# the IBM Directory Server configuration information is imported" >> $cfg_script
    echo " " >> $cfg_script
    echo "# IBM Directory Server Configuration Information " >> $cfg_script
    echo " " >> $cfg_script
	echo "NUM_INSTANCES=\"$NUM_INSTANCES\"" >> $cfg_script
	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
	   	echo "TDS_SERVICE_LABEL=\"$TDS_SERVICE_LABEL\"" >> $cfg_script
	fi
    echo "TDS_INSTALL_DIR=\"$TDS_INSTALL_DIR\"" >> $cfg_script
    echo "TDS_PASSWORD=\"$TDS_PASSWORD\"" >> $cfg_script
    if [[ $TDS_VERSION < $TDS_VERSION_64 ]];then
	echo "WAS_INSTALL_DIR=\"$WAS_INSTALL_DIR\"" >> $cfg_script
	echo "APPLICATION_ID=\"$APPLICATION_ID\"" >> $cfg_script
	echo "WAS_PROFILE_DIR=\"$WAS_PROFILE_DIR\"" >> $cfg_script
	echo "WAS_SERVER_NAME=\"$WAS_SERVER_NAME\"" >> $cfg_script
    fi
    echo "#!/bin/sh" > $app_monitor_script
    echo "# Script Automatically created on `date`" >> $app_monitor_script
    echo "# Any changes made to this file will be lost next time" >> $app_monitor_script
    echo "# the IBM Directory Server configuration information is imported" >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "# IBM Directory Server monitor script " >> $app_monitor_script
    echo " " >> $app_monitor_script
#    echo "$tds_monitor_script \"$cfg_script\"" >> $app_monitor_script
	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
		echo "export INSTANCE_NAME=\"$TDS_INSTANCE_NAME\"" >> $app_monitor_script
	else
		echo "export INSTANCE_NAME=\"\"" >> $app_monitor_script
	fi
	echo "$tds_monitor_script " >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "exit \$?" >> $app_monitor_script

	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
    	complete_scripts $TDS_FALLOVER_NODE
	else
		chmod u+x $create_script
		chmod u+x $delete_script
		chmod 700 $cfg_script
		chmod u+x $app_monitor_script
	fi
}

###############################################################################
#
# Function: process_arguments
# This is a subsystem-specific version of the routine that processes
# command line arguments. 
#
###############################################################################
process_arguments() {
    logmsg HAWS_DEBUG "$MSG_TDS_PROCARG" "Processing 'tds' arguments...\n"

    while getopts :n:l:u:w:p:i:o:P:f:s:I:N:A: c 
      do 
        case $c in
        n) TDS_FALLOVER_NODE=$OPTARG
        	logmsg HAWS_INFO "$MSG_FALLOVER_NODE_NAME_SET" "Fallover Node specified as %s\n" $TDS_FALLOVER_NODE
	    	;;
	 	l) TDS_SERVICE_LABEL=$OPTARG
	    	logmsg HAWS_INFO "$MSG_SERVICE_LABEL_SET" "Service Label set to %s\n" $TDS_SERVICE_LABEL
	    	;;
	 	P) TDS_SERVICE_LABEL_PREFIX=$OPTARG
	    	logmsg HAWS_INFO "$MSG_SERVICE_LABEL_SET" "Service Label prefix set to %s\n" $TDS_SERVICE_LABEL
	    	;;
	 	w) TDS_PASSWORD=$OPTARG
	    	logmsg HAWS_INFO "$MSG_TDS_PASSWORD_SET" "TDS Password set.\n"
	    	;;
	 	p) TDS_PORT=$OPTARG
	    	logmsg HAWS_INFO "$MSG_TDS_PORT_SET" "TDS Port set to %s\n" $TDS_PORT
	    	;;
	 	i) TDS_DB_USER=$OPTARG
	    	logmsg HAWS_INFO "$MSG_TDS_DB_USER_SET" "TDS DB Admin user set to %s\n" $TDS_DB_USER
	    	;;
	 	I) NUM_INSTANCES=$OPTARG
			logmsg HAWS_INFO "$MSG_TDS_DB_USER_SET" "Number of TDS instances set to %s\n" $NUM_INSTANCES
			;;
	 	o) WAS_NODE=$OPTARG
	    	logmsg HAWS_INFO "$MSG_WAS_NODE_SET" "WAS Node set to %s\n" $WAS_NODE
	    	;;
        f)
            MANUAL_CONFIG_FILE=$OPTARG
            ;;

        s)
            PRIMARYNODE=$OPTARG
            ;;
		N)
			TDS_INSTANCE_NAME=$OPTARG
			;;
		A)
			APPLICATION_ID=$OPTARG
			;;
        \?)
	    logmsg HAWS_ERROR "$MSG_UNKNOWN_OPTION" "Unrecognized command line option specified\n"
            exit $HAWS_EXIT_FAIL;;   
         esac 
      done 
    logmsg HAWS_DEBUG "$MSG_TDS_PROCDONE" "Done processing 'tds' arguments...\n"

    if [[ -n $MANUAL_CONFIG_FILE ]]; then
        exit $HAWS_EXIT_SUCCESS
    fi

    status=0
	
	TDS_CONFIG_TYPE=`cat $TMPFILE`

	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
	
	    # Make sure that a fallover node was specified
    	if [[ "$TDS_FALLOVER_NODE" = "" ]] ; then
			logmsg HAWS_ERROR "$MSG_NO_FALLOVER" "You must specify a fallover node using the -n flag\n"
			status=1
    	fi

    	# Make sure that a service label was specified
    	if [[ "$TDS_SERVICE_LABEL" = "" ]] ; then
			logmsg HAWS_ERROR "$MSG_NO_SERVICE_LABEL" "You must specify a service label using the -l flag\n"
			status=1
    	fi
	
	    # Make sure that a tds port was specified
    	if [[ "$TDS_PORT" = "" ]] ; then
    		logmsg HAWS_ERROR "$MSG_TDS_NO_PORT" "You must specify a TDS port using the -p flag\n"
   			status=1
    	fi

		# Make sure that a tds db user was specified
    	if [[ "$TDS_DB_USER" = "" ]] ; then
        	logmsg HAWS_ERROR "$MSG_TDS_NO_DB_USER" "You must specify a TDS database  user using the -i flag\n"
        	status=1
    	fi
		
		# Make sure that a tds instance name is specified
		if [[ "$TDS_INSTANCE_NAME" = "" ]] ; then
			logmsg HAWS_ERROR "You must specify a TDS instance name using -N flag\n"
		fi
	fi

    # Make sure that a tds password was specified
    if [[ "$TDS_PASSWORD" = "" ]] ; then
	logmsg HAWS_ERROR "$MSG_TDS_NO_PASSWORD" "You must specify a TDS password using the -w flag\n"
	status=1
    fi

    if [[ status -ne 0 ]] ; then
	exit $HAWS_EXIT_FAIL
    fi
}


###############################################################################
# Function: remote_copy_tds_conf
###############################################################################
#
# Copy the TDS configuration file /etc/ibmslapd.conf to the fallover
# node.
###############################################################################
remote_copy_tds_conf() {

    tds_copy_status=0
    IBMSLAPD_CONF=$TDS_INSTALL_DIR"/etc/ibmslapd.conf"
    IBMSLAPD_CONF_HAWS=$TDS_INSTALL_DIR"/etc/ibmslapd.conf.haws"
    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "Saving $IBMSLAPD_CONF on node $TDS_FALLOVER_NODE to $IBMSLAPD_CONF_HAWS\n"
    cl_rsh $TDS_FALLOVER_NODE "if [[ -f $IBMSLAPD_CONF ]] ; then cp -fp $IBMSLAPD_CONF $IBMSLAPD_CONF_HAWS ; fi"  > /dev/null 2>&1
    if [[ $? -eq 0 ]] ; then
	logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "Copying $IBMSLAPD_CONF to node $TDS_FALLOVER_NODE\n"
	cl_rcp -F $IBMSLAPD_CONF $TDS_FALLOVER_NODE:$IBMSLAPD_CONF  > /dev/null 2>&1
	if [[ $? -ne 0 ]] ; then
	    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "Copy did not work\n"
	    tds_copy_status=1
	fi
    else
	logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "Save did not work\n"
	tds_copy_status=1
    fi

    if [[ $tds_copy_status -ne 0 ]] ; then
	logmsg HAWS_WARN "$MSG_TDS_WARN_CONF_COPY" "Cannot copy TDS conf file %s to node %s.\n" $IBMSLAPD_CONF $TDS_FALLOVER_NODE
    fi
}

###############################################################################
# Function: processConfigFile
###############################################################################
#
# This function reads the manual configuration file and setup the required
# environment to proceed further in setting up TDS in PowerHA environemnt 
#
###############################################################################
processConfigFile() {
	if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then
		create_script_names "tds" $PRIMARYNODE 
	fi
	create_script_names "tds" $PRIMARYNODE $TDS_FALLOVER_NODE

}

###############################################################################
# Function: create_config_file
###############################################################################
create_config_file() {
	conf_script=$1
    #Copy previous configuration script
    if [[ -f $conf_script ]]; then
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Copying previous script $conf_script\n"
    cp -fp $conf_script ${conf_script}.prev > /dev/null 2>&1
    fi

    echo "#!/bin/sh" > $conf_script
    echo "# Script Automatically created on `date`" >> $conf_script
    echo "# Any changes made to this file will be lost next time" >> $conf_script
    echo "# the IBM Directory Server configuration information is imported" >> $conf_script
    echo " " >> $conf_script
    echo "# IBM Directory Server Configuration Information " >> $conf_script
    echo " " >> $conf_script
   if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then
       echo "NUM_INSTANCES=\"$NUM_INSTANCES\"" >> $conf_script
   else
       echo "TDS_SERVICE_LABEL=\"$TDS_SERVICE_LABEL\"" >> $conf_script
   fi
    echo "TDS_INSTALL_DIR=\"$TDS_INSTALL_DIR\"" >> $conf_script
    echo "TDS_VG=\"$TDS_VG\"" >> $conf_script
    echo "TDS_DB_DIR=\"$TDS_DB_DIR\"" >> $conf_script
    echo "TDS_DB_VG=\"$TDS_DB_VG\"" >> $conf_script
    echo "TDS_ADMIN=\"$TDS_ADMIN\"" >> $conf_script
    echo "TDS_PASSWORD=\"$TDS_PASSWORD\"" >> $conf_script
    echo "TDS_PORT=\"$TDS_PORT\"" >> $conf_script
    echo "TDS_DB_USER=\"$TDS_DB_USER\"" >> $conf_script
    echo "TDS_CONF_FILE=\"$TDS_CONF_FILE\"" >> $conf_script
    echo "TDS_VERSION=\"$TDS_VERSION\"" >> $conf_script
    if [[ $TDS_VERSION < $TDS_VERSION_64 ]]; then
        echo "WAS_CELL=\"$WAS_CELL\"" >> $conf_script
        echo "WAS_NODE=\"$WAS_NODE\"" >> $conf_script
        echo "WAS_SERVER_NAME=\"$WAS_SERVER_NAME\"" >> $conf_script
        echo "WAS_INSTALL_DIR=\"$WAS_INSTALL_DIR\"" >> $conf_script
    fi
	echo "APPLICATION_ID=\"$APPLICATION_ID\"" >> $conf_script

	chmod 700 $conf_script

    if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
        cl_rcp $conf_script $TDS_FALLOVER_NODE:$conf_script > /dev/null 2>&1
    fi
}


###############################################################################
#
# SCRIPT EXECUTION SECTION
#
###############################################################################
# This section of the script is used to call into the various predefined
# to perform the actual script execution.
#
# The intent of this section is to provide a high-level view of how this
# script operates.
###############################################################################
process_arguments $*

set -A instanceList
set -A instanceHomeList

if [[ -n $MANUAL_CONFIG_FILE ]]; then
    processConfigFile
else
    verify_tds_version
    # Monitor websphere only in case of tds version less than 6.4
    if [[ $TDS_VERSION < $TDS_VERSION_64 ]];then
        # Check if the websphere server is running before configuring tds.
        if [[ -z $(ps -ef | grep $TDS_INSTALL_DIR/appsrv/profiles | grep -v grep) ]]; then
            dspmsg -s $IMPORT_MSGSET $TDSSACAT 712 "ERROR: Websphere server is not running.\n\
Start websphere server before configuring TDS to powerHA.\n" 1>&2
            exit 1
        fi
        get_tds_ewas_install_dir
        parse_websphere_config
        init_java
        get_server_names
    elif [[ -n $(lslpp -l | grep webadmin) ]];then
        dspmsg -s $IMPORT_MSGSET $TDSSACAT 711 "Websphere will be ignored if the tds version is greater than or equal to 6.4.\n" 1>&2
    fi

	if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then
		create_script_names "tds" $PRIMARYNODE "" ""
		get_instance_list
		i=1
		let NUM_INSTANCES=${NUM_INSTANCES}+1
		
		while [[ $i -lt $NUM_INSTANCES ]];
		do
			confpath=${instanceHomeList[$i]}"/idsslapd-"${instanceList[$i]}"/etc"
			conf_script=$HAWS_HOME"/config/HAWS_CFG_tds_"${instanceList[$i]}".cfg"

			get_tds_conf_path $confpath
			parse_tds_config
			get_tds_vg ${instanceHomeList[$i]}
			get_tds_db_vg
			create_config_file $conf_script
			let i=${i}+1
		done
		let NUM_INSTANCES=${NUM_INSTANCES}-1		
	else
		instanceHome=""
		get_instance_home $TDS_INSTANCE_NAME
		confpath=$instanceHome"/idsslapd-"$TDS_INSTANCE_NAME"/etc"
    	get_tds_conf_path $confpath
    	parse_tds_config
    	get_tds_vg $instanceHome
    	get_tds_db_vg
	
		confpath=$instanceHome"/idsslapd-"$TDS_INSTANCE_NAME"/etc"
        conf_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$TDS_INSTANCE_NAME".cfg"
		create_config_file $conf_script
		tmp_confScript=$HAWS_HOME"/config/HAWS_CFG_tds.cfg"
    	create_script_names "tds" $PRIMARYNODE $TDS_FALLOVER_NODE $TDS_INSTANCE_NAME
	fi
fi

validate_tds_config

# If not Validating, generate the scripts
if [[ $VALIDATE_FLAG -ne 1 ]] ; then
	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]; then
		remote_copy_tds_conf
	fi
    generate_import_script
fi
