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

printf "\n\nStarting Applaud Solutions Installer version $prog_version...\n\n"

#
# Quit if too many arguments have been supplied
#
if [ $# -gt 1 ];
then
   printf "\n$program: installation has only one argument.\n\n"
   printf "$usage\n\n"
   exit 0;
fi

#
# Print help if requested
#
if [ $# -eq 1 -a "$1" = "help" ];
then
   printf "\n$program: installation has only one argument.\n\n"
   printf "$usage\n\n"
   exit 0;
fi

#
# Print help if requested
#
if [ $# -eq 1 -a "$1" = "?" ];
then
   printf "\n$program: installation has only one argument.\n\n"
   printf "$usage\n\n"
   exit 0;
fi

#
# Set the APPS username; error if $FNDNAM is not set
#
apps_un=$FNDNAM
if [ ${apps_un}x = "x" ];
then
  printf "\nERROR: Please initialize your application environment \n"
  printf "before running this install.\n\n"
  exit 1;

fi

#
# Use the APPS password if it has been passed in, otherwise request it
#
apps_pw=$1
while test "${apps_pw:=}x" = "x"
do
  printf "\nEnter the $apps_un password: "
  stty -echo
  read apps_pw
  stty echo
  printf "\n\n"
done

#
# Set key environment variables
#
patch_top=`dirname $0`;
xxas_bin=$patch_top/XXAS_TOP/bin
setenv_file=$xxas_bin/xxas_com_setenv.sh
prereq_file=$xxas_bin/xxas_com_prereq.sh
jdbc_file=$xxas_bin/xxas_com_jdbc.sh
chkptch_file=$xxas_bin/xxas_com_chkptch.sh
setschema_file=$xxas_bin/xxas_com_setschema.sh
oneti_file=$xxas_bin/xxas_com_oneti.sh
chkfiles_file=$xxas_bin/xxas_com_chkfiles.sh
doinstall_file=$xxas_bin/xxas_com_doinstall.sh
postreq_file=$xxas_bin/xxas_com_postreq.sh
XXAS_PATCH_TOP=$patch_top; export XXAS_PATCH_TOP
XXAS_APPS_UN=$apps_un; export XXAS_APPS_UN
XXAS_APPS_PW=$apps_pw; export XXAS_APPS_PW
XXAS_LOGFILE=xxas_install_`date +%F_%H%M%S`.log; export XXAS_LOGFILE
XXAS_LOG=$xxas_bin/xxas_com_log.sh; export XXAS_LOG

#
# Check the patch log script exists
#
if test ! -f "$XXAS_LOG" ; then
   printf "\nERROR: The following log script does not exist:\n"
   printf "$XXAS_LOG\n\n"
   exit 1;
fi

#
# Initialize logging
#
$XXAS_LOG "Starting Applaud Solutions Installer version $prog_version."

#
# Check the patch log file was created
#
if test ! -f "$XXAS_LOGFILE" ; then
   printf "\nERROR: The following patch log file could not be created:\n"
   printf "$XXAS_LOGFILE\n\n"
   exit 1;
fi

$XXAS_LOG "Started on: `date`"
$XXAS_LOG "The environment for this session is:"
$XXAS_LOG "Patch Top:       $XXAS_PATCH_TOP"
$XXAS_LOG "APPS Username:   $XXAS_APPS_UN"
printf "The log file for this install session is:\n"
printf "$XXAS_LOGFILE\n\n"

#
# Check the patch environment file exists
#
if test ! -f "$setenv_file" ; then
   printf "\nERROR: The following Environment File cannot be found:\n"
   printf "$setenv_file\n\n"
   exit 1;
fi

#
# Check the environment and set all environment variables
#
. $setenv_file

#
# Check the pre-requisite checker file exists
#
if test ! -f "$prereq_file" ; then
   printf "\nERROR: The following Pre-requisite Checker File cannot be found:\n"
   printf "$prereq_file\n\n"
   exit 1;
fi

#
# Check the pre-reqs
#
. $prereq_file
if test -s $XXAS_PATCH_TOP/missing_patches.txt; then
	cat $missing_patches
	rm -f $XXAS_PATCH_TOP/missing_patches.txt
	exit 1;
fi

#
#
# Check the patch checker file exists
#
if test ! -f "$chkptch_file" ; then
   printf "\nERROR: The following Install File cannot be found:\n"
   printf "$chkptch_file\n\n"
   exit 1;
fi

#
# Check the patch
#
. $chkptch_file

#
# Check the set schema file exists
#
if test ! -f "$setschema_file" ; then
   printf "\nERROR: The following Install File cannot be found:\n"
   printf "$setschema_file\n\n"
   exit 1;
fi

#
# Set the schema and related environment variables
#
. $setschema_file

#
# Check the set one-time install file exists
#
if test ! -f "$oneti_file" ; then
   printf "\nERROR: The following Install File cannot be found:\n"
   printf "$oneti_file\n\n"
   exit 1;
fi

#
# If this is the first install, perform the one-time installation
#
if test "$XXAS_ONETI" = "Y" ;
then
  #
  # Run the one-time install
  #
  $oneti_file
  exit_code=$?

  if [ $exit_code -ne 0 ];
  then
    printf "\nERROR: A problem occured during one-time installation.\n\n"
    exit 1;
  fi
fi

#
# Check the JDBC Connection file exists
#
if test ! -f "$jdbc_file" ; then
   printf "\nERROR: The following JDBC Connection File cannot be found:\n"
   printf "$jdbc_file\n\n"
   exit 1;
fi

#
# Set and test the JDBC connection
#
. $jdbc_file

#
# Check the patch files for any additional prerequisites
#
if test ! -f "$chkfiles_file" ; then
   printf "\nERROR: The following Install File cannot be found:\n"
   printf "$chkfiles_file\n\n"
   exit 1;
fi
. $chkfiles_file

#
# Check the main install file exists
#
if test ! -f "$doinstall_file" ; then
   printf "\nERROR: The following Install File cannot be found:\n"
   printf "$doinstall_file\n\n"
   exit 1;
fi

#
# Run the main installation
#
#printf "\nPatch installation will take several minutes so please do not\n"
#printf "interupt this session.\n\n"
printf "Installing, please wait...\n\n"
$doinstall_file
exit_code=$?
if [ $exit_code -ne 0 ];
then
  printf "\nERROR: A problem occured during installation.\n\n"
  exit 1;
fi

#
# Check the post req file exists
#
if test ! -f "$postreq_file" ; then
   printf "\nERROR: The following Install File cannot be found:\n"
   printf "$postreq_file\n\n"
   exit 1;
fi

#
# Confirm that installation has been completed
#
printf "\nInstallation has completed successfully.\n\n"

#
# Run the postreq file
#
$postreq_file
exit_code=$?
if [ $exit_code -ne 0 ];
then
  printf "\nERROR: A problem occured whilst checking post-requisites.\n\n"
  exit 1;
fi

exit 0
 
