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

#
# Set the location of the patch manifest
#
manifest_file=$XXAS_PATCH_METADATA/xxas_com_manifest.txt
ptchstatus_file=$XXAS_PATCH_SQL/xxas_com_ptchstatus.sql
ptch_nodetype_file=$XXAS_PATCH_SQL/xxas_com_nodetype.sql
seed_ptch_status=$XXAS_PATCH_SQL/xxas_com_ptchcount.sql

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

#
# Loop through the manifest to find the patch line
#
for line in `cat $manifest_file`;
do
  if test `echo "$line" | awk -F: '{print $1}'` = "patch" ;
  then
    XXAS_PATCH_NAME="`echo "$line" | awk -F: '{print $2}'`"
    XXAS_PATCH_DESC="`echo "$line" | awk -F: '{print $3}'`"
  fi
done

#
# Check the Patch Name is set
#
$XXAS_LOG "Checking patch name..."
if [ ${XXAS_PATCH_NAME}x = "x" ];
then
  printf "\nERROR: The patch name could not be read from the manifest file.\n"
  exit 1
fi
$XXAS_LOG "$XXAS_PATCH_NAME"

#
# Check the Patch Description is set
#
$XXAS_LOG "Checking patch description..."
if [ ${XXAS_PATCH_DESC}x = "x" ];
then
  printf "\nERROR: The patch description could not be read from the manifest file.\n"
  exit 1
fi
$XXAS_LOG "$XXAS_PATCH_DESC"

#
# Store the patch details in the environment
#
export XXAS_PATCH_NAME
export XXAS_PATCH_DESC

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


