#
#
# Installation script for Xgnuplot
#
#

#
# define default values
#

set path gnuplot ; set src [pwd] ; set wish /usr/local/bin/wish
if [file exists install.in] then {
  source install.in
}
#
# define default fonts and renderings for the install
#
set f1 -*-times-bold-i-*-*-18-*
set f2 -*-times-bold-i-*-*-14-*
set fg1 blue

#
# create the window
#
label .t1 -text "Xgnuplot Install" -relief flat -borderwidth 2
label .t2 -text "Author P. Alexander     pa@mrao.cam.ac.uk" \
  -relief flat -borderwidth 2
  catch {.t1 configure -font $f1 -foreground $fg1}
  catch {.t2 configure -font $f2 -foreground $fg1}

frame .d -relief groove -borderwidth 2
  pack [frame .d.wish -relief flat] \
        -side top -padx 10 -pady 10 -fill x
   pack [label .d.wish.l -text "Path to wish executable:" -width 30 \
         -anchor w -relief flat] \
         -side left -anchor w
   pack [entry .d.wish.e -relief sunken -borderwidth 2 -width 30 \
         -textvariable wish] \
         -side left -anchor w
  pack [frame .d.src -relief flat] \
        -side top -padx 10 -pady 10 -fill x
   pack [label .d.src.l -text "Installation directory:" -width 30 \
         -anchor w -relief flat] \
         -side left -anchor w
   pack [entry .d.src.e -relief sunken -borderwidth 2 -width 30 \
         -textvariable src] \
         -side left -anchor w
  pack [frame .d.path -relief flat] \
        -side top -padx 10 -pady 10 -fill x
   pack [label .d.path.l -text "Command to launch gnuplot:" -width 30 \
         -anchor w -relief flat] \
         -side left -anchor w
   pack [entry .d.path.e -relief sunken -borderwidth 2 -width 30 \
         -textvariable path] \
         -side left -anchor w

frame .c -relief groove -borderwidth 2
  pack [button .c.dismiss -text Dismiss -width 10 -command exit] \
        -side left -anchor c -expand 1 -pady 10
  pack [button .c.install -text Install -width 10 -command xgpl_install] \
        -side left -anchor c -expand 1 -pady 10
  pack [button .c.help -text Help -width 10 -command xgpl_help] \
        -side left -anchor c -expand 1 -pady 10

# build window
  pack .t1 -side top -fill x -padx 10 -anchor c -expand 1 -pady 15
  pack .t2 -side top -fill x -padx 10 -anchor c -expand 1 -pady 10
  pack .d -side top -fill x -padx 10 -anchor c -expand 1 -pady 10
  pack .c -side top -fill x -padx 10 -anchor c -expand 1 -pady 10

# setup main window
  wm title . "Xgnuplot Install"
  wm iconname . "Xgnuplot Install"


#
# Procedure to perform the install
#
proc xgpl_install { } {
  global path src wish
  cd $src
  exec make
  set fid [open $src/Xgnuplot.in r]
  set text [read $fid]
  close $fid
  regsub -all {%WISH%} $text $wish text1
  regsub -all {%PATH%} $text1 $path text
  regsub -all {%SRC%} $text $src text1
  set fid [open $src/Xgnuplot w]
  puts -nonewline $fid $text1
  close $fid
  set fid [open install.in w]
  puts $fid "set src $src"
  puts $fid "set path $path"
  puts $fid "set wish $wish"
  close $fid
  exec chmod a+x $src/Xgnuplot
}

#
# Provide a "Help" window to display the README file
#
proc xgpl_help { } {
  set w .help
  catch {destroy $w} ; toplevel $w
  wm title $w "Xgnuplot Install Help"
  wm iconname $w "Xgnuplot Install Help"
  frame $w.t -relief groove -borderwidth 2
  pack [scrollbar $w.t.scroll -width 3m -command "$w.t.text yview" \
        -relief sunken -borderwidth 2] -side right -fill y
  pack [text $w.t.text -relief flat -width 70\
        -setgrid 1 -yscroll "$w.t.scroll set"] \
        -side top -fill both -expand 1 -padx 5 -pady 5
  pack $w.t -side top -padx 10 -pady 10 -expand 1 -fill both
  frame $w.c -relief groove -borderwidth 2
  pack [button $w.c.dismiss -text Dismiss -width 10 -command "destroy $w"] \
        -side left -anchor c -expand 1 -pady 10
  pack $w.c -side top -fill x -padx 10 -pady 10
  if [file exists ./README] then {
    set fid [open ./README r]
    $w.t.text insert 0.0 [read $fid]
    close $fid
  }
  $w.t.text configure -state disabled
}
