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

APSApplication . -name SRSlowSectorTurnOn -version $CVSRevisionAuthor \
  -overview "Provides for slow turn on of storage ring power supplies by sector."

set StatusText Ready.
APSScrolledStatus .status -parent .userFrame -width 80 \
        -textVariable StatusText

proc SetStatusText {text} {
    global StatusText
    set StatusText $text
    update
}

set sector 1
set pause 3
set repeats 1
APSFrameGrid .entries -parent .userFrame -xList {f1 f2 f3} 
set w .userFrame.entries
APSLabeledEntry .f1 -parent $w.f1 -label "Sector: " -textVariable sector \
  -width 4 -contextHelp "Enter the number of the sector to turn on" \
 -packOption "-side top -expand 1"
APSLabeledEntry .f2 -parent $w.f2 -label "Pause (sec): " -textVariable pause \
  -width 4 -contextHelp "Enter the number of seconds to pause after each command to the IOC." \
 -packOption "-side top -expand 1"
APSLabeledEntry .f3 -parent $w.f3 -label "Repeats: "  -textVariable repeats \
  -width 4 -contextHelp "Enter the number of times to repeat the attempt to turn on a sector." \
 -packOption "-side top -expand 1"

set turnOn(Q) 0
set turnOn(S) 0
set turnOn(H) 0
set turnOn(V) 0

APSCheckButtonFrame .family -parent .userFrame -label "Family: " \
  -buttonList {Q S H V} \
  -variableList {turnOn(Q) turnOn(S) turnOn(H) turnOn(V)} \
  -contextHelp "Pick the families you want to turn on" \
  -orientation horizontal

APSButton .done -parent .userFrame -text DONE -command exit \
  -contextHelp "Exits this application."
APSButton .go -parent .userFrame -text GO \
  -command RunSRTurnOnSector \
  -contextHelp "Turns on the families in the sector you have entered."

proc RunSRTurnOnSector {} {
    global turnOn sector pause repeats errorInfo
    bell
    set numberDone 0
    foreach family {Q S H V} {
        if $turnOn($family) {
            if [catch {SRTurnOnSector -family $family \
                         -sector $sector -pause [expr int($pause*1000)] -repeats $repeats \
                         -statusCallback SetStatusText} result] {
                APSAlertBox [APSUniqueName .] -errorMessage "$result"
                return
            }
            incr numberDone
        }
    }
    if !$numberDone {
        SetStatusText "Select one or more families."
    }
}

