# An experimental installation program for Postilion
# May 2, 1999 Nic Bernstein

#  Postilion software and its included text and images
#  Copyright (C) 1998, 1999, 2000 Nic Bernstein
#
#  The full text of the legal notices is contained in the file called
#  COPYING, included with this distribution.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#!/bin/sh
#The next line restarts using wish \
exec wish "$0" "$@"

set ver [split $tk_patchLevel .]
set major [lindex $ver 0]
set minor [lindex $ver 1]
set patch [lindex $ver 2]

if {8 != $major} {
    puts "You have version $major.$minor, patch level $patch of Tk installed"
    puts "You must have Tk version 8.0, patch level 2 or higher installed"
    puts "prior to installing Postilion"
    puts "If you have a suitable version of \"wish\" installed"
    puts "please restart this program as \"/path/to/wish setup\""
    exit
}

if {0 != $minor} {
    puts "You have version $major.$minor, patch level $patch of Tk installed"
    puts "You must have Tk version 8.0, patch level 2 or higher installed"
    puts "Postilion may not function with version 8.1 or 8.2 of Tk"
    puts "If you have a suitable version of \"wish\" installed"
    puts "please restart this program as \"/path/to/wish setup\""
    exit
}

if {2 > $patch} {
    puts "You have version $major.$minor, patch level $patch of Tk installed"
    puts "You must have Tk version 8.0, patch level 2 or higher installed"
    puts "prior to installing Postilion"
    puts "If you have a suitable version of \"wish\" installed"
    puts "please restart this program as \"/path/to/wish setup\""
    exit
}

##
## ChangePrefix
## 
## This procedure is triggered whenever the prefix variable is written
## and recalculates the exec, bin and lib variables
##
## Arguments:
## name1 name2 op - standard tcl trace arguments, unused here

proc ChangePrefix {} {
    global prefix exec

    set exec $prefix
    ChangeExec
}

proc ChangeExec {} {
    global exec bindir libdir

    set bindir $exec/bin
    set libdir $exec/lib
}

