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

printf "Checking for additional pre and post requisites.\n"
printf "This will take a while so please do not\n"
printf "interrupt this session..\n\n"
$XXAS_LOG "Checking for additional pre and post requisites:"
#
# 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
ptchcount_file=$XXAS_PATCH_SQL/xxas_com_ptchcount.sql
get_appltop_file=$XXAS_PATCH_SQL/xxas_com_get_appltopid.sql

#get_appltop_id ()
#{
#$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$get_appltop_file $1
#}
#
# Default environment variables
#
XXAS_APP_TIER_RESTART="N"
XXAS_BG_STEPS="N"
XXAS_REST_DEPLOYMENT="N"
XXAS_RUN_ADCGNJAR="N"

#
# Force App Tier restart if this session is the first-time install
#
if test "$XXAS_ONETI" = "Y" ;
then
  XXAS_APP_TIER_RESTART="Y"
fi

#
# 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

#
# Loop through all files in the manifest and check for additional
# pre and post-requisites.
#
$XXAS_LOG "Reading manifest file:"
for line in `cat $manifest_file`;
do
  #
  # Convert the argument to variables
  #
  action_type=`echo "$line" | awk -F: '{print $1}'`
  file_top=`echo "$line" | awk -F: '{print $2}'`
  file_dir=`echo "$line" | awk -F: '{print $3}'`
  file_name=`echo "$line" | awk -F: '{print $4}'`
  patch_ver=`echo "$line" | awk -F: '{print $5}'`
  phase=`echo "$line" | awk -F: '{print $6}'`

  #
  # Only look at 'file' actions
  #
  if test "$action_type" = "file" ;
  then
    #
    # Check the File Top
    #
    if [ ${file_top}x = "x" ];
    then
       printf "\nERROR: The File Top could not be read from the following manifest:\n"
       printf "$manifest\n\n"
       exit 1;
    fi

    #
    # Check the File Directory
    #
    if [ ${file_dir}x = "x" ];
    then
       printf "\nERROR: The File Directory could not be read from the following manifest:\n"
       printf "$manifest\n\n"
       exit 1;
    fi

    #
    # Check the File Name
    #
    if [ ${file_name}x = "x" ];
    then
       printf "\nERROR: The File Name could not be read from the following manifest:\n"
       printf "$manifest\n\n"
       exit 1;
    fi

    #
    # Check the File Version
    #
    if [ ${patch_ver}x = "x" ];
    then
       printf "\nERROR: The File Version could not be read from the following manifest:\n"
       printf "$manifest\n\n"
       exit 1;
    fi

    #
    # Check the File Phase
    #
    if [ ${phase}x = "x" ];
    then
       printf "\nERROR: The file phase could not be read from the following manifest:\n"
       printf "$manifest\n\n"
       exit 1;
    fi
    
    #
    # Check for files that use an alternative to the xxas_ prefix so we can later
    # compile customer objects as well as xxas objects.
    #
    XXAS_CUSTOM_OBJ_PREFIX="XXAS"
    file_prefix=`echo "$file_name" | awk -F_ '{print $1}'`
    if test "$file_prefix" != "xxas" ; then
        XXAS_CUSTOM_OBJ_PREFIX=$file_prefix
    fi

    #
    # Set and check the Source File exists
    #
    source_file=$XXAS_PATCH_TOP/$file_top/$file_dir/$file_name
    if test ! -f "$source_file" ; then
      printf "\nERROR: The following patch source file cannot be found:\n"
      printf "$source_file\n\n"
      exit 1;
    fi

    chk_ver_file=$XXAS_PATCH_SQL/xxas_com_filever.sql
    #
    # Check that the check version SQL file exists
    #
    if test ! -f "$chk_ver_file" ; then
      printf "\nERROR: The following SQL file cannot be found:\n"
      printf "$chk_ver_file\n\n"
      exit 1;
    fi

    db_ver=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$chk_ver_file "$file_name"`
    exit_code=$?
    if [ $exit_code -ne 0 ];
    then
      printf "\nERROR: Unable to determine the file version for file:\n"
      printf "$file_name\n"
      exit 1;
    fi

    #
    # Determine additional pre-requisites based on actual files that
    # need to be applied
    #
    if test "$file_top" = "JAVA_TOP" ;
    then
      if [ $patch_ver -gt $db_ver ];
      then
        $XXAS_LOG "Found unapplied JAVA_TOP object..."
        $XXAS_LOG "$file_name"
        XXAS_APP_TIER_RESTART="Y"
		XXAS_RUN_ADCGNJAR="Y"
      fi
    fi
    
    if test "$file_name" = "REST_post_install.txt" ;
    then
		#patch_status=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstatus_file $XXAS_PATCH_NAME`
		patch_count=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchcount_file $XXAS_PATCH_NAME $HOST_NAME`
		if test "${patch_count}" != "C" ; then
			#if [ "$patch_status" = "S" -o "$patch_status" = "C" -o "$patch_status" = "X" ]; then
			$XXAS_LOG "REST Servlet deployment needed..."
			$XXAS_LOG "$file_name"
			XXAS_REST_DEPLOYMENT="Y"
		else
			if [ $patch_ver -gt $db_ver ];
			then
				$XXAS_LOG "REST Servlet deployment needed..."
				$XXAS_LOG "$file_name"
				XXAS_REST_DEPLOYMENT="Y"
			fi
		fi
		#fi
    fi

    case $phase in
    pkh|pkb|hooks)
      if [ $patch_ver -gt $db_ver ];
      then
        $XXAS_LOG "Found unapplied package or API hook object..."
        $XXAS_LOG "$file_name"
        XXAS_APP_TIER_RESTART="Y"
      fi
    ;;
    jsp)
      if [ $patch_ver -gt $db_ver ];
      then
        $XXAS_LOG "Found unapplied JSP object..."
        $XXAS_LOG "$file_name"
        XXAS_APP_TIER_RESTART="Y"
      fi
    ;;
    mds)
      if [ $patch_ver -gt $db_ver ];
      then
        $XXAS_LOG "Found unapplied OAF object..."
        $XXAS_LOG "$file_name"
        XXAS_APP_TIER_RESTART="Y"

        #
        # If the JDBC connection could not be obtained automatically,
        # request it here.
        #
        if [ "${XXAS_JDBC_CONN}x" = "x" ];
        then
          #
          # Request the JDBC connect string
          #
          $XXAS_LOG "Don't have JDBC connection; requesting..."
          printf "\nA JDBC connection could not be obtained automatically and "
          printf "is required to\n"
          printf "install certain metadata.\n\n"
          printf "Example:\n"
          printf "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)"
          printf "(HOST=vprod.applaudsolutions.local)(PORT=1521))"
          printf "(CONNECT_DATA=(SERVICE_NAME=PROD)))\n"
          while test "${jdbc_conn:=}x" = "x"
          do
            printf "\nEnter the JDBC Connect String: "
            read jdbc_conn
            printf "\n\n"
          done
          XXAS_JDBC_CONN=$jdbc_conn; export XXAS_JDBC_CONN
          $XXAS_LOG "JDBC Connect String: $XXAS_JDBC_CONN"
        fi
      fi
    ;;
    tab|seq|ind)
      if [ $patch_ver -gt $db_ver ];
      then
        #
        # The schema password is required for this install
        #
		$XXAS_LOG "Found new schema object..."
        $XXAS_LOG "$file_name"
        if [ "${XXAS_SCHEMA_PW}x" = "x" ];
        then
          #
          # Fetch the password for this schema
          #
          $XXAS_LOG "Don't have schema password; requesting schema password..."
          printf "\nThis patch delivers new objects which must be loaded into\n"
          printf "the custom schema.\n"
          while test "${schema_pw:=}x" = "x"
          do
            printf "\nEnter the $XXAS_SCHEMA_UN password: "
            stty -echo
            read schema_pw
            stty echo
            printf "\n\n"
            #
            # Test the schema login
            #
            $XXAS_PATCH_BIN/xxas_com_testsqlp.sh $XXAS_SCHEMA_UN $schema_pw
            exit_code=$?
            if [ $exit_code -ne 0 ];
            then
              unset schema_pw
            fi
          done
          XXAS_SCHEMA_PW=$schema_pw; export XXAS_SCHEMA_PW
          $XXAS_LOG "accepted"
		 fi
        if [ "$file_name" = "xxas_com_patch_upd_tab.sql"  ]; then
			appltop_id=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$XXAS_PATCH_SQL/xxas_com_get_appltopid.sql $HOST_NAME`
			xxas_com_applylog=xxas_com_apply_`date +%F_%H%M%S`.log
			$XXAS_SQLPLUS -s $XXAS_SCHEMA_UN/$XXAS_SCHEMA_PW @$XXAS_PATCH_SQL/$file_name \
						   $HOST_NAME $appltop_id > $XXAS_PATCH_TOP/$xxas_com_applylog 2>&1
			exit_code=$?
			if [ $exit_code -ne 0 ];
			then
				printf "\nERROR: The following file failed to apply:\n\n"
				printf "$XXAS_PATCH_SQL/$file_name\n\n"
				printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
				exit 1;
			else
				rm -f $xxas_com_applylog
				#
				# Update the metadata with the applied file.
				# BG-specific files are registered when applied online.
				#
				upd_applied_file=$XXAS_PATCH_SQL/xxas_com_updfile.sql
				if test ! -f "$upd_applied_file" ; then
				  printf "\nERROR: The following SQL file cannot be found:\n\n"
				  printf "$upd_applied_file\n\n"
				  exit 1;
				fi
				$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$upd_applied_file $file_name \
								 $file_top $file_dir $patch_ver $phase
				exit_code=$?
				if [ $exit_code -ne 0 ];
				then
				  printf "\nERROR: The metadata failed to upload for the following file:\n\n"
				  printf "$file_name\n\n"
				  exit 1;
				fi
			fi
		fi
      fi
    ;; 
    bg)
      if [ $patch_ver -gt $db_ver ];
      then
        $XXAS_LOG "Found Business Group specific data..."
        $XXAS_LOG "$file_name"
        XXAS_BG_STEPS="Y"
      fi
    ;;
    esac
  fi
done

#
# Export the environment variables
#
export XXAS_APP_TIER_RESTART
export XXAS_BG_STEPS
export XXAS_CUSTOM_OBJ_PREFIX
export XXAS_REST_DEPLOYMENT
export XXAS_RUN_ADCGNJAR
$XXAS_LOG "Middle Tier Restart Required: $XXAS_APP_TIER_RESTART"
$XXAS_LOG "Business Group Specific Steps: $XXAS_BG_STEPS"
$XXAS_LOG "Custom Object Prefix: $XXAS_CUSTOM_OBJ_PREFIX"

