#!/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)]
set apsttk 1

set CVSRevisionAuthor "\$Revision: 1.14 $ \$Author: borland $"

APSApplication . -name SR-RPS -version $CVSRevisionAuthor \
  -overview "SR-RPS provides convenience controls for storage ring raw power supplies."

set RpsStatus "Ready."
APSScrolledStatus .status -parent .userFrame -width 80 \
  -textVariable RpsStatus -packOption "-fill x -expand true"

proc SetRpsStatus {text} {
    global RpsStatus
    set RpsStatus $text
    update
    bell
}

proc MakeActionWidget {widget args} {
    set parent ""
    APSParseArguments {parent}
    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x 

    APSButton .execute -text EXECUTE \
      -command "ExecuteSrRpsCommands; APSDisableButton $w.execute.button" \
      -parent $w \
      -contextHelp "Executes the command configured by the various settings.  It must be enabled by the ENABLE button and may be disabled by the DISABLE button" -width ""
    APSDisableButton $w.execute.button
    APSButton .enableExec -text ENABLE -command "APSEnableButton $w.execute.button" -parent $w \
      -contextHelp "Enables the EXECUTE button." -width ""
    APSButton .disableExec -text DISABLE -command "APSDisableButton $w.execute.button" -parent $w \
      -contextHelp "Disables the EXECUTE button." -width ""
}

proc ExecuteSrRpsCommands {} {
    global SetValue CommandChoice RpsSectorSelection RpsSelection
    if {$CommandChoice==""} {
        SetRpsStatus "You must select one of the command choices before pressing EXECUTE"
        return
    }
    set sectors [MakeSectorList [array get RpsSectorSelection]]
    if {[string length $sectors]==0} {
        SetRpsStatus "You must select one or more sectors before pressing EXECUTE"
        return
    }
    set allGroups 1
    foreach selection [array names RpsSelection] {
        if {!$RpsSelection($selection)} {
            set allGroups 0
            break
        }
    }
    set count 0
    foreach selection [array names RpsSelection] {
        if {!$allGroups && !$RpsSelection($selection)} { continue }
        incr count
        if $allGroups {
            set selection R
        }
        SetRpsStatus "Working on $selection"
        if [catch {APSSrRpsAction -action $CommandChoice -group $selection \
                     -sectors "$sectors"} result] {
            SetRpsStatus [APSGetErrorText]
        }
        if $allGroups {
            break
        }
    }
    if {!$count} {
        SetRpsStatus "You must select one or more groups before pressing EXECUTE"
        return
    }
    SetRpsStatus "Ok."
}

proc MakeCommandChoiceWidget {widget args} {
    global SetValue ConfigFile ConfigDir CommandChoice
    set parent ""
    APSParseArguments {parent}
    
    set CommandChoice ""

    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x

    label $w.title -text "Raw Power Supply Commands" -relief ridge
    pack $w.title -side top -fill x
    
    frame $w.cb
    pack $w.cb -side top -fill x
    radiobutton $w.cb.on -value on -variable CommandChoice -relief ridge -text ON
    radiobutton $w.cb.off -value off -variable CommandChoice -relief ridge -text OFF
    pack $w.cb.on $w.cb.off -side left -fill x 

}

proc MakeRpsSectorsWidget {widget args} {
    global SectorSelection
    set parent ""
    APSParseArguments {parent}
    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x

    label $w.title -text "Storage Ring Sector Selection" -relief ridge
    pack $w.title -side top -fill x

    frame $w.ops -bd 2
    pack $w.ops -side top -fill x
    APSButton .setAll -text "SELECT ALL" -command "SetAllRpsSectors 1" -parent $w.ops  -width ""
    APSButton .clearAll -text "CLEAR ALL" -command "SetAllRpsSectors 0" -parent $w.ops  -width ""
    APSButton .toggle -text "TOGGLE" -command ToggleAllRpsSectors -parent $w.ops -width ""

    foreach zone {A B C D E F} {
        APSButton .setZone$zone -text "ZONE $zone" \
          -command "SetRpsZoneSectors $zone" -parent $w.ops  -width ""
    }
    
    frame $w.check -bd 2 
    pack $w.check -side top -fill x
    set sector 1

    for {set half 1} {$half<3} {incr half} {
        frame $w.check.q$half 
        pack $w.check.q$half -side top -fill x
        set start [expr ($half-1)*20+1]
        set end   [expr $start+19]
        for {set sector $start} {$sector<=$end} {incr sector 2} {
            set other [expr $sector-1]
            if {$other==0} { set other 40 }
            if {$other<10} { set other 0$other}
            if {$sector<10} { 
                set cbLabel $other:0$sector
            } else {
                set cbLabel $other:$sector
            }
            set SectorSelection($sector) 0
            checkbutton $w.check.q$half.s$sector -text S$cbLabel \
              -variable RpsSectorSelection($sector) -relief ridge 
            pack $w.check.q$half.s$sector -side left -fill x
        }
    }
    
}

proc MakeRpsWidget {widget args} {
    global RpsSelection 
    set parent ""
    APSParseArguments {parent}
    set w $parent$widget
    frame $w -bd 4 -relief raised
    pack $w -side top -fill x

    label $w.title -text "Raw Power Supply Group Selection" -relief ridge
    pack $w.title -side top -fill x

    frame $w.ops -bd 2 
    pack $w.ops -side top -fill x
    APSButton .setAll -text "SET ALL" -command "SetAllRpsSelections 1" -parent $w.ops -width ""
    APSButton .clearAll -text "CLEAR ALL" -command "SetAllRpsSelections 0" -parent $w.ops -width ""
    
    frame $w.check -bd 2 
    pack $w.check -side top -fill x
    foreach choice {R1 R2 R3 R4} {
        set RpsSelection($choice) 0
        checkbutton $w.check.c$choice -text $choice \
          -variable RpsSelection($choice) -relief ridge 
        pack $w.check.c$choice -side left -fill x
    }
    SetAllRpsSelections 0
}

proc SetAllRpsSelections {value} {
    global RpsSelection
    foreach name [array names RpsSelection] {
        set RpsSelection($name) $value
    }
}

proc SetAllRpsSectors {value} {
    global RpsSectorSelection
    for {set sector 1} {$sector<41} {incr sector 2} {
        set RpsSectorSelection($sector) $value
    }
}

proc ToggleAllRpsSectors {} {
    global RpsSectorSelection
    for {set sector 1} {$sector<41} {incr sector 2} {
        set value [subst \$RpsSectorSelection($sector)]
        set RpsSectorSelection($sector) [expr $value?0:1]
    }
}

proc SetRpsZoneSectors {zone} {
    global RpsSectorSelection
    SetAllRpsSectors 0
    switch $zone {
        A {lappend SectorList 2 3 4 5 6 7 8 9 }
        B {lappend SectorList 10 11 12 13 14 15 16 17}
        C {lappend SectorList 18 19 20 21 22 23}
        D {lappend SectorList 24 25 26 27 28 29}
        E {lappend SectorList 30 31 32 33 34 35}
        F {lappend SectorList 36 37 38 39 40  1}
        default {
            SetRpsStatus "Invalid zone $zone"
        }
    }
    foreach sector $SectorList {
        set RpsSectorSelection($sector) 1
    }
}


MakeCommandChoiceWidget .coms -parent .userFrame
MakeRpsWidget .rps -parent .userFrame
MakeRpsSectorsWidget .sectors -parent .userFrame
MakeActionWidget .ops -parent .userFrame
