#!/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`

usage="\t$program"

$XXAS_LOG "Staring main install:"

#
# Set the location of the patch manifest and apply script
#
manifest_file=$XXAS_PATCH_METADATA/xxas_com_manifest.txt
ptchstart_file=$XXAS_PATCH_SQL/xxas_com_ptchstart.sql
ptchend_file=$XXAS_PATCH_SQL/xxas_com_ptchend.sql
applyfile=$XXAS_PATCH_BIN/xxas_com_applyfile.sh
multi_apply_file=$XXAS_PATCH_BIN/xxas_com_multi_apply.sh
countinvalid_file=$XXAS_PATCH_SQL/xxas_com_chkinvalid.sql
recomp_file=$XXAS_PATCH_SQL/xxas_com_recompile.sql
get_appltop_file=$XXAS_PATCH_SQL/xxas_com_get_appltopid.sql
ptchsession_file=$XXAS_PATCH_SQL/xxas_com_ptchsession.sql
ptch_nodelist_file=$XXAS_PATCH_SQL/xxas_com_nodelist.sql
nls_inst_lang=$XXAS_PATCH_SQL/xxas_com_nls_inst_lst.sql

#
# Check that the manifest file exists
#
if test ! -f "$manifest_file" ; then
  printf "\nERROR: The patch manifest cannot be found:\n"
  printf "$manifest_file\n\n"
  exit 1;
fi

#
# Verify that the register patch file exists
#
if test ! -f "$ptchstart_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$ptchstart_file\n\n"
  exit 1;
fi

#
# Verify that the update patch status file exists
#
if test ! -f "$ptchend_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$ptchend_file\n\n"
  exit 1;
fi

#
# Verify that the applyfile exists
#
if test ! -f "$applyfile" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$applyfile\n\n"
  exit 1;
fi

#
# Verify that the get_appltop_file exists
#
if test ! -f "$get_appltop_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$get_appltop_file\n\n"
  exit 1;
fi

#
# Verify that the ptchsession_file exists
#
if test ! -f "$ptchsession_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$ptchsession_file\n\n"
  exit 1;
fi

#
# Verify that the ptch_nodelist_file exists
#
if test ! -f "$ptch_nodelist_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$ptch_nodelist_file\n\n"
  exit 1;
fi

#
# Verify that the ptch_nodelist_file exists
#
if test ! -f "$multi_apply_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$multi_apply_file\n\n"
  exit 1;
fi

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

#
# Register this patch installation has started
#
# Check multinode or single node
#

apps_rel_file=$XXAS_PATCH_SQL/xxas_com_appsrel.sql
apps_release=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_rel_file`
major_apps_release=`echo "$apps_release" | awk -F. '{print $1$2}'`

$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptch_nodelist_file all > all_nodes.txt

if [ "$major_apps_release" = "122" -a `cat all_nodes.txt |wc -l` -gt 1 ]; then
#	if [ `cat all_nodes.txt |wc -l` -gt 1 ]; then
		$XXAS_LOG "Found multiple nodes for this instance..."
		$XXAS_LOG "Proceeding with multi node steps..."
		. $multi_apply_file
		$XXAS_LOG "done"
#	fi
else
	max_session_id=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchsession_file`
		
	#Increment the session id
	session_id=`expr $max_session_id + 1`
	#set node_type=master for current node
	node_type=master

	#Define the current node as admin_node
	admin_node=$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}x" = "x" ]; then
		ac_status=N
	elif [ "$XXAS_REST_DEPLOYMENT" = "Y" ]; then
		ac_status=Y
	else
		ac_status=N
	fi
	
	#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 $admin_node \
				$node_type $appltop_id $ac_status S
	$XXAS_LOG "done"
fi
#
# Create the patch directory
#
$XXAS_LOG "Checking for patch install directory..."
installed_patch_dir="$XXAS_PROD_TOP/patch/$XXAS_PATCH_NAME"
if test ! -d "$installed_patch_dir" ; then
  $XXAS_LOG "Creating: $installed_patch_dir"
  mkdir -p $installed_patch_dir
  chmod 775 $installed_patch_dir
fi
$XXAS_LOG "done"

#
# Copy the manifest file to the installed patch directory
#
$XXAS_LOG "Copying patch manifest..."
cp $manifest_file $installed_patch_dir/
$XXAS_LOG "done"

