#!/usr/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/tds/sbin/cl_gettds.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 
# @(#)45	1.1  src/43haes/usr/sbin/cluster/sa/tds/sbin/cl_gettds.sh, hacmp, 61haes_r714 11/28/11 14:59:04
###############################################################################
##
## NAME:	cl_gettds
##
## DESCRIPTION:	
##             This retrieves the information regarding existing
## Tivoli Directory Server HACMP resources
## when called with -n option it retrieves only primary node.
## when called with -o option it retrieves all the information.
##
## SYNTAX:	
##    cl_gettds -n
##    cl_gettds -o
##
## EXIT CODES:	
##    0 - success
##    1 - failure
##
## KLIB Functions:
##    KLIB_SA_get_metadata
##
##
###############################################################################

###############################################################################
#
# Name: usage
#
# Prints usage message and exits the program.
#
# Arguments:  none
#
# Returns:    does not return
#
################################################################################
usage() {
    set +u
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    set -u

    dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 650 "Usage:\ncl_gettds [-o | -n]\n"
    exit 1
}


# Load the common functions, logmsg, dbgmsg, errmsg, abort
. /usr/es/lib/ksh93/common_functions.ksh93

typeset _SHPATH=/usr/es/sbin/cluster/sa/sbin/
. $_SHPATH/saapp_common


PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../../utilities/cl_get_path all)"
PATH=$PATH:/usr/bin:/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

[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

TDSSA_HOME="/usr/es/sbin/cluster/sa/tds"
TDSSA_ETC_PATH="$TDSSA_HOME/etc"
TDSSA_SBIN_DIR="$TDSSA_HOME/sbin"
HAWS_HOME="/usr/es/sbin/cluster/sa/tds"

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

KLIB_MSGCATALOG="tdssa.cat"
KLIB_MSGSET=7

#Get the arguments
if set -- $(getopt A:no $* 2>&1); then

    while [ $# != 0 ]; do
	case "$1" in

        -A)
            APPLICATION_NAME=$2
            shift
            ;;

	    -n)

		USEHACMPODM="FALSE"
		;;

	    -o)

		USEHACMPODM="TRUE"
		;;

	    --)
		shift
		break
		;;
	esac
	shift
    done
fi

if [[ -z $USEHACMPODM ]]; then
    usage
fi

# primary node is where TDS is running
typeset primary=`get_local_nodename`


# Get the primary node only
if [[ "$USEHACMPODM" == "FALSE" ]]; then
    # Default Application Id
    typeset appid="TDS_"$primary
    
    echo "#applicationname:primary"
    echo "$appid:$primary"
    exit 0
fi

echo `clquerysaapp -a $APPLICATION_NAME "TDS_INSTANCE_NAME"` | cut -d'=' -f2 | read instance

# Get the information from metadata odm for the specified cell and websphere node.
if [[ "$USEHACMPODM" == "TRUE" ]]; then

	if [[ "$TDS_CONFIG_TYPE" = "DISTRIBUTED"  ||  "$TDS_CONFIG_TYPE" = "PEERTOPEER" ]]; then

    # Get tds information from the config file
    config_file="$HAWS_HOME/config/HAWS_CFG_tds_$instance.cfg"
    if [[ -f $config_file ]]; then
        . $config_file
    else
        #errr: config file doesn't exist.
        dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 670 "ERROR: Config file %s for Tivoli Directory Server does not exist.\n" $config_file
        exit 1
    fi

    echo "#applicationname:primary:num_instances"
    echo "$APPLICATION_NAME:$primary:$NUM_INSTANCES"

	else
    typeset TDS_TAKEOVER_NODE_NAME
    typeset TDS_SERVICE_LABEL
    set -a
        eval $(clquerysaapp -a $APPLICATION_NAME TDS_TAKEOVER_NODE_NAME)
        eval $(clquerysaapp -a $APPLICATION_NAME TDS_SERVICE_LABEL)
    set +a

    if [[ -z $TDS_TAKEOVER_NODE_NAME || -z $TDS_SERVICE_LABEL ]] ; then
	    dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 660 "WARNING: Tivoli Directory Server PowerHA SystemMirror Resources are not configured.\n"
	    exit 1
    fi

    # Get tds information from the config file
    config_file="$HAWS_HOME/config/HAWS_CFG_tds_$instance.cfg"
    if [[ -f $config_file ]]; then
	    . $config_file
    else
        #errr: config file doesn't exist.
	    dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 670 "ERROR: Config file %s for Tivoli Directory Server does not exist.\n" $config_file
	    exit 1
    fi

    echo "#applicationname:primary:secondary:serviceip:tdspassword:tdsport:tdsdbinstance:tdsinstance"
    echo "$APPLICATION_NAME:$primary:$TDS_TAKEOVER_NODE_NAME:$TDS_SERVICE_LABEL::$TDS_PORT:$TDS_DB_USER:$TDS_INSTANCE_NAME"
    fi
#    exit 0
fi

