#!/bin/sh
############################################################################
#
# MODULE:       v.centroids
# AUTHOR:       Hamish Bowman
# PURPOSE:      Add missing centroids  (frontend to v.category opt=add)
# COPYRIGHT:    (c) 2006 Hamish Bowman, and the GRASS Development Team
#               This program is free software under the GNU General Public
#               License (>=v2). Read the file COPYING that comes with GRASS
#               for details.
#
#############################################################################

#%Module
#% description: Adds missing centroids to closed boundaries
#% keywords: vector, centroid, area
#%End

#%option
#% key: input
#% type: string
#% gisprompt: old,vector,vector
#% key_desc: name
#% description: Name of input vector map 
#%required: yes
#%end

#%option
#% key: output
#% type: string
#% gisprompt: new,vector,vector
#% key_desc: name
#% description: Name for output vector map
#% required: yes
#%end

#%option
#% key: option
#% type: string
#% description: Action to be taken
#% options: add
#% answer: add
#% required: no
#%end

#%option
#% key: layer
#% type: integer
#% description: Layer number
#% answer: 1
#% required: no
#%end

#%option
#% key: cat
#% type: integer
#% description: Category number starting value
#% answer: 1
#% required: no
#%end

#%option
#% key: step
#% type: integer
#% description: Category increment
#% answer: 1
#% required: no
#%end


if  [ -z "$GISBASE" ] ; then
    echo "You must be in GRASS GIS to run this program." 1>&2
    exit 1
fi

if [ "$1" != "@ARGS_PARSED@" ] ; then
    exec g.parser "$0" "$@"
fi


if [ "$GIS_OPT_OPTION" = "add" ] ; then

   # check we have boundaries
   NUM_BOUND=`v.info "$GIS_OPT_INPUT" | grep "Number of boundaries:" | cut -f2 -d: | awk '{print $1}'`

   if [ $NUM_BOUND -eq 0 ] ; then
       echo "ERROR: Input vector map contains no boundaries." 1>&2
       exit 1
   fi

   # add centroids
   v.category input="$GIS_OPT_INPUT" output="$GIS_OPT_OUTPUT" option=add \
     type=area layer="$GIS_OPT_LAYER" cat="$GIS_OPT_CAT" step="$GIS_OPT_STEP"

   exit $?
fi

exit 0
