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

set standalone_ftp_mode 0
set nhcsite ftp.nhc.noaa.gov
set nhctopdir /pub/products/nhc
set subdirs [list marine strike disc public]

source ../paths.tcl


proc usrmsg {msg {statwin 0}} {
   global notk
#  set f [open ftplog a] ; puts $f $msg ; flush $f ; close $f
   if {$notk} {
      if {$statwin == 0} { puts $msg }
   } else {
      set stlist [list jdgmsg statmsg errmsg crashmsg]
      .ftpmsg.text insert end "$msg\n" [lindex $stlist $statwin]
      .ftpmsg.text see end
      update idletasks
   }
}

proc tk_finish {} {
   global notk standalone_ftp_mode
   if {$notk && $standalone_ftp_mode} { exit }
   if {$notk} { return }
   if {$standalone_ftp_mode} {
      button .ftpmsg.done -text "Dismiss" -command { exit }
   } else {
      button .ftpmsg.done -text "Dismiss" -command { destroy .ftpmsg }
   }
   pack .ftpmsg.done -side top
}


proc noftplib {msg} {
  global ftp_available notk
   set ftp_available 0

   catch { destroy .ftpmsg }
   if {[catch {toplevel .ftpmsg}]} {
      set notk 1
   } else {
      set notk 0
      text .ftpmsg.text -relief sunken -bd 2 -width 80 -height 25 \
         -yscrollcommand ".ftpmsg.scroll set" -font {Courier 12}
      scrollbar .ftpmsg.scroll -command ".ftpmsg.text yview"
      pack .ftpmsg.scroll -side right -fill y
      pack .ftpmsg.text -side top -expand yes -fill both
   }

   usrmsg $msg
   if {$notk == 0} { tk_finish }
}

set ftp_available 1
if {[catch { package require Tcl 8.2 }]}  { noftplib "Need at least Tcl 8.2 for tcllib!" }
if {[catch { package require ftp }]}      { noftplib "Need tcllib 1.3 or better!" }
if {[catch { package require textutil }]} { noftplib "Need tcllib 1.3 or better!" }
set ::ftp::VERBOSE 1


proc ftpmsg {s msg {state ""}} {
   global closing_ftp ftpcrash

   set lvl 1 ;# not an error
   if {[regexp {C: [45]} $msg]} {
      if {$closing_ftp == 0 && [regexp {C: [45]21} $msg]} { set ftpcrash 1 }
      set lvl 2 ;# error
   } elseif {[regexp {C: 221} $msg]} { set ftpcrash 1 }
   if {$state == "error"} { set lvl 2 } ;# error
   usrmsg $msg $lvl
}

proc openftp {ftpsite {username anonymous} {passwd ftpuser@domain}} {
   global ftpcrash ; if {$ftpcrash} { return "" }
   return [::ftp::Open $ftpsite $username $passwd -output ftpmsg]
}
proc ftpcd {f dir} {
   global ftpcrash ; if {$ftpcrash} { return }
   ::ftp::Cd $f $dir
}
proc getls {f {pattern ""}} {
   global ftpcrash ; if {$ftpcrash} { return "" }
   return [::ftp::NList $f $pattern]
}
proc ftpf_tm {f fn} {
   global ftpcrash ; if {$ftpcrash} { return "" }
   return [::ftp::ModTime $f $fn]
}
proc closeftp {f} {
   global ftpcrash ; if {$ftpcrash} { return }
   usrmsg "Finished!  Closing FTP connection." 3
#  usrmsg "Closing FTP connection...this" 3
#  usrmsg "sometimes causes Tcl/Tk to crash." 3
#  usrmsg "If it does, don't worry---we're" 3
#  usrmsg "done here anyways...." 3
   after 1000 ;
   ::ftp::Close $f
}
proc ftp_mode {f mode} {
   global ftpcrash ; if {$ftpcrash} { return }
   ::ftp::Type $f $mode
}
proc ftp_get {f fn} {
   global ftpcrash ; if {$ftpcrash} { return "" }
   ::ftp::Get $f $fn -variable ret
   return $ret
}