#get the node type for the host
#
$XXAS_LOG "Retrieving current node type..."
node_type=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptch_nodetype_file $HOST_NAME $XXAS_PATCH_NAME`
$XXAS_LOG "$node_type"

if [ $node_type = 'master' ]; then
	#
	# Get the status of this patch for master nodes
	#
	$XXAS_LOG "Retrieving current patch status..."
	patch_status=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstatus_file $XXAS_PATCH_NAME master $HOST_NAME`
	exit_code=$?
	if [ $exit_code -ne 0 ];
	then
		printf "\nERROR: The status of the following patch could not be determined.\n"
		printf "$XXAS_PATCH_NAME\n\n"
		exit 1;
	fi
	$XXAS_LOG "$patch_status"
	#
	# Confirm installation with the user.
	#
	$XXAS_LOG "Confirming patch installation with user..."
	quit_install="N"
	case $patch_status in
	  X)
	  printf "You are about to install the following patch:\n\n"
	  printf "Patch Name: \t\t$XXAS_PATCH_NAME\n"
	  printf "Patch Description: \t$XXAS_PATCH_DESC\n"
	  printf "\nUse of this software requires you to accept the terms of the\n"
	  printf "End User License Agreement (EULA)\n"
	  while true
	  do
		printf "\nDo you accept the terms of the EULA and wish to continue with\n"
		printf "installation? (Y or N): "
		read continue_flag
		case $continue_flag in
		  y|Y|YES|yes|Yes)
		  	ptch_name=`echo $XXAS_PATCH_NAME | cut -f1 -d"-"`
			if [ "$ptch_name" != "xxasemp" ]; then
				status_seed=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$seed_ptch_status $XXAS_PATCH_NAME null`
				if [ "$status_seed" = "X" ]; then
					printf "\nERROR: Install the Seeded patch first\n"
					printf "       For more details refer to installation guide\n"
					printf "       https://docs.applaudsolutions.com/installation \n\n"
					exit 1;
				else
					break
				fi
			else
				break
			fi
		  ;;
		  n|N|NO|no|No)
			printf "\nThe installation has been cancelled.\n\n"
			quit_install="Y"
			break
		  ;;
		esac
	  done
	  ;;
	  S)
	  printf "The following patch failed to complete during the last install:\n\n"
	  printf "Patch Name: \t\t$XXAS_PATCH_NAME\n"
	  printf "Patch Description: \t$XXAS_PATCH_DESC\n\n"
	  printf "The installation will restart automatically.\n"
	  while true
	  do
		printf "\nDo you want to continue with the installation (Y or N): "
		read continue_flag
		case $continue_flag in
		  y|Y|YES|yes|Yes)
			break
		  ;;
		  n|N|NO|no|No)
			printf "\nThe installation has been cancelled.\n\n"
			quit_install="Y"
			break
		  ;;
		esac
	  done
	  ;;
	  C)
	  printf "This patch has already been installed:\n\n"
	  printf "Patch Name: \t\t$XXAS_PATCH_NAME\n"
	  printf "Patch Description: \t$XXAS_PATCH_DESC\n\n"
	  printf "If this is a multi-node system with several file systems you should\n"
	  printf "install this patch on all application file systems.\n\n"
	  printf "If the application tier file system is shared across all\n"
	  printf "nodes, you do not need to reapply this patch.\n"
	  while true
	  do
		printf "\nDo you want to reapply this patch (Y or N): "
		read continue_flag
		case $continue_flag in
		  y|Y|YES|yes|Yes)
			break
		  ;;
		  n|N|NO|no|No)
			printf "\nThe installation has been cancelled.\n\n"
			quit_install="Y"
			break
		  ;;
		esac
	  done
	  ;;
	esac
else
	#
	# Get the status of this patch for slave nodes
	#
	$XXAS_LOG "Retrieving current patch status..."
	patch_status=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstatus_file $XXAS_PATCH_NAME slave $HOST_NAME`
	exit_code=$?
	if [ $exit_code -ne 0 ];
	then
		printf "\nERROR: The status of the following patch could not be determined.\n"
		printf "$XXAS_PATCH_NAME\n\n"
		exit 1;
	fi
	$XXAS_LOG "$patch_status"

	#
	# Confirm installation with the user.
	#
	$XXAS_LOG "Confirming patch installation with user..."
	quit_install="N"
	case $patch_status in
	  X)
	  printf "You are about to install the following patch:\n\n"
	  printf "Patch Name: \t\t$XXAS_PATCH_NAME\n"
	  printf "Patch Description: \t$XXAS_PATCH_DESC\n"
	  printf "\nUse of this software requires you to accept the terms of the\n"
	  printf "End User License Agreement (EULA), which is available at\n"
	  while true
	  do
		printf "\nDo you accept the terms of the EULA and wish to continue with\n"
		printf "installation? (Y or N): "
		read continue_flag
		case $continue_flag in
		  y|Y|YES|yes|Yes)
		  	ptch_name=`echo $XXAS_PATCH_NAME | cut -f1 -d"-"`
			if [ "$ptch_name" != "xxasemp" ]; then
				status_seed=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$seed_ptch_status $XXAS_PATCH_NAME null`
				if [ "$status_seed" = "X" ]; then
					printf "\nERROR: Install the Seeded patch first\n"
					printf "       For more details refer to installation guide\n"
					printf "       https://docs.applaudsolutions.com/installation \n\n"
					exit 1;
				else
					break
				fi
			else
				break
			fi
		  ;;
		  n|N|NO|no|No)
			printf "\nThe installation has been cancelled.\n\n"
			quit_install="Y"
			break
		  ;;
		esac
	  done
	  ;;
	  S)
	  printf "The following patch failed to complete during the last install:\n\n"
	  printf "Patch Name: \t\t$XXAS_PATCH_NAME\n"
	  printf "Patch Description: \t$XXAS_PATCH_DESC\n\n"
	  printf "The installation will restart automatically.\n"
	  while true
	  do
		printf "\nDo you want to continue with the installation (Y or N): "
		read continue_flag
		case $continue_flag in
		  y|Y|YES|yes|Yes)
			break
		  ;;
		  n|N|NO|no|No)
			printf "\nThe installation has been cancelled.\n\n"
			quit_install="Y"
			break
		  ;;
		esac
	  done
	  ;;
	  C)
	  printf "This patch has already been installed:\n\n"
	  printf "Patch Name: \t\t$XXAS_PATCH_NAME\n"
	  printf "Patch Description: \t$XXAS_PATCH_DESC\n\n"
	  printf "If you wish to reinstall this patch, apply it on master node first\n\n"
	  printf "If the application tier file system is shared across all\n"
	  printf "nodes, you do not need to reapply this patch on this node.\n"
	  $XXAS_LOG "Shared/non-shared slave node, no need to apply the patch"
	  quit_install="Y"
	  ;;
	esac
fi
#
# Quit installation is selected
#
if test "$quit_install" = "Y" ;
then
  $XXAS_LOG "Installation terminated at the user's request"
  exit 1;
fi

$XXAS_LOG "confirmed"


