#!/bin/sh  
# \
exec oagtclsh "$0" "$@"
#
# $Log: not supported by cvs2svn $
# Revision 1.3  2001/02/20 16:15:51  borland
# Now looks at the postprocessingWorkstation data in config file.
#
# Revision 1.2  1999/07/06 13:54:39  borland
# This version cycles over files for all workstations.
#
# Revision 1.1  1997/06/03 18:55:52  borland
# First version of scripts to remove expired files.
#
#

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
APSDefineMonitoringVariables

set debug 0

set hostname [lindex [split $apsScriptHost .] 0]

set configFile $apsDataLoggerConfigDir/timeSeries.config
if ![file exists $configFile] {
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $configFile not found"
    exit 1
}

# select out the rows of the config file pertaining to this workstation
set tmpFile /tmp/[APSTmpString]
APSAddToTempFileList $tmpFile
if [catch {exec sddsprocess $configFile $tmpFile \
             -match=column,postprocessingWorkstation=$hostname -nowarning \
             -define=parameter,Rows,n_rows,type=long} result] {
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $result"
    continue
}

# open the SDDS file
if [catch {sdds open $tmpFile} fid] {
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $fid" 
    continue
}

# see if there are any rows of data for this workstation
if [catch {sdds getParameter $fid Rows} rows] {
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $rows"
    continue
}
if $debug {
    puts stderr "$rows items for workstation $hostname"
}
if $rows {
    foreach variable {subDirectory rootname} {
        if [catch {sdds getColumn $fid $variable} ${variable}List] {
            APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: [subst \$${variable}List]"
        }
        if $debug {
            puts stderr "$variable: [subst \$${variable}List]"
        }
    }
    if [llength $subDirectoryList]!=$rows {
        APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: too little data in column!"
    }

    for {set index 0} {$index<[llength $rootnameList]} {incr index} {
        if $debug {
            puts stderr "Starting [lindex $subDirectoryList $index]/[lindex $rootnameList $index]"
        }
        if [catch {APSRemoveExpiredTimeSeriesData \
                     -subDirectory [lindex $subDirectoryList $index] \
                     -rootname [lindex $rootnameList $index]} result] {
            APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: postprocessing error: $result"
        }
    }
}
