#!/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

proc FindLatestCPPatches {args} {
    set group appVersionUpdate.log
    set dirList [glob /net/phoebus/oagmgr/logDaemonData.*]
    foreach dir $dirList {
	append wildcard "${dir}/appVersionUpdate/*.log* ${dir}/$group ${dir}/save/$group.????? ${dir}/save/$group.?????.gz "
    }
    set dataDir /home/helios/oagData/logDaemonData/appVersionUpdate
    set fileList [lsort [eval glob /home/helios/oagData/logDaemonData/$group \
                           $dataDir/*.log* $wildcard]]

    set sec [expr {[clock seconds] - 86400}]
    set newFiles ""
    foreach f $fileList {
        if {[file mtime $f] > $sec} {
            lappend newFiles $f
        }
    }
    if {![llength $newFiles]} {
        exit
    }
    set tmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $tmpFile
    if {[catch {eval exec sddscombine $newFiles -pipe=out -merge | sdds2stream -pipe=in -col=Time,applicationName,user,previousVersion,newVersion,comment} results]} {
        exit
    }
    set output ""
    foreach "Time name user previous new comment" $results {
        if {([string first "helios" $new] == -1) && ([string first "/usr/local" $new] == -1)} {
            continue
        }
        if {$Time < $sec} {
            continue
        }
        append output "${name}:\n"
        append output " Updated By: ${user}\n"
        append output " Comment: ${comment}\n\n"
    }
    if {[string length $output] > 0} {
        set output [join [split $output \{] \\\{]
        set output [join [split $output \}] \\\}]
        set output [join [split $output \[] \\\[]
        set output [join [split $output \]] \\\]]
        foreach address "gfystro soliday yinesun" {
            APSSendEMail -address $address -message "$output" -subject "cppatch info for last 24 hours"
        }
    }
}

FindLatestCPPatches