##
## Let's get started now
## Here we will get some configuration information from the user
##
proc Screen1 {} {
    global prefix exec bindir libdir tclconfig tkconfig handler


    $handler.text configure -state normal -font {Times 14}
    $handler.text delete 0.0 end
    $handler.text insert 0.0 \
	"This program will prepare to install 
Postilion on your computer.  Please 
confirm the settings to the right.

If you are not sure of these settings,
please consult your system 
administrator before continuing
(press \"Cancel\" now)."
    $handler.text configure -state disabled

    set w [frame $handler.settings]
    foreach record {{prefix "Installation Directory"} \
			{exec "Exec prefix"} \
			{bindir "Bin Directory"} {libdir "Lib Directory"} \
			{tclconfig "TCL Config location"} \
			{tkconfig "TK Config location"}} {
	set field [lindex $record 0]
	set text [lindex $record 1]
	frame $w.$field
	label $w.$field.label -text $text
	entry $w.$field.entry -relief sunken -textvariable $field -width 40
	pack $w.$field.label $w.$field.entry -side left -padx 5
	pack $w.$field -side top -pady 5
    }
    bind $w.prefix.entry <FocusOut> ChangePrefix
    bind $w.exec.entry <FocusIn> ChangePrefix
    bind $w.exec.entry <FocusOut> ChangeExec

    focus $w.prefix.entry
    place $w -in $handler -x 280 -y 70 -width 280 -height 200

    $handler.buttons.cancel configure -command "exit 0" -state normal
    $handler.buttons.prev configure -state disabled -command ""
    $handler.buttons.next configure -command "destroy $w" -state normal
    $handler.buttons.finish configure -command "" -state disabled
    bind $handler <Key-Return> {break}
    bind $handler <Key-Escape> "exit 0"    

    tkwait window $w
    Screen2
}

proc Screen2 {} {
    global prefix exec bindir libdir tclconfig tkconfig handler
    
    $handler.text configure -state normal
    $handler.text delete 0.0 end
    $handler.text insert 0.0 \
	"Attempting to configure Postilion.

This step will learn as much as 
possible about your system config-
uration, and create the necessary
files to build Postilion on your
system.

Please wait, output will be shown
to the right"
    $handler.text configure -state disabled
    update idletasks
    

    set w [text $handler.config -relief sunken -bd 2]
    place $w -in $handler -x 280 -y 70 -width 280 -height 260

    $handler.buttons.cancel configure -command "exit 0" -state normal
    $handler.buttons.prev configure -state normal -command "destroy $w; Screen1"
    $handler.buttons.next configure -command "destroy $w" -state normal \
         -default active
    $handler.buttons.finish configure -command "" -state disabled
    bind $handler <Key-Return> "destroy $w"
    bind $handler <Key-Escape> "exit 0"    

    $w configure -state normal -font {courier 12}
#    set result [exec ./configure --quiet --prefix=$prefix \
#		    --exec-prefix=$exec --bindir=$bindir \
#		    --libdir=$libdir --with-tcl-config=$tclconfig \
#		    --with-tk-config=$tkconfig]
    if [catch {open "|./configure --quiet --prefix=$prefix \
		    --exec-prefix=$exec --bindir=$bindir \
		    --libdir=$libdir --with-tcl-config=$tclconfig \
		    --with-tk-config=$tkconfig" r+} config_buffer] {
	return
    } 
    while { -1 != [gets $config_buffer line]} {
	$w insert end $line
	$w insert end \n
	update idletasks
    }
    $w tag configure done -foreground red
    $w insert end "Configuration done."
    $w tag add done "end - 1 line" end
    close $config_buffer
    $w configure -state disabled

    tkwait window $w
    Screen3
}

proc Screen3 {} {
    global prefix exec bindir libdir tclconfig tkconfig handler env
    
    $handler.text configure -state normal
    $handler.text delete 0.0 end
    $handler.text insert 0.0 \
	"Building Postilion

This will take a while...
    ...please wait, output 
will be shown to the right"
    $handler.text configure -state disabled
    update idletasks
    
    set w [text $handler.config -relief sunken -bd 2]
    place $w -in $handler -x 280 -y 70 -width 280 -height 260

    $handler.buttons.cancel configure -command "exit 0" -state normal
    $handler.buttons.prev configure -state normal -command "destroy $w; Screen2"
    $handler.buttons.next configure -command "destroy $w" -state normal \
         -default active
    $handler.buttons.finish configure -command "" -state disabled
    bind $handler <Key-Return> "destroy $w"
    bind $handler <Key-Escape> "exit 0"    

    $w configure -state normal -font {courier 12}
    # Run make but suppress messages, compiler warnings and language warnings
    if [catch {open "|make -s EXTRA_CFLAGS=-w DOTEXT=1" r+} make_buffer] {
	return
    } 
    while { -1 != [gets $make_buffer line]} {
	$w insert end $line
	$w insert end \n
	update idletasks
    }
    $w tag configure done -foreground red
    $w insert end "Build complete!"
    $w tag add done "end - 1 line" end
    close $make_buffer
    $w configure -state disabled

    tkwait window $w
    IsRoot
}

proc IsRoot {} {
    global bindir libdir

    if {[file writable $bindir] && [file writable $libdir]} {
	Screen5
    } else {
	Screen4
    }
}

proc Screen4 {} {
    global prefix exec bindir libdir tclconfig tkconfig handler
    
    set pwd [exec pwd]
    $handler.text configure -state normal
    $handler.text delete 0.0 end
    $handler.text insert 0.0 \
	"You do not have sufficient
rights either to $bindir
$libdir, or both
to install Postilion.  
Please consult your system 
administrator and ask them 
to install Postilion by typing:
  cd $pwd
  \"make install\"
"
    $handler.text configure -state disabled
    update idletasks
    
    set w [text $handler.config -relief sunken -bd 2]
    place $w -in $handler -x 280 -y 70 -width 280 -height 260

    $handler.buttons.cancel configure -command "exit 0" -state normal
    $handler.buttons.prev configure -state normal -command "destroy $w; Screen3"
    $handler.buttons.next configure -command "" -state disabled
    $handler.buttons.finish configure -command "destroy $w" -state normal \
	-default active
    bind $handler <Key-Return> "destroy $w"
    bind $handler <Key-Escape> "exit 0"    

    tkwait window $w
    exit 0
}

proc Screen5 {} {
    global prefix exec bindir libdir tclconfig tkconfig handler
    
    set pwd [exec pwd]
    $handler.text configure -state normal
    $handler.text delete 0.0 end
    $handler.text insert 0.0 \
	"Installing Postilion

This will take a while...
    ...please wait, results
will be shown to the right"
    $handler.text configure -state disabled
    update idletasks
    
    set w [text $handler.config -relief sunken -bd 2]
    place $w -in $handler -x 280 -y 70 -width 280 -height 260

    $handler.buttons.cancel configure -command "exit 0" -state normal
    $handler.buttons.prev configure -state normal -command "destroy $w; Screen3"
    $handler.buttons.next configure -command "" -state disabled \
	-default disabled
    $handler.buttons.finish configure -command "destroy $w" -state normal \
	-default active
    bind $handler <Key-Return> "destroy $w"
    bind $handler <Key-Escape> "exit 0"    

    $w configure -state normal -font {courier 12}
    if [catch {open "|make -s install -r" r+} make_buffer] {
	return
    } 
    while { -1 != [gets $make_buffer line]} {
	$w insert end $line
	$w insert end \n
	update idletasks
    }
    $w tag configure done -foreground red
    $w insert end "Install complete!"
    $w tag add done "end - 1 line" end
    close $make_buffer
    $w configure -state disabled

    tkwait window $w
    exit 0
}

wm withdraw .
set handler .new
set prefix "/usr/local"
set exec $prefix
set bindir $exec/bin
set libdir $exec/lib
set tclconfig $tcl_library
set tkconfig $tk_library


# Create the window which will house the setup and install process
toplevel $handler -class Postilion -height 400 -width 600 \
	-relief raised
wm resizable $handler 0 0
wm transient $handler .
wm title $handler "Postilion Setup"

# Create the message display area
label $handler.label -relief flat -bd 0 -text "Postilion" \
	-font {Helvetica 24 bold italic} -fg darkblue
text $handler.text -relief sunken -bd 2

# Create the buttons which will control operation
frame $handler.buttons
button $handler.buttons.cancel -text Cancel -state normal \
	-relief raised -width 9 -height 2 -anchor center\
	-highlightthickness 0
button $handler.buttons.prev -text Previous -state disabled \
	-relief raised -width 9 -height 2 -anchor center\
	-highlightthickness 0
button $handler.buttons.next -text Next -state disabled \
	-relief raised -width 9 -height 2  -anchor center \
	-highlightthickness 0
button $handler.buttons.finish -text Finish -state disabled \
	-relief raised -width 9 -height 2 -anchor center \
	-highlightthickness 0

pack $handler.buttons.cancel $handler.buttons.prev \
	$handler.buttons.next $handler.buttons.finish \
	-padx 5 -side left -expand 0

# Place everything
place $handler.label -in $handler -x 20 -y 20 \
	-width 120 -height 40
place $handler.text -in $handler -x 20 -y 70 \
	-width 260 -height 260
place $handler.buttons -in $handler -x 140 -y 350 \
	-width 380 -height 30

# Call the first screen
Screen1
