#!/bin/sh
#\
exec wish "$0" "$@"
# Install interface for the Tcl plugin.
#
wm title . "Install Tcl Plugin"

set tclversion tcl7.7
set tkversion tk4.3
set pluginversion "1.0 Final"
set netscape "netscape http://www.sunlabs.com/tcl/plugin/applets.html"
set user ""
catch {set user $env(LOGNAME)}
catch {set user $env(USER)}
set email $user@[info hostname]

set PRODUCT "Netscape Tcl Plugin $pluginversion"
set targetOS [exec cat targetOS]

option add *Entry.relief sunken
frame .top -bd 5
message .top.title -aspect 2000 -justify center \
    -text "Install $PRODUCT"
if [catch {
    image create photo tclimage -file tcl.gif
    label .top.tcl -image tclimage
    image create photo tkimage -file tk.gif
    label .top.tk -image tkimage
}] {
}
 
message .msg -aspect 2000
frame .buttons -bd 10
button .ok -text "Easy Install" -command EasyInstall
button .custom -text "Custom" -command Custom
button .site -text "Site-wide install" -command Site
button .readmeb -text "Read Me" -command Readme
button .licenseb -text "View license" -command ViewLicense
button .helpb -text "Help" -command Help
button .faqb -text FAQ -command FAQ
button .quit -text "Quit" -command {
    catch {close $logfile}
    exit $exitcode
}
button .back -text "Back" -command {
    destroy .0 .1
    pack forget .back
    .ok configure -text "Easy Install" -command EasyInstall
    FirstScreen
}
foreach b {.ok .custom .site .quit} {
    bind $b <Return> "$b flash ; $b invoke"
}
button .log -text "View Log" -command viewLog
button .trouble -text "Submit Trouble Report" -command "troubleReport {}"
button .testb -text "Test It!" -command testIt
set custom 0
set exitcode 1

set logFileName install.log.[pid]
if {[catch {set logfile [open $logFileName w]} msg] != 0} {
    set logfile {}
}

proc FirstScreen {} {
    global logFileName

    catch {
	pack forget .top
	pack forget .msg
	pack forget .buttons
	pack forget .ok .custom .site
	pack forget .quit .helpb .readmeb .licenseb .faqb
    }
    .msg configure -aspect 2000 -text \
"The tcl plugin requires two parts, a shared libary (.so) and
some Tcl libraries.  If you select Easy Install, these are installed
in ~/.netscape/plugins and ~/.tclplug, respectively.

Log file for install: [pwd]/$logFileName, please include it in
trouble reports.
"
    if {[winfo exists .top.tcl]} {
	pack .top.tcl .top.title .top.tk -side left
    } else {
	pack .top.title -side top
    }
    pack .top
    pack .msg -fill x
    pack .buttons -side bottom -fill x
    pack .ok .custom .site -in .buttons -side left -padx 2
    pack .quit .helpb .readmeb .licenseb .faqb -in .buttons -side right -padx 2
    focus .ok
}

