#!/bin/sh
#
# rdmgendcwmaps
#
# Utility to scan a directory for the Digital Charts of the World
# shapefiles and process them through buildmap
#
# Usage:
#
# rdmgendcwmaps [maps=<map-directory-path>] [verbose|noindex|test] [dryrun] <files>...
#

usage()
{
   echo 'Usage: rdmgendcwmaps [maps=<map-directory-path>] [verbose|noindex|test] [<files> ...]' >&2
   exit 1
}

maps=/usr/share/roadmap
prov='*'

if [ $# -eq 0 ] ; then
  usage
fi

verbose=''
gendir=Y
DRYRUN=N

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

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

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

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

for i in $*
do
  base=`basename $i`
  FIPS=`expr substr $base 1 5`
  echo $BUILDMAP -f DCW -n -m $maps $verbose $FIPS $i
  if [ $DRYRUN != 'Y' ] ; then
    $BUILDMAP -f DCW -n -m $maps $verbose $FIPS $i
  fi
done

if [ $gendir = 'Y' ] ; then

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

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

