#!/bin/sh
# $Id$
# *===========================================================================*
# |                 Copyright (c) 2008 Applaud Solutions UK Ltd               |
# |                             All rights reserved                           |
# *===========================================================================*

header_string="$Id$"
prog_version=`echo "$header_string" | awk '{print $3}'`
program=`basename $0 .sh`

get_appltop_file=$XXAS_PATCH_SQL/xxas_com_get_appltopid.sql
ptchstatus_file=$XXAS_PATCH_SQL/xxas_com_ptchstatus.sql
ptchstart_file=$XXAS_PATCH_SQL/xxas_com_ptchstart.sql
ptchend_file=$XXAS_PATCH_SQL/xxas_com_ptchend.sql
ptchsession_file=$XXAS_PATCH_SQL/xxas_com_ptchsession.sql
ptch_nodelist_file=$XXAS_PATCH_SQL/xxas_com_nodelist.sql
ptch_nodetype_file=$XXAS_PATCH_SQL/xxas_com_nodetype.sql

#Get the appltop_id for the given node
#get_appltop_id ()
#{
#$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$get_appltop_file $1
#}

update_status_node ()
{
#patch not applied on these nodes
max_session_id=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchsession_file`
				
#Define the current node
node_name=$HOST_NAME
				
#Get the appltop_id for the master node
appltop_id=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$XXAS_PATCH_SQL/xxas_com_get_appltopid.sql $HOST_NAME`

# Get AC status
if [ $XXAS_REST_DEPLOYMENT = "Y" ]; then
	ac_status=Y
else
	ac_status=N
fi
}

update_slave_status ()
{
#preparing super_node map and super_node_list and shared_slave_node_list
#get the list of nodes

$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptch_nodelist_file $node_name > node_list.txt
			
if [ -f admin_shared_nodes.txt ]; then
	rm -f admin_shared_nodes.txt
fi
if [ -f second_level_nodes.txt ]; then
	rm -f second_level_nodes.txt
fi

if test -s node_list.txt ; then
	for i in `cat node_list.txt`
	do
		s_node_type=slave
		s_appltop_id=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$XXAS_PATCH_SQL/xxas_com_get_appltopid.sql $i`
		
		if [ $s_appltop_id = $appltop_id -o $s_appltop_id = 0 ]; then
			$XXAS_LOG "Updating patch application status for slave nodes..."
			$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstart_file $XXAS_PATCH_NAME \
					"'$XXAS_PATCH_DESC'" $XXAS_BG_STEPS $session_id $i \
					$s_node_type $s_appltop_id $ac_status S
			$XXAS_LOG "done"
			echo $i >> admin_shared_nodes.txt
		else
			$XXAS_LOG "Updating patch application status for slave nodes..."
			$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstart_file $XXAS_PATCH_NAME \
					"'$XXAS_PATCH_DESC'" $XXAS_BG_STEPS $session_id $i \
					$s_node_type $s_appltop_id $ac_status X
			$XXAS_LOG "done"
			echo $i >> second_level_nodes.txt
		fi
		done
fi
}

is_master=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptch_nodetype_file $HOST_NAME $XXAS_PATCH_NAME`
		
if [ $is_master = 'master' ]; then
	#Get the patch status for the master node
	m_current_status=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstatus_file $XXAS_PATCH_NAME master $HOST_NAME`

	if [ $m_current_status = 'S' ]; then
		update_status_node
		$XXAS_LOG "Updating patch application status for master nodes..."
		$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstart_file $XXAS_PATCH_NAME \
				"'$XXAS_PATCH_DESC'" $XXAS_BG_STEPS $max_session_id $node_name \
				$is_master $appltop_id $ac_status S
		$XXAS_LOG "done"
	fi
	if [ $m_current_status = 'X' -o $m_current_status = 'C' ]; then
		update_status_node
		#Increment the session id
		session_id=`expr $max_session_id + 1`
			
		#Insert records into xxas_com_applied_patches for the current patch for master node
		$XXAS_LOG "Updating patch application status for master node..."
		$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstart_file $XXAS_PATCH_NAME \
				 "'$XXAS_PATCH_DESC'" $XXAS_BG_STEPS $session_id $node_name \
				 master $appltop_id $ac_status S
		$XXAS_LOG "done"
		
		update_slave_status
	fi
else
	update_status_node
	$XXAS_LOG "Updating patch application status for slave nodes..."
	$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstart_file $XXAS_PATCH_NAME \
			"'$XXAS_PATCH_DESC'" $XXAS_BG_STEPS $max_session_id $node_name \
				$is_master $appltop_id $ac_status S
	$XXAS_LOG "done"
fi

if test -f appltop_id1; then
	rm -f appltop_id1
fi

if test -f s_appltop_id1; then
	rm -f s_appltop_id1
fi
