#!/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.00 $ \$Author: borland $"

APSApplication . -name SRFillAndKickOut -version $CVSRevisionAuthor \
  -overview {This interface provides a workaround to problems with slow bunch kickout.}

set mainStatus ""
proc SetMainStatus {text} {
    global mainStatus
    set mainStatus $text
    update
}

APSScrolledStatus .status -parent .userFrame -textVariable mainStatus -width 90

proc SetUpSlowBunchKickout {} {
    set tmpfile /tmp/[APSTmpString]
    if [catch {exec sddswget -PV=MCR-IETS:TTC:BD:pattern $tmpfile} result] {
        SetMainStatus "problem getting values from MCR-IETS:TTC:BD:pattern: $result"
        return
    }

    if [catch {exec sddssort $tmpfile $tmpfile.sorted -unique -col=Waveform,decr} result] {
        SetMainStatus "problem with sddssort: $result"
        return
    }

    if [catch {exec sdds2stream $tmpfile.sorted -rows=bare } numBunches] {
        SetMainStatus "problem with sdds2stream: $numBunches"
        return
    }


    if [catch {exec sddsprocess $tmpfile.sorted -pipe=out \
                 -print=col,ControlName,S-INJ:BucketFillQueue%03ldC,Index \
                 -print=col,ValueString,%ld,Waveform \
                 | sddscasr -pipe=in -restore} result] {
        SetMainStatus "problem with sddsprocess: $result"
        return
    }
    
    if [catch {exec caput S-INJ:LoadNewActiveFillPatternC 1} result] {
        SetMainStatus "problem with caput: $result"
        return
    }

    if [catch {exec caput S-INJ:FillQueueNumberOfBunchesC $numBunches } result] {
        SetMainStatus "problem with caput: $result"
        return
    }
    SetMainStatus "Set up for SBK"
}

proc PerformSBK {} {
    if [catch {exec cavput -list=S-INJ:SetSlowBunchKickoutC.PROC=1} result] {
        SetMainStatus "Error: $result"
        return
    }
    SetMainStatus "SBK initiated"
}

proc SetFillPattern {} {
    global fillPatternFile nBunches
    if [catch {exec sddscasr -restore $fillPatternFile($nBunches)} result] {
        SetMainStatus "Error: $result"
        return
    }
    after 1000
    if [catch {exec cavput -list=S-INJ:LoadNewActiveFillPatternC=1} result] {
        SetMainStatus "Error: $result"
        return
    }
    SetMainStatus "Set fill pattern"
}

proc PerformFill {} {
    SetMainStatus "Initiating fill"
    SetFillPattern
    if [catch {exec cavput -list=S-INJ:SetInjSequencerToOpsModeC.PROC=1} result] {
        SetMainStatus "Error: $result"
        return
    }
    after 1000
    if [catch {exec cavput -list=S-INJ:SetFillThenSwapoutC.PROC=1} result] {
        SetMainStatus "Error: $result"
        return
    }
    SetMainStatus "Initiated fill"
}

APSFrame .sbk -parent .userFrame -label "Slow bunch kickout"
set w .userFrame.sbk.frame
APSButton .setSBK -parent $w -text "Set up slow bunch kickout" -command SetUpSlowBunchKickout
APSButton .runSBK -parent $w -text "Perform slow bunch kickout" -command PerformSBK

set nBunchesList [list]
foreach patternFile [glob /home/helios/oagData/sr/balancedFillOrders/*.putMe] {
    eval set nBunches [os editstring Z-%/.sdds.putMe// $patternFile]
    lappend nBunchesList $nBunches
    set fillPatternFile($nBunches) $patternFile
}
set nBunchesList [lsort -integer $nBunchesList]

APSFrame .pattern -parent .userFrame -label "Fill pattern setup"
set w .userFrame.pattern.frame
set nBunches 162
APSRadioButtonFrame .rb -parent $w -label "Fill pattern" -limitPerRow 10 -variable nBunches \
  -buttonList $nBunchesList -valueList $nBunchesList -orientation horizontal

APSButton .setPattern -parent $w -text "Set fill pattern" -command SetFillPattern
APSButton .fill -parent $w -text "Perform fill"  -command PerformFill
