
# JStrack:  copyright 1997/2010 by Jim Graham, N5IAL, all rights reserved.
#

proc default_changed {variable args} {
   upvar #0 $variable lvar
   global defaultvalue
   if {$lvar != $defaultvalue($variable)} { return 1 }
   foreach v $args {
      upvar #0 $v lvar
      if {$lvar != $defaultvalue($v)} { return 1 }
   }
   return 0
}


# Options menu:  "Save Config"
proc save_config {{global 0}} {
   global units storms auto_fc auto_radii auto_update c_h c_w env
   global install_dir jstrackrc trackdir auto_load_new defmap text_font
   global warn_at_nm update_prog pos_est_prog current_prog forecast_prog
   global ptrloc home_latt home_long

   if {$global} {
      set f [open $trackdir/options.tcl w]
   } else {
      set f [open $jstrackrc w]
   }
   puts $f "# WARNING:  DO NOT ADD ANYTHING TO THIS FILE THAT ISN'T ALREADY HERE.
# THIS FILE IS CLOBBERED WHEN YOU SAVE THE CONFIGURATION FROM JStrack.
# Put changes like that in sysconfig.tcl instead.
"
   puts $f "#---------------------"
   set thedate [clock format [clock seconds] -format "%d%h%y %H:%M:%S"]
   puts $f "# JStrack configuration (non-default values) saved on $thedate"
   puts $f ""
   puts $f "# NOTE:  The definition of jstrackrc is no longer set from"
   puts $f "# this file.  It's OS-specific, and should not really need"
   puts $f "# to be changed (not to mention the fact that setting it"
   puts $f "# from here breaks the OS-based if-then statements!)."
   puts $f "# Therefore, it is now ONLY set in defaults.tcl (and"
   puts $f "# sysconfig.tcl if it exists)."
   puts $f ""
   if {[default_changed defmap]} {
      puts $f {# There are several maps you can use.  In this section, you define which}
      puts $f {# map you will use by default.}
      puts $f {#}
      puts $f {# Select from one of these maps (you can change maps from within JStrack}
      puts $f {# and from the command line):}
      puts $f {#}
      puts $f {# NEW MAPS:}
      puts $f {# set defmap large_js          ;# Overall picture (recommended/default)}
      puts $f {# set defmap east_coast_js     ;# East coast (north of FL)}
      puts $f {# set defmap gulf_cab2_js      ;# Gulf coast + FL East coast}
      puts $f ""
      puts $f "set defmap $defmap"
      puts $f ""
   }
   if {[default_changed units]} {
      puts $f "# units:  set to either MPH for miles/MPH or KT for NM/KT"
      puts $f "set units $units"
      puts $f ""
   }
   if {[default_changed home_latt home_long]} {
      puts $f "# home_latt and home_long are your home coordinates (for the"
      puts $f "# big 'X' on the display).  Default:  my home coords."
      puts $f "set home_long $home_long"
      puts $f "set home_latt $home_latt"
      puts $f ""
   }
   if {[default_changed auto_fc auto_radii]} {
      puts $f "# set auto_fc to 1 (non-zero) if you want forecast positions"
      puts $f "# to be plotted automatically for the latest storm position."
      puts $f "# auto_radii works the same for wind radii (now plots current"
      puts $f "# and forecast wind radii)."
      puts $f "set auto_fc $auto_fc"
      puts $f "set auto_radii $auto_radii"
      puts $f ""
   }
   if {[default_changed auto_update]} {
      puts $f "# set auto_update to 1 if you want JStrack to watch for storm"
      puts $f "# data file updates in the background."
      puts $f "set auto_update $auto_update"
      puts $f ""
   }
   if {[default_changed auto_load_new]} {
      puts $f "# If auto_load_new is set and is a non-zero value, new storms will"
      puts $f "# be loaded automatically (if JStrack is running, of course)."
      puts $f "set auto_load_new $auto_load_new"
      puts $f ""
   }
   if {[winfo width .c] != $c_w || [winfo height .c] != $c_h} {
      puts $f "# c_h and c_w are the height and width of the canvas"
      puts $f "# ON YOUR SCREEN.  In other words, it's the size of the"
      puts $f "# window in which the full-sized canvas is displayed."
      puts $f "set c_w [expr {[winfo width .c] -4}]"
      puts $f "set c_h [expr {[winfo height .c] -4}]"
      puts $f ""
      puts $f "#"
      puts $f "# Old values:"
      puts $f "#    c_w:  $c_w"
      puts $f "#    c_h:  $c_h"
      puts $f ""
   }
   if {[default_changed text_font]} {
      puts $f "# text_font is the font for normal text in JStrack (default"
      puts $f "# is {Courier 12}"
      puts $f "set text_font {$text_font}"
      puts $f ""
   }
   if {[default_changed show_ptrloc]} {
      puts $f "# If show_ptrloc is set to a non-zero value, the current"
      puts $f "# location of the mouse, in latt/long, will be displayed"
      puts $f "# and updated as the mouse moves.  The default is to have"
      puts $f "# this feature turned on."
      puts $f "set show_ptrloc $ptrloc"
      puts $f ""
   }
   if {[history keep] != 20} {
      puts $f "# OPTIONAL:  You may increase the number of lines kept in"
      puts $f "# Tcl's history buffer (default is 20) by using the"
      puts $f "# following command:"
      puts $f "#     history keep {n}"
      puts $f "# where {n} is the (integer) number of lines to keep, e.g.,"
      puts $f "#     history keep 50"
      puts $f "history keep [history keep]"
      puts $f ""
   }
   if {[default_changed show_ptrloc]} {
      puts $f {# If you want JStrack to warn you when either the current position is}
      puts $f {# within a given range (the default is 60 nm) or the forecast track}
      puts $f {# takes it within the same range, set warn_at_nm to the range you want}
      puts $f {# to use (must be in nautical miles).  Remember, miles = nm * 1.15.}
      puts $f {# The setting below shows the system's default setting of 60 nm.}
      puts $f {#}
      puts $f {# set warn_at_nm 60}
      puts $f ""
   }
   if {[default_changed warn_at_nm]} {
      puts $f "set warn_at_nm $warn_at_nm"
      puts $f ""
   }
   if {[default_changed update_prog pos_est_prog current_prog forecast_prog]} {
      puts $f {# PROGRAMS ASSOCIATED WITH EVENTS}
      puts $f ""
      puts $f {# If you want an external program (e.g., some type of sound file, etc.)}
      puts $f {# to be run when the storm data is reloaded by JStrack, i.e., when new}
      puts $f {# data arrives for a storm currently in memory, set the update_prog}
      puts $f {# variable to the value of the program you want to run.  For example:}
      puts $f {#}
      puts $f {# set update_prog "vplay /home/jim/tv/simpsons/doh.wav"}
      puts $f ""
      puts $f {# If you want to pass the name of the storm to a script (e.g., to}
      puts $f {# only sound off about data for a given storm, to use a voice synth.,}
      puts $f {# etc.), use format shown below.}
      puts $f {#}
      puts $f {# NOTE THE USE OF '{' AND '}' INSTEAD OF PLAIN QUOTES---THIS IS REQUIRED}
      puts $f {# TO PREVENT THE EXPANSION OF $nm (which is the name of the storm in all}
      puts $f {# lowercase).  Another variable you have access to from the procedure that}
      puts $f {# calls this is $name, which is the name of the storm in all uppercase (as}
      puts $f {# it is used within the program).}
      puts $f ""
      puts $f {# set update_prog {say new data on $nm}}
      puts $f ""
      puts $f "set update_prog {$update_prog}"
      puts $f ""
      puts $f {# Likewise, if you want an alarm on a position estimate (when there}
      puts $f {# was no full update), set pos_est_prog to the external command you}
      puts $f {# want run.  $nm and $name apply exactly as for $update_prog.}
      puts $f ""
      puts $f {# set pos_est_prog {say new data on $nm}}
      puts $f ""
      puts $f "set pos_est_prog {$pos_est_prog}"
      puts $f ""
      puts $f {# Normally, warnings associated with proximity of a storm (to the}
      puts $f {# home coordinates you define) will only be a text message and a red}
      puts $f {# background behind the menus.  If you want an alarm to sound, etc.,}
      puts $f {# using some external program, define it here.  The first variable,}
      puts $f {# current_prog, is for times when the storm's current position is}
      puts $f {# within $warn_at_nm nautical miles from your location.  The second}
      puts $f {# variable, forecast_prog, is when the forecast track takes it}
      puts $f {# within that distance from your location.}
      puts $f {#}
      puts $f {# Variables available (from the command line, as for update_prog and}
      puts $f {# pos_est_prog):}
      puts $f {#       name       --- storm name (uppercase)}
      puts $f {#       nm         --- storm name (lowercase)}
      puts $f {#       timestamp  --- current time for latest advisory (e.g., 02/0900Z)}
      puts $f {#       warn_at_nm --- within this distance (in nm) from home}
      puts $f ""
      puts $f {# set current_prog {say $nm is now within $warn_at_nm nautical miles}}
      puts $f {# set forecast_prog \\}
      puts $f {#       {say $nm is expected to come within $warn_at_nm nautical miles}}
      puts $f ""
      puts $f "set current_prog {$current_prog}"
      puts $f "set forecast_prog {$forecast_prog}"
      puts $f ""
   }
   close $f
}


