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

. /usr/es/lib/ksh93/func_include

############################################################################
#
# Name:         isPrimaryNode
#
# Description:  Finds out the Node is primary or not
#
# Arguments:
#
# Returns:      n/a
#
#############################################################################
function isPrimaryNode
{
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x


	return 1
}

############################################################################
#
# Name:         print_server_systemv_discover
#
# Description:  Performs SystemV print server discovery query on the local node
#
# Arguments:
#
# Returns:      n/a
#
#############################################################################
function print_server_systemv_discover
{
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x

	typeset -i installed=1

	#
	: For SystemV print subsystem, the required filesets are as follows
	: bos.svprint.rte
	: bos.svprint.fonts
	: bos.svprint.hpnp
	: bos.svprint.ps
	: bos.terminfo.svprint.data
	: bos.msg.en_US.svprint - Not considering being message fileset
	#
	for fileset in "bos.svprint.rte bos.svprint.fonts bos.svprint.hpnp \
bos.svprint.ps bos.terminfo.svprint.data"
	do
		/usr/bin/lslpp -l $fileset > /dev/null 2>&1
		(( $? != 0 )) && {
			installed=0
			break
		}
	done

	print -n $(dspmsg -s 10 print_server_sa.cat 1 "Print Subsystem Smart Assist")
        print -n ":PRINT_SERVER:"
        print -n $(dspmsg -s 10 print_server_sa.cat 2 "SystemV")
        print -n ":PRINT_SERVER_SYSTEMV:"
        echo "$installed"
}

############################################################################
#
# Name:         print_server_powerpc_discover
#
# Description:  Performs Power PC print server discovery query on the local node
#
# Arguments:
#
# Returns:      n/a
#
#############################################################################
function print_server_powerpc_discover
{
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
	typeset -i installed=1

	#
        : For Power PC print subsystem, the required filesets are as follows
        : bos.rte.printers
	: printers.rte
        #
	for fileset in "bos.rte.printers printers.rte"
        do
                /usr/bin/lslpp -l $fileset > /dev/null 2>&1
                (( $? != 0 )) && {
                        installed=0
                        break
                }
        done

        print -n $(dspmsg -s 10 print_server_sa.cat 1 "Print Subsystem Smart Assist")
        print -n ":PRINT_SERVER:"
        print -n $(dspmsg -s 10 print_server_sa.cat 3 "PowerPC")
        print -n ":PRINT_SERVER_POWERPC:"
        echo "$installed"
}

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

FPATH=/usr/es/lib/ksh93/aix:/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 ACTION COMPONENT
while getopts q:n:PDd:c: option
do
	case $option in
		n)
			#
			: List of input nodes
			#
			nodes=$OPTARG
			nodes=${nodes//:/ }
			nodes=${nodes//,/ }
			;;
		q)	#
			: Perform a query for a modify screen
			#
			application_id=$OPTARG
			ACTION=query
			;;
		D)
			#
			: Run discovery
			#
			ACTION=discover
			;;
		c)
			#
			: component - PowerPC or System V
			#
			COMPONENT=$OPTARG
			COMPONENT=$(echo "$COMPONENT" | sed 's/ *#[^#]*$//')
			;;
		P)
			#
			: Check the primary node
			#
			typeset ret_val
			isPrimaryNode
			ret_val=$?
			exit $ret_val
			;;
		d)
			#
			: configuration assist discovery
			#
			saDiscover=$OPTARG
			[[ "$saDiscover" == "SystemV" ]] && print_server_systemv_discover
			[[ "$saDiscover" == "PowerPC" ]] && print_server_powerpc_discover
			exit 0
			;;
		*)
			;;
	esac
done


[[ "$ACTION" == "discover" ]] && {
	echo "#primary:takeover:print_subsystem:application_name"
	
	#
	: Check the primary node
	#
	typeset primary_node
	for node in $nodes; do
		if [[ "$node" == "$LOCALNODENAME" ]]; then
			isPrimaryNode
		else
			cl_rsh $node "/usr/es/sbin/cluster/sa/printServer/sbin/cl_print_server_query -P" 2>/dev/null
		fi
		[[ $? == 0 ]] && {
			primary_node=$node
			break
		}
	done

	[[ "$COMPONENT" == "PRINT_SERVER_SYSTEMV" ]] && component="SystemV"
	[[ "$COMPONENT" == "PRINT_SERVER_POWERPC" ]] && component="PowerPC"

	echo "$primary_node: :$component:print_subsystem_$component"
	exit 0
}

[[ "$ACTION" == "query" ]] && {
	echo "#primary:takeover:print_subsystem:service_ip:netmask_prefix_len:network"
	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 $(clquerysaapp -a $application_id | grep PRINT_SERVER_SUBSYSTEM)
	eval $(clvt query resource_group $RESOURCE_GROUP | grep SERVICE_LABEL)
	set +a

	[[ -n $RESOURCE_GROUP ]] && {
		NODES=$(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)
	NETWORK=$(cl_harvestIP_scripts -u $SERVICE_LABEL | tail -1 | cut -d ":" -f 3)

	echo $primary:$takeover:$PRINT_SERVER_SUBSYSTEM:$SERVICE_LABEL:$PREFIX_LEN:$NETWORK
	exit 0
}
