#!/usr/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,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/filenet/sbin/cl_filenet_pe_query.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/filenet/sbin/cl_filenet_pe_query.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
############################################################################
#
# Name:	cl_filenet_pe_query
#
# Description:	Performs application discovery query on the local node
#
# Arguments:	
#
# Returns:	n/a
#
#############################################################################

. /usr/es/lib/ksh93/func_include

#############################################################################
# Functions:
#
#	listUsers
#	isPrimaryNodeForInstance
#
#############################################################################

############################################################################
#
# Name: listUsers
#
# Description:  Lists the process engine users
#
# Arguments: list of nodes
#
# Returns: Always returns 0     
#
#############################################################################

function listUsers {
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
	typeset nodes=$*

	[[ -z $LOCALNODENAME ]] && {
		LC_ALL=C ps -efa | grep TM_daemon | grep -v grep | grep -v root | awk '{print $1}' | uniq
		return 0
	}
	for node in $nodes; do
		[[ "$node" == "$LOCALNODENAME" ]] && {
			LC_ALL=C ps -efa | grep TM_daemon | grep -v grep | grep -v root | awk '{print $1}' | uniq
		} || {
			cl_rsh $node \
			"/usr/es/sbin/cluster/sa/filenet/sbin/cl_filenet_pe_query -I"
		}
	done
	return 0
}

############################################################################
#
# Name: isPrimaryNodeForUser
#
# Description: Checking the primary node for the process engine user
#
# Arguments: N/A
#
# Returns: If local node primary node, return 0
#	   else return 1
#
#############################################################################

function isPrimaryNodeForUser {
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x

	LC_ALL=C ps -eaf | grep -w TM_daemon | grep -v grep | grep -w $user_name > /dev/null 2>&1
	(( $? == 0 )) && {
		mount | grep $user_name/local > /dev/null 2>&1
		(( $? == 0 )) && {
			return 0
		}
	}

	return 1
}

############################################################################
#
# Name: saPEdiscover
#
# Description: Initial process engine discovery for smart assist to find out
#		Process engine has been installed or not.
#
# Arguments: N/A
#
# Returns: N/A
#
#############################################################################
function saPEdiscover {
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x

	typeset -i pe_installed=0

	lslpp -cw '*/bin/initfnsw' > /dev/null 2>&1
	(( $? == 0 )) && pe_installed=1

	print -n $(dspmsg -s 8 filenetsa.cat 1 "FileNet P8 Smart Assist")
        print -n ":FILENET:"
        print -n $(dspmsg -s 8 filenetsa.cat 2 "Process Engine")
        print -n ":FILENET_PROCESS_ENGINE:"
        echo "$pe_installed"
}

############################################################################
#
# Name: saCEdiscover
#
# Description: Initial content engine discovery for smart assist to find out
#               content engine has been installed or not.
#
# Arguments: N/A
#
# Returns: N/A
#
#############################################################################
function saCEdiscover {
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
	typeset -i ce_installed=0

	ce_installed=$(eval $(KLIB_AIX_ODM_get_odm_fields "name=DISCOVERY_COMMAND and \
component_id=WAS_6.0_APP_SERVER" HACMPsa value) | cut -d ':' -f 5)

        print -n $(dspmsg -s 8 filenetsa.cat 1 "FileNet P8 Smart Assist")
        print -n ":FILENET:"
        print -n $(dspmsg -s 8 filenetsa.cat 3 "Content Engine")
        print -n ":FILENET_CONTENT_ENGINE:"
        echo "$ce_installed"
}

############################################################################
#
# Name: saAEdiscover
#
# Description: Initial application engine discovery for smart assist to find out
#               application engine has been installed or not.
#
# Arguments: N/A
#
# Returns: N/A
#
#############################################################################

function saAEdiscover {
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
        typeset -i ae_installed=0

        ae_installed=$(eval $(KLIB_AIX_ODM_get_odm_fields "name=DISCOVERY_COMMAND and \
component_id=WAS_6.0_APP_SERVER" HACMPsa value) | cut -d ':' -f 5)

        print -n $(dspmsg -s 8 filenetsa.cat 1 "FileNet P8 Smart Assist")
        print -n ":FILENET:"
        print -n $(dspmsg -s 8 filenetsa.cat 4 "Application Engine")
        print -n ":FILENET_APPLICATION_ENGINE:"
        echo "$ae_installed"
}

############################################################################
#
# Name: DbPEdiscover
#
# Description: Initial DB for process engine discovery for smart assist to find out
#              DB2 installed or not.
#
# Arguments: N/A
#
# Returns: N/A
#
#############################################################################
function DbPEdiscover {
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
        typeset -i db_installed=0

	db_installed=$(eval $(KLIB_AIX_ODM_get_odm_fields "name=DISCOVERY_COMMAND and \
component_id=DB2_NON_DPF_SINGLE" HACMPsa value) | cut -d ':' -f 5)

	print -n $(dspmsg -s 8 filenetsa.cat 1 "FileNet P8 Smart Assist")
        print -n ":FILENET:"
        print -n $(dspmsg -s 8 filenetsa.cat 6 "DB for Process Engine")
        print -n ":FILENET_DB_PROCESS_ENGINE:"
        echo "$db_installed"
}

