#!/usr/bin/env tclsh
#<20230201.1426.23>
# The soul purpose of this file is to provide a *.tcl file that will cause windows
# to run it with the program (hopefully tclsh) attached to *.tcl files
#
# we then source the real filerunner.
########################################################
#  This is the boiler plate code ver <20160420.1642.12>#                 
########################################################
# This bit of code figures out where the rest of the   #  
# routines are on the assumption that they are in the  #
# same directory as the initial code file.             #
# If 'setIt' is 1 or not coded auto_path is set in any #
# case the resulting dir is returned to the caller     #
# A second dir is also returned, which is the same     #
# with a leading C:/ removed as required by Wrap code  # 
# for windows. If not windows the two will be the same.#
#                                                      #
# To function correctly this code MUST be called prior #
# to completion of the 'source' command that brings it #
# in. Also, since it is used to set up auto_path it    #
# can not be auto loaded.  It may be sourced, but      #
# again the from where issue is there.                 #
#                                                      #
# It is best if this is just merger with the using     #
# code in a location prior to its call.                #
#                                                      #
proc cSetAutoPath {new} {
  if {$new ni $::auto_path} {lappend ::auto_path $new}
}

proc setAutoPath {{setIt 1}} {
  set it [info script]
  set it [expr {$it == "" ? "[pwd]/*" : $it}]
  set it [file dir [file dir [file norm $it/*]]]
  # Wrap code requires we not have the drive letter...
  if {[namespace exists freewrap]} {
    set it [regsub {^[a-zA-Z]:/} $it {/}]
  }
  if {$setIt} {
    cSetAutoPath $it
  }
  return $it
}
##########################################################
#            End of boiler plate code                    #
##########################################################
#
#
lappend ::startTimes [list [clock milliseconds] "fr.tcl First light"]
set trueFr  [setAutoPath 0]/fr
set ex [info nameofex]
# This bit removes any special local dirs from auto_path
# This is done mostly to prevent shipping a filerunner that 
# depends on local files...And so we get the right code when debugging
if {[info exists env(TCLLIBPATH)] } {
  foreach path $env(TCLLIBPATH) {
    set indx [lsearch -exact $auto_path $path]
    if {$indx != -1} {
      set auto_path [lreplace $auto_path $indx $indx]
    }
  }
  # auto_reset
}
# In the past we wanted to run under tclsh in MSW. This, it appears
# was based on a false assumption (that wish did not do pipes correctly).
# Here we insure that we are in wish.
if {[string match -nocase {*tclsh*} [file tail $ex]] &&\
	$tcl_platform(platform) == {windows} &&\
	! [namespace exists freewrap] &&\
	[set target [auto_execok wish]] != {}} {
  exec $target $trueFr {*}$argv &
  exit 0
}

set r [catch [list source $trueFr] out]

if {$r != 0} {
  puts "Sourceing $trueFr failed with error: $out"
}
# all done
