#!/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.5 $ \$Author: soliday $"
APSApplication . -name "Tcl/TK SDDS Plotter" -version $CVSRevisionAuthor

set command $argv

if {[string length $command] == 0} {
    if {[catch {exec sddsplot} usage]} {
    }
    puts stderr "tk$usage"
    exit 1
}

set selectFrames 0
if {[string length $command] > 0} {
    foreach op $command {
        set i [string first -selectFrames $command]
        if {($i != -1)} {
            set command [string replace $command $i [expr {$i + 12}]]
            set selectFrames 1
            break
        }
    }
}

if {$selectFrames} {
    set i 0
    while 1 {
        if {![info exists apsPlot$i]} {
            break
        }
        incr i
    }
}

APSPlotCanvas .plot1 -parent .userFrame -sddsplotOptions $command

if {$selectFrames} {
    set select 1
    set selectedList ""
    for {set j 1} {$j <= [set apsPlot${i}(pages)]} {incr j} {
        set selected($j) 1
        append selectedList "1 "
    }
    
    APSFrame .selectframe \
        -parent .userFrame \
        -packOption "-side top -fill x"
    APSRadioButtonFrame .select \
        -parent .userFrame.selectframe.frame \
        -label "Selected" \
        -buttonList "Yes No" \
        -valueList "1 0" \
        -variable select \
        -orientation horizontal \
        -contextHelp "Used to select plot frames to use. Selected frames will be passed back to the calling program."
    
    dp_atexit append {
        set selected($lastPage) $select
        set select "$selected([set apsPlot${i}(page)])"
        set lastPage [set apsPlot${i}(page)]
        set selectedList ""
        for {set j 1} {$j <= [set apsPlot${i}(pages)]} {incr j} {
            append selectedList "$selected($j) "
        }
        puts $selectedList
    }
    
    set lastPage 1
    while {1} {
        tkwait variable apsPlot${i}(page)
        set selected($lastPage) $select
        set select "$selected([set apsPlot${i}(page)])"
        set lastPage [set apsPlot${i}(page)]
        set selectedList ""
        for {set j 1} {$j <= [set apsPlot${i}(pages)]} {incr j} {
            append selectedList "$selected($j) "
        }
    }
}

