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

#
# Test the sqlplus executable and that the apps logon works
#
$XXAS_LOG "Testing SQLPlus executable and APPS login..."
$XXAS_PATCH_BIN/xxas_com_testsqlp.sh $XXAS_APPS_UN $XXAS_APPS_PW
exit_code=$?
if [ $exit_code -ne 0 ];
then
  exit 1;
fi
$XXAS_LOG "ok"

#
# Verify that the FNDLOAD executable is available
#
$XXAS_LOG "Checking FNDLOAD executable..."
if test ! -x $XXAS_FNDLOAD ; then
   printf "\nERROR: Unable to locate "\$"FND_TOP/bin/FNDLOAD. Please check
your environment.\n\n"
   exit 1;
fi
$XXAS_LOG "ok"

#
# Set the location of the prereq source file
#
prereq_src=$XXAS_PATCH_METADATA/xxas_com_prereq.txt
prereq_file=$XXAS_PATCH_SQL/xxas_com_chkprereq.sql
db_rel_file=$XXAS_PATCH_SQL/xxas_com_dbrel.sql
apps_rel_file=$XXAS_PATCH_SQL/xxas_com_appsrel.sql

#
# Check that the pre-req source file exists
#
if test ! -f "$prereq_src" ; then
  printf "\nERROR: The following pre-requisites file cannot be found:\n"
  printf "$prereq_src\n\n"
  exit 1;
fi

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

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

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

#
# Get the Oracle Database version
#
$XXAS_LOG "Fetching ORACLE database release..."
db_release=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$db_rel_file`
exit_code=$?
if [ $exit_code -ne 0 ];
then
  printf "\nERROR: The Oracle RDBMS version could not be determined.\n"
  exit 1;
fi
$XXAS_LOG "$db_release"

#
# Check the database version is at least 9i
#
$XXAS_LOG "Checking ORACLE database release is at least 11.1.0.7..."
major_db_release=`echo "$db_release" | awk -F. '{print $1$2$3$4}'`
if [ $major_db_release -le 11106 ];
then
  printf "\nERROR: The Oracle database must be Oracle 11.1.0.7 or higher.\n"
  printf "Detected database version: $db_release\n"
  exit 1;
fi
$XXAS_LOG "ok"

#
# Export the db release variable
#
XXAS_MAJOR_DB_REL=$major_db_release
export XXAS_MAJOR_DB_REL

#
# Get the Oracle Apps release
#
$XXAS_LOG "Fetching Oracle Applications release..."
apps_release=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_rel_file`
exit_code=$?
if [ $exit_code -ne 0 ];
then
  printf "\nERROR: The Oracle Applications release could not be determined.\n"
  exit 1;
fi
$XXAS_LOG "$apps_release"

#
# Check all apps pre-requisite patches are applied
#
missing_patches=$XXAS_PATCH_TOP/missing_patches.txt
major_apps_release=`echo "$apps_release" | awk -F. '{print $1$2}'`
for prereq in `cat $prereq_src`
do
  prereq_rel=`echo "$prereq" | awk -F: '{print $1}'`
  prereq_patch=`echo "$prereq" | awk -F: '{print $2}'`
  #
  # Check each pre-req patch
  #
  if [ "$prereq_rel" = "$major_apps_release" ];
  then
    #
    # If the prereq applies to this release, check whether
    # the patch has been applied.
    #
    $XXAS_LOG "Checking pre-requisite patch $prereq_patch..."
    patch_applied=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$prereq_file "$prereq_patch"`
    exit_code=$?
    if [ $exit_code -ne 0 ];
    then
      printf "\nERROR: Unable to check Oracle patch prerequisites.\n"
      printf "Status: $patch_applied\n"
      exit 1;
    fi
    if [ ! "$patch_applied" = "Y" ];
    then
      printf "\nERROR: The following pre-requisite Oracle patch has not been applied: $prereq_patch\n" >> $missing_patches
      #printf "\nPatch $prereq_patch\n"
    fi
    $XXAS_LOG "ok"
  fi
done

#
# Export the apps release variable
#
XXAS_MAJOR_APPS_REL=$major_apps_release
export XXAS_MAJOR_APPS_REL


