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

package require http

set nhcbase  http://www.nhc.noaa.gov/archive/text
set year [clock format [clock seconds] -format %Y]

set nhclinks(TCMAT) $nhcbase/TCMAT
set nhclinks(TCPAT) $nhcbase/TCPAT
set nhclinks(TCDAT) $nhcbase/TCDAT

# Windows-EXE-specific stuff
if {[string compare -nocase [file tail [info nameofexecutable]] "jstrack.exe"] == 0} {
   source ../paths.tcl.winexe
} else {
   source ../paths.tcl
}

source $rfcdir/filtlib.tcl


# Get remote file listing from $link, where $link is a directory and
# the server allows us to do this.
#
proc get_ls {link} {
   if {[catch { set token [::http::geturl $link] }]} { return -1 }
   upvar #0 $token state

   regexp {([234][0-9][0-9]) (.*)} $state(http) a code msg
   if {$code != 200} {
      return -1
   }
   set fnlist [list]
   foreach line [split $state(body) \n] {
      if {[regexp -nocase {<A HREF="(TC[MDP]AT[1-5].[0-9]*.txt)">} $line a fn]} {
         lappend fnlist $fn
      }
   }
   return $fnlist
}


# grab $url and save it to $filename; return 0 if ok, -1 on error
#
proc get_remote {link filename} {
   set f [open $filename w]

puts "link:  $link"
puts "filename:  $filename"
puts ""

   if {[catch { set token [::http::geturl $link -channel $f] }]} {
      return -1
   }
   upvar #0 $token state
   regexp {([234][0-9][0-9]) (.*)} $state(http) a code msg
   if {$code != 200} { return -1 }
   close $f
   return 0
}


# process_newdata for NHC products---downloads anything newer than the
# timestamp files, stores the file, and then processes it with
# filt_read_advisories in filtlib.tcl ... returns the number of
# files processed.
#
proc process_newdata {product} {
   global nhclinks year

   set errors 0
   set processed 0

   for {set i 1} {$i <= 5} {incr i} {
      set url $nhclinks($product)$i/$year/

      set tmplist [list]
      set fnlist [get_ls $url]
# if {$fnlist == -1} { return "CONNECTION TO NHC SITE FAILED"}
      set local_tm [file mtime ts/$product$i]
      set local_time [clock format $local_tm -format %Y%m%d%H%M -gmt true]

      foreach fn $fnlist {
         regexp {TC[MDP]AT[1-5].([0-9]*).txt} $fn a file_tm
         if {$file_tm >= $local_time} {
            if {[get_remote $url/$fn $fn] == 0} {
               incr processed
               filt_read_advisories $fn
               file delete $fn
            }
         }
      }
      file mtime ts/$product$i [clock seconds]
   }
   return $processed
}


# EXAMPLE USAGE:
#
# set processed [process_newdata TCMAT]
# puts "Processed $processed updates for FORECAST"
#
# set processed [process_newdata TCPAT]
# puts "Processed $processed updates for ADVISORY"
#
# set processed [process_newdata TCDAT]
# puts "Processed $processed updates for DISCUSSION"



