#!/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 "\n\nStarting Applaud Solutions Business Group Data Installer\n"
printf "version $prog_version...\n\n"

#
# Separate each main argument into variables
#
conc_request_id=`echo $* | cut -f2 -d" " | cut -c11-`
apps_login=`echo $* | cut -f3 -d" " | cut -c11- | sed 's/\"//g'`
fnd_user_id=`echo $* | cut -f4 -d" " | cut -c12-`
fnd_user_name=`echo $* | cut -f5 -d" " | cut -c14- | sed 's/\"//g'`
patch_name=`echo $* | cut -f9 -d" "| sed 's/\"//g'`
bg_id=`echo $* | cut -f10 -d" "| sed 's/\"//g'`

#
# Validate the apps login is set
#
if test ! "$apps_login" ; then
  printf "\nERROR: The APPS credentials could not be read.\n\n"
  exit 1;
fi
apps_un=`echo $apps_login | awk -F/ '{print $1}' | tr '[:lower:]' '[:upper:]'`
apps_pw=`echo $apps_login | awk -F/ '{print $2}'`

#
# Validate the patch_name argument is set
#
if test ! "$patch_name" ; then
  printf "\nERROR: The Patch Name could not be read.\n\n"
  exit 1;
fi

#
# Validate the business group id argument is set
#
if test ! "$bg_id" ; then
  printf "\nERROR: The Business Group Id could not be read.\n\n"
  exit 1;
fi

#
# Validate that APPL_TOP is set
#
if test ! "$APPL_TOP" ; then
   printf "\nERROR: The "\$"APPL_TOP environment variable is not set.\n\n"
   exit 1;
fi

#
# Validate that APPL_TOP is a valid directory
#
if test ! -d "$APPL_TOP" ; then
   printf "ERROR: The following "\$"APPL_TOP directory does not exist:\n"
   printf "$APPL_TOP\n"
   exit 1;
fi

#
# Validate that the Product Top is a valid directory
#
prod_top=$APPL_TOP/xxas/1.0.1
if test ! -d "$prod_top" ; then
   printf "ERROR: The following Product Top does not exist:\n"
   printf "$prod_top\n"
   exit 1;
fi

#
# Validate that the Patch Top is a valid directory
#
patch_top=$prod_top/patch/$patch_name
if test ! -d "$patch_top" ; then
   printf "ERROR: The following patch directory does not exist:\n"
   printf "$patch_top\n\n"
   exit 1;
fi

#
# Set environment variables
#
XXAS_APPS_UN=$apps_un; export XXAS_APPS_UN
XXAS_APPS_PW=$apps_pw; export XXAS_APPS_PW
XXAS_APPS_LOGIN=$XXAS_APPS_UN/$XXAS_APPS_PW; export XXAS_APPS_LOGIN
XXAS_PROD_TOP=$prod_top; export XXAS_PROD_TOP
XXAS_PROD_BIN=$XXAS_PROD_TOP/bin; export XXAS_PROD_BIN
XXAS_PROD_SQL=$XXAS_PROD_TOP/sql; export XXAS_PROD_SQL
XXAS_PROD_IMPORT=$XXAS_PROD_TOP/import; export XXAS_PROD_IMPORT
XXAS_PATCH_TOP=$patch_top; export XXAS_PATCH_TOP
XXAS_PATCH_NAME=$patch_name; export XXAS_PATCH_NAME
XXAS_SQLPLUS=$ORACLE_HOME/bin/sqlplus; export XXAS_SQLPLUS
XXAS_FND_USER_ID=$fnd_user_id; export XXAS_FND_USER_ID
XXAS_BUSINESS_GROUP_ID=$bg_id; export XXAS_BUSINESS_GROUP_ID
HOST_NAME=`hostname|awk -F. '{print $1}'`; export HOST_NAME

#
# Set IFS to just a new line to avoid delimiting by space
#
IFS="
"
export IFS

#
# Check that the SQLPlus Test Script is available
#
sqlplustest_file=$XXAS_PROD_BIN/xxas_com_testsqlp.sh
if test ! -f "$sqlplustest_file" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$sqlplustest_file\n\n"
  exit 1;
fi

#
# Test the sqlplus executable and that the apps logon works
#
$XXAS_PROD_BIN/xxas_com_testsqlp.sh $XXAS_APPS_UN $XXAS_APPS_PW
exit_code=$?
if [ $exit_code -ne 0 ];
then
  exit 1;
fi

# Set the location of the patch manifest
#
manifest_file=$XXAS_PATCH_TOP/xxas_com_manifest.txt
ptchstatus_file=$XXAS_PROD_SQL/xxas_com_ptchstatus_node.sql

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

#
# Check that the patch status exists
#
ptchstatus_file=$XXAS_PROD_SQL/xxas_com_ptchstatus_node.sql
if test ! -f "$ptchstatus_file" ; then
  printf "\nERROR: The following SQL file cannot be found:\n"
  printf "$ptchstatus_file\n\n"
  exit 1;
fi

#
# Get the status of this patch
#
patch_status=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$ptchstatus_file $XXAS_PATCH_NAME master $HOST_NAME`
exit_code=$?
if [ $exit_code -ne 0 ];
then
  printf "\nERROR: The status of the following patch could not be determined.\n"
  printf "$XXAS_PATCH_NAME\n\n"
  exit 1;
fi

#
# Check the status of this patch
#
if [ ! "$patch_status" = "C" ];
then
  printf "\nERROR: This patch has not been succesfully installed.\n"
  printf "Please check with your Database Administrator.\n\n"
  exit 1;
fi

#
# Check that the given business group Id exists
#
bgexists_file=$XXAS_PROD_SQL/xxas_com_chkbg.sql
if test ! -f "$bgexists_file" ; then
  printf "\nERROR: The following SQL file cannot be found:\n"
  printf "$bgexists_file\n\n"
  exit 1;
fi
bg_exists=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$bgexists_file \
           $XXAS_BUSINESS_GROUP_ID`
if [ ! "$bg_exists" = "Y" ];
then
  printf "\nERROR: The following Business Group cannot be found:\n"
  printf "Id: $XXAS_BUSINESS_GROUP_ID\n\n"
  exit 1;
fi

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

#
# Install all business group data
#
for line in `cat $manifest_file`;
do
  line_action=`echo "$line" | awk -F: '{print $1}'`
  line_phase=`echo "$line" | awk -F: '{print $6}'`
  if test "$line_action" = "file" ;
  then
    if test "$line_phase" = "bg" ;
    then
      #
      # Apply each file
      #
      $applyfile "$line"
      exit_code=$?
      if [ $exit_code -ne 0 ];
      then
        exit 1;
      fi
    fi
  fi
done

#
# Confirm that installation has been completed
#
printf "\nBusiness Group Data Installation has completed successfully.\n"

exit 0
 
