#!/usr/local/bin/wish8.4
# JStrack:  copyright 1997/2008 by Jim Graham, N5IAL, all rights reserved.

set unix 0
if {[string compare $tcl_platform(platform) "unix"] == 0} { set unix 1 }
set s "JStrack (mostly) Automatic Configuration"
if {[catch {pack [label .l -text $s]}]} {
   puts $s
   set mputs puts
   set notk 1
} else {

   set ans [tk_messageBox -type yesno -message \
      "Are you using the EXE version here?"]
   switch -- $ans {
      yes {
         tk_messageBox -type ok -message \
            "Do not use autoconfig, then...paths.tcl will take care of itself."
         exit
         }
      no {}
   }

   wm protocol . WM_DELETE_WINDOW { exit }
   set mputs tk_puts
   set notk 0
   text .text -relief sunken -bd 2 -width 80 -height 30 \
        -yscrollcommand ".scroll set" -font {Courier 12}
   scrollbar .scroll -command ".text yview"
   pack .scroll -side right -fill y
   pack .text -side top -expand yes -fill both
}


proc unix_cmd {fn binpath} {
   global unix mputs
   if {$unix == 0} { return } ;# Only run this proc on Unix

   if {[string compare $binpath ""]} {
      set f [open $fn r] ; set fstr [read $f] ; close $f
      set f [open $fn w]
      puts $f "#!$binpath" ; puts $f $fstr
      close $f
   }
   if {[catch { eval exec "chmod 755 $fn" }]} {
      $mputs "WARNING:  Could not change permissions on"
      $mputs "          $fn"
      $mputs "          You need to run:"
      $mputs "             \"chmod 755 $fn\""
   }
}


proc tk_puts {args} {
   global mputs

   if {$mputs == "tk_puts"} {
      set w .text

      if {[llength $args] == 1} {
         $w insert end "[lindex $args 0]\n"
      } elseif {[llength $args] == 2 && [lindex $args 0] == "-nonewline"} {
         $w insert end [lindex $args 1]
      }
      $w see end
   }
   update
#  after 750 ;
}

proc tcl_puts {args} {
   if {[llength $args] == 1} {
      puts [lindex $args 0]
   } elseif {[llength $args] == 2 && [lindex $args 0] == "-nonewline"} {
      puts -nonewline [lindex $args 0]
   }
   after 750 ;
}

$mputs "Checking out your Tcl/Tk installation...."

set tclv [info tclversion]
if {$tclv == 8.2} {
   $mputs {
You have Tcl/Tk version 8.2....  This is OK for now, but you should
really consider upgrading to 8.3.x or 8.4.x!
}
} elseif {$tclv == 8.3 || $tclv == 8.4} {
   $mputs "
You have Tcl/Tk version $tclv...Ok.
"
} elseif {$tclv == 8.0 || $tclv == 8.1} {
   $mputs {
Be advised that you have a very old version of Tcl/Tk, and some features
in JStrack (e.g., updates via FTP) WILL NOT WORK.  Consider upgrading to
Tcl/Tk 8.3.x or 8.4.x.
}
} elseif {$tclv < 8} {
   $mputs {
Your version of Tcl/Tk is way too ancient.  It MIGHT work with JStrack,
but I won't make any promises.  Some features (which require at least
Tcl/Tk 8.2) most definitely will not work.  These include updates via
FTP, and probably all of the new features like satellite imagery, etc.
}
} else {
   $mputs {
WARNING:  Tcl/Tk versions 8.5 and up) may not be supported by this
version of JStrack.  Some language changes broke the code at 8.5, and
I haven't tested any versions above 8.4.

}
}

if {[file exists paths.tcl]} {
   catch { file rename paths.tcl paths.tcl.old }
}
set f [open paths.tcl w]

