#!/bin/sh 
#
# rdmgenstatemaps
#
# Process the US state or Canadian province boundary map through buildmap.
#

usage()
{
    cat >&2 <<EOF
rdmgenstatemaps [which=<which>] src=<path-to-.shp-file> \\
        [maps=<map-directory-path>] [verbose|noindex|test] [dryrun]
  where the src=<> parameter points to a directory containing the unpacked
  contents of st99_d00_shp.zip, available from:
    http://www.census.gov/geo/cob/bdy/st/st00shp/st99_d00_shp.zip
    (for states:  "which=AK" to generate Alaskan borders
                  "which=HI" to generate Hawaiian borders
                  "which=CONTINENTAL" to generate for continental USA)
  or from BND1_shape.zip, available from:
    http://www.geobase.ca/geobase/en/search.do?produit=cgb1
    (for provinces: use 'which=PROVINCES')
EOF
   exit 1
}


# the state boundary map data is available from:
# http://www.census.gov/geo/www/cob/st2000.html
# specifically, 
# http://www.census.gov/geo/cob/bdy/st/st00shp/st99_d00_shp.zip
#
# the provincial boundary map data is available from:
# http://www.geobase.ca/geobase/en/data/cgb/index.html

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

verbose=''
gendir=Y
DRYRUN=N

#default to just continental usa
shape='STATES=continental'
base=st99_d00.shp
FIPS=81002
case $1 in
    which=*)
        which=`expr $1 : 'which=\(.*\)'`
        case $which in
            ALL|all)
                echo Generating all USA
                base=st99_d00.shp
                shape='STATES'
                FIPS=81001
                ;;
            CONTINENTAL|continental)
                echo Generating just continental USA
                shape='STATES=continental'
                base=st99_d00.shp
                FIPS=81002
                ;;
            AK|ak)
                echo Generating just Alaska
                shape='STATES=AK'
                base=st99_d00.shp
                FIPS=81003
                ;;
            HI|hi)
                echo Generating just Hawaii
                shape='STATES=HI'
                base=st99_d00.shp
                FIPS=81004
                ;;
            PROVINCES|provinces)
                echo Generating just Canada provinces
                shape='PROVINCES'
                base=prov_ab_p_geo83_e.shp
                FIPS=81061
                ;;
            *)
                echo Invalid which value: $which >&2
                usage
                ;;

        esac
        shift
    ;;
esac

case $1 in
    src=*) srcpath=`expr $1 : 'src=\(.*\)'`
            shift
            ;;
esac
file=$srcpath/$base

if [ ! -f $file ]
then
    echo $0: no such file $file >&2
    exit 1
fi

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

if [ ! -d $maps ]
then
    echo $0: no such directory $maps >&2
    exit 1
fi

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 [ ! "$BUILDMAP" ]
then
    if [ -e ./buildmap ] ; then
       BUILDMAP=./buildmap
    else
       BUILDMAP=buildmap
    fi
fi




echo $BUILDMAP -f $shape -m $maps $verbose $FIPS $file
if [ $DRYRUN != 'Y' ] ; then
    $BUILDMAP -f $shape -m $maps $verbose $FIPS $file
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 --maps=$maps
    if [ $DRYRUN != 'Y' ] ; then
        $BUILDUS -s --maps=$maps
    fi
fi 
