#!/opt/tcl/bin/sywish
#!/opt/tcl/bin/wishx
#!/opt0/tcl/bin/sywish
#
#-------------------------------------------------------------------------
# initialize interp
#-------------------------------------------------------------------------
# first build your interp with all necessary extensions.
# always start with a sybtcl/tclX equipped wish!
#
global env
#
set odir opt
# set odir opt0
set tcllib /$odir/tcl/lib
set tclroot /$odir/share/tcl
set ucodb $tclroot/lib/ucodb
set klib $env(KTL_LIBRARY_PATH)
#
##############################################################################
# Mode switches
#
# Devel vs Deploy (Devel 1 vs Devel 0)
#	in Devel mode, frames are obtrusive (for editing)
#	in Deploy mode, frames are minimal (better l&f)
# Fake vs Real (Fake 1 vs Fake 0)
#	in Fake mode, there is no KTL backend and all condition
#		proc is done via trace.
# Engr vs Obsr (Engr 1 vs Engr 0)
#	Engr mode is the canvas, free placement model with invidual 
#		set/monitor buttons on Setter widgets;  also frame 3 
#		(Actual/Desired and Go buttons by default) is visible
# 	Obsr mode is the mode where widget frames are hidden and
#		individual set/mon buttons are missing
# Safe vs NotSafe (Safe 0 vs Safe 1) 
#	Safe mode prohibits any kw writes.  All KTL write commands
#		are intercepted.  KTL reads work normally.
#
# we turn these bits on and off with argv0 and the cmd line argument.
#
# if our name is engr then we run in Engr mode.
# if our name is anything else then we run in Obsr mode.
# if the cmd line flags contain S then we run in Safe mode 
# if the cmd line flags contain D then we run in Devel mode
# if the cmd line flags contain F then we run in Fake mode 
#	(this is mutually exclusive with Safe, of course).
#
set Mode [file tail $argv0]
set Fake 0
set Safe 0
set Devel 0
#
if {$Mode != "engr"} {
	set Mode obsr
}
#
set argv [string toupper $argv]
if {$argv != ""} {
	if {[string first F $argv] >= 0} {
		set Fake 1
		puts stderr "****----  FAKE MODE ON:  NO SERVICE!  ----****"
	} elseif {[string first S $argv] >= 0} {
		set Safe 1
		puts stderr "****----  SAFE MODE ON:  NO WRITING!  ----****"
	}
	if {[string first D $argv] >= 0} {
		set Devel 1
		puts stderr "****----  DEVL MODE ON:  BIG FRAMES!  ----****"
	}
	if {[string first H $argv] >= 0} {
		puts stderr "Usage:  ktui/engr/obsr/Name S|D|F"
		puts stderr "(any cmd name other than engr runs in obsr mode)"
		puts stderr "		S Safe mode"
		puts stderr "		D Developer mode"
		puts stderr "		F Fake mode"
		puts stderr "		(S and F are mutually exclusive, F wins)"
		exit 0
	}
}
#
##############################################################################
#
# load the KTL "ktcl" library to talk to KTL services
set err [catch {load $klib/libKtk.so} res]
if {$err} {
	puts stderr "SORRY, can't proceed, no libKtk.so or load error:"
	puts stderr "$res"
	exit 1
}
#
# Got enough widgets?  Never!
#
# load the VU meters
set err [catch {load $tcllib/libMeters.so} res]
if {$err} {
	puts stderr "SORRY, can't proceed, no libMeters.so or load error:"
	puts stderr "$res"
	exit 1
}
# load the LLNL turndial widget
set err [catch {load $tcllib/libturndial.so}]
if {$err} {
	set have_turndial 0
	puts stderr "No Turndial widget library found."
} else {
	set have_turndial 1
}
# load the BLT widgets
set err [catch {load $tcllib/libBLT.so.2} res]
if {$err} {
	puts stderr "No BLT widget library or error loading:"
	puts stderr "$res"
	puts stderr "That means no plot meters."
	set have_blt 0
}
#
# Now set up your interp -- fix bogus tk4 bindings using Raines bindings.
# also load turndial bindings.  load any tlibs you may need.
#
# use the tkmail bindings, with whatever hacks we add locally
# if the enhanced bindxtnd.tcl is not the default, source it
        if {[info proc tkBindDefVar]==""} {
          foreach key [bind Text] { bind Text $key {} }
          foreach key [bind Entry] { bind Entry $key {} }
          source $ucodb/tkbind/bindxtnd.tcl
          source $ucodb/tkbind/text.tcl
          source $ucodb/tkbind/entry.tcl
          # source additional packages here
        }
