#!/bin/sh
# the next line restarts using wish \
exec wish -f "$0" "$@"

set font -*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*
set server_name_font -*-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-* 

set arch [exec "tm" "getarch"]

# See if the TM_SERVER environment variable is set

set exists [lsearch [array names env] TM_SERVER]
if { $exists == -1 } {
	set server %%DEFAULT_SERVER%%
} else {
	set server $env(TM_SERVER)
	}

if { $argv == "-t" } {
	set header_padding 0
	set font -*-Helvetica-Medium-R-Normal--*-80-*-*-*-*-*-*
	set server_name_font -*-Helvetica-Bold-R-Normal--*-140-*-*-*-*-*-* 
} else	{
	set header_padding 13
}

label .msg -wraplength 4i -justify left -text " $server $arch status " \
	-font $server_name_font
pack .msg -side top

set design " unknown design "
label .design -wraplength 4i -justify left -text $design \
	-font $font
pack .design -side top -anchor w -padx 20

set designdate "  "
label .designdate -wraplength 4i -justify left -text $designdate \
	-font $font
pack .designdate -side top -anchor w -padx 20

set status1 " unknown status1 "
label .status1 -wraplength 4i -justify left -text $status1 \
	-font $font
pack .status1 -side top -anchor w -padx 20

set status2 " unknown status2 "
label .status2 -wraplength 4i -justify left -text $status2 \
	-font $font
pack .status2 -side top -anchor w -padx 20

frame .buttons
pack  .buttons -side bottom -expand y -fill x -pady 2m
button .buttons.dismiss -text Dismiss -command "destroy ."
pack .buttons.dismiss -side left -expand 1


set tm [open "|tm -" "r+"]
fconfigure $tm -buffering none -blocking 0

set script {
	puts $tm "getdesigndir"
	puts $tm "getdesigndate"
	puts $tm "getstatus1"
	puts $tm "getstatus2"
	}

set linenumber 1

fileevent $tm readable {
	if { [eof $tm] == 1 } {
		puts "tmstatus: lost contact with tmumon; exiting"
		exit 1
		}
	switch [expr $linenumber] {
		1 {set newdesign [gets $tm]
			if { $design != $newdesign } {
				.design configure -text $newdesign
				set design $newdesign
				}
			}

		2 {set newdesigndate [gets $tm]
			if { $designdate != $newdesigndate } {
				.designdate configure -text "Loaded: [string range $newdesigndate 0 15]"
				set designdate $newdesigndate
				}
			}

		3 {set newstatus1 [gets $tm]
			if { $status1 != $newstatus1 } {
				.status1 configure -text $newstatus1
				set status1 $newstatus1
				}
			}

		4 {set newstatus2 [gets $tm]
			if { $status2 != $newstatus2 } {
				.status2 configure -text $newstatus2
				set status2 $newstatus2
				}
			}

		}
	if { $linenumber < 4 } {
		set linenumber [expr $linenumber + 1]
	} else {
		set linenumber 1
		after 500 $script
		}
	}

after 100 $script
