#!/bin/sh
#
# Script to retrieve NWS data files. 
#
# Run this from the INSTALLED location of the script, i.e.
#    /usr/local/share/xastir/scripts
# or /usr/share/xastir/scripts
#
#
# Originally written 2006/03/07 by Steven, WM5Z, and Curt, WE7U.
#
# Copyright (C) 2000-2026 The Xastir Group
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# See README.MAPS for a bit more information on the program.
#
#
# NOTE:  Run this script as root.
#
# MAINTAINERS:  Go here to find out what the latest versions are:
# <http://www.nws.noaa.gov/geodata/>
# <http://www.weather.gov/gis/AWIPSShapefiles>



# Please only have ONE of each variable listed here!  Note that the "Valid
# Date" listed on the NWS web pages is the date at which the Shapefile
# should START to be used.  Don't just blindly put the newest filename
# here if that start-date hasn't arrived yet!
#
FILE1="w_03mr26"    # GREEN: NWSM Library: County Warning Area Boundaries
FILE2="z_03mr26"    # GREEN: NWSM Library: Public Forecast Zone Boundaries
FILE3="mz03mr26"    # GREEN: NWSM Library: Coastal and Offshore Marine Zones
FILE4="oz03mr26"   # GREEN: NWSM Library: Coastal and Offshore Marine Zones
FILE5="hz20fe25"    # GREEN: NWSM Library: Coastal and Offshore Marine Zones
FILE6="fz03mr26"    # GREEN: NWSM Library: Fire Weather Zone Boundaries
FILE7="c_03mr26"    # RED: Counties, States, Provinces: U.S. Counties

prefix=@prefix@

cd ${prefix}/share/xastir/Counties || exit 1


# Remove any old zip files hanging around in this directory
#
rm -f *.zip 2>&1 >/dev/null


# Fetch new copies, unzip into place, delete archive.
#
#
DIR=WSOM
for d in $FILE1 $FILE2 $FILE3 $FILE4 $FILE5 $FILE6; do
  if [ -e $d.shp ]; then
    echo "Already have $d shapefile, skipping..."
  else
    # Remove possible older copies
    cut=`echo $d|cut -c1-2 -`
    rm -f $cut*.shx $cut*.shp $cut*.dbf $cut*.prj $cut*.zip 2>&1 >/dev/null
    wget https://www.weather.gov/source/gis/Shapefiles/$DIR/$d.zip
    unzip $d.zip
    rm -f *.zip
  fi
done

DIR=County
for d in $FILE7; do
  if [ -e $d.shp ]; then
    echo "Already have $d shapefile, skipping..."
  else
    # Remove possible older copies
    cut=`echo $d|cut -c1-2 -`
    rm -f $cut*.shx $cut*.shp $cut*.dbf $cut*.prj $cut*.zip 2>&1 >/dev/null
    wget https://www.weather.gov/source/gis/Shapefiles/$DIR/$d.zip
    unzip $d.zip
    rm -f *.zip
  fi
done

####################
# After download/install of map files above, run commands such as these:
#
#   cd /usr/local/share/xastir; testdbfawk -D config -d Counties/mz11au16.dbf 2>&1 | head -5
#   cd /usr/local/share/xastir; testdbfawk -D config -d Counties/w_11au16.dbf 2>&1 | head -5
#
# to determine whether there's dbfawk support for each of the NWS files. NWS
# changes the signature on their files often so we need to create a new
# dbfawk for each in xastir/config, test it, then add it into the
# Makefile.am file there and commit, then do another "make install" to put
# the dbfawk's into place.
# Fixed the issue where there's an 'a' or 'b' after the filename root. NWS likes
# to do that, but have non-a/b files extracted from the zip, which made the below
# tests fail.
####################
#

exitcode=0

for d in $FILE1 $FILE2 $FILE3 $FILE4 $FILE5 $FILE6 $FILE7; do

  e=`echo $d | sed -e's/\(.*[0-9]\)[a-f]$/\1/'`

  if [ -e $e.dbf ]; then
    echo
    echo $e.dbf
    # Run in a separate shell so we don't mess up the current directory for the -e test above
    result=`cd ${prefix}/share/xastir; ${prefix}/bin/testdbfawk -D config -d Counties/$e.dbf 2>&1 | head -3 | tail -1`
    echo $result
    if [ "$result" = "No matching dbfawk signature found" ]; then
        echo "Fix dbfawk for $e!"
        exitcode=1
    fi
  fi
done
echo

if [ $exitcode -eq 1 ] ; then
    echo "One or more dbfawk files is out of date.  Fix the problem."
fi

exit $exitcode

