
proc APSDebugPathHelp {} {
    return \
"Usage: APSDebugPath

This routine reconfigures both the Tcl/Tk auto_path (for procedures)\
and the search PATH (for executables) so that debugging can be done from\
different locations than those used by users.

If the environment variable OAG_DEBUG_AUTO_PATH is set, then it\
preappends the paths in OAG_DEBUG_AUTO_PATH to the auto_path and causes
auto_load to reinitialize the next time it is used.

If the environment variable OAG_DEBUG_PATH is set, then it preappends\
the paths in OAG_DEBUG_PATH to the environment variable PATH, except\
that if . was the first element in PATH, then it remains the first.

Otherwise it does nothing, so it can safely be left in the script.

The paths in both OAG_DEBUG_AUTO_PATH and OAG_DEBUG_PATH are colon delimited\
if there are more than one.
"
}

proc APSDebugPath {} {
    global auto_path auto_oldpath auto_index env 

    # Set debug auto_path
    if {[info exists env(OAG_DEBUG_AUTO_PATH)]} {
        set pathlist [split $env(OAG_DEBUG_AUTO_PATH) ":"]
        set len [llength $pathlist]
        for {set i [expr {$len - 1}]} {$i >= 0} {incr i -1} {
            set ipath [lindex $pathlist $i]
            set auto_path [linsert $auto_path 0 $ipath]
        }
        # auto-load reads all the tclIndex files in the auto_path the first time
        # it is called, and makes all the commands it will ever need.  Changing
        # the path after it has done this ordinarily has no effect.  The
        # following lines cause it to reread the files.
        unset -nocomplain auto_index
        unset -nocomplain auto_oldpath
    }
    
    # Set debug PATH
    if {[info exists env(OAG_DEBUG_PATH)]} {
        set pathlist [split $env(OAG_DEBUG_PATH) ":"]
        set len [llength $pathlist]
        # Insure PATH is not empty (unlikely)
        if {![info exists env(PATH)]} {
            set env(PATH) ""
        }
        # Look for leading .
        set leadingdot 0
        set curpathlist [split $env(PATH) ":"]
        if {![string compare [lindex $curpathlist 0] "."]} {
            set leadingdot 1
            set env(PATH) [string range $env(PATH) 2 end]
        }
        # Preappend to PATH
        for {set i [expr {$len - 1}]} {$i >= 0} {incr i -1} {
            set ipath [lindex $pathlist $i]
            set env(PATH) "${ipath}:$env(PATH)"
        }
        # Put the leading . back if it was there
        if {$leadingdot} {
            set  env(PATH) ".:$env(PATH)"
        }
        # Trim any leading or trailing :'s
        string trim $env(PATH) ":"
    }
}
