#!/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 lastPing 0
set pingTimeout 20
proc PingRunControl {args} {
    global runControlPV lastPing
    set sec [clock seconds]
    if {[expr $lastPing + 2] > $sec} {
        return
    } else {
        set lastPing $sec
    }

    catch {APSRunControlPing} status
    switch $status {
        RUNCONTROL_OK {}
        RUNCONTROL_ABORT {
            return -code error "PARTimingFeedforward was aborted."
        }
        RUNCONTROL_TIMEOUT -
        RUNCONTROL_ERROR {
            return -code error "Unable to ping runcontrol record of $runControlPV: $status."
        }
    }
}

proc UpdateTimingPVs {args} {
    global coefFile freq freq0 tmpRoot
    
    set deltaFreq [expr $freq - $freq0]
    
    if {$deltaFreq!=0} {
        if [catch {exec sddscasr $tmpRoot.ref -save -pipe=out \
                     | sddsconvert -pipe -del=col,Value0 \
                     | sddsprocess -pipe -scan=col,Value0,ValueString,%lf \
                     | sddsprocess -pipe \
                     "-redefine=col,Value,$deltaFreq 1.0e7 / Coeff * Value0 +" \
                     | sddsprocess -pipe "-reprint=col,ValueString,%lf,Value" \
                     | tee $tmpRoot.set \
                     | sddscasr -pipe=in -restore } result] {
            return -code error "Error updating timings: $result"
        }            
    }
    set freq0 $freq
}

exec medm -attach -x -macro RCPV=OAG190RC  ./sr/psApp/APSRunControlSingle.adl &

set runControlPV OAG190RC

set coefFile /home/helios/oagData/par/dispChrom/inputFiles/freqRampInjTimingPVList
set freq0 [exec cavget -list=A014-IETS:BTC:BInjSetFreqM -pend=30]
set tmpRoot /tmp/[APSTmpString]
if [catch {exec sddscasr $coefFile -save -pipe=out \
             | sddsprocess -pipe=in $tmpRoot.ref "-match=col,ControlName=A014-IETS:BTC:BInjSetFreqM,!" \
             -scan=col,Value0,ValueString,%lf } result] {
    puts stderr "Error1: $result"
    exit
}

if [catch {APScavput -list=$runControlPV.CLR=1 -pend=20} result] {
    puts stderr "[exec date] error clear run control $runControlPV record: $result"
    exit
}

if [catch {APSRunControlInit -pv $runControlPV \
	       -description "PAR Timing feedforward" \
	       -timeout [expr $pingTimeout * 1000] } result] {
    puts stderr "[exec date] Unable to initialize runcontrol for PAR timing feedforward, the runcontrol record may not be cleared, press the clear button on the medm screen to clear the record and restart: $result"
    exit
}

APSRunControlLogMessage -message "Running" -severity 0

PingRunControl 
if [pv linkw freq A014-IETS:BTC:BInjSetFreqM 30] {
    puts stderr "Error link pvs: $errorCode"
    exit 1
}
pv linkw BIK B:IK:StatusCALC
pv umon freq UpdateTimingPVs
while {1} {
    pv getw BIK
    if $BIK {
	#exit if BIK tripped
	exec cavput -list=$runControlPV.ABRT=1 -pend=10
	exit 1
    }
    if [catch {PingRunControl} result] {
        puts stderr "$result"
        exit 1
    }
    APSWaitWithUpdate -waitSeconds 1 -updateInterval 1
}

exit
