#!/bin/sh
#
# startup tcl/tk programs by selecting a compressed binary to source
# set required environment variables for various programs
# requires modified Tcl init.tcl that knows how to source compressed files

# Tcl-To-Go addon floppy for muLinux 
# tom poindexter <tpoindex@nyx.net>
# see ../doc/Tcl-To-Go.README for more information


##############################################################################
# tcl/tk locations

# toplevel directory, if you unpack Tcl-To-Go into another directory, this
# should be the only variable to change 
#     also see below: TCL_LIBRARY TK_LIBRARY LD_LIBRARY_PATH PATH
#     also: check env vars of programs that dont use STARTUP.SH: 
#	     mmencode

tcltkdir=/usr/tcltk

gzdir=$tcltkdir/lib/bingz
tcltklibdir=$tcltkdir/lib

# set tcl/tk library paths, these are only needed if $tcltkdir is 
# other than /usr/tcltk

#export TCL_LIBRARY TK_LIBRARY
#TCL_LIBRARY=$tcltklibdir/tcl8.0
#TK_LIBRARY=$tcltklibdir/tk8.0

# include our lib path, in order to find shared libs 
#export LD_LIBRARY_PATH
#LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib:/usr/local/lib:$tcltklibdir

# include our bin path, in order to find tclsh & wish interpreters
export PATH
PATH=$PATH:$tcltkdir/bin


##############################################################################
# set application specific environment vars, if not previously set.
# as more apps are added that require specific env vars, add them here

export TCLTKLIB DIRC_LIB LDSDRAW_LIBRARY FIREMAN_LIB TKAPPS \
       VTCL_HOME TUBA_LIB NETPLUG_PLUGINS MMUCL_DIR TCLTEXTEDIT_LIB \
       TKFTP_LIB_DIR WISH TKPAINT_LIB USER HOST DOMAIN \
       TCLTICKER_LIB

# set the variables only if not previously set

: ${TCLTKLIB:=$tcltklibdir}
: ${DIRC_LIB:=$tcltklibdir/dirc}
: ${LDSDRAW_LIBRARY:=$tcltklibdir/ldsdraw}
: ${FIREMAN_LIB:=$tcltklibdir/fireman}
: ${TKAPPS:=$tcltklibdir/tkapps}
: ${VTCL_HOME:=$tcltklibdir/vtcl}
: ${TUBA_LIB:=$tcltklibdir/tuba}
: ${NETPLUG_PLUGINS:=$tcltklibdir/netplug}
: ${MMUCL_DIR:=$tcltklibdir/mmucl}
: ${TCLTEXTEDIT_LIB:=$tcltklibdir/edit}
: ${TKFTP_LIB_DIR:=$tcltklibdir/tkftp}
: ${WISH:=wish}
: ${TKPAINT_LIB:=$tcltklibdir/tkpaint}
: ${USER:=root}
  hostname=`cat -s /proc/sys/kernel/hostname 2>/dev/null`
: ${HOST:=$hostname}
  domainname=`cat -s /proc/sys/kernel/domainname 2>/dev/null`
: ${DOMAIN:=$domainname}
: ${TCLTICKER_LIB:=$tcltklibdir/tclticker}


##############################################################################
# save arguments, and find out the basename of which we have been invoked
# aka, cheap 'basename' hack

# (args should probably be collected into 'wish' specific args, e.g.,
#  -colormap -display -geometry -name -sync -visual -use, maybe later.)

args=$*

saveifs="$IFS"
IFS=/
eval set -- $0
filename=$*
file=$1
IFS="$saveifs"
for f in $filename
do
  file=$f
done


##############################################################################
# create startup script: set argv0, set tk default title, set to kill wish
# if window is deleted, then source compressed file, evaling with real args

startup="set argv0 \"$0\"
	 catch { wm title    . \"$file\" }
	 catch { wm protocol . WM_DELETE_WINDOW exit }
	 source \"$gzdir/$file\"
"

eval exec wish -name $file -- $args <<__EOF__
  $startup
__EOF__

