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

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/linux-x86_64]

set apsttk 1
APSApplication . -name "Quick SDDS Logger"

set status "Ready..."
APSScrolledStatus .status \
  -parent .userFrame \
  -textVariable status \
  -width 100 \
  -packOption "-fill x"

ttk::labelframe .userFrame.f1 -text "Logging Input and Output Files"
pack .userFrame.f1 -anchor nw -fill x

set InputFile ""
ttk::label .userFrame.f1.label0 \
  -text "Input File: "
ttk::entry .userFrame.f1.entry0 \
  -textvariable InputFile
grid .userFrame.f1.label0 -row 0 -column 0 -sticky e
grid .userFrame.f1.entry0 -row 0 -column 1 -sticky we
grid columnconfigure .userFrame.f1 1 -weight 1

set OutputFile ""
ttk::label .userFrame.f1.label1 \
  -text "Output File: "
ttk::entry .userFrame.f1.entry1 \
  -textvariable OutputFile
grid .userFrame.f1.label1 -row 1 -column 0 -sticky e
grid .userFrame.f1.entry1 -row 1 -column 1 -sticky we

frame .userFrame.f2
pack .userFrame.f2 -anchor nw

APSButton .newInputFile \
  -parent .userFrame.f2 \
  -text "Create New / Edit Existing Input File" \
  -command "SelectNewEditExistingInputFile" \
  -width ""

APSButton .existingInputFile \
  -parent .userFrame.f2 \
  -text "Select Existing Input File" \
  -command "SelectExistingInputFile" \
  -width ""

ttk::labelframe .userFrame.f3 -text "Logging Options"
pack .userFrame.f3 -anchor nw

set sampleInterval 1
ttk::label .userFrame.f3.label0 \
  -text "Sample Interval : "
ttk::entry .userFrame.f3.entry0 \
  -textvariable sampleInterval
ttk::label .userFrame.f3.label0b \
  -text "seconds"
grid .userFrame.f3.label0 -row 0 -column 0 -sticky e
grid .userFrame.f3.entry0 -row 0 -column 1 -sticky w
grid .userFrame.f3.label0b -row 0 -column 2 -sticky w
  
set runTime 60
set runTimeUnits [list seconds minutes hours days]
set timeUnits seconds
ttk::label .userFrame.f3.label1 \
  -text "Run Time: "
ttk::entry .userFrame.f3.entry1 \
  -textvariable runTime
ttk::combobox .userFrame.f3.combobox1 \
  -values $runTimeUnits \
  -textvariable timeUnits
grid .userFrame.f3.label1 -row 1 -column 0 -sticky e
grid .userFrame.f3.entry1 -row 1 -column 1 -sticky w
grid .userFrame.f3.combobox1 -row 1 -column 2 -sticky w
  
set flushInterval 1
ttk::label .userFrame.f3.label2 \
  -text "Flush Interval: "
ttk::entry .userFrame.f3.entry2 \
  -textvariable flushInterval
grid .userFrame.f3.label2 -row 2 -column 0 -sticky e
grid .userFrame.f3.entry2 -row 2 -column 1 -sticky w

set dailyFiles 0
ttk::label .userFrame.f3.label3 \
  -text "Daily Files: "
ttk::checkbutton .userFrame.f3.checkbutton3 \
  -text "" \
  -variable dailyFiles
grid .userFrame.f3.label3 -row 3 -column 0 -sticky e
grid .userFrame.f3.checkbutton3 -row 3 -column 1 -sticky w

set verbose 0
ttk::label .userFrame.f3.label4 \
  -text "Verbose: "
ttk::checkbutton .userFrame.f3.checkbutton4 \
  -text "" \
  -variable verbose
grid .userFrame.f3.label4 -row 4 -column 0 -sticky e
grid .userFrame.f3.checkbutton4 -row 4 -column 1 -sticky w

frame .userFrame.f4
pack .userFrame.f4 -anchor nw

APSButton .startLogger \
  -parent .userFrame.f4 \
  -text "Start Logger" \
  -command "StartLogger" \
  -width ""

APSButton .startQuickSDDSplot \
  -parent .userFrame.f4 \
  -text "Launch Plotting Tool" \
  -command "LaunchPlottingTool" \
  -width ""

proc SelectNewEditExistingInputFile {args} {
    global InputFile OutputFile status

    set f [tk_getSaveFile -confirmoverwrite 0]
    if {$f == ""} {return}
    if {[file exists $f]} {
        if {[catch {exec sddscheck $f -printErrors} results]} {
            set status "Existing file is not an SDDS file"
            return
        }
        if {[catch {exec sddsquery $f -col} results]} {
            set status "Error reading the SDDS column names: $results"
            return
        }
        set i1 [lsearch -exact $results ControlName]
        set i2 [lsearch -exact $results ReadbackName]
        if {$i1 == -1} {
            set status "Error: Did not find required ControlName column"
            return
        }
        if {$i2 == -1} {
            set status "Error: Did not find required ReadbackName column"
            return
        }
        set InputFile $f
        set OutputFile [file root $f].output.sdds
        set status "Editing Logger Input File $f" 
    } else {
        if {[catch {exec sddsmakedataset $f -ascii \
                      -column=ControlName,type=string \
                      -data=Example1,Example2,Example3 \
                      -column=ReadbackName,type=string \
                      -data=Example1,Example2,Example3 \
                      -column=Units,type=string \
                      -data=Amps,Volts,Seconds} results]} {
            set status "Error creating new input file: $results"
            return
        }
        set InputFile $f
        set OutputFile [file root $f].output.sdds
        set status "Creating Logger Input File $f"
    }
    exec sddsedit $f &
}

proc SelectExistingInputFile {args} {
    global InputFile OutputFile status

    set f [tk_getOpenFile]
    if {$f == ""} {return}
    if {[catch {exec sddscheck $f -printErrors} results]} {
        set status "Invalid input file"
        return
    }
    if {[catch {exec sddsquery $f -col} results]} {
        set status "Error reading the SDDS column names: $results"
        return
    }
    set i1 [lsearch -exact $results ControlName]
    set i2 [lsearch -exact $results ReadbackName]
    if {$i1 == -1} {
        set status "Error: Did not find required ControlName column"
        return
    }
    if {$i2 == -1} {
        set status "Error: Did not find required ReadbackName column"
            return
    }
    set InputFile $f
    set OutputFile [file root $f].output.sdds
    set status "Logger Input File $f selected"
}

proc StartLogger {args} {
    global status InputFile OutputFile sampleInterval runTime 
    global timeUnits flushInterval dailyFiles verbose
    
    set command "sddslogger $InputFile $OutputFile -sampleInterval=${sampleInterval},seconds -time=${runTime},$timeUnits -flushInterval=$flushInterval"
    if {$dailyFiles} {
        append command " -dailyFiles"
    }
    if {$verbose} {
        append command " -verbose"
    }
    set status "Running: $command"
    APSExecLog .execLog -unixCommand $command
}

proc LaunchPlottingTool {args} {
    global status OutputFile dailyFiles
    
    if {$dailyFiles} {
        set dataFileList [glob -nocomplain $OutputFile-????-???-????.????]
    } else {
        set dataFileList $OutputFile
    }
    exec quickSDDSplot -dataFileList $dataFileList &
}







