#!/bin/csh

##############################################################################
#
# Filename         - $Workfile:   gen_stats.csh  $
#
#                     Name              Date      Version     CR Number
# Author           - See revision history below.
# Last Update      - See revision history below.
#
# Project Name     - TAGTRAK(Oracle database)
#
# Source Code
# Identifier       - 0/3/300/8010/112
#
# Design Spec
# Section          - 
#
# Contents         - Script to generate Tagtrak loading stats for the
#                    last whole hour
#
# Language         - UNIX C-shell script
#
# Classification   - Commercial In Confidence
#
#          (c) Ultra Electronics Limited, 1998
#
##############################################################################

##############################################################################
# @(#)$Workfile:   gen_stats.csh  $  $Revision:   1.4  $
#
# Revision History
#
# Note that user tagtrak is many users. For these users the user
# name is entered as the last line in the change description.
#
# $Log:   P:/PVCSDATA/UltraTrakCore/Database/archive_n_backup/gen_stats.csh-arc  $
# 
#    Rev 1.4   08 Nov 2006 11:44:42   ian.byass
# Move Core 9.14.1 to Trunk
# 
#    Rev 1.3.1.0   14 Mar 2006 13:44:36   billy.hayer
# CA01779
# 
#    Rev 1.3   06 Mar 2006 14:43:52   david.rolfe
# Core 9.13 BRAVO merged to trunk
# 
#    Rev 1.2.1.1   21 Dec 2005 18:10:32   himanshu.saini
# CA01474 - remove superfluous screen output by gen_stats and archive_n_backup
# 
#    Rev 1.2.1.0   17 Nov 2005 17:02:06   david.rolfe
# Bravo Baseline
# 
#    Rev 1.2   15 Nov 2005 10:32:16   david.rolfe
# Core 9.12 moved to tip
# 
#    Rev 1.1   Apr 22 2005 11:12:52   yomi.kogbe
# CA01277
# 
#    Rev 1.0   Sep 22 2003 14:25:06   mik.davis
# Initial revision.
# 
#    Rev 1.3   Jun 19 2003 11:25:22   mik.davis
# FAS/FALCP/CA/053: Invalidate a flight's HBMD if its flight status is rolled back from 'PD'
# 
# FAS/FALCP/CA/056: Bug fixes to copy_pax.sql, proc8.sql, arrival_flight_load_popup_v.sql
# 
# FAS/FALCP/CA/013: Flight Closeout
# 
# FAS/FALCP/CA/045: Carrier code Lookup fixes for Bara FIDS
# 
# FAS/FALCP/CA/058: For fetching inbound flight states from the database.
# 
#    Rev 1.2   02 Apr 1998 19:49:24   tagtrak
# OR:
# CR:
# REV:
# Initial Issue
# See label for concession
# 
#    Rev 1.1   20 Mar 1998 15:03:38   tagtrak
# OR:
# CR:
# REV:
# Removed superflous reference to unrelated sql script
# 
#    Rev 1.0   26 Feb 1998 15:47:36   tagtrak
# OR:
# CR:
# REV:
# Initial core product version
#
##############################################################################


$ORACLE_HOME/bin/sqlplus -s '/as sysdba' << EOF
        SET PAGESIZE 0  ECHO OFF FEEDBACK OFF HEADING OFF HEAD OFF TERMOUT OFF
        @$cmdpath/test_database
EOF

set db_mode = `cat $cmdpath/primary.lst | egrep 'PRIMARY|PHYSICAL' | awk '{print $1}'`

logger -p user.info -t stats "db mode is $db_mode"

#
# We expect airport code  arguments.
#
if ( $#argv !=  1 ) then
	echo "usage: $0 supply airport code i.e. SYD"
	echo "$0  SYD"
	exit 1
endif

logger -p user.info -t stats "Generating stats"

#
# Determine Oracle username and password for live schema
# either by using the environment vars or the default values.
#
if ( $?LIVE_USER_NAME ) then
	set username = $LIVE_USER_NAME
	logger -p user.info -t stats "(using oracle account: $username)"
else
	set username = "live"
endif

if ( $?LIVE_PASSWORD ) then
	set password = $LIVE_PASSWORD
else
	set password = "live"
endif

#
# Location of sql scripts.
#
if ( $?SQL_SCRIPT_PATH ) then
	set sqldir = $SQL_SCRIPT_PATH"/"
else
	set sqldir = ""
endif

if ( $db_mode == "" ) then
   logger -p user.info -t gen_stats \
                "Warning: gen_stats not executed : Cannot Determine database mode."

else if ( $db_mode == "PRIMARY" ) then

	#
	# Use Oracle SQL*Plus to execute the procedure.
	#
	sqlplus -s $username/$password <<EOT
	whenever oserror exit sql.oscode rollback
	whenever sqlerror exit sql.sqlcode rollback
	set feedback off
	execute stats_last_hour(null, '$1');
	quit;
EOT

else
	logger -p user.info -t gen_stats \
                "Warning: gen_stats  not executed : Database transaction not allowed on standby database."

endif

set sqlstatus = $status

if ($sqlstatus != 0) then
	logger -p user.info -t stats "Warning: operation did not complete succesfully (status $sqlstatus)."
else
	logger -p user.info -t stats "Finished OK."
else
endif
