#!/usr/bin/env wish

# define three components to the window:

# text line with data value
# image region with image
# quit button

if [catch {exec /apps/elixir/bin/gconfig DATDIR} datdir] { 
    set picpath "ERROR" 
} else {
    set picpath "$datdir/plots"
}

set imname "(empty)"
set name "(empty)"
set pid [pid]
set index 0

proc mk_layout { } {

    global file lasttime picpath

    set lasttime 0
    if {$picpath == "ERROR"} {
	set file "ERROR"
    } else {
	set file "$picpath/empty.gif"
    }
    wm withdraw .

    set w [toplevel .t]
    wm title .t "user display"
    
    # top level containers
    frame $w.menubar -relief raised -borderwidth 2
    frame $w.picture -relief raised -borderwidth 2
    frame $w.dataline -relief raised -borderwidth 2

    pack $w.menubar -fill x
    pack $w.picture -fill x
    pack $w.dataline -fill x

    mk_menubar $w.menubar
    mk_picture $w.picture
    mk_dataline $w.dataline

    update
}

proc do_quit {} {
    
    exit 

}

proc mk_picture {w} {

    global file

    if {$file == "ERROR"} {
	label $w.name -text "Config Error: missing DATDIR"
	pack  $w.name -side top -fill both
    } else {
	image create photo picture -file $file
	label $w.name -image picture
	pack  $w.name -side top -fill both
    }
}

proc mk_menubar {w} {

    global file picpath cycle

    menubutton $w.file -text "File" -menu $w.file.menu
    pack $w.file -side left

    menu $w.file.menu -tearoff 0
    $w.file.menu add command -label "Quit" -command { do_quit }
    bind all <Alt-q> { do_quit }
    bind all <Control-q> { do_quit }
    
    menubutton $w.plot -text "Plots" -menu $w.plot.menu
    pack $w.plot -side left

    label $w.label -text "$file"
    pack $w.label -side right

    menu $w.plot.menu -tearoff 0
    $w.plot.menu add command -label "Cycle"           -command { set cycle 1 ; cycleImages }
    $w.plot.menu add command -label "biases"          -command { set cycle 0 ; set file "$picpath/biases.gif"; refresh_now;  }
    $w.plot.menu add command -label "skyprobe recent" -command { set cycle 0 ; set file "$picpath/skyprobe_recent.gif"; refresh_now;  }
    $w.plot.menu add command -label "skyprobe night"  -command { set cycle 0 ; set file "$picpath/skyprobe_night.gif"; refresh_now;  }
    $w.plot.menu add command -label "sky vs time"     -command { set cycle 0 ; set file "$picpath/sky.time.gif"; refresh_now;  }
    $w.plot.menu add command -label "sky vs airmass"  -command { set cycle 0 ; set file "$picpath/sky.airmass.gif"; refresh_now;  }
    $w.plot.menu add command -label "Recent Seeing"    -command { set cycle 0 ; set file "$picpath/seeing_recent.gif"; refresh_now;  }
    $w.plot.menu add command -label "Night Seeing"    -command { set cycle 0 ; set file "$picpath/seeing_night.gif"; refresh_now;  }
    $w.plot.menu add command -label "FWHM vs airmass" -command { set cycle 0 ; set file "$picpath/fwhm.airmass.gif"; refresh_now;  }
    $w.plot.menu add command -label "field (small)"   -command { set cycle 0 ; set file "$picpath/field.small.gif"; refresh_now;  }
    $w.plot.menu add command -label "field (large)"   -command { set cycle 0 ; set file "$picpath/field.large.gif"; refresh_now;  }
    $w.plot.menu add command -label "temperatures"    -command { set cycle 0 ; set file "$picpath/temps.time.gif"; refresh_now;  }
    $w.plot.menu add command -label "last image (10x10)"   -command { set cycle 0 ; set file "$picpath/current.b10.gif"; refresh_now;  }
    $w.plot.menu add command -label "last image (zoom)"     -command { set cycle 0 ; set file "$picpath/current.zoom.gif"; refresh_now;  }
    $w.plot.menu add command -label "last short image" -command { set cycle 0 ; set file "$picpath/lastshort.zoom.gif"; refresh_now;  }
    $w.plot.menu add command -label "focus (unit)"     -command { set cycle 0 ; set file "$picpath/focus.unit.gif"; refresh_now;  }

}

proc mk_dataline {w} {

    global fwhm imname name file
    set time [clock format [clock seconds] -format "%H:%M:%S"]

    label $w.label -text "name: $name  |  FHWM: 0.0  |  FILTER: X  |  time: $time            | image: $imname"
    pack $w.label -fill x

}

proc cycleImages { } {

    global file cycle picpath index

    set images { skyprobe_recent.gif sky.time.gif seeing_recent.gif biases.gif temps.time.gif current.b10.gif current.zoom.gif lastshort.zoom.gif }


    if { $cycle == 0 } { return }

    set file "$picpath/[lindex $images $index]"
    refresh_now

    incr index
    if { $index == [llength $images] } {
	set index 0
    }

    after 30000 {
	cycleImages
    }
}

proc do_refresh { } {

    global picpath imname name file

    after 1000 {
	
	set time [clock format [clock seconds] -format "%H:%M:%S"]
	set name "(empty)"
	set fwhm 0.0
	set filter "X"

	if {! [catch {open $picpath/seeing.dat r} data] } {
	    gets $data line
	    close $data
	    set list [split $line]
	    if {[llength $list] == 4} {
		set name [lindex $list 0]
		catch { set fwhm [format "%5.2f" [lindex $list 1]] } status
		set filter [lindex $list 3]
            }
	}
	
	.t.dataline.label configure -text "name: $name  |  FHWM: $fwhm  |  FILTER: $filter  |  time: $time           | image: $imname"
	.t.menubar.label configure -text "file: $file"
	update
	do_refresh 
    }
}

proc do_refresh_pic { } {

    global file lasttime imname name pid

    after 2000 {
	
	# get file status & time
	if {! [catch {file stat $file stats}] } {
	    set newtime $stats(mtime)
	    if {$newtime > $lasttime} {
		set tmpfile "$file.$pid"
		catch { exec ln -f $file $tmpfile }
		catch { picture read $tmpfile -shrink } 
		catch { exec rm -f $tmpfile }
		set imname $name
		set lasttime $newtime
		update
	    }
	}
	do_refresh_pic
    }
}

proc bgerror {result} {
	puts "edisp error: $result"
}

proc refresh_now { } {

    global file lasttime imname name pid

    if {[file exists $file]} {
	set tmpfile "$file.$pid"
	catch { exec ln -f $file $tmpfile }
	catch { picture read $tmpfile -shrink }
	catch { exec rm -f $tmpfile }

	file stat $file stats
	set imname $name
	set lasttime $stats(mtime)
	update
	
    }
}

mk_layout
do_refresh
do_refresh_pic
