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

#
# Validate the number of arguments
#
if [ $# -ne 1 ];
then
   printf "\n$program: this program requires one argument.\n\n"
   printf "$usage\n\n"
   exit 1;
fi

#
# Convert the argument to variables
#
manifest=$1
action_type=`echo "$manifest" | awk -F: '{print $1}'`
file_top=`echo "$manifest" | awk -F: '{print $2}'`
file_dir=`echo "$manifest" | awk -F: '{print $3}'`
file_name=`echo "$manifest" | awk -F: '{print $4}'`
patch_ver=`echo "$manifest" | awk -F: '{print $5}'`
phase=`echo "$manifest" | awk -F: '{print $6}'`

#
# Check the Action Type
#
if [ ${action_type}x = "x" ];
then
   printf "\nERROR: The Action Type could not be read from the following manifest:\n"
   printf "$manifest\n\n"
   exit 1;
elif test "$action_type" != "file" ;
then
   printf "\nERROR: The Action Type in the manifest must be set to 'file' but is set to '$action_type'\n\n"
   exit 1;
fi

#
# 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 the File exists
#
eval target_top=\${$file_top}
target_dir=$target_top/$file_dir
target_file=$target_dir/$file_name
if test ! -f "$target_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$target_file\n\n"
  exit 1;
fi

chk_ver_file=$XXAS_PROD_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

#
# Apply the file as long as this patch does not deliver an older
# version. Note that the file can be re-run for each business group.
#
if [ $patch_ver -ge $db_ver ];
then
  #
  # Apply the file with the given business group
  #
  $XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$target_file $XXAS_BUSINESS_GROUP_ID \
                   $XXAS_FND_USER_ID
  exit_code=$?
  if [ $exit_code -ne 0 ];
  then
    printf "\nERROR: The following file failed to apply:\n\n"
    printf "$target_file\n\n"
    exit 1;
  fi
  #
  # Update the metadata with the applied file
  #
  upd_applied_file=$XXAS_PROD_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

exit 0

