#!/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] <action:all|metaonly|nometa>"

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

#
# Convert the argument to variables
#
manifest=$1
action=$2
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}'`

#
# Set the location of the prereq source file
#
apps_rel_file=$XXAS_PATCH_SQL/xxas_com_appsrel.sql
xxas_com_apply_nls=$XXAS_PATCH_BIN/xxas_com_apply_nls.sh
#
# 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

#
# 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 Apply Action
#
if [ ${action}x = "x" ];
then
  action="all"
elif test "$action" != "all" -a "$action" != "metaonly" -a \
          "$action" != "nometa" ;
then
   printf "\nERROR: The Action must be 'all', 'metaonly' or 'nometa' but is '$action'\n\n"
   exit 1;
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

#
# Check the Destination File Top directory
#
eval target_top=\${$file_top}
if test ! -d "$target_top" ; then
  printf "\nERROR: The following Product Top cannot be found:\n"
  printf "$target_top\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

#
# Copy the file if the patch delivers a newer version or the SAME version
# of the file. It is copied when the version is the same because in multi-
# node installations (when the APPL_TOP is not shared) the patch must be
# applied to every APPL_TOP so that the files exist on all nodes. So even
# if the file is on the db it may not have been copied to the APPL_TOP.
#
if [ $patch_ver -ge $db_ver -a "$action" != "metaonly" ];
then
  #
  # Create the Destination File Directory if it does not already exist
  #
  target_dir=$target_top/$file_dir
  if test ! -d "$target_dir" ; then
    mkdir -p $target_dir
    chmod 775 $target_dir
  fi
  #
  # Copy the Patch File
  #
  $XXAS_LOG "Copying file."
  target_file=$target_dir/$file_name
  cp $source_file $target_file
  chmod 775 $target_file
fi

#
# Only apply the file to the database if the patch delivers a newer
# version and this isn't a metadata-only run.
# Jsps are recompiled even if the same version is being applied - this
# way jsps on multiple file systems are all compiled.
#
if [ \( $patch_ver -gt $db_ver \) -o \
     \( $patch_ver -ge $db_ver -a \( "$phase" = "jsp" -o "$phase" = "unzip" -o "$phase" = "nls" \) \) ];
