#!/bin/sh
# \
  exec oagwish "$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)]

APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.0 $ \$Author: shang $"

proc SetStatus {text} {
    global status 
    set status "[exec date] $text"
    update
}

set prevCorrName "NA"
proc UpdateCorrector {args} {
    global corrName SFBValue DACValue prevCorrName errorCode
    if ![string length $corrName] {
        return
    }
    if [string compare $corrName $prevCorrName]==0 {
        #corrector did not change
        return
    }
    set pvList [list SFB:${corrName}:CurrentAO ${corrName}:DacAI]
    set varList {SFBValue DACValue}
    if [string length $prevCorrName] {
        pv unlink $varList
    }
    SetStatus "Link $corrName ..."
    if [pv linkw $varList $pvList] {
        return -code error "Error link $corrName: $errorCode"
    }
    pv umon $varList
    set prevCorrName $corrName
    SetStatus "done."
}

proc ApplyDelta {args} {
    set sign -1
    APSStrictParseArguments {sign}
    global corrName delta
    set change [expr $sign * $delta]
    set waveformDir /home/helios/oagData/sr/orbitControllaw/waveforms
    if [regexp {H} $corrName] {
        set wfFile $waveformDir/hcorrInfo.sdds
        set runControlPV DP:S:OrbitControlLawXSDDS
    } elseif [regexp {V} $corrName] {
        set wfFile $waveformDir/vcorrInfo.sdds
        set runControlPV DP:S:OrbitControlLawYSDDS
    } else {
        return -code error "invaid corrector name - $corrName."
    }
    SetStatus "check if datapool is running..."
    if [catch {exec cavget -list=$runControlPV.RUN -pend=10 } running] {
        return -code error "Error reading $runControlPV.RUN: $running"
    }
    if !$running {
        SetStatus "Datapool is not running. can not apply changes to corrector."
        return
    }
    set tmpRoot /tmp/[APSTmpString]
    
    if [catch {exec sddsprocess $wfFile  -reprint=par,WaveformPV,%s,DeltaWaveformPV \
                 "-redefine=col,Waveform,DeviceName \"$corrName\" streq ? $change : 0 $ " $tmpRoot.$corrName } result] {
        return -code error "error process datapool corrector: $result"
    }
    if [catch {exec sddswput $tmpRoot.$corrName } result] {
        return -code error "Error change $corrName: $result"
    }
    SetStatus "$corrName changed by $change A."
}

# Needed for APSParseArguments
set status "Press return after you input corrector name"
APSApplication . -name badCorrectorPunchDown -version $CVSRevisionAuthor \
  -overview "pown down input corrector."

APSScrolledStatus .ss -parent .userFrame -textVariable status \
  -packOption "-side top -fill x" -height 4 -width 60 \
  -contextHelp "Provides execution status and operation hints."

set corrName ""
set delta 1 
APSLabeledOutputFrame .readback -parent .userFrame -label "Corrector SFB/DAC readback(A):         " -variableList {SFBValue DACValue} \
  -orientation horizontal -width 12
APSLabeledEntry .step -parent .userFrame -label "Corrector change delta (A)" -textVariable delta -width 25
APSLabeledEntry .corr -parent .userFrame -label "Corrector Name:" -textVariable corrName -width 25 
bind .userFrame.corr.entry <Return> "UpdateCorrector"
bind .userFrame.corr.entry <Leave> "UpdateCorrector"
APSButton .plus -parent .userFrame  -text "Apply +"  -command "ApplyDelta -sign 1"
APSButton .minus -parent .userFrame -text "Apply -" -command "ApplyDelta -sign -1"