#
# Runs a particular phase of the patch
#
run_phase () {
  $XXAS_LOG "Starting phase $1:"
  grep $1 $manifest_file > ${1}.txt
  if test -s ${1}.txt; then
	#for line in `cat $manifest_file`;
	  for line in `cat ${1}.txt`;
	  do
		line_action=`echo "$line" | awk -F: '{print $1}'`
		line_phase=`echo "$line" | awk -F: '{print $6}'`
		if test "$line_action" = "file" ;
		then
		  if test "$line_phase" = "$1" ;
		  then
			#
			# Apply each file
			#
			$XXAS_LOG "  Applying file $line..."
			$applyfile "$line"
			exit_code=$?
			if [ $exit_code -ne 0 ];
			then
			  exit 1;
			fi
			$XXAS_LOG "  Completed file $line."
		  fi
		fi
	  done
  fi
  $XXAS_LOG "Completed phase $1."
  rm -f ${1}.txt
}

#
# Run each stage of the patch
#
run_phase "copyonly"
run_phase "unzip"
run_phase "schema"
run_phase "tab"
run_phase "seq"
run_phase "sql_s_un"
run_phase "ind"
run_phase "syn"
run_phase "type"
run_phase "pkh"
run_phase "view"
run_phase "pkb"
run_phase "dat_app"
run_phase "view_no_dep"
run_phase "upd"
run_phase "jsp"
run_phase "dat_dg"
run_phase "dat_menu"
run_phase "dat_lkp"
run_phase "dat_cp"
run_phase "dat_rg"
run_phase "dat_rs"
run_phase "dat_flex"
run_phase "dat_xmlp"
run_phase "dat_xmls"
run_phase "dat_resp"
run_phase "dat_role"
run_phase "dat_msg"
run_phase "dat_form"
run_phase "dat_prof"
run_phase "dat_rset"
run_phase "dat_eitsec"
run_phase "dat_amettype"
run_phase "dat_ameattr"
run_phase "dat_ameattru"
run_phase "dat_amesappg"
run_phase "dat_amesaagc"
run_phase "dat_ameconk"
run_phase "dat_amerule"
run_phase "dat_ameactu"
run_phase "dat_xxasmenu"
run_phase "dat_page"
run_phase "dat_pg"
run_phase "dat_prf"
run_phase "dat_alert"
run_phase "dat_attach"
run_phase "dat_udtrtl"
run_phase "wft"
run_phase "wfx"
run_phase "mds"
run_phase "hooks"
run_phase "manual"
run_phase "upd_post_dat"
run_phase "bg"
run_phase "nls"

#
# Verify that the invalid object checker file exists
#
if test ! -f "$countinvalid_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$countinvalid_file\n\n"
  exit 1;
fi

#
# Verify that the invalid object checker file exists
#
if test ! -f "$recomp_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$recomp_file\n\n"
  exit 1;
fi

#
# Count the invalid objects
#
$XXAS_LOG "Counting invalid objects..."
old_num_invalid=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$countinvalid_file $XXAS_CUSTOM_OBJ_PREFIX`
$XXAS_LOG "$old_num_invalid"
if [ $old_num_invalid -gt 0 ];
then
  #
  # Recompile invalid objects
  #
  $XXAS_LOG "Recompiling invalid objects..."
  $XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$recomp_file $XXAS_CUSTOM_OBJ_PREFIX
  $XXAS_LOG "done"
  #
  # Warn if there are still invalid objects
  #
  $XXAS_LOG "Re-counting invalid objects..."
  new_num_invalid=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$countinvalid_file $XXAS_CUSTOM_OBJ_PREFIX`
  $XXAS_LOG "$new_num_invalid"
  if [ $new_num_invalid -gt 0 ];
  then
    printf "\n\nWARNING: $new_num_invalid object(s) failed to recompile.\n\n"
  fi
fi

#
# Copy all log files to the installed patch directory
#
$XXAS_LOG "Copying log files to the installed patch directory..."
if ls *.log > /dev/null 2>&1
then
  cp *.log $installed_patch_dir/
fi
$XXAS_LOG "done"

#
# Change patch status to completed
#
$XXAS_LOG "Changing patch status to complete..."
$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchend_file $XXAS_PATCH_NAME $HOST_NAME
$XXAS_LOG "done"

exit 0

