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

$XXAS_LOG "Starting one-time install:"
#
# Set the applyfile script
#
applyfile=$XXAS_PATCH_BIN/xxas_com_applyfile.sh

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

#
# Apply wrapper function
#
apply () {
  $XXAS_LOG "Applying: $1..."
  $XXAS_LOG "Action: $2"
  $applyfile $1 $2
  exit_code=$?
  if [ $exit_code -ne 0 ];
  then
    exit 1;
  fi
  $XXAS_LOG "Finished applying: $1"
}

$XXAS_LOG "Checking for XXAS Product Top..."
#
# Create the XXAS Product Top
#
if test ! -d "$XXAS_TOP" ; then
  $XXAS_LOG "Creating Product Top"
  mkdir -p $XXAS_TOP
  chmod 775 $XXAS_TOP
fi
$XXAS_LOG "Product Top ok"

#
# Create the minimum objects to start recording file manifest information:
# The schema, manifest table and synonym.
#
apply "file:XXAS_TOP:sql:xxas_com_schema.sql:1:schema" "nometa"
apply "file:XXAS_TOP:sql:xxas_com_reg_schema.sql:4487:register" "nometa"
apply "file:XXAS_TOP:sql:xxas_com_file_tab.sql:1:tab" "nometa"
apply "file:XXAS_TOP:sql:xxas_com_file_syn.sql:1:syn" "nometa"

#
# Populate the missing metadata for the previous files
#
apply "file:XXAS_TOP:sql:xxas_com_schema.sql:1:schema" "metaonly"
apply "file:XXAS_TOP:sql:xxas_com_reg_schema.sql:4487:register" "metaonly"
apply "file:XXAS_TOP:sql:xxas_com_file_tab.sql:1:tab" "metaonly"
apply "file:XXAS_TOP:sql:xxas_com_file_syn.sql:1:syn" "metaonly"

#
# Apply the rest of the essential tables and synonyms.
#
apply "file:XXAS_TOP:sql:xxas_com_install_tab.sql:1:tab"
apply "file:XXAS_TOP:sql:xxas_com_patch_tab.sql:1:tab"
apply "file:XXAS_TOP:sql:xxas_com_install_syn.sql:1:syn"
apply "file:XXAS_TOP:sql:xxas_com_patch_syn.sql:1:syn"

#
# Finally, load the name of the custom schema to prevent
# subsequent one-time installs.
#
apply "file:XXAS_TOP:sql:xxas_com_install.sql:1:sql_s_un"
#
# Updating XXAS schema password
#
$XXAS_LOG "Updating XXAS schema password"
FNDCPASS $XXAS_APPS_LOGIN 0 Y system/$XXAS_SYSTEM_PW ORACLE $XXAS_SCHEMA_UN $XXAS_SCHEMA_PW >/dev/null 2>&1
if [ $? -ne 0 ]; then
	printf "WARNING: Please updated the XXAS SCHEMA password \n"
	       "WARNING: at the end of installtion using FNDCPASS\n\n"
fi
$XXAS_LOG "Finished one-time install."

exit 0


