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

set usage "usage: convertToPVMonitorFile -input <filename> -rootname <string>\n\
The input file must have three columns:\n\
ControlName     The PV name\n\
AlarmLimit      The value at which an alarm should occur\n\
TripLimit       The value at which a trip should occur\n"

set input ""
set rootname ""
set args $argv
if {[APSStrictParseArguments {input rootname}] || ![string length $input] || \
    ![string length $rootname]} {
    return -code error "$usage"
}
foreach type {Alarm Trip} {
    if [file exists $rootname-$type.config] {
        return -code error "exists already: $rootname-$type.config"
    }
}

# stop injection
set command(Alarm) "cavput -list=Mt:Ddg3chan -list=1 -list=.GATE=0"
# dump the beam
set command(Trip) "cavput -list=S:MPS:dumpBeamSQ=1"

foreach type {Alarm Trip} {
    set output $rootname-$type.config
    exec sddsprocess $input $output \
      -define=col,MinLimit,0 \
      -define=col,MaxLimit,${type}Level \
      -define=col,Tolerance,0 \
      -define=col,MaxReading,1e300 \
      -define=col,CountRequired,1,type=long \
      "-print=parameter,AlarmStartCmd,$command($type)" \
      "-print=parameter,AlarmStopCmd,"
}
