#!/bin/sh  
# \
exec oagtclsh "$0" "$@"

#
# $Log: not supported by cvs2svn $
# Revision 1.1  1997/01/23 20:28:01  borland
# First version of script to compress alarm log data.
#
#

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSDebugPath
APSStandardSetup

cd /home/helios/oagData/monitoring/Alarms
set debug 1

set blanks 0
set offset -29
while 1 {
    if $blanks>30 break
    incr offset -1
    set tag [APSOffsetDateInfo -today 1 -offset $offset -dateFormat Y-J-MD]
    if $debug {
        puts stderr "Looking at $tag"
    }
    set fileList [glob -nocomplain *-${tag}.gz]
    if [llength $fileList]==0 {
        incr blanks
        continue
    }
    set blanks 0
    if $debug {
        puts stderr "Processing $tag"
    }
    foreach file $fileList {
        set newFile summaryFiles/$file
        if [string compare [file type $file] link]==0 {
            if $debug {
                puts stderr "$file is a link to [file readlink $file]"
            }
            set file0 [file readlink $file]
            if ![file exists $file0] {
                puts stderr "Warning: $file0 does not exist---$file removed"
                catch {exec rm $file}
                continue
            }
            set linkName $file
            set file $file0
        } else {
            set linkName ""
        }
        if {[catch {eval exec sddscombine $file -pipe=out -merge \
                      | sddssort -column=ControlNameIndex -pipe -unique=count \
                      | gzip -c > $newFile \
                  } message]} {
            puts stderr "$argv0: $message"
            continue
        }
        if [catch {exec rm -f $file} result] {
            puts stderr $result
        }
        if {[llength $linkName] && [catch {exec rm -f $linkName} result]} {
            puts stderr $result
        }
    }
}