############################################################################
#
# Name: DbCEdiscover
#
# Description: Initial DB for content engine discovery for smart assist to find out
#              any DB2 instances are configured or not.
#
# Arguments: N/A
#
# Returns: N/A
#
#############################################################################
function DbCEdiscover {

	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
        typeset -i db_installed=0

        db_installed=$(eval $(KLIB_AIX_ODM_get_odm_fields "name=DISCOVERY_COMMAND and \
component_id=DB2_NON_DPF_SINGLE" HACMPsa value) | cut -d ':' -f 5)

	print -n $(dspmsg -s 8 filenetsa.cat 1 "FileNet P8 Smart Assist")
	print -n ":FILENET:"
	print -n $(dspmsg -s 8 filenetsa.cat 5 "DB for Content Engine")
	print -n ":FILENET_DB_CONTENT_ENGINE:"
	echo "$db_installed"

}

#############################################################################
# Global Variables:
#############################################################################

FPATH=/usr/es/lib/ksh93/aix:/usr/es/lib/ksh93/filenet:/usr/es/lib/ksh93/hacmp/:/usr/es/lib/ksh93/util/:/usr/es/lib/ksh93/util/list
PATH=$PATH:/usr/es/sbin/cluster/sa/sbin/:/usr/es/sbin/cluster/utilities/

LOCALNODENAME=$(get_local_nodename 2>/dev/null)

#############################################################################
# Main:
#############################################################################

typeset option nodes instance ACTION
while getopts q:n:ILPDd:u: option
do
	case $option in
		n)
			nodes=$OPTARG
			nodes=${nodes//:/ }
			nodes=${nodes//,/ }
			;;
		q)	# Perform a query for a modify screen
			application_id=$OPTARG
			ACTION=query
			;;
		u)
			# User name to run discovery on
			user_name=$OPTARG
			;;
		I)
			# List the accessible instances on this node
			nodes=$LOCALNODENAME
			ACTION=list
			;;
		D)
			# Run discovery
			ACTION=discover
			;;
		L)
			# List instances on all provided node names
			ACTION=list
			;;
		P)
			# Check the primary node for selected instance
			typeset ret_val
			isPrimaryNodeForUser
			ret_val=$?
			exit $ret_val
			;;
		d)
			# configuration assist discovery
			saDiscover=$OPTARG
			[[ "$saDiscover" == "AE" ]] && saAEdiscover
			[[ "$saDiscover" == "CE" ]] && saCEdiscover
			[[ "$saDiscover" == "PE" ]] && saPEdiscover
			[[ "$saDiscover" == "DB_PE" ]] && DbPEdiscover
			[[ "$saDiscover" == "DB_CE" ]] && DbCEdiscover
			exit 0
			;;
		*)
			;;
	esac
done


[[ "$ACTION" == "list" ]] && {
	[[ -z $nodes ]] && {
		typeset -A list
		KLIB_HACMP_list_nodes list
		nodes=${list[*]}
	}
	listUsers $nodes | sort -u
	exit 0
}

[[ "$ACTION" == "discover" ]] && {
	echo "#primary:takeover:application_name:config_vg"
	
	#
	# Check the primary node for selected process engine user
	#
	typeset primary_node
	typeset config_vg
	for node in $nodes; do
		if [[ "$node" == "$LOCALNODENAME" ]]; then
			isPrimaryNodeForUser
		else
			cl_rsh $node "/usr/es/sbin/cluster/sa/filenet/sbin/cl_filenet_pe_query -u $user_name -P" 2>/dev/null
		fi
		[[ $? == 0 ]] && {
			primary_node=$node
			break
		}
	done

	config_vg=$(KLIB_AIX_get_vg_by_path /${user_name}/local)
	echo "$primary_node: :FileNet_ProcessEngine_user_$user_name:$config_vg"
	exit 0
}

[[ "$ACTION" == "query" ]] && {
	echo "#primary:takeover:process_engine_user:process_engine_config_vg:service_ip:netmask_prefix_len"
	RESOURCE_GROUP=$(clodmget -q sa_key=$application_id -f group -d "=" HACMPgroup)

	# remove quotes
	RESOURCE_GROUP=${RESOURCE_GROUP//\"/}

	[[ -z $RESOURCE_GROUP ]] && exit 1

	set -a
	eval $(LC_ALL=C clquerysaapp -a $application_id | grep PROCESS_ENGINE_USER_NAME)
	eval $(LC_ALL=C clquerysaapp -a $application_id | grep PROCESS_ENGINE_CONFIG_VG)
	eval $(LC_ALL=C clvt query resource_group $RESOURCE_GROUP | grep SERVICE_LABEL)
	set +a

	[[ -n $RESOURCE_GROUP ]] && {
		NODES=$(LC_ALL=C clvt query resource_group $RESOURCE_GROUP | grep -w NODES | awk -F'=' '{ print $2 }')
		NODES=${NODES//\"/}
		echo $NODES | read primary takeover
	}

	PREFIX_LEN=$(cl_harvestIP_scripts -u $SERVICE_LABEL | tail -1 | cut -d ":" -f 13)

	echo $primary:$takeover:$PROCESS_ENGINE_USER_NAME:$PROCESS_ENGINE_CONFIG_VG:$SERVICE_LABEL:$PREFIX_LEN
	exit 0
}