proc sortlist {f l dir} {
   global ftpdir
   if {[file exists $ftpdir/timestamp.$dir]} {
      set last_tm [file mtime $ftpdir/timestamp.$dir]
   } else {
      set last_tm 0
   }
   set lastftp [clock format $last_tm -format %Y%m%d%H -gmt true]
   set r1 [list]
   foreach fn $l {
      set tm [file rootname $fn]
      if {$tm > $lastftp} {
         lappend r1 $fn
      } elseif {$tm == $lastftp} {
         if {[ftpf_tm $f $fn] > $last_tm} { lappend r1 $fn }
      }
   }
   set ftmp [open $ftpdir/timestamp.$dir w]
   close $ftmp
   return [lsort $r1]
}


proc proc_ftpcrash {} { tk_finish }


proc update_ftp {} {
   global nhcsite nhctopdir subdirs tclshbin filt_path notk
   global standalone_ftp_mode ftp_available closing_ftp ftpcrash

   if {$ftp_available == 0} { return }
   set closing_ftp 0
   set ftpcrash 0

   catch { destroy .ftpmsg }
   if {[catch {toplevel .ftpmsg}]} {
      set notk 1
   } else {
      set notk 0
      text .ftpmsg.text -relief sunken -bd 2 -width 80 -height 25 \
         -yscrollcommand ".ftpmsg.scroll set" -font {Courier 12}
      scrollbar .ftpmsg.scroll -command ".ftpmsg.text yview"
      pack .ftpmsg.scroll -side right -fill y
      pack .ftpmsg.text -side top -expand yes -fill both
      .ftpmsg.text tag configure jdgmsg -font {Courier 10 bold} -foreground #0000dd
#     .ftpmsg.text tag configure crashmsg -font {Courier 14 bold} -foreground #dd0000
      .ftpmsg.text tag configure crashmsg -font {Courier 14 bold} -foreground #0000dd
      .ftpmsg.text tag configure errmsg -font {Courier 10 bold} -foreground #dd0000
      .ftpmsg.text tag configure statmsg -font {Courier 10} -foreground #000000
      if {$standalone_ftp_mode} { wm withdraw . }
   }

   usrmsg "Opening connection to site..."
   set f [openftp $nhcsite]
   if {$f < 0} {
      usrmsg "could not open $nhcsite ... aborting"
      if {$standalone_ftp_mode && $notk} {
         exit 1
      } else {
         tk_finish
      }
   }

   foreach subdir $subdirs {
      if {$ftpcrash} { proc_ftpcrash ; return }
      ftpcd $f $nhctopdir/$subdir
      if {$ftpcrash} { proc_ftpcrash ; return }
      usrmsg $nhctopdir/$subdir....
      set fnlist [sortlist $f [getls $f *.atl] $subdir]
      if {$ftpcrash} { proc_ftpcrash ; return }

      foreach fn $fnlist {
         usrmsg "Grabbing $fn"
         set inadv 0

         ftp_mode $f text
         if {$ftpcrash} { proc_ftpcrash ; return }
         set ret [ftp_get $f $fn]
         if {$ftpcrash} { proc_ftpcrash ; return }
         set rlist [textutil::splitx $ret {

*}]
         foreach line $rlist {
            regsub -all {
} $line {} t
            regsub -all {} $t {} t2
            regsub -all {} $t2 {} t3
            set line [string trimleft [string trimright $t3]]

            if {[regexp KNHC $line] || [regexp KMIA $line]} {
               if {$inadv} {
                  close $fout
                  usrmsg "   processing data..."
                  catch { exec $tclshbin $filt_path < tmp_ftp$f.txt }
                  file delete -force tmp_ftp$f.txt
               }
               set fout [open tmp_ftp$f.txt w]
               set inadv 1
            }
            if {$inadv} { puts $fout $line }
         }
         if {$inadv} {
            close $fout
            usrmsg "   processing data..."
            catch { exec $tclshbin $filt_path < tmp_ftp$f.txt }
            file delete -force tmp_ftp$f.txt
         }
      }
   }

   if {$ftpcrash} { proc_ftpcrash ; return }
   set closing_ftp 1
   closeftp $f
   usrmsg "DONE"
   if {$notk == 0} { tk_finish }
}