proc EasyInstall {} {
    global env PRODUCT user targetOS
    logMsg "Installing the plugin distribution for $targetOS"
    logMsg "System parameters: [exec uname -a]"
    logMsg "Doing easy install of $PRODUCT on [info hostname] for $user"
    logMsg "Started at [clock format [clock seconds] -gmt no]"
    logMsg ""
    logMsg "Installation Parameters:"
    logMsg \
	"    Installing plugin shared library to $env(HOME)/.netscape/plugins"
    logMsg "    Installing script libraries to $env(HOME)/.tclplug"
    logMsg "------------------------------------------------------------------"
    logMsg ""
    logMsg "Creating $env(HOME)/.netscape if it doesn't exist yet"
    catch {exec mkdir $env(HOME)/.netscape}
    foreach dir [list $env(HOME)/.netscape/plugins \
		    $env(HOME)/.tclplug] {
	if {![file isdirectory $dir]} {
	    logMsg "Making directory: $dir"
	    if [catch {exec mkdir $dir} err] {
		logMsg "Could not create directory $dir, aborting"
		.msg config -text $err
		return
	    }
	} else {
	    logMsg "Directory already exists, reusing: $dir"
	}
    }
    pack forget .custom .ok .site

    Install $env(HOME)/.netscape/plugins $env(HOME)/.tclplug
}
proc Site {} {
    global env
    global logFileName

    pack forget .custom .site
    pack .back -in .buttons
    .msg config -aspect 2000 -text \
"Netscape recommends /usr/local/lib/netscape/plugins as the place to
install plugins site-wide, or alternatively, /usr/lib/netscape/plugins.
If you chose /usr/local/lib/netscape/plugins, you can install the
Tcl library in /usr/local/lib/netscape/tclplug; if you chose to use
/usr/lib/netscape/plugins you can install the Tcl library in
/usr/lib/netscape/tclplug. If you chose either of these set-ups, your
users will not have to set any environment variables.

If you install the plugin shared library in another directory, your
users will have to set the environment variable NPX_PLUGIN_PATH to
where you installed the shared library. You may wish to provide a site
wide wrapper shell script that sets this variable to the correct
location before invoking Netscape Navigator.

If you then install the Tcl library in the directory computed by
\$NPX_PLUGIN_PATH/../tclplug, your users will not need to set an
additional environment variable to let the Tcl plugin find its libraries. 

If you decide to install the Tcl libraries elsewhere, you may wish to
include an environment variable assignment for TCL_PLUGIN_DIR in the
same shell script.

By default, the site-wide install uses /usr/local/lib/netscape/plugins as
the destination of the shared library, and /usr/local/lib/netscape/tclplug
for the Tcl libraries.

Log file for install: [pwd]/$logFileName, please include it in
trouble reports.
"
    set env(NPX_PLUGIN_PATH) /usr/local/lib/netscape/plugins
    set env(TCL_PLUGIN_DIR) /usr/local/lib/netscape/tclplug
    set i 0
    foreach var {NPX_PLUGIN_PATH TCL_PLUGIN_DIR} {
	frame .$i
	label .$i.l$var -text $var -anchor w -width 20
	entry .$i.e$var -textvariable env($var) -width 0
	bind .$i.e$var <Return> {CustomInstall "site wide"}
	pack .$i -fill x -padx 10
	pack .$i.l$var -side left
	pack .$i.e$var -side top -fill x
	incr i
    }
    .ok config -text "Install" -command {CustomInstall "site wide"}
}
proc Custom {} {
    global env
    global logFileName

    pack forget .custom .site
    pack .back -in .buttons
    .msg config -aspect 2000 -text \
"If you select custom locations, you must set the environment
variables NPX_PLUGIN_PATH and TCL_PLUGIN_DIR to reflect the
custom locations. NPX_PLUGIN_PATH should identify the location
where the shared library is stored, and TCL_PLUGIN_DIR should
denote the location where the Tcl and Tk libraries are put.

Log file for install: [pwd]/$logFileName, please include it in
trouble reports.
"
    set i 0
    set env(NPX_PLUGIN_PATH) $env(HOME)/.netscape/plugins
    set env(TCL_PLUGIN_DIR) $env(HOME)/.tclplug
    foreach var {NPX_PLUGIN_PATH TCL_PLUGIN_DIR} {
	frame .$i
	label .$i.l$var -text $var -anchor w -width 20
	entry .$i.e$var -textvariable env($var) -width 0
	bind .$i.e$var <Return> "CustomInstall custom"
	pack .$i -fill x -padx 10
	pack .$i.l$var -side left
	pack .$i.e$var -side top -fill x
	incr i
    }
    .ok config -text "Install" -command "CustomInstall custom"
}
proc CustomInstall {kindOf} {
    global env custom user targetOS
    logMsg "Installing the plugin distribution for $targetOS"
    logMsg "Doing $kindOf install on [info hostname] for $user,\
	started at [clock format [clock seconds] -gmt no]"
    logMsg ""
    logMsg "Installation Parameters:"
    logMsg \
	"    Installing plugin shared library to $env(NPX_PLUGIN_PATH)"
    logMsg "    Installing script libraries to $env(TCL_PLUGIN_DIR)"
    logMsg "------------------------------------------------------------------"
    logMsg ""
    foreach var {NPX_PLUGIN_PATH TCL_PLUGIN_DIR} {
	if {![info exist env($var)] ||
	    [string length [string trim $env($var)]] == 0} {
	    focus .e$var
	    .msg config -text "Please set $var"
	    logMsg "Environment variable not set, aborting: env($var)"
	    return
	}
	if ![file isdirectory $env($var)] {
	    logMsg "Creating directory: $env($var)"
	    if [catch {exec mkdir $env($var)} err] {
		logMsg "Could not create directory $env($var), aborting"
		.msg config -text $err
		return
	    }
	} else {
	    logMsg "Directory already exists, reusing: $env($var)"
	}
    }
    set custom 1
    Install $env(NPX_PLUGIN_PATH) $env(TCL_PLUGIN_DIR)
}
proc Install {plugdir libdir} {
    global tclversion tkversion
    global logFileName

    .msg config -text  "Copying Tcl Plugin ..." ; update
    logMsg "Copying libtclplugin.so to: $plugdir"
    if [catch {exec cp -p libtclplugin.so $plugdir} err] {
	.msg config -text $err
	logMsg "Copy of libtclplugin.so to $plugdir failed, aborting"
	return
    }
    .msg config -text "Copying Script Libraries ..." ; update
    logMsg "Creating directory $libdir/html if it doesn't exist yet"
    catch {exec mkdir $libdir/html}
    catch {exec chmod 777 $libdir/html}
    catch {exec rm -f $libdir/html/.licseen.txt}    
    logMsg "Making content of $libdir/html writable"
    catch {eval {exec chmod 644} [glob -nocomplain $libdir/html/*.html]}
    if [catch {exec cp -p -r html $libdir} err] {
	logMsg "Could not copy contents of [pwd]/html to $libdir,\
		aborting"
	.msg config -text $err
	return
    } else {
	logMsg "Copied content of [pwd]/html to $libdir/html"
    }
    logMsg "Creating directory $libdir/$tclversion if it doesn't exist yet"
    catch {exec mkdir $libdir/$tclversion}
    logMsg "Making content of $libdir/$tclversion writable"
    catch {eval {exec chmod 644} [glob -nocomplain $libdir/$tclversion/*.tcl] \
		$libdir/$tclversion/tclIndex}
    if [catch {exec cp -p -r $tclversion $libdir} err] {
	logMsg "Could not copy contents of [pwd]/$tclversion to $libdir,\
		aborting"
	.msg config -text $err
	return
    } else {
	logMsg "Copied content of [pwd]/$tclversion to $libdir/$tclversion"
    }
    logMsg "Making content of $libdir/$tclversion non-writable"
    catch {eval {exec chmod 444} [glob -nocomplain $libdir/$tclversion/*.tcl] \
		$libdir/$tclversion/tclIndex}
    logMsg "Creating directory $libdir/$tkversion if it doesn't exist yet"
    catch {exec mkdir $libdir/$tkversion}
    logMsg "Making content of $libdir/$tkversion writable"
    catch {eval {exec chmod 644} [glob -nocomplain $libdir/$tkversion/*.tcl] \
		[glob -nocomplain $libdir/$tkversion/*.gif] \
		[glob -nocomplain $libdir/$tkversion/*.xbm] \
		[glob -nocomplain $libdir/$tkversion/*.ps] \
		$libdir/$tkversion/tclIndex}
    if [catch {exec cp -p -r $tkversion $libdir} err] {
	logMsg "Could not copy contents of [pwd]/$tkversion to $libdir,\
		aborting"
	.msg config -text $err
	return
    } else {
	logMsg "Copied content of [pwd]/$tkversion to $libdir/$tkversion"
    }
    logMsg "Making content of $libdir/$tkversion non-writable"
    catch {eval {exec chmod 444} [glob -nocomplain $libdir/$tkversion/*.tcl] \
		[glob -nocomplain $libdir/$tkversion/*.gif] \
		[glob -nocomplain $libdir/$tkversion/*.xbm] \
		[glob -nocomplain $libdir/$tkversion/*.ps] \
		$libdir/$tkversion/tclIndex}
    logMsg "\nInstall completed at [clock format [clock seconds] -gmt no]"
    pack forget .ok .back
    global custom exitcode
    set exitcode 0
    if $custom {
	global env
	.msg config -text \
"Done!

      Comments, bug reports and questions -->

      email to jyl@eng.sun.com
      Please include log file: [pwd]/$logFileName

      Visit the demos at http://www.sunlabs.com/tcl/plugin/applets.html
      or test the plugin now by hitting the \"Test It!\" button.

Be sure to update your environment:"
} else {
	.msg config -text "Done!

      Comments, bug reports and questions -->

      email to jyl@eng.sun.com
      Please include log file: [pwd]/$logFileName

      Visit the demos at http://www.sunlabs.com/tcl/plugin/applets.html
      or test the plugin now by hitting the \"Test It!\" button."
    }
    pack .trouble .testb .log -in .buttons -side right
}

proc Help {} {
    global tclversion tkversion
    catch {destroy .help}
    toplevel .help
    wm title .help "Help for Tcl Plugin Install"
    text .help.t -height 16
    .help.t insert insert \
"There are two parts to the Tcl Plugin:

1) libtclplugin.so is the shared library that is linked into Navigator.
This file must be installed in a directory where Navigator expects to
find plugins. The default location is ~/.netscape/plugins. The Easy Install
button will copy it there, and the Custom Install button lets you specify
a different place. If you decide to put it somewhere else, you must
set the NPX_PLUGIN_PATH environment variable to match this location.

2) The $tclversion and $tkversion directories contain Tcl script libraries
for use by the Tcl plugin. The default location for these is ~/.tclplug.
The Easy Install button will install the libraries in ~/.tclplug, and the
Custom Install button allows you to specify a different location. If you
decide to install these in a different location, you must set the
environment variable TCL_PLUGIN_DIR to match that location.

"
    pack .help.t

    button .help.quit -text Dismiss -command {destroy .help}
    pack .help.quit
}
proc Readme {} {
    catch {destroy .readme}
    toplevel .readme
    frame .readme.top
    wm title .readme "README for Tcl Plugin"
    text .readme.t -yscrollcommand {.readme.s set}
    scrollbar .readme.s -command {.readme.t yview} -orient vertical
    pack .readme.s -side right -fill y -in .readme.top
    pack .readme.t -side left -fill both -expand true -in .readme.top
    pack .readme.top
    if [catch {open README} in] {
	.readme.t insert insert $in
    } else {
	.readme.t insert insert [read $in]
	close $in
    }
    button .readme.quit -text Dismiss -command {destroy .readme}
    pack .readme.quit
}
proc ViewLicense {} {
    catch {destroy .license}
    toplevel .license
    frame .license.top
    wm title .license "License for Tcl Plugin"
    text .license.t -yscrollcommand {.license.s set}
    scrollbar .license.s -command {.license.t yview} -orient vertical
    pack .license.s -side right -fill y -in .license.top
    pack .license.t -side left -fill both -expand true -in .license.top
    pack .license.top
    if [catch {open license.terms} in] {
	.license.t insert insert $in
    } else {
	.license.t insert insert [read $in]
	close $in
    }
    button .license.quit -text Dismiss -command {destroy .license}
    pack .license.quit
}
proc FAQ {} {
    catch {destroy .faqview}
    toplevel .faqview
    frame .faqview.top
    wm title .faqview "Frequently Asked Questions for Tcl Plugin"
    text .faqview.t -yscrollcommand {.faqview.s set}
    scrollbar .faqview.s -command {.faqview.t yview} -orient vertical
    pack .faqview.s -side right -fill y -in .faqview.top
    pack .faqview.t -side left -fill both -expand true -in .faqview.top
    pack .faqview.top
    if [catch {open FAQ} in] {
	.faqview.t insert insert $in
    } else {
	.faqview.t insert insert [read $in]
	close $in
    }
    button .faqview.quit -text Dismiss -command {destroy .faqview}
    pack .faqview.quit
}

proc viewLog {} {
    global logFileName logfile
    catch {close $logfile}
    catch {destroy .logview}
    toplevel .logview
    frame .logview.top
    wm title .logview "Log of Installation"
    text .logview.t -yscrollcommand {.logview.s set}
    scrollbar .logview.s -command {.logview.t yview} -orient vertical
    pack .logview.s -side right -fill y -in .logview.top
    pack .logview.t -side left -fill both -expand true -in .logview.top
    pack .logview.top
    if [catch {open $logFileName} in] {
	.logview.t insert insert $in
    } else {
	.logview.t insert insert [read $in]
	close $in
    }
    button .logview.quit -text Dismiss -command {destroy .logview}
    pack .logview.quit
}

proc troubleReport {{err {}}} {
    global logFileName logfile
    catch {close $logfile}
    catch {destroy .troubleview}
    toplevel .troubleview
    wm title .troubleview "Submit a trouble report"
    message .troubleview.m -aspect 2000 -text \
"The window below contains the log of your attempt to install the Tcl 
Plugin. Please type any additional information you wish to include in
the report.

When you hit the \"Send Report\" button, the report will be sent by
email to jyl@eng.sun.com."
    pack .troubleview.m -fill x
    frame .troubleview.top
    text .troubleview.t -yscrollcommand {.troubleview.s set}
    scrollbar .troubleview.s -command {.troubleview.t yview} -orient vertical
    pack .troubleview.s -side right -fill y -in .troubleview.top
    pack .troubleview.t -side left -fill both -expand true -in .troubleview.top
    pack .troubleview.top
    .troubleview.t insert insert "Type your comments here: \n\n"
    if [catch {open $logFileName} in] {
	.troubleview.t insert insert $in
    } else {
	.troubleview.t insert insert [read $in]
	close $in
    }
    if {"$err" != ""} {
	.troubleview.t insert insert "\n\n$err"
    }
    .troubleview.t mark set insert 1.end
    .troubleview.t tag add sel 1.0 1.end
    focus .troubleview.t
    frame .troubleview.buttons -bd 10
    button .troubleview.quit -text Dismiss -command {destroy .troubleview}
    button .troubleview.submit -text "Send Report" -command {
	sendTroubleReport
	destroy .troubleview
    }
    frame .troubleview.email
    label .troubleview.email.l -text "Your email address (for replies):"
    entry .troubleview.email.e -textvariable email
    pack .troubleview.email.l .troubleview.email.e -side left
    pack .troubleview.buttons -side bottom
    pack .troubleview.email -side bottom
    pack .troubleview.quit .troubleview.submit -in .troubleview.buttons \
	-side left
}

proc sendTroubleReport {} {
    global email

    set text [.troubleview.t get 1.0 end]
    set msg "To: jyl@eng.sun.com
From: $email
Subject: trouble report

System info: [exec uname -a]
Trouble report:
$text
"
    exec /usr/lib/sendmail jyl@eng.sun.com << $msg
}

proc testIt {} {
    global netscape

    catch {destroy .test}
    toplevel .test
    message .test.m -aspect 2000 -text \
"Please type in the command to invoke Netscape Navigator. I've initialized
the entry box below to a command that will visit our page of example tclets.
When you press \"Launch\", I will execute the command in the entry box. It
might take several seconds for Netscape Navigator to start."
    pack .test.m -side top
    frame .test.top
    label .test.l -text "Command to invoke Netscape Navigator: "
    entry .test.e -textvariable netscape -width 60
    pack .test.l .test.e -side left -in .test.top
    pack .test.top -side top
    frame .test.buttons
    button .test.launch -text "Launch" -command {
	catch {destroy .test}
	after 10 {
	    set err [invokeIt]
	    if {$err == ""} {
		askIfWorked
	    } else {
		askIfTrouble $err
	    }
	}
    }
    button .test.dismiss -text "Dismiss" -command {destroy .test}
    pack .test.launch .test.dismiss -side left -in .test.buttons
    pack .test.buttons -side bottom
    bind .test.e <Return> {
	catch {destroy .test}
	after 10 {
	    set err [invokeIt]
	    if {$err == ""} {
		askIfWorked
	    } else {
		askIfTrouble $err
	    }
	}
    }
    focus .test.e
}

proc invokeIt {} {
    global netscape

    catch {eval exec $netscape} msg
    return $msg
}

proc askIfWorked {} {
    catch {destroy .ask}
    toplevel .ask
    message .ask.m -aspect 2000 -text \
"If the Tcl Plugin performed correctly, you may hit the \"Dismiss\"
button now. Otherwise, you may wish to submit a trouble report, by
using the \"Submit Trouble Report\" button below. Please include a
detailed description of what went wrong."
    frame .ask.buttons
    button .ask.buttons.dismiss -text Dismiss -command {destroy .ask}
    button .ask.buttons.submit -text "Submit Trouble Report" \
	-command {destroy .ask; troubleReport {}}
    pack .ask.buttons.submit .ask.buttons.dismiss -side left
    pack .ask.m -side top
    pack .ask.buttons -side bottom
}

proc askIfTrouble {err} {
    global netscape
    catch {destroy .ask}
    toplevel .ask
    message .ask.m -aspect 2000 -text \
"I got this error from trying to execute \"$netscape\":

	$err

It seemes something went wrong. Please submit a trouble report,
using the \"Submit Trouble Report\" button below. Please include a
detailed description of what went wrong."
    frame .ask.buttons
    button .ask.buttons.dismiss -text Dismiss -command {destroy .ask}
    button .ask.buttons.submit -text "Submit Trouble Report" \
	-command [list doTroubleReport $err]
    pack .ask.buttons.submit .ask.buttons.dismiss -side left
    pack .ask.m -side top
    pack .ask.buttons -side bottom
}

proc doTroubleReport {err} {
    destroy .ask
    troubleReport $err
}

proc logMsg {msg} {
    global logfile
    if {"$logfile" != ""} {
	puts $logfile $msg
    }
}

FirstScreen
