package provide Tools 1.0
namespace eval Browser {
variable _path ""

proc _ask {} {

	if {[string equal $::tcl_platform(platform) windows]} {
		set strFileTypes {{"Executable Files" .exe} {"All Files" *}}
	} else {
		set strFileTypes {}
	}
	return [tk_getOpenFile -filetypes $strFileTypes -parent . -title "Locate Browser"]
}

proc path {} {

	if {$Browser::_path == ""} {
		set Browser::_path [Browser::_find]
		if {$Browser::_path == ""} {
			set Browser::_path [Browser::_ask]
		}
	}

	return $Browser::_path
}

proc _find {} {
	
	set pathResult ""
	switch -exact -- $::tcl_platform(platform) {
		unix {		
			set listPath [split $::env(PATH) ":"]
			foreach dir $listPath {
				set path [file join $dir netscape]
				if {[file executable $path]} {set pathResult $path; break}
				set path [file join $dir Mosaic]
				if {[file executable $path]} {set pathResult $path; break}
				set path [file join $dir hotjava]
				if {[file executable $path]} {set pathResult $path; break}
			}	
		}	
		windows {
			package require registry
			set keyRoot HKEY_CLASSES_ROOT
			if {![catch "set keyClass [registry get $keyRoot\\.html {}]"]} {			
				if {$keyClass != ""} {
					set strCommand [registry get "$keyRoot\\$keyClass\\shell\\open\\command" ""]					
					if {[string equal [string index $strCommand 0] "\""]} {
						set end [string first "\"" [string range $strCommand 1 end]]
						set pathResult [string range $strCommand 1 $end]
					} else {
						set pathResult [lindex [split $strCommand] 0]
					}
				}
			} else {
				set fileExplorer "iexplorer.exe"
				set fileNetscape "netscape.exe"

				set listPath [split $::env(PATH) ";"]
				foreach dir $listPath {
					set path [file join $dir $fileExplorer]
					if {[file executable $path]} {set pathResult $path; break}
					set path [file join $dir $fileNetscape]
					if {[file executable $path]} {set pathResult $path; break}
				}
			}
		}
	} ;#end switch		
		return $pathResult
}
} ;# end namespace Browser