#!/bin/sh
#
# $Id: get-gnis,v 1.16 2012/11/01 18:57:19 we7u Exp $
#
# Script to retrieve GNIS files by state. 
#
# Written 20041205 Dan Brown N8YSZ
#
# Copyright (C) 2000-2012  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.
#
# Look at the README for more information on the program.
#

GNIS_SITE=http://geonames.usgs.gov/docs/stategaz
SUFFIX=_Features_20090401

x=`dirname $0`
. $x/values

if [ $# -lt 1 ]
then
    printf "%s: error - Need at least one state to download\n" $0
    printf "Usage: %s ST [ST]... \n" $0
    exit 1 
fi

cd /tmp 

while [ $1 ]
do 

    MYSTATE=`printf ${1} | tr a-z A-Z `

    printf "Retrieving GNIS file for %s\n" ${MYSTATE}

    rm -f ${MYSTATE}${SUFFIX}.zip ${MYSTATE}${SUFFIX}.txt
    if (wget ${GNIS_SITE}/${MYSTATE}${SUFFIX}.zip)
    then
        unzip ${MYSTATE}${SUFFIX}.zip 
    else 
        rm -f ${MYSTATE}${SUFFIX}.zip ${MYSTATE}${SUFFIX}.txt
        wget ${GNIS_SITE}/${MYSTATE}${SUFFIX}.txt
    fi

    if ( [ -f ${MYSTATE}${SUFFIX}.txt ] )
    then 
        printf "File successfully downloaded. Moving to ${prefix}/share/xastir/GNIS\n" 
        sudo mv ${MYSTATE}${SUFFIX}.txt ${prefix}/share/xastir/GNIS/${MYSTATE}.gnis
	if [ ${MYSTATE} = "AK" -o ${MYSTATE} = "HI" ]; then
		sudo recode utf16..utf8 ${prefix}/share/xastir/GNIS/${MYSTATE}.gnis
	fi
    else 
        printf "File for %s not successfully downloaded.\n" ${MYSTATE}
    fi 

shift

done 


