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

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)]
APSStandardSetup

set dataDir /home/helios/oagData/Alarms
set normalMailAddress soliday@anl.gov
set errorMailAddress  soliday@anl.gov
set keepGenerations 0

set usageMessage "usage: procesDailyFiles -group <name> \[-dateString <YYYY>-<JJJ>\]"
set usageExample "example: processDailyFiles -group booster -dateString 1996-001"
if [llength $argv]<2 {
    puts stderr $usageMessage
    puts stderr $usageExample
    exit
}

proc ScanArgs {args} {
    global errorMailAddress group dateString
    set group ""
    set dateString ""
    catch {APSStrictParseArguments {group dateString}} result
    if $result {
        APSSendEMail -address $errorMailAddress -message "$result"
        exit
    }
}

eval ScanArgs $argv
set logfile $group.plogfile
if ![file exists $logfile] {
    exec date > $logfile
} else {
    exec date >> $logfile
}

#
# the files to be processed are those from yesterday
# also check last 360 days files for any that are unprocessed.
#
APSDateBreakDown -yearVariable year -twoDigitYear 0 -julianDayVariable jday 
set daysScanned 1
set first 1
cd $group
while {$daysScanned<360} {
    set dateList [APSOffsetDateInfo -offset -1 -julianDay $jday -year $year -dateFormat list]
    set year   [lindex $dateList 0]
    set jday   [lindex $dateList 1]
    set month  [lindex $dateList 2]
    set day    [lindex $dateList 3]
    set tag $year-$jday-${month}${day}
    
    if [string length $dateString] {
        set flist [glob -nocomplain $group-$dateString-????.\[0-9\]\[0-9\]\[0-9\]\[0-9\]]
        set count [llength $flist]
        if !$count {
            set message "No files found while running compressAlarmLogs and processDailyFiles: $group $dateString"
            puts stderr $message
            cd ..
            exit
        }
        set flist [lsort $flist]
        set name1 [file rootname [lindex $flist 0]]
        scan $name1 "$group-%s" tag
        set outputRoot $group-$tag
    } else {
        # make list of output files
        set outputRoot "$group-${tag}"
        set flist [glob -nocomplain ${outputRoot}.\[0-9\]\[0-9\]\[0-9\]\[0-9\]]
        set count [llength $flist]
        if {!$count && $first} {
            set message "No files found while running compressAlarmLogs and processDailyFiles: $group $month/$day/$year"
            APSSendEMail -address $normalMailAddress -message $message
            exec echo $message >> $logfile
        }
        set flist [lsort $flist]
    }

    if [llength $flist] {
        if [file exists $logfile] {
            if [file exists $logfile.tmp] {
                exec rm $logfile.tmp
            }
            exec tail -500 $logfile > $logfile.tmp
            exec mv $logfile.tmp $logfile
        }

        if $count {
            # make a list of good and bad files
            set goodlist {}
            set badlist {}
            foreach file $flist {
                catch {exec sddscheck $file} check
                switch $check {
                    "nonexistent" -
                    "badHeader" {
                        lappend badlist $file
                    }
                    "corrupted" {
                        # recover corrupted file
                        exec echo "Recovering corrupted file: $file" >> $logfile
                        catch {exec sddsconvert -recover $file} result
                        exec echo "Recovery result: $result" >> $logfile
                        lappend goodlist $file
                    }
                    default {
                        lappend goodlist $file
                    }
                }
            }

            # record good/bad file information
            if [llength $badlist] {
                exec echo "Bad files: $badlist" >> $logfile
                APSSendEMail -address $normalMailAddress -message "Bad files found when compressing Alarm logs: $tag $group"
            }
            if [llength $goodlist] {
                if [catch {eval exec sddscombine $goodlist ${outputRoot}.gz} result] {
                    exec echo "Combine error: $result" >> $logfile
                    APSSendEMail -address $normalMailAddress -message "Combine error for $outputRoot: $result"
                    file delete ${outputRoot}.gz
                } else {
                    exec echo "Good files: $goodlist --\> ${outputRoot}.gz" >> $logfile
                }
            }


            if [file exists $outputRoot.gz] {
                if {$keepGenerations} {
                    # compress all files and move to generations subdirectory
                    foreach file $flist {
                        if [catch {exec gzip $file} result] {
                            exec echo "gzip on $file: $result" >> $logfile
                        }
                        if [file exists $file.gz] {
                            exec mv $file.gz generations/
                        } else {
                            exec mv $file generations/
                        }
                    }
                } else {
                    if [catch {eval exec rm $flist} result] {
                        puts stderr $result
                    }
                }
                exec echo "Done collating [exec date]" >> $logfile
            }
            exec chmod a-w ${outputRoot}.gz
        }
    }
    incr daysScanned
    set first 0
}
cd ..
