#!/bin/sh
#
# Create RoadMap map files from the US Census Bureau ZIP files.
#
# The main purpose of this script is to avoid keeping the TIGER
# files in uncompressed form, waisting large amounts of disk space.
#
# The buildmap and buildus programs will be found either in the
# current directory, or via $PATH.  Pass in $BUILDMAP and/or
# $BUILDUS to override.
#
#

usage()
{
   cat >&2 <<EOF

usage:
  rdmgenmaps <tiger-path> [maps=<map-directory-path>]
                       [format=2000|2002|2004|2005|2006]
                       [verbose|noindex|test]
                       [<state-id> | county-fips ] ...

Examples:
   All of New England:
	rdmgenmaps /var/lib/roadmap MA NH VT ME CT RI
   For New York and Los Angeles:
        rdmgenmaps /var/lib/roadmap maps=/tmp/cities 06037 36061

   A state or region ID can also be numeric (e.g. California is
   "06"), as defined by Tiger data.
EOF
   exit 1
}

bad_abbrev()
{
    echo Bad state abbreviation $1 >&2
    exit 1
}

# Default setup: preferred map path is used, latest format, all states.

FORMAT="--format=2002"
MAPPATH="--maps=/var/lib/roadmap"
TMPDIR=/var/tmp
state=''
cleanup=Y
gendir=Y
verbose=''
DRYRUN=N


# state codes, from app_a02.txt
AL=01; AK=02; AZ=04; AR=05; CA=06; CO=08; CT=09; DE=10; DC=11;
FL=12; GA=13; HI=15; ID=16; IL=17; IN=18; IA=19; KS=20; KY=21;
LA=22; ME=23; MD=24; MA=25; MI=26; MN=27; MS=28; MO=29; MT=30;
NE=31; NV=32; NH=33; NJ=34; NM=35; NY=36; NC=37; ND=38; OH=39;
OK=40; OR=41; PA=42; RI=44; SC=45; SD=46; TN=47; TX=48; UT=49;
VT=50; VA=51; WA=53; WV=54; WI=55; WY=56; AS=60; GU=66; MP=69;
PR=72; UM=74; VI=78;



# Analyze the command line arguments.

TIGERDIR=$1
test -d "$TIGERDIR" || usage
shift

case $1 in
   maps=*) MAPPATH="--maps=`expr $1 : 'maps=\(.*\)'`"
           shift
           ;;
esac

case $1 in
   format=2000) FORMAT="--format=2000"
                shift
                ;;
   format=2002) FORMAT="--format=2002"
                shift
                ;;
   format=2004) FORMAT="--format=2002"
                shift
                ;;
   format=2005) FORMAT="--format=2002"
                shift
                ;;
   format=2006) FORMAT="--format=2002"
                shift
                ;;
esac

case $1 in
    verbose) verbose=-v; shift ;;
    noindex) gendir=N; shift ;;
    test)    verbose=-v; gendir=N; cleanup=N; shift ;;
esac

case $1 in
    dryrun) DRYRUN=Y shift ;;
esac

set_county_list()
{
   case $1 in
      # if it's a county code, then all listed counties will be processed
      [0-9][0-9][0-9][0-9][0-9])
	       if [ ! -e $TIGERDIR/[Tt][Gg][Rr]${1}.[Zz][Ii][Pp] ]
	       then
		  echo "No file tgr*.zip found for $1 in $TIGERDIR" >&2
		  exit 1
	       fi
	       counties=$1
	       return
	       ;;

      # state as letter abbrev
      [A-Z][A-Z])
	       # user gives "MA", dereference $MA
	       state=`eval echo $\`echo $1\``
	       test "$state" || bad_abbrev $1
	       ;;
      # state as numeric code
      [0-9][0-9])
	       state=$1
	       ;;

      # continue to support older "state=.." syntax for a release or two
      state=[A-Z][A-Z])
	       abbr=`expr $1 : 'state=\(.*\)'`
	       test "$abbr" || usage
	       state=`eval echo $\`echo $abbr\``
	       test "$state" || bad_abbrev $abbr
	       ;;
      state=[0-9][0-9])
	       state=`expr $1 : 'state=\(.*\)'`
	       test "$state" || usage
	       ;;
      # empty list means all counties of all states
      ALL)
      	       unset state
	       ;;
      *)
	       usage
	       ;;
   esac

   unset counties

   # list all the available counties for the given state
   for i in $TIGERDIR/[Tt][Gg][Rr]${state}*.[Zz][Ii][Pp]
   do
      if [ -e $i ] ; then

         base=`basename $i`
         county=`expr substr $base 4 5`
         counties="$counties $county"
      else
         echo "No file tgr${state}*.zip found for $state in $TIGERDIR" >&2
	 exit 1
      fi
   done

}


if [ ! "$BUILDMAP" ]
then
    if [ -e ./buildmap ] ; then
       BUILDMAP=./buildmap
    else
       BUILDMAP=buildmap
    fi
fi

process_one_county() {

   if [ -e $TIGERDIR/TGR$1.ZIP ] ; then
      echo unzip $TIGERDIR/TGR$1.ZIP -d $TMPDIR/roadmap 
      if [ $DRYRUN != 'Y' ] ; then
         unzip $TIGERDIR/TGR$1.ZIP -d $TMPDIR/roadmap > /dev/null
      fi
   elif [ -e $TIGERDIR/tgr$1.zip ] ; then
      echo unzip $TIGERDIR/tgr$1.zip -d $TMPDIR/roadmap
      if [ $DRYRUN != 'Y' ] ; then
         unzip $TIGERDIR/tgr$1.zip -d $TMPDIR/roadmap > /dev/null
      fi
   else
      echo No file $TIGERDIR/tgr$1.zip or $TIGERDIR/TGR$1.ZIP to unzip >&2
      exit 1
   fi
   rt1=$TMPDIR/roadmap/TGR$1.RT1
   if [ $DRYRUN = 'Y' -o -e $rt1 ] ; then
      echo "$BUILDMAP $verbose $FORMAT $MAPPATH $1 $rt1"
      if [ $DRYRUN != 'Y' ] ; then
         $BUILDMAP $verbose $FORMAT $MAPPATH $1 $rt1 || exit 1
      fi
   fi
   if [ $cleanup = 'Y' ] ; then
      rm -f $TMPDIR/roadmap/*
   fi
}


rm -rf $TMPDIR/roadmap
mkdir $TMPDIR/roadmap

unset allcounties
if [ $# -gt 0 ]
then
    for arg in $*
    do
	set_county_list $arg
	test "$counties" || usage
	allcounties="$allcounties $counties"
    done
else
    set_county_list ALL
    test "$counties" || usage
    allcounties="$counties"
fi

for c in $allcounties
do
    process_one_county $c
done

if [ $cleanup = 'Y' ] ; then
   rmdir $TMPDIR/roadmap
fi

if [ $gendir = 'Y' ] ; then

   echo "Generating usdir.rdm, please wait.."

   if [ ! "$BUILDUS" ]
   then
       if [ -e buildus ] ; then
          BUILDUS=./buildus
       else
          BUILDUS=buildus
       fi
   fi
   echo $BUILDUS -s $MAPPATH
   if [ $DRYRUN != 'Y' ] ; then
       $BUILDUS -s $MAPPATH
   fi
fi

