#!/bin/bash
# * CCAT Startup        (Digital UNIX/CCAT)       9/10/1998  *
# *****************************************************************
# *                                                               *
# *    Copyright (c) Digital Equipment Corporation, 1991, 1995    *
# *                                                               *
# *   All Rights Reserved.  Unpublished rights  reserved  under   *
# *   the copyright laws of the United States.                    *
# *                                                               *
# *   The software contained on this media  is  proprietary  to   *
# *   and  embodies  the  confidential  technology  of  Digital   *
# *   Equipment Corporation.  Possession, use,  duplication  or   *
# *   dissemination of the software and media is authorized only  *
# *   pursuant to a valid written license from Digital Equipment  *
# *   Corporation.                                                *
# *                                                               *
# *   RESTRICTED RIGHTS LEGEND   Use, duplication, or disclosure  *
# *   by the U.S. Government is subject to restrictions  as  set  *
# *   forth in Subparagraph (c)(1)(ii)  of  DFARS  252.227-7013,  *
# *   or  in  FAR 52.227-19, as applicable.                       *
# *                                                               *
# *****************************************************************
# * ccat_startup
# *
# * This script file is meant to be executed during reboot just   
# * after crashdc is run.  It checks for a valid crash file, and
# * checks to see if that crash file is newer than the last one 
# * processed.  If so, it invokes CCAT with the ccat direct 
# * command, passing the crash file name as an argument.   
# *
# * It generally resides in: /usr/opt/hp/svctools/common/bin         
# *
# *
# *****************************************************************
#
# Modification History
#
# Dec. 13, 1996   G. Lengyel	Put a flag in the while loop to
#				indicate that crashdc was running.
#				Use this to indicate later that a 
#				new crash-data file was created.
#
# Mar. 5, 1998    B. Bradach    Added comments to script so that
#                               each section is more fully described.
#
# Sep. 9, 1998    B. Bradach    Added check to see if the crash file 
#				is newer than the last one sent.
#
# Jan. 10 2002    J.Warihay     Eliminate all the old steps that
#                               were left over from the cadcunix
#                               script.  This eliminates any references
#                               to "tmp" directories, a potential
#                               security hole.   Added comments.
#
# 08/12/02   J.warihay     PTR 91-22-600  Update for new directory  
#                          structures - common/specific.
#   
# 12/04/02   J.Warihay     PTR 91-22-849  Move last and history files   
#                          into ../compaq/svctools/specific/ccat/data
#
# 04/28/03   R.Renzetti	   Added Path variable for HPUX startup.
# 11/11/03   J.arihay      PTR 91-22-1559  Change message if bounds file 
#                          is not found.  It only exists after the machine  
#                          has crashed for the first time.
#
#*********************************************************************


export PATH=/usr/bin:/usr/sbin

echo "CCAT Startup Routine not supported on HPUX platform"
exit

echo "Running CCAT Startup Routine"

crashdcRan=0

#
# See if crashdc is still running. If it is, wait until done before continuing.
# If we don't wait, we get an empty crash report.
#

while ps ax | grep crashdc | grep -v grep > /dev/null
do
    crashdcRan=1
    echo "...waiting for crashdc to finish..."
    sleep 15 
done
crashdcRan=1
SAVECORE_DIR=`/usr/sbin/rcmgr get SAVECORE_DIR`
#
# conditional:
# if SAVECORE_DIR is NULL then set DIR to /var/adm/crash; otherwise
# set DIR to value of SAVECORE_DIR
#
DIR=${SAVECORE_DIR:-"/var/adm/crash"}

# 
# Define the various directory paths needed
#
BOUNDS=$DIR/bounds
DataDir=/usr/opt/hp/svctools/specific/ccat/data
SourceDir=/usr/opt/hp/svctools/common/bin
Scan=$SourceDir/scan
LastSent=$DataDir/last
History=$DataDir/history

#
# Check to see if $DIR exists ("$DIR") 
#          AND (-a) it is a directory (-d $DIR)
# -a is a logical AND 
# -d check to see if directory
# -s check if size > 0
# -r check for read permission
#
if [ "$DIR" -a -d $DIR -a -s $BOUNDS -a -r $BOUNDS ] 
then 
#
# set TEMP to the contents of file $BOUNDS
# The number in the BOUNDS file is one greater 
# than the current crash file number
#
    TEMP=`/bin/cat $BOUNDS`

#
# get the crash number
#
    CRASHNUM=`/bin/expr $TEMP - 1`

    #
    # If crash number is a reasonable number, then set the DATA variable and continue...
    #

    if [ $CRASHNUM -ge 0 ] 
    then 
	DATA=$DIR/crash-data.$CRASHNUM 

    #
    # check to see if the DATA file exists and is a regular file ( -f )
    #
	if [ -f $DATA ]			# was [ -f $DATA -a -r $DATA] 
	then 
            #
            # check to see if the LastSent file exists and is a regular file ( -f )
            #

	    if [ -f $LastSent ] 
	    then 
                #
                # check to see if crash file is newer than the last one sent
                #
		NewOne=`/bin/find $DIR -name crash-data.$CRASHNUM -follow -newer $LastSent`

		if [ $NewOne ] 
		then
		    cat $LastSent >> $History
		fi #NewOne
	    else
		NewOne=1
	    fi #LastSent



            #
            # If the current crash file is newer, update the last processed file, and invoke
            # CCAT with the ccat direct command.  Otherwise, we're done.
            #
	    if [ "X$NewOne" = "X" ] 
		then
			echo "No new crash file found. Exiting..."
			exit
                else
                        ccat direct $DIR/crash-data.$CRASHNUM
                        $Scan $DATA > $LastSent 2>&1                         

	    fi   # if X$newone = X

        fi # if DATA file exists

    fi # if crashnum ge 0

 else
     echo "No new crash file found. Exiting..."
 fi

