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

proc read_adv {} {
   puts ""
   puts "1) Read Forecast/Advisory from NHC"
   puts "2) Read Advisory from NHC"
   puts "3) Read Discussion from NHC"
   puts "4) Read Probabilities from NHC"
   puts "0) Never mind...."
   puts ""
   puts -nonewline "> "
   flush stdout
   set choice [gets stdin]
   puts ""
   if {[string compare "$choice" "1"] == 0} {
      set type FORECAST/ADVISORY ; set ext fc
   } elseif {[string compare "$choice" "2"] == 0} {
      set type ADVISORY ; set ext adv
   } elseif {[string compare "$choice" "3"] == 0} {
      set type DISCUSSION ; set ext dis
   } elseif {[string compare "$choice" "4"] == 0} {
      set type PROBABILITIES ; set ext prob
   } else { return }
   read_adv_storm $type $ext
}


proc read_adv_storm {type ext} {
   global storms

   set i 0
   array set selected_storm {}
   catch {
      foreach filename [glob $storms/*.trk] {
         incr i
         set selected_storm($i) [file tail [file rootname $filename]]
         puts "$i) Read $type for [string toupper $selected_storm($i)]"
      }
   }
   incr i ; puts "$i) Read $type for storm from past years" ; set loadpast $i
   puts "0) never mind..."
   set choice [gets stdin]
   puts ""
   if {$choice > 0 && $choice < $i} {
      set name $selected_storm($choice)
      puts -nonewline "What advisory number do you want to see?  "
      flush stdout
      set adv_num [gets stdin]
      read_advisories display_advisories $name $type $adv_num
   } elseif {$choice == $loadpast} {
      read_adv_past_storm $type $ext
   }
}


proc read_adv_past_storm {type ext} {
   global storms

   set i 0
   array set selected_storm {}
   catch {
      foreach dir [glob $storms/old/{1,2}*] {
         set dir [file tail $dir]
         if {[file isdirectory $storms/old/$dir] && $dir != "locks"} {
            foreach filename [glob $storms/old/$dir/*.trk] {
               incr i
      set selected_storm($i)    $old/dir/[file rootname [file tail $filename]]
      puts "$i) [string toupper $dir/[file rootname [file tail $filename]]]"
            }
         }
      }
   }
   puts "0) never mind..."
   set choice [gets stdin]
   puts ""
   if {$choice > 0 && $choice <= $i} {
      set name [file tail $selected_storm($choice)]
      set dir [file dirname $selected_storm($choice)]
      puts -nonewline "What advisory number do you want to see?  "
      flush stdout
      set adv_num [gets stdin]
      read_advisories display_advisories $name $type $adv_num 1 $dir
   }
}


proc adv_type_ext {type} {
   if {$type == "PROB" || $type == "PROBABILITIES"} {         return [list PROBABILITIES prob]
   } elseif {$type == "FC" || $type == "FORECAST/ADVISORY"} { return [list FORECAST/ADVISORY fc]
   } elseif {$type == "ADV" || $type == "ADVISORY"} {         return [list ADVISORY adv]
   } elseif {$type == "DISC" || $type == "DISCUSSION"} {      return [list DISCUSSION dis]
   }
}


proc read_advisories {dsp_proc name type adv_num {verbose 1} {dir ""}} {
   global storms stfile

   set lname [string tolower $name]
   set name [string toupper $name]
   set type [string toupper $type]
   set adv_num [string toupper $adv_num]

   foreach {type ext} [adv_type_ext $type] { break }

   set filename storms/[string tolower $stfile($lname).$ext]

   if {[file exists $filename] == 0} {
      if {$verbose == 0} {catch {destroy .nhcinfo ; update}}
      error "No NHC/TPC advisory data found for $name in $filename]"
      return
   }

   set f [open $filename r]
   if {$verbose} {puts "Reading $filename..." ; flush stdout}
   set flist [split [read $f] "\r\n"]
   close $f

   # normal search
   set start_findme "$name \[^ ]* *$type *NUMBER *$adv_num\[A-Z]?$"
   # handles intermediate advisories, cases like "ADVISORY NUMBER 5...CORRECTED" and so on
   set start_findme2 "$name \[^ ]* *$type *NUMBER *$adv_num\[^0-9].*$"

   if {$verbose} {puts -nonewline "Searching..." ; flush stdout}
   set match 0 ; set header 0 ; set mlist() [list]
   foreach line $flist {
      if {[regexp {.*KNHC .*([0-3][0-9][012][0-9][0-5][0-9])} $line a curr_tm]} {
         set tlist [list $line] ; set header 1
      } elseif {$header && [regexp $start_findme $line]} {
         set mlist($curr_tm) $tlist
         lappend mlist() $curr_tm
         set match 1
      } elseif {$header && [regexp $start_findme2 $line]} {
         set mlist($curr_tm) $tlist
         lappend mlist($curr_tm) $line
         lappend mlist() $curr_tm
         set match 1
      } elseif {$match} {
         lappend mlist($curr_tm) $line
      } else {
         lappend tlist $line
      }
   }

   if {[llength $mlist()] == 0} {
      error "Could not find requested advisory."
      if {$verbose == 0} {catch {destroy .nhcinfo ; update}}
      return
   }

   set idx [lindex [lsort $mlist()] end] ;# grab the most current for that advisory number
   $dsp_proc $mlist($idx)
}


proc display_advisories {nodelist} {
   global env expect_library tclx_library
   set lineno 0
   if {[array get env PAGER] != "" && [info exists expect_library]} {
      set tmpfile [open 00stdsp.tmp w]
      foreach line $nodelist { puts $tmpfile "$line" }
      close $tmpfile
      spawn $env(PAGER) 00stdsp.tmp
      interact
      file delete 00stdsp.tmp
   } elseif {[array get env PAGER] != "" && [info exists tclx_library]} {
      set tmpfile [open 00stdsp.tmp w]
      foreach line $nodelist { puts $tmpfile "$line" }
      close $tmpfile
      system "$env(PAGER) 00stdsp.tmp"
      file delete 00stdsp.tmp
   } else {
      foreach line $nodelist {
         incr lineno
         puts "$line"
         if {$lineno == 22} {
            puts -nonewline "\[more]"
            flush stdout
            gets stdin
            set lineno 0
         }
      }
   }
}

