#!/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 CVSRevisionAuthor "\$Revision: 1.9 $ \$Author: soliday $"
APSApplication . -name "IOC SDDS Epics Task Setup" -version $CVSRevisionAuthor \
    -overview "This applications allows the user to reset or modify the settings for sddscontrollaw and sddsfeedforward tasks that are running in the IOCs"
set status Ready...
APSScrolledStatus .status \
    -parent .userFrame \
    -textVariable status \
    -packOption "-fill x" \
    -width 70

set tmpFile /tmp/[APSTmpString]
APSAddToTempFileList $tmpFile


proc CreateWidgets {args} {
    set PVs ""
    set default ""
    APSStrictParseArguments {PVs default}
    set index 0
    global defaultSet currentValue
    APSScroll .scrollWin \
	-parent .userFrame \
	-name "" \
	-packOption "-fill both -expand true" 
    .userFrame.scrollWin.frame.canvas config -yscrollincrement 36
    if {[llength $PVs] > 10} {
	set height 360
    } else {
	set height [expr {36 * [llength $PVs]}]
    }
    .userFrame.scrollWin.frame.canvas configure -height $height
    set w .userFrame.scrollWin.frame.canvas.frame
    foreach name $PVs def $default {
	APSFrame .f$index \
	    -parent $w \
	    -packOption "-side top -anchor w"
	APSLabel .label \
	    -parent $w.f$index.frame \
	    -text [format %-30s $name] \
	    -packOption "-side left"
	APSRadioButtonFrame .rbf \
	    -parent $w.f$index.frame \
	    -label "  Default" \
	    -buttonList "Yes No" \
	    -valueList "yes no" \
	    -variable defaultSet($index) \
	    -orientation horizontal \
	    -packOption "-side left"
	$w.f$index.frame.rbf.frame.button1 configure \
	    -state disabled \
	    -disabledforeground black
	$w.f$index.frame.rbf.frame.button2 configure \
	    -state disabled \
	    -disabledforeground black
	APSButton .modify \
	    -parent $w.f$index.frame \
	    -text "Modify" \
	    -command "Modify -PV $name -index $index -default \"$def\""
	APSButton .info \
	    -parent $w.f$index.frame \
	    -text "Info" \
	    -command "exec medm -attach -x -macro SDDSPV=$name /usr/local/iocapps/adlsys/linac/sddsEpicsLauncher.adl &"
	incr index
    }
    .userFrame.scrollWin.frame.canvas config -scrollregion "0 0 1000 [expr {36 * $index}]"
    APSButton .resetAll \
      -parent .userFrame \
      -text "Reset All" \
      -command ResetAll

}

proc Modify {args} {
    set PV ""
    set default ""
    set index 0
    APSStrictParseArguments {PV index default}
    destroy .modify$index
    APSDialogBox .modify$index \
	-name $PV
    destroy .modify$index.buttonRow.cancel
    APSDialogBoxAddButton .set \
	-parent .modify$index \
	-text "Set ControlLaw Options" \
	-command "SetValues -pv $PV -index $index ; destroy .modify$index"
    APSDialogBoxAddButton .reset \
	-parent .modify$index \
	-text "Reset Default Value" \
	-command "SetValues -pv $PV -index $index -options \"$default\" ; destroy .modify$index"
    APSScrolledText .options \
	-parent .modify$index.userFrame \
	-height 7 \
	-width 80
    global t$index
    set t$index .modify$index.userFrame.options.text
    [set t$index] configure -bg white
    global currentValue
    [set t$index] insert end $currentValue($index)

}

proc GetValues {args} {
    set indexList ""
    set pvList ""
    APSStrictParseArguments {indexList pvList}
    global tmpFile defaultSet inputdata apsNetworkDomain
    if {[catch {exec cavget -list=[join $pvList ,] -pend=2} results]} {
	APSSetVarAndUpdate status "Error: Unable to access PVs"
	return
    }
    if {[lsearch $results "?"] != -1} {
	APSSetVarAndUpdate status "Error: Unable to access PVs"
	return
    }
    if {[catch {exec sddswget -pvNames=[join $pvList .OPTN,].OPTN $tmpFile} results]} {
	APSSetVarAndUpdate status "Error loading $pvList : $results"
	return
    }

    if {[catch {sdds load $tmpFile data} results]} {
	APSSetVarAndUpdate status "Error loading $pvList : $results"
	return
    }
    global currentValue
    set n 0
    foreach pv $pvList index $indexList {
	set currentValue($index) [string trim [string trim [join [lindex $data(Column.Waveform) $n] ""] \000 ]]
	if {[string compare [lindex [lindex $inputdata(Column.Default) 0] $index] $currentValue($index)] == 0} {
	    set defaultSet($index) yes
	} else {
	    set defaultSet($index) no
	    if {(![string compare $apsNetworkDomain "accel.ntw0rk"]) || (![string compare $apsNetworkDomain "aps4.anl.gov"])} {
		if {[llength $currentValue($index)] == 0} {
		    SetValues -pv $pv -index $index -options [lindex [lindex $inputdata(Column.Default) 0] $index]
		}
	    }
	}
	incr n
    }
}

proc SetValues {args} {
    set index 0
    set pv ""
    set options ""
    APSStrictParseArguments {pv index options}
    global tmpFile t$index
    exec sddswget -pvNames=$pv.OPTN $tmpFile
    if {[catch {sdds load $tmpFile data} results]} {
	APSSetVarAndUpdate status "Error loading $pv.OPTN : $results"
	return
    }
    set length [llength [lindex $data(Column.Waveform) 0]]
    if {![llength $options]} {
	set options "[join [split [[set t$index] get 1.0 end] \n]]"
    }
    set l2 [string length $options]
    for {set i 1; set j 0} {$j < 2000} {incr i; incr j} {
	if {$i <= $l2} {
	    lappend tmp [string index $options $j]
	} else {
	    lappend tmp ""
	}
    }
    set data(Column.Waveform) [list $tmp]
    if {[catch {sdds save $tmpFile data} results]} {
	APSSetVarAndUpdate status "Error saving $pv.OPTN : $results"
	return
    }
    if {[catch {exec sddswput $tmpFile} results]} {
	APSSetVarAndUpdate status "Error saving $pv.OPTN : $results"
	return
    }

    APSSetVarAndUpdate status "$pv.OPTN options set"
    GetValues -indexList $index -pvList $pv
}

proc ResetAll {args} {
    global indexList inputdata
    set pvList [lindex $inputdata(Column.ControlName) 0]

    foreach pv $pvList index $indexList {
        SetValues -pv $pv \
          -index $index \
          -options [lindex [lindex $inputdata(Column.Default) 0] $index]
    }

}


set inputfile /home/helios/OAG/oagData/IOC/IOCtaskSetup
#set inputfile /home/oxygen/SOLIDAY/oag/apps/src/tcltkapp/oagapp/junk3
if {[catch {sdds load $inputfile inputdata} results]} {
    APSSetVarAndUpdate status $results
} else {
    set indexList ""
    for {set n 0} {$n < [llength [lindex $inputdata(Column.ControlName) 0]]} {incr n} {
	lappend indexList $n
    }
    GetValues -indexList $indexList -pvList [lindex $inputdata(Column.ControlName) 0]
    CreateWidgets \
	-PVs [lindex $inputdata(Column.ControlName) 0] \
	-default [lindex $inputdata(Column.Default) 0]
}