if {$have_turndial} {
source $ucodb/turndial.tcl
}
set bitmapdir $ucodb/bitmaps
loadlibindex $ucodb/ucodb.tlib
loadlibindex $ucodb/ktui.tlib
# loadlibindex ./ktui.tlib
#
#	Set this to 0 if you don't have "ElectroLog", otherwise set it
#	to the port number that your electrolog uses.
set ElogPort 51215
set ElogName /$odir/share/tcl/bin/electrolog
#
puts stderr "Loaded all libs, ready to roll."
#
set helpdir $ucodb/help
# set helpdir ./help
#
proc tellTale {{n1 {}} {n2 {}} op} {
	puts stderr "Warning, $op to $n1..."
}
set tcl_precision 17
trace variable tcl_precision w tellTale
#
#
# shortcut:  if you have a file with names of procs, and you launch
# ktui with name of file as sole arg, it will peel apart its own tlib
# and write each proc out to the ./Tlib dir.
#if {$argv != ""} {
#	saveProcs $argv
#	exit 0
#}
#-------------------------------------------------------------------------
# initialize globals
#-------------------------------------------------------------------------
#
# used by the command editor mode of mkEdit (see ucodb.tlib)
set CmdPtr 0
#
set Detach 0
#
# This is a hideous hack to get around expr parsing.  It was that,
# or search a list of 400 keywords...
#
set MathFuncs "sin cos tan atan log exp"
#
set uname guest
set base keck
set server UCO-SCIDB
set pass rsvp
# ONLY if sybase
if {[info commands sybconnect] != ""} {
set err [catch {set dbpipe1 [sybOpen $base $uname $pass $server]}]
if {!$err} {
set dbpipe2 [sybOpen $base $uname $pass $server]
set memtbl keck.dbo.Memes
set mbntbl keck.dbo.Mbundles
set mvatbl keck.dbo.Mvalues
} else {
puts stderr "NO SYBASE CONNECTION."
}
}
set MeterId 0
set SetMeters "Knob TextBox Odometer"
set SeeMeters "DialGauge GasGauge StripChart Plot BarChart OnOff Odometer"
keylset Mtrans(Knob) var variable min from max to
keylset Mtrans(Odometer) var textvariable 
keylset Mtrans(TextBox) var textvariable 
keylset Mtrans(OnOff) var variable 
keylset Mtrans(OnOffLight) var textvariable 
keylset Mtrans(Slider) var variable 
set MeterType TextBox
# Default file names.  dbd for dashboard, kwd for keywords
# the others don't mean anything (yet).
set LayoutFile KTUI.dbd
set SetupFile KTUIsetup.dat
set KeywordFile KTUI.kwd
set CurrentFile KTUIcurrent.dat
#
set Services ""
set Khandles ""
set Vc 0
set MenuFont "-Adobe-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-*"
set Pcolours(0) red
set Pcolours(1) yellow
set Pcolours(2) green
set Pcolours(3) blue
set Pcolours(4) cyan
set PKlist ""
#
# Message area background colour rotates among this list, so as
# to attract your attention when the message changes
set MsgColours(0) linen
set MsgColours(1) honeydew
set MsgColours(2) papayawhip
set MsgColours(3) azure
set MsgColours(4) lemonchiffon
set MsgColours(5) lavender
set MsgColours(6) bisque
set MsgColours(7) mistyrose
#
set InactiveFg gray90
set InactiveBg gray75
#
set GraphicTypes "line rectangle text pie chord arc oval polygon"
#
set Grid_Index(gl) MainLabel
set Grid_Index(co) Header
set Grid_Index(l_) Labels
set Grid_Index(a_) Actual
set Grid_Index(d_) Desired
set Grid_Index(go) Button
set Grid_Atts(gl) "foreground font text"
set Grid_Atts(co) "foreground font"
set Grid_Atts(l_) "foreground font"
set Grid_Atts(a_) "foreground font"
set Grid_Atts(d_) "foreground background font"
set Grid_Atts(go) "foreground background font text"
#
set WindowId 0
set oper ""
if {$Fake} {
	append oper "Fake "
}
if {$Safe} {
	append oper "Safe "
}
if {$Devel} {
	append oper "Devel"
}
set oper [join $oper -]
keylset Windows(0) name "DashBoard-II Prototype KTL GUI : $Mode : $oper" geom "+300+300"
#
set GraphicId 0
#
set Start [clock seconds]
set FStart [clock format $Start]
#
set ServiceList [list acs dcs dcs2 deimos esi hires lris nirc pfcam]
#
set lfp [open ktui.log a]
# set tfp [open ktui.trace w]
# cmdtrace on $tfp
interDep
#
#
puts $lfp "\n\n\n***-------------------------------------------------------------***"
puts $lfp "KTUI LOG START [clock format [clock seconds]] ... Mode : $Mode"
puts $lfp "***-------------------------------------------------------------***\n\n"
#
#-------------------------------------------------------------------------
# initialize main window and go
#-------------------------------------------------------------------------
#
mkDashboard
#
focus .f0.cmd
#
mkMsg "Ready to go:  Please choose a Service."
#
