#!/bin/sh  
# \
  exec oagtclsh "$0" "$@"
#
# $Log: not supported by cvs2svn $
# Revision 1.7  2001/05/19 15:14:22  borland
# Now picks out only the jobs for the workstation that it is running on.
#
# Revision 1.6  2001/05/19 15:09:18  borland
# *** empty log message ***
#
# Revision 1.5  2001/03/20 03:48:58  borland
# Taskss are now sorted in priority order.
#
# Revision 1.4  2001/02/12 21:17:18  borland
# Modified to accomodate processing by a single workstation.
#
# Revision 1.3  1999/11/29 14:26:06  borland
# Improved temporary file management.
#
# Revision 1.2  1996/11/22 01:55:10  borland
# Changed names of data logging and configuration directory variables, plus
# variable values, in order to make addition of glitch loggers easier.
#
# Revision 1.1  1996/11/21 15:14:19  borland
# First version of scripts and procedures to manage time-series data logging
# cron jobs.
#
#

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
APSDefineMonitoringVariables

set debug 0
set dryRun 0

set hostname [lindex [split $apsScriptHost .] 0]
if $debug {
    puts stderr "hostname is $hostname"
}

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 giving names of loggers.
# 
set tmpFile /tmp/[APSTmpString]
set tmpID [APSUniqueName tmp]
APSAddToTmpFileList -ID $tmpID -fileList $tmpFile
if [catch {exec sddssort $configFile -pipe=out -column=postprocessingPriority,incr \
             | sddsprocess -pipe=in $tmpFile \
             -match=column,postprocessingWorkstation=$hostname -nowarning \
             -define=parameter,Rows,n_rows,type=long} result] {
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $result"
    exit 1
}

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

# see if there are any rows of data for this workstation
if [catch {sdds getParameter $fid Rows} rows] {
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $rows"
    exit 1
}
if $debug {
    puts stderr "$rows items for workstation $hostname"
}
if !$rows exit

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 $dryRun {
        continue
    }
    if [catch {exec postprocessTimeSeriesLog \
                 -subDirectory [lindex $subDirectoryList $index] \
                 -rootname [lindex $rootnameList $index]} result] {
        APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: postprocessing error: $result"
    }
}