then
  $XXAS_LOG "Applying file."
  if [ "$action" != "metaonly" ];
  then
    #
    # Apply the file according to its phase.
    #
    case $phase in
    copyonly|bg)
      #
      # do Nothing
      #
    ;;
    unzip)
      #
      # Unzip the contents into the target directory
      #
      unzip -qo -d $target_dir $target_file
      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
    ;;
    schema)
      #
      # Only run this step if creating the XXAS schema.
      # connect with SYSTEM privileges.
      #
      if test "$XXAS_SCHEMA_UN" = "XXAS" ;
      then
		xxas_com_applylog=xxas_com_apply_`date +%F_%H%M%S`.log
        $XXAS_SQLPLUS -s SYSTEM/$XXAS_SYSTEM_PW @$target_file \
                         $XXAS_SCHEMA_UN $XXAS_SCHEMA_PW > $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 "$target_file\n\n"
          printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
		  exit 1;
	  else
		  rm -f $xxas_com_applylog
        fi
      fi
    ;;
	register)
      #
      # Only run this step if creating the XXAS schema.
      #
      if test "$XXAS_SCHEMA_UN" = "XXAS" ;
      then
		xxas_com_applylog=xxas_com_apply_`date +%F_%H%M%S`.log
        $XXAS_SQLPLUS -s $XXAS_APPS_UN/$XXAS_APPS_PW @$target_file \
                         $XXAS_SCHEMA_UN > $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 "$target_file\n\n"
          printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
		  exit 1;
	  else
		  rm -f $xxas_com_applylog
        fi
      fi
    ;;
    tab|seq)
      #
      # Create a new table or sequence.
      # Connect as the schema owner; pass the APPS user name.
      #
	if [ "$file_name" = "xxas_com_patch_upd_tab.sql"  ]; then
		appltop_id=`$XXAS_SQLPLUS -s $XXAS_SCHEMA_UN/$XXAS_SCHEMA_PW @$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 @$target_file \
                       $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 "$target_file\n\n"
			printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
			exit 1;
		else
			rm -f $xxas_com_applylog
		fi
	else
		xxas_com_applylog=xxas_com_apply_`date +%F_%H%M%S`.log
		$XXAS_SQLPLUS -s $XXAS_SCHEMA_UN/$XXAS_SCHEMA_PW @$target_file \
                       $XXAS_APPS_UN > $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 "$target_file\n\n"
			printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
			exit 1;
		else
			rm -f $xxas_com_applylog
		fi
	fi
    ;;
    ind)
      #
      # Create a new index as the schema owner.
      #
	  xxas_com_applylog=xxas_com_apply_`date +%F_%H%M%S`.log
      $XXAS_SQLPLUS -s $XXAS_SCHEMA_UN/$XXAS_SCHEMA_PW @$target_file > $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 "$target_file\n\n"
        printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
        exit 1;
	  else
		rm -f $xxas_com_applylog
      fi
    ;;
    syn|sql_s_un)
      #
      # Run as the APPS user; pass the schema owner
      #
	  xxas_com_applylog=xxas_com_apply_`date +%F_%H%M%S`.log
      $XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$target_file $XXAS_SCHEMA_UN > $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 "$target_file\n\n"
		printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
        exit 1;
	  else
		rm -f $xxas_com_applylog
      fi
    ;;
    type|view|view_no_dep|pkh|pkb|upd|dat_dg|hooks|upd_post_dat)
      #
      # Run a sql script as the APPS user
      #
	  xxas_com_applylog=xxas_com_apply_`date +%F_%H%M%S`.log
      $XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$target_file > $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 "$target_file\n\n"
		printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
        exit 1;
	  else
		rm -f $xxas_com_applylog
      fi
    ;;
    dat_app|dat_role|dat_menu|dat_lkp|dat_cp|dat_rg|dat_rs|dat_flex|dat_xmlp|dat_resp|dat_msg|dat_form|dat_prof|dat_eitsec|dat_amettype|dat_ameattr|dat_ameattru|dat_amesappg|dat_amesaagc|dat_ameconk|dat_amerule|dat_ameactu|dat_rset|dat_xxasmenu|dat_page|dat_pg|dat_prf|dat_alert|dat_attach|dat_udtrtl)
      #
      # Set the loader file
      #
	  xxas_com_applylog=xxas_com_apply_ldt_`date +%F_%H%M%S`.log
      case $phase in
        dat_app)      loader=$FND_TOP/patch/115/import/afscapp.lct ;;
        dat_role)     loader=$FND_TOP/patch/115/import/afrole.lct ;;
        dat_menu)     loader=$FND_TOP/patch/115/import/afsload.lct ;;
        dat_lkp)      loader=$FND_TOP/patch/115/import/aflvmlu.lct ;;
        dat_cp)       loader=$FND_TOP/patch/115/import/afcpprog.lct ;;
        dat_rg)       loader=$FND_TOP/patch/115/import/afcpreqg.lct ;;
        dat_rs)       loader=$FND_TOP/patch/115/import/afcprset.lct ;;
        dat_flex)     loader=$FND_TOP/patch/115/import/afffload.lct ;;
        dat_xmlp)     loader=$XDO_TOP/patch/115/import/xdotmpl.lct ;;
        dat_resp)     loader=$FND_TOP/patch/115/import/afscursp.lct ;;
        dat_msg)      loader=$FND_TOP/patch/115/import/afmdmsg.lct ;;
        dat_form)     loader=$FND_TOP/patch/115/import/affrmcus.lct ;;
        dat_rset)     loader=$FND_TOP/patch/115/import/afcprset.lct ;;
        dat_prof)     loader=$FND_TOP/patch/115/import/afscprof.lct ;;
        dat_eitsec)   loader=$PER_TOP/patch/115/import/perinfts.lct ;;
        dat_amettype) loader=$AME_TOP/patch/115/import/amescvar.lct ;;
        dat_ameattr)  loader=$AME_TOP/patch/115/import/amesmatt.lct ;;
        dat_ameattru) loader=$AME_TOP/patch/115/import/amesmatr.lct ;;
        dat_amesappg) loader=$AME_TOP/patch/115/import/amesappg.lct ;;
        dat_amesaagc) loader=$AME_TOP/patch/115/import/amesaagc.lct ;;
        dat_ameconk)  loader=$AME_TOP/patch/115/import/amesconk.lct ;;
        dat_amerule)  loader=$AME_TOP/patch/115/import/amesrulk.lct ;;
        dat_ameactu)  loader=$AME_TOP/patch/115/import/amesactu.lct ;;
        dat_xxasmenu) loader=$XXAS_TOP/import/xxas_menu_loader.lct ;;
        dat_page)     loader=$XXAS_TOP/import/xxas_page_loader.lct ;;
        dat_pg)       loader=$XXAS_TOP/import/xxas_full_page_loader.lct ;;
        dat_prf)      loader=$XXAS_TOP/import/xxas_performance_loader.lct ;;
        dat_alert)    loader=$ALR_TOP/patch/115/import/alr.lct ;;
        dat_attach)   loader=$FND_TOP/patch/115/import/afattach.lct ;;
		dat_udtrtl)   loader=$XXAS_TOP/patch/115/import/xxas_udt_row_mls.lct ;;
        *)
          printf "\nERROR: The phase "$phase" for the following file is unknown:\n\n"
          printf "$target_file\n\n"
          exit 1;
      esac

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

      #
      # Use FNDLOAD to load the data
      #
      $XXAS_FNDLOAD $XXAS_APPS_LOGIN 0 Y UPLOAD $loader $target_file 2> /dev/null
      exit_code=$?
      #
      # Skip over failed application files. This will happen if the app short code
      # exists with a different Id; it's fine to continue with the existing app
      #
      if [ $exit_code -ne 0 -a "$phase" != "dat_app" ];
      then
	  	failed_ldt=`ls -tlr L*.log |tail -1|awk '{print $9}'`
		cat $failed_ldt > $xxas_com_applylog
        printf "\nERROR: The following file failed to apply:\n\n"
        printf "$target_file\n\n"
		printf "Please check the log file $xxas_com_applylog for error(s).\n\n"
        exit 1;
      fi
    ;;
    wft)
      #
      # Load the Workflow file using WFLOAD
      #
      $XXAS_WFLOAD $XXAS_APPS_LOGIN 0 Y UPGRADE $target_file 1> /dev/null 2> /dev/null
      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
    ;;
    wfx)
      #
      # Load the Workflow Business Event/Subscription using WFXLoad
      
      wfx_rslt=`adjava oracle.apps.fnd.wf.WFXLoad \
               -u $XXAS_APPS_UN $XXAS_APPS_PW "$XXAS_JDBC_CONN" thin US $target_file \
               1> /dev/null 2> /dev/null`
      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
    ;;
    jsp)
      #
      # The JSP compiler is located in a different place for 11i and 12
      #
      if [ $XXAS_MAJOR_APPS_REL -lt 120 ];
      then
        #
        # Use the 11i JSP compiler
        #
        jspcompiler=$JTF_TOP/admin/scripts/ojspCompile.pl
      else
        jspcompiler=$FND_TOP/patch/115/bin/ojspCompile.pl
      fi

      #
      # Verify that the jsp compiler exists
      #
      if test ! -f "$jspcompiler" ; then
        printf "\nERROR: The following JSP compiler cannot be found:\n"
        printf "$loader\n\n"
        exit 1;
      fi

      #
      # Compile the JSP
      #
      jsp_rslt=`$jspcompiler --compile -s $file_name 2> /dev/null`
      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
    ;;
    mds)
      #
      # Load the OAF metadata
      #
      mds_rslt=`java oracle.jrad.tools.xml.importer.XMLImporter $target_file \
               -username $XXAS_APPS_UN -password $XXAS_APPS_PW \
               -rootdir $XXAS_PROD_TOP/mds -rootPackage /xxas/oracle/apps/xxas \
               -dbConnection "$XXAS_JDBC_CONN"`
      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
    ;;
    dat_xmls)
      #
      # Load an XML Publisher Template Source file
      #
      # Derive various parameters from the filename itself.
      #
      xmls_appshortname=`echo "$file_name"|awk -F_ '{print $3}'`
      xmls_lobcode=`echo "$file_name"|awk -F_ '{print $4}'`
      xmls_language=`echo "$file_name"|awk -F_ '{print $5}'`
      xmls_territory=`echo "$file_name"|awk -F_ '{print $6}'|awk -F. '{print $1}'`
      xmls_fileext=`echo "$file_name"|awk -F. '{print $NF}'|tr '[:lower:]' '[:upper:]'`
      
      #
      # Check the App Short Name
      #
      if [ ${xmls_appshortname}x = "x" ];
      then
         printf "\nERROR: The Application Short Name could not be derived from the following Template Source File:\n"
         printf "$target_file\n\n"
         exit 1;
      fi
      #
      # Check the LOB Code
      #
      if [ ${xmls_lobcode}x = "x" ];
      then
         printf "\nERROR: The LOB Code could not be derived from the following Template Source File:\n"
         printf "$target_file\n\n"
         exit 1;
      fi
      #
      # Check the Language
      #
      if [ ${xmls_language}x = "x" ];
      then
         printf "\nERROR: The Language Code could not be derived from the following Template Source File:\n"
         printf "$target_file\n\n"
         exit 1;
      fi
      #
      # Default the Territory
      #
      if [ ${xmls_territory}x = "x" ];
      then
         xmls_territory="00"
      fi
      #
      # Check the File Extension
      #
      if [ ${xmls_fileext}x = "x" ];
      then
         printf "\nERROR: The File Type could not be derived from the following Template Source File:\n"
         printf "$target_file\n\n"
         exit 1;
      fi
      #
      # Check the File Content Type
      #
      if [ ${xmls_fileext} = "RTF" ];
      then
         xmls_contenttype="application/rtf"
      elif [ ${xmls_fileext} = "XLS" ];
      then
         xmls_contenttype="application/vnd.ms-excel"
      fi
      #
      # Check the File Content Type
      #
      if [ ${xmls_contenttype}x = "x" ];
      then
         printf "\nERROR: The File Content Type could not be derived from the following Template Source File:\n"
         printf "$target_file\n\n"
         exit 1;
      fi
      
      xmlt_rslt=`java oracle.apps.xdo.oa.util.XDOLoader UPLOAD \
              -DB_USERNAME $XXAS_APPS_UN \
              -DB_PASSWORD $XXAS_APPS_PW \
              -JDBC_CONNECTION "$AD_APPS_JDBC_URL" \
              -LOB_TYPE TEMPLATE \
              -APPS_SHORT_NAME "$xmls_appshortname" \
              -LOB_CODE "$xmls_lobcode" \
              -LANGUAGE "$xmls_language" \
              -TERRITORY "$xmls_territory" \
              -XDO_FILE_TYPE "$xmls_fileext" \
              -FILE_CONTENT_TYPE "$xmls_contenttype" \
              -FILE_NAME $target_file \
              -OWNER XXAS \
              -CUSTOM_MODE FORCE`
     exit_code=$?
     #
     # Commented out because XDOLoader reports XML errors on templates that still work
     # (ie, not fatal errors)
     #
     # if [ $exit_code -ne 0 ];
     # then
     #   printf "\nERROR: The following file failed to apply:\n\n"
     #   printf "$target_file\n\n"
     #   exit 1;
     # fi
    ;;
    manual)
      #
      # Output the manual steps and confirm with the user they have
      # been completed. 
      #    
	apps_release=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$apps_rel_file`
	m_apps_release=`echo "$apps_release" | awk -F. '{print $1}'`
	if [ "$target_file" = "$target_dir/xxas_com_env_manual.txt" -a "$m_apps_release" = "12" ]; then
		printf "\nUpdating APPS env with XXAS_TOP environment variable\n"
		XXAS_CUSTOM_FILE=`grep customfile= $APPL_TOP/APPS$CONTEXT_NAME.env|awk -F= '{print $2}'`
		if test -f $XXAS_CUSTOM_FILE; then
			XXAS_TOP_CHK=`grep "XXAS_TOP=" $XXAS_CUSTOM_FILE`
			if [ "${XXAS_TOP_CHK}x" = "x" ]; then
				printf "Appending XXAS_TOP env variable to $XXAS_CUSTOM_FILE\n"
				echo XXAS_TOP=$APPL_TOP/xxas/1.0.1 >> $XXAS_CUSTOM_FILE
				echo export XXAS_TOP >> $XXAS_CUSTOM_FILE
			fi
		else
			printf "$XXAS_CUSTOM_FILE is not present, creating it now\n"
			echo XXAS_TOP=$APPL_TOP/xxas/1.0.1 > $XXAS_CUSTOM_FILE
			echo export XXAS_TOP >> $XXAS_CUSTOM_FILE
		fi
	else
	  	clear
      		printf "\nThis patch requires manual steps to be performed. Please follow\n"
      		printf "the below instructions and take care to perform them exactly\n"
      		printf "as described:\n\n"
      		cat $target_file
      		while true
      		do
        	printf "\nPlease confirm that you have completed these steps (Y): "
        	read continue_flag
        	case $continue_flag in
          	y|Y|YES|yes|Yes)
            	break
          	;;
        	esac
      		done
	fi
    ;;
	nls)
	  #
	  # Get the list of installed languages.
	  #
	  nls_inst_lang=$XXAS_PATCH_SQL/xxas_com_nls_inst_lst.sql
	  $XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$nls_inst_lang > installed_lang.txt

	  if test -s installed_lang.txt; then
		  #
		  # Unzip the contents into the target directory
		  #
		  unzip -qo -d $target_dir $target_file
		  unzip -qo -d $XXAS_PATCH_TOP/$file_top/$file_dir $target_file
		  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
		  $xxas_com_apply_nls $patch_ver $file_dir
	  fi
    ;;
    *)
      printf "\nERROR: The phase "$phase" for the following file is unknown:
\n\n"
      printf "$target_file\n\n"
      exit 1;
    ;;
    esac
  fi
  if [ "$action" != "nometa" -a "$phase" != "bg" ];
  then
    $XXAS_LOG "Updating metadata."
    #
    # 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

exit 0

