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

#
# Examines data logging directories to find directories with no
# data but existing lock file.  Removes the lock files.  This 
# will patch the problem with hanging lock files, apparently a
# Solaris bug.
#

#
# $Log: not supported by cvs2svn $
# Revision 1.1  2002/06/21 13:34:26  borland
# First version.
#
#

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
set CVSRevisionAuthor "\$Revision: 1.2 $ \$Author: soliday $"

APSDefineMonitoringVariables
 
set tmpFile /tmp/[APSTmpString]
APSAddToTempFileList $tmpFile
   
if [catch {exec sddsprocess $apsDataLoggerConfigDir/timeSeries.config \
             -pipe=out -filter=column,doRun,1,1 \
             | sddssort -pipe=in $tmpFile -column=workstation} result] {
    APSSetVarAndUpdate status $result
    return
}
if [catch {exec cavget -noquote -list=DataLoggersOnOffBO} result] {
    return
}
if [string compare $result "LOGGERS DISABLED"]==0 {
    return
}
if [catch {sdds open $tmpFile} inID] {
    APSSetVarAndUpdate status $inID
    catch {sdds close $inID}
    catch {exec rm $tmpFile}
    return
}
foreach column {subDirectory rootname workstation loggerGroup} {
    if [catch {sdds getColumn $inID $column} ${column}List] {
        catch {sdds close $inID}
        catch {exec rm $tmpFile}
        APSSetVarAndUpdate status [subst \$${column}List]
    }
}
lappend rootnameList SRFastLog1
lappend workstationList charis
lappend subDirectoryList monitoring/SRFastLog1
lappend loggerGroupList ""
catch {sdds close $inID}
catch {exec rm $tmpFile}
set number [llength [subst $rootnameList]]
set tag [exec date +%Y-%j]
set timeNow [expr int([exec timeconvert -now | token -last])]
for {set index 0} {$index<$number} {incr index} {
    set rootname [lindex $rootnameList $index]
    set subDirectory [lindex $subDirectoryList $index]
    set pattern $apsOAGDataDir/$subDirectory/$rootname-$tag-????.*
    set workstation [lindex $workstationList $index]
    set loggerGroup [lindex $loggerGroupList $index]
    set dataFiles [lsort [glob -nocomplain $pattern]]
    set count [llength $dataFiles]
    if !$count {
        # no data found
        if [file exists $apsOAGDataDir/$subDirectory/$rootname.lock] {
            file delete -force $apsOAGDataDir/$subDirectory/$rootname.lock
#            APSSendEMail -address borland -message "Warning: lock file removed for $subDirectory/$rootname" 
            APSSendEMail -address soliday \
              -message "Warning: lock file removed for $subDirectory/$rootname" 
        }
    }
}