puts $f {# This file is used for defining paths common to both the tracker and}
puts $f {# the various filter scripts (and a few others).}
puts $f {#}
puts $f {# Lines such as this one, which start with '#', are comments.}
puts $f {# Strings within this file must be quoted if they contain spaces. Strings}
puts $f "# in tcl can be quoted either with the usual double-quotes or with '{'"
puts $f "# and '}'.  Note that these are handled VERY differently...."
puts $f {#}
puts $f {# If you have a quoted string such as "This is a very $mood day" ... the}
puts $f {# contents of the variable, $mood, will be substituted when the string is}
puts $f {# read.  Likewise, if you have a command called "get_mood", another}
puts $f {# version of the previous string might be "This is a very [get_mood] day."}
puts $f {#}
puts $f {# The '$' and '[' characters are special.  If you need them in a string,}
puts $f {# escape them with a backslash, e.g., "This software costs \$0.00."}
puts $f {# Fortunately, you're not likely to need these for anything found in}
puts $f {# this file!}
puts $f {#}
puts $f {# Strings quoted with '{' and '}' are handled very differently.}
puts $f {# Basically, variables, etc., within the string are not evaluated}
puts $f {# immediately when the string is read.  Use this type of quoting only}
puts $f {# if you're familiar with Tcl.}
puts $f {}

# Make sure we know where tclsh is...and while we're at it, fix the
# path to use the full name instead of the dos 8.3 name (if needed).

if {[regexp -nocase tclsh [info nameofexecutable]]} {
   set shell Tcl
   set tclshpath [info nameofexecutable]
   # Works *IF* Tcl's filenames haven't been messed with
   regsub -nocase tclsh [info nameofexecutable] wish wishpath
} else {
   set shell Tcl/Tk
   set wishpath [info nameofexecutable]
   # Works *IF* Tcl's filenames haven't been messed with
   regsub -nocase wish [info nameofexecutable] tclsh tclshpath
}

if {$tcl_platform(platform) == "windows"} {
   regsub "C:/PROGRA~1/" $tclshpath "C:/Program Files/" tclshpath2
   set tclshpath $tclshpath2
   regsub "C:/PROGRA~1/" $wishpath "C:/Program Files/" wishpath2
   set wishpath $wishpath2
}

puts $f {# The location for tclsh8x(.exe) and wish8x(.exe)}

set fn [file rootname [file rootname $tclshpath]]
if {[regexp tclsh $fn] && [file exists $tclshpath]} {
   puts $f "set tclshbin \"$tclshpath\""
   $mputs "Found tclsh in [file dirname $tclshpath] ... GOOD"
   set tclsh_ok 1
} else {
   puts $f {# WARNING:  path to tclsh could not be determined...}
   puts $f "# set tclshbin \"$tclshpath\""
   $mputs "WARNING:  Could not find tclsh!  You will need to edit paths.tcl yourself...."
   set tclsh_ok 0
}
set fn [file rootname [file rootname $wishpath]]
if {[regexp wish $fn] && [file exists $wishpath]} {
   puts $f "set wishbin \"$wishpath\""
   $mputs "Found wish in [file dirname $wishpath] ... GOOD"
   set wish_ok 1
} else {
   puts $f {# WARNING:  path to wish could not be determined...}
   puts $f "# set wishbin \"$wishpath\""
   $mputs "WARNING:  Could not find wish!  You will need to edit paths.tcl yourself...."
   set wish_ok 0
}

$mputs "Looking for specific bits and pieces of JStrack"

puts $f {}

puts $f {# Change the directory below to refer to the location where these}
puts $f {# scripts are being installed.  If you are always running this from}
puts $f {# the current directory (i.e., you're running it from the directory}
puts $f {# where the scripts are installed), you're done.}
puts $f {}
puts $f {# My systems}
puts $f {#    set install_dir d:/jdg/src/ht     ;# work, home w/ XP machine}
puts $f {#    set install_dir /home/jim/src/ht  ;# home w/ FreeBSD and Linux machines}
puts $f {#}
puts $f {# Typical location (multi-user use) for Unix systems}
puts $f {#    set install_dir /usr/local/lib/jstrack}
puts $f {# Typical location (multi-user use) for WinXX systems}
puts $f {#    set install_dir c:/jstrack}

# Try to find out where the filter is located....

set dirtest [file dirname $argv0]
if {[string compare $dirtest "."] == 0} { set dirtest [pwd] }

if {[file exists $dirtest/filt] && [file exists $dirtest/tracker]} {
   puts $f {# install_dir value determined by autoconfig.tcl:}
   puts $f "set install_dir \"$dirtest\""
   $mputs "Found install dir:  $dirtest"
   set install_dir $dirtest
}

puts $f {# Directory for storm data (must be an absolute path, e.g.,}
puts $f {# /usr/local/lib/jstrack or $install_dir/tracker/storms).}
puts $f {#}
puts $f {# NOTE:  For filt.tcl, you can override this variable on the command}
puts $f {# line with}
puts $f {#    filt.tcl -stormdir /foo/bar [filename]}
puts $f {# where filename is optional.}
puts $f {}
# Eventually want to be smarter...don't assume the default.  But for now....
puts $f {# FYI/WARNING:  autoconfig is assuming the default location for storm data!}
puts $f {set storms $install_dir/tracker/storms}

$mputs {
=============================================================================
FYI/WARNING:  JStrack's autoconfig assumes that you're putting storm data}
$mputs "in the <install_dir>/tracker/storms directory ($dirtest/tracker/storms)."
$mputs {
If this is not the case, you will need to fix the definition yourself.
=============================================================================
}

puts $f {}
puts $f {# ####################################################################}
puts $f {# Assuming you haven't changed the directory layout of the JStrack   #}
puts $f {# distribution, you're done.  If you've changed the layout, you need #}
puts $f {# to modify the following to match what you've done....  Note that,  #}
puts $f {# depending on what you've changed, you may also need to modify any  #}
puts $f {# files that source this file!                                       #}
puts $f {# ####################################################################}


   puts $f {set trackdir $install_dir/tracker}
   puts $f {set libdir $trackdir/lib}
   puts $f {set imgdir $trackdir/lib/images}
   puts $f {set rfcdir $install_dir/filt}
   puts $f {set filt_path $install_dir/filt/filt.tcl}
   puts $f {set ftpdir $install_dir/ftpfilt}
   puts $f {set ftpfilt $install_dir/ftpfilt/ftp.tcl}
   puts $f {set httpfilt_path  $install_dir/httpfilt}

puts $f ""
close $f


$mputs "Done!"
$mputs ""
$mputs {If any of the paths above didn't look right, or if you know}
$mputs {that there's a problem, edit paths.tcl with any text editor}
$mputs {and make any necessary corrections.  Remember, strings with}
$mputs {spaces must be in quotes and comments begin with '#' as the}
$mputs {first character.  Refer to the examples in paths.tcl.}

if {$notk == 0} {
   pack [button .done -text "Dismiss" -command { exit }] -side top
}


