#!/bin/sh  
# \
exec oagtclsh "$0" "$@"
#
# $Log: not supported by cvs2svn $
# Revision 1.8  1999/11/14 02:50:13  borland
# Fixed bug that left temp file around if no loggers were started on a
# workstation.
#
# Revision 1.7  1999/07/13 19:09:05  borland
# Integrated startup of sddslogger jobs with traditional data loggers, using
# a single configuration file and startup script.
#
# Revision 1.6  1997/07/22 15:46:05  borland
# Added -sequentialStart option to permit starting jobs in sequence rather
# than simultaneously.  Will use for restart attempts to reduce workstation
# load.
#
# Revision 1.5  1997/05/20 19:37:53  borland
# Fixed bug in last mod.
#
# Revision 1.4  1997/05/20 19:36:29  borland
# Now supports -nextDay argument.
#
# Revision 1.3  1996/12/13 23:37:33  borland
# Now deletes temporary files.
#
# Revision 1.2  1996/11/22 01:55:12  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:21  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 args $argv
set nextDay 0
set sequentialStart 0
if {[APSStrictParseArguments {nextDay sequentialStart}]==-1} {
    APSSendDataLoggerErrorEmail -message "Bad commandline arguments: $argv0 $args"
    exit 1
}

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]
if [catch {exec sddsprocess $configFile $tmpFile \
             -match=column,workstation=$hostname -nowarning \
             -define=parameter,Rows,n_rows,type=long} result] {
    catch {exec rm $tmpFile}
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $result"
    exit 1
}

# open the SDDS file
if [catch {sdds open $tmpFile} fid] {
    catch {exec rm $tmpFile}
    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] {
    catch {exec rm $tmpFile}
    APSSendDataLoggerErrorEmail -message "$argv0 on $hostname: $rows"
    exit 1
}
if $debug {
    puts stderr "$rows items for workstation $hostname"
}
if !$rows {
    catch {exec rm $tmpFile}
    exit
}

foreach variable {subDirectory rootname monitorProgram} {
    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!"
}
catch {exec rm $tmpFile}

set sddsloggerSeen 0
set sddspvaloggerSeen 0
for {set index 0} {$index<[llength $rootnameList]} {incr index} {
    if $debug {
        puts stderr "Starting [lindex $subDirectoryList $index]/[lindex $rootnameList $index]"
    }
    if [string compare sddslogger [lindex $monitorProgramList $index]]==0 {
        set sddsloggerSeen 1
        continue
    }
    if [string compare sddspvalogger [lindex $monitorProgramList $index]]==0 {
        set sddspvaloggerSeen 1
        continue
    }
    if !$sequentialStart {
        exec startTimeSeriesLogger \
          -subDirectory [lindex $subDirectoryList $index] \
          -rootname [lindex $rootnameList $index] -nextDay $nextDay &
    } else {
        if 0 {
            exec startTimeSeriesLogger \
              -subDirectory [lindex $subDirectoryList $index] \
              -rootname [lindex $rootnameList $index] -nextDay $nextDay
        } else {
            if [catch {APSStartTimeSeriesLogger -subDirectory [lindex $subDirectoryList $index] \
                         -rootname [lindex $rootnameList $index] -nextDay $nextDay} result] {
                APSSendDataLoggerErrorEmail -message "$argv0 $args:  $result"
            }
        }
    }
}
if $sddsloggerSeen {
    APSStartSddslogger -configFile $configFile 
}
if $sddspvaloggerSeen {
    APSStartSddspvalogger -configFile $configFile 
}
