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

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

# Windows-EXE-specific stuff
if {[string compare -nocase [file tail [info nameofexecutable]] "autoupdate.exe"] == 0} {
   set topdir $starkit::topdir
   regsub {/autoupdate.exe.*$} $topdir {} t
   cd $t
   source ../paths.tcl.winexe
} else {
   source ../paths.tcl
}

proc min {a b} { if {$a < $b} { return $a } ; return $b }
proc max {a b} { if {$a > $b} { return $a } ; return $b }
source httplib.tcl

# Default interval between updates is 15 minutes.  Far more than enough.
set interval 15

# Don't abuse the NHC's server!  This 15 minute minimum interval setting
# is per the NHC webmaster's (unofficial) request.  Do NOT change this.

set no_abuse 15

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_http {} {
   global date_time
   
   set date_time "Checking for updates in TCMAT (Forecast/Advisory)"
   update
   set cnt [process_newdata TCMAT]
   .intv_fm.lbl configure -text "$cnt updates for the Forecast/Advisory"
   update

   set date_time "Checking for updates in TCPAT (Public Advisory)"
   update
   set cnt [process_newdata TCPAT]
   .intv_fm.lbl configure -text "$cnt updates for the Public Advisory"
   update

   set date_time "Checking for updates in TCDAT (Discussion)"
   update
   set cnt [process_newdata TCDAT]
   .intv_fm.lbl configure -text "$cnt updates for the Discussion"
   update
   set_intv
   after 2500  ;
   .intv_fm.lbl configure -text " (minutes)"
   update
}

wm geometry . +200+200
wm protocol . WM_DELETE_WINDOW { exit }
wm resizable . 0 0
wm title . "Schedule NHC/TPC HTTP 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 >= 15} { 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 black
.intv_fm.lbl configure -font {Times 18} -background black -foreground #00ffff
.intv_fm.lblb configure -font {Times 18} -background black -foreground yellow
.next_tm configure -font {Times 18} -background black -foreground yellow

run_http ;# start off with an update

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







