#
# <20200302.1241.53>

proc load_now_auto_execok {} {}
#
# scraped this up using 'info body'
namespace eval auto_execok {
  variable haveWSL 0
# Do this just once...
  if {[file exists /proc/version]} {
    set ver [read [set fid [open /proc/version]]]
    set haveWSL [string match -nocase {*Microsoft*} $ver]
    close $fid
  }
}

proc auto_execok {name} {
  global auto_execs env

  if {[info exists auto_execs($name)]} {
    return $auto_execs($name)
  }
  # set auto_execs($name) ""
  if {[llength [file split $name]] != 1} {
    if {[file executable $name] && ![file isdirectory $name]} {
      set auto_execs($name) [list $name]
    }
    return $auto_execs($name)
  }
  set dirs [split $env(PATH) :]
  lappend exts {}
  # If it looks like this is a WSL system and no ext, look for both ".exe' and no ext
  if {$::auto_execok::haveWSL && [file ext $name] == {}} {lappend exts ".exe"}
  
  foreach dir $dirs {
    if {$dir eq ""} {
      set dir .
    }
    foreach ext $exts {
      frputs ext dir name
      set file [file join $dir $name$ext]
      if {[file executable $file] && ![file isdirectory $file]} {
	if {[file norm $dir] in $dirs} {
	  set auto_execs($name) [list $file]
	}
	return [list $file]
      }
    }
  }
  return ""
}
