#!/bin/sh
# \
  exec oagwish  "$0" "$@"

# $Log: not supported by cvs2svn $

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 CVSRevisionAuthor "\$Revision: 1.1 $ \$Author: emery $"

proc SetStatus {text} {
    global status
    set status "$text"
    update
}

proc ExtractControllawOption {args} {
    global option matrix
    set plane ""
    APSParseArguments {plane}


    switch $plane {
        h {
            set coord x
            set Coord X
            set Plane H
        }
        v {
            set coord y
            set Coord Y
            set Plane V
        }
        default {
            return -code error "plane has to be h or v!"
        }
    }
    APSDialogBox .submit$plane -name "Edit $plane controllaw option" 
    APSDialogBoxAddButton .do -parent .submit$plane -text "Submit" -command "SubmitControllawOption -plane $plane"

    if [catch {APScagetTextFromWaveform -pvName DP:S:OrbitControlLaw${Coord}SDDS.OPTN } currentOpt] {
        return -code error "Unable to read options: $currentOpt"
    }
    set currentOpt [split $currentOpt " "]

# parse out the list
    set i 0
    foreach item $currentOpt {
        # check for option
        if [regexp {^\-} $item] {
            set option($plane.$i) $item
            APSLabeledEntry .entry$i -parent .submit$plane -label "Option $i: " -textVariable option($plane.$i) -width 120
            incr i
        } else {
            # it's the matrix!
            global matrix
            set matrix($plane) $item
            APSLabeledEntry .matrix -parent .submit$plane -label "Matrix: " -textVariable matrix($plane) -width 120
        }
    }
    set option($plane.n) $i
}

proc SubmitControllawOption {args} {
    global option matrix
    set plane ""
    APSParseArguments {plane}

    switch $plane {
        h {
            set coord x
            set Coord X
            set Plane H
        }
        v {
            set coord y
            set Coord Y
            set Plane V
        }
        default {
            return -code error "plane has to be h or v!"
        }
    }
    set optionsToSubmit $matrix($plane)
    for {set i 0} {$i < $option($plane.n)} {incr i} {
        lappend optionsToSubmit [set option($plane.$i)]
    }

    SetStatus "Set options to DP:S:OrbitControlLaw${Coord}SDDS.OPTN"
    if [catch {APScaputTextToWaveform -pvName DP:S:OrbitControlLaw${Coord}SDDS.OPTN -text "$optionsToSubmit"} result] {
        return -code error "Unable to set options: $result"
    }
    puts stderr "APScaputTextToWaveform -pvName DP:S:OrbitControlLaw${Coord}SDDS.OPTN -text $optionsToSubmit"
    SetStatus "done."
    
}

ExtractControllawOption -plane h
ExtractControllawOption -plane v
APSApplication . -name "DatapoolCommandEditor" -version $CVSRevisionAuthor \
  -overview "DatapoolCommandEditor provides basic editing of commands sent to the H and V plane sddscontrollaws. Only options that are presently used can be edited."

set status Ready.
APSScrolledStatus .status -parent .userFrame -width 60 \
  -textVariable status
