#!/bin/csh

##############################################################################
#
# Filename         - $Workfile:   del_arch_logs.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 purge archived redo log files which
#                    are older than the configured period.
#
# Language         - UNIX C-shell script
#
# Classification   - Commercial In Confidence
#
#          (c) Ultra Electronics Limited, 1998
#
##############################################################################

##############################################################################
# @(#)$Workfile:   del_arch_logs.csh  $  $Revision:   1.4.3.0  $
#
# 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/del_arch_logs.csh-arc  $
# 
#    Rev 1.4.3.0   28 Sep 2007 15:36:02   dave.banks
# CA02772 - File erroneously removed now reinstated
# 
#    Rev 1.4   15 Nov 2005 10:32:02   david.rolfe
# Core 9.12 moved to tip
# 
#    Rev 1.3   Jul 01 2005 12:54:32   ian.byass
# UAS/FALCP/CA/01425 del_arch_logs.csh does not clean up standby redo log files
# 
#    Rev 1.2   Apr 22 2005 11:11:06   yomi.kogbe
# CA01277
# 
#    Rev 1.1   Jun 02 2004 11:02:26   RJS
# CA/713 - remove log purge files after a week
# 
#    Rev 1.0.3.0   May 17 2004 16:12:32   RJS
# CA/713 - remove log purge files after a week
# 
#    Rev 1.0   Sep 22 2003 14:24:56   mik.davis
# Initial revision.
# 
#    Rev 1.3   Jun 19 2003 11:25:12   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.3   03 Apr 1998 09:39:48   tagtrak
# OR:
# CR:
# REV:
# Initial Issue
# See label for concession
# 
#    Rev 1.2   02 Apr 1998 19:53:20   tagtrak
# OR:
# CR:
# REV:
# Initial Issue
# See label for concession
# 
#    Rev 1.1   20 Mar 1998 14:53:34   tagtrak
# OR:
# CR:
# REV:
# Minor amends noticed while creating ATS
# 
#    Rev 1.0   25 Feb 1998 17:47:48   tagtrak
# OR:
# CR:
# REV:
# Initial Core Product version
#
##############################################################################

#
# We expect zero arguments.
#
if ( $#argv != 0 ) then
	echo "usage: $0"
	exit 1
endif

#
# Ensure we have the location of the config file.
#
if ( ! $?confpath ) then
	echo "Error: confpath environment variable not set - Aborting"
	logger -p user.info -t archlogpurge \
		"Error: confpath environment variable not set - Aborting"
	exit 1
endif

set config = $confpath/arbk.conf

#
# Archived log files more than 'maxage' days old will be purged.
#
set maxage = `grep ARCHLOG_LIFE $config | nawk '{ print $2 }'`

if ( $maxage == "" ) then
	echo "Error: Cannot extract ARCHLOG_LIFE param from $config - Aborting"
	logger -p user.info -t archlogpurge \
		"Error: Cannot extract ARCHLOG_LIFE param from $config - Aborting"
	exit 1
endif

logger -p user.info -t archlogpurge \
	"Removing Archived redo logs > $maxage days old"


#
# The parameter stored in Oracle can have a '?' to represent the
# value of ORACLE_HOME, so we need to replace this ourselves.
#

set archdest = `grep archive_dir $confpath/db.conf | awk '{print $2}'`

#
# Derive the directory and filename root from the parameter value.
#

set archdir = $archdest:h


set archroot = `grep archive_root $confpath/db.conf | awk '{print $2}'`


#
# Get the details of the Standby Log File location
#

set standbydest = `grep standby_dir $confpath/db.conf | awk '{print $2}'`
set standbydir = $standbydest:h

#
# Unsafe to proceed if we do not have positive directory and
# filename root values to work with.
#
if ( $archdir == "" ) then
	echo "Error: Unable to determine archive log directory. Aborting."
	logger -p user.info -t archlogpurge \
		"Error: Unable to determine archive log directory. Aborting."
	exit 1
endif

if ( $archroot == "" ) then
	echo "Error: Unable to determine archive log filename root. Aborting."
	logger -p user.info -t archlogpurge \
		"Error: Unable to determine archive log filename root. Aborting."
	exit 1
endif

eval set archdir = $archdir

set arbkdir = `grep -i archive_bk_dir $confpath/db.conf | awk '{print $2}'`
if ( $arbkdir == "" ) then
	echo "Warning: Unable to determine archive log backup directory."
	logger -p user.info -t archlogpurge \
		"Warning: Unable to determine archive log backup directory."
endif

set reportfile = ~/logpurge`date +%d%m%y`
echo "Archive Log Purge task - report at " `date` > $reportfile
#
# Find, record, and remove archive log files older than maxage days.
#
find $archdir -name "${archroot}*" -mtime +$maxage -print -exec rm -f {} \; >> $reportfile

set rc = $status

if ( $rc == 0 ) then
	find $arbkdir -name "${archroot}*" -mtime +$maxage -print -exec rm -f {} \; >> $reportfile
	set rc = $status
endif

set rc2 = 0
if ( $rc == 0 ) then
        eval set standbydir = $standbydir
        find $standbydir -name "${archroot}*" -mtime +$maxage -print -exec rm -f {} \; >> $reportfile
        set rc2 = $status
else
        logger -p user.info -t archlogpurge \
                   "Warning: Standby Archive Logs not processed due to previous error condition"
endif

if ( $rc2 != 0 ) then
        logger -p user.info -t archlogpurge \
                   "Warning: Standby Archive Logs terminated with abnormal status ($rc)"
endif

if ( $rc == 0 ) then
	# Delete logpurge reportfiles more than a week old
	find ~/ -mtime +7 -name logpurge\* -exec \rm {} \;
	set rc = $status
endif

if ( $rc != 0  && $arbkdir != "" ) then
	logger -p user.info -t archlogpurge \
	"Warning: Finished with abnormal status ($rc)- check output from task."
	exit 1
else
	logger -p user.info -t archlogpurge "Finished OK."
endif;
