# browser.tcl
# Routines to interface to Web Browsers.

# WebTk is a Tk application so we can just send it a Tcl command.

proc Browse_WebTk {url} {
    set interps [winfo interps]
    set ix [lsearch -glob $interps webtk*]
    if {$ix >= 0} {
	send [lindex $interps $ix] [list Url_DisplayNew $url]
	LogUser "WebTk is displaying $url"
    } else {
	exec webtk $url &
	LogUser "Starting WebTk"
    }
}

# Netscape has a backdoor that uses its -remote function to send
# an openURL command to a running netscape process.

proc Netscape {url} {
    if [ catch { exec netscape -remote openURL($url) >& /dev/null} tmp ] {
	if { [ string first "not running on" $tmp ] != -1 } {
	    exec netscape $url &
	    LogUser "Starting netscape"
	}
    } else {
	LogUser "Netscape is displaying $url"
    }
}
# Mosaic has a backdoor that uses a file in /tmp to contain a URL.
# The name of the file is /tmp/Mosaic.pid
# and you send mosaic a SIGUSER1 to get it to look at the file

set uri(mosaicApp) mosaic

proc Mosaic {url} {
    Mosaic_Load $url
}
proc Mosaic_Load { url {newwin goto} } {
    global mosaic uri
    if ![info exists mosaic(pid)] {
	set mosaic(pid) [PsByName $uri(mosaicApp)]
	if {[string length $mosaic(pid)] == 0} {
	    LogUser "Starting $uri(mosaicApp)"
	    set mosaic(pid) [exec $uri(mosaicApp) $url &]
	    return
	} else {
	    LogUser "$uri(mosaicApp) running with pid $mosaic(pid)"
	}
    }
    if [catch {open /tmp/Mosaic.$mosaic(pid) w} out] {
	error $out
    }
    puts $out $newwin
    puts $out $url
    close $out
    catch {unset mosaic(fail)}
    if [catch {exec kill -USR1 $mosaic(pid)} err] {
	if [info exists mosaic(fail)] {
	    LogUser "Cannot signal xmosaic $mosaic(pid)"
	    unset mosaic(pid)
	    unset mosaic(fail)
	    error $err
	} else {
	    set mosaic(fail) $err
	    unset mosaic(pid)
	    Mosaic_Load $url
	}
    }
}

