# browser.tcl
#
# ****************************************************************************
# Copyright (c) 2001 Digi International Inc., All Rights Reserved
# 
# This software contains proprietary and confidential information of Digi
# International Inc.  By accepting transfer of this copy, Recipient agrees
# to retain this software in confidence, to prevent disclosure to others,
# and to make no use of this software other than that for which it was
# delivered.  This is an unpublished copyrighted work of Digi International
# Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
# prohibited.
# 
# Restricted Rights Legend
#
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
# Technical Data and Computer Software clause at DFARS 252.227-7031 or
# subparagraphs (c)(1) and (2) of the Commercial Computer Software -
# Restricted Rights at 48 CFR 52.227-19, as applicable.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
# ***************************************************************************

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

proc _ask {} {
    set strMsg "Error: Default web browser not found.  Please locate a web "
    append strMsg "browser with the following dialog box."
    tk_messageBox -message $strMsg -icon error -type ok -default ok -title "Web browser not found"
	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]
					}
                    if {![file executable $pathResult]} {
                        set pathResult ""
                    }
				}
			} 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