#!/usr/local/bin/wish8.4

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


source ../paths.tcl
source $libdir/miscb.tcl

set interval 10
# Don't abuse the ftp server!  These products are issued every six hours
# (with an intermediate advisory in between if a storm is close to land).
# The only reason why I don't limit checks to those normal times is the
# possibility (which we saw once or twice this year) of special advisories,
# where something changes rapidly, and products are issued outside of the
# normal hours.  For normal use (where you're tracking a storm that's only
# a potential, not immediate, threat), checking for updates once/hour is
# MORE THAN ENOUGH.
#
# If you are awake, and sitting there waiting THAT impatiently for an
# update, initiate the ftp yourself from within JStrack.....  Do NOT leave
# this script in a mode where it's unattended and banging away at the
# NHC/TPC's FTP server too much.
set no_abuse 10

set format "%d %h %Y %H:%M:%S"
set next_sec [clock scan "now + $interval minutes"]
set next_time [clock format $next_sec -format $format]
set date_time [clock format [clock seconds] -format $format]

proc set_intv {} {
   global interval next_time next_sec format no_abuse
   set interval [max $interval $no_abuse]
   set next_sec [clock scan "now + $interval minutes"]
   set next_time [clock format $next_sec -format $format]
}


proc run_ftp {} {
   global date_time tclshbin ftpdir
   set date_time "Updating via FTP"
   update
   catch { exec $tclshbin $ftpdir/ftp.tcl >> $ftpdir/autoftp.log }
   set_intv
}

wm protocol . WM_DELETE_WINDOW { exit }
wm resizable . 0 0
wm title . "Schedule NHC/TPC FTP Updates"

label .curr_lb -text "Current Time:  "
label .curr_tm -textvariable date_time
grid .curr_lb -row 1 -column 1
grid .curr_tm -row 1 -column 2 -sticky ew

label .intv_lb -text "Update Interval:   "
frame .intv_fm
button .intv_cmd -text "SET" -command {
   if {$interval >= 10} { set_intv }
}
grid .intv_lb  -row 2 -column 1
grid .intv_fm  -row 2 -column 2 -sticky ew
grid .intv_cmd -row 2 -column 3

entry .intv_fm.ent -textvariable interval -width 4
label .intv_fm.lbl -text " (minutes)"
label .intv_fm.lblb -text " "
pack .intv_fm.ent .intv_fm.lbl -side left -anchor w
pack .intv_fm.lblb -side left -expand y -fill x -anchor w

label .next_lb -text "Next Update:   "
label .next_tm -textvariable next_time -font {Times 18} -background black -foreground #dd0000
grid .next_lb -row 3 -column 1
grid .next_tm -row 3 -column 2

button .exit -text "Exit" -command { exit }
grid .exit -row 4 -columnspan 3

.curr_tm configure -font {Times 18} -background black -foreground #00ffff
.intv_fm.ent configure -font {Times 18} -background #d9d9d9 -foreground #000000
.intv_fm.lbl configure -font {Times 18} -background black -foreground #00ffff
.intv_fm.lblb configure -font {Times 18} -background black -foreground #00ffff
.next_tm configure -font {Times 18} -background black -foreground #dd0000


while {1} {
   after 333 ;
   set t [clock seconds]
   if {$t >= $next_sec} { run_ftp }
   set date_time [clock format $t -format $format]
   if {$interval < 10} {
      set no_abuse [max $no_abuse 10]
      set interval $no_abuse
      set_intv
   }
   update
}






