#!/bin/sh
############################################################################
#
# MODULE:       v.dissolve
# AUTHOR:       M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
#                 New Zealand
#               Markus Neteler for column support
# PURPOSE:      Dissolve common boundaries between areas with common cat
#                 (frontend to v.extract -d)
# 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: Dissolves boundaries between adjacent areas sharing a common category number or attribute.
#% keywords: vector, area, dissolve
#%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: layer
#% type: integer
#% label: Layer number. If -1, all layers are extracted.
#% description: A single vector map can be connected to multiple database tables. This number determines which table to use.
#% answer: 1
#% gisprompt: old_layer,layer,layer_all
#% required : no
#%end
#%option
#% key: column
#% type: string
#% description: Name of column used to dissolve common boundaries
#% required : no
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
#%end

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

# save command line
if [ "$1" != "@ARGS_PARSED@" ] ; then
    CMDLINE=`basename "$0"`
    for arg in "$@" ; do
        CMDLINE="$CMDLINE \"$arg\""
    done
    export CMDLINE
    exec g.parser "$0" "$@"
fi

#### setup temporary file
TMPMAP="${GIS_OPT_OUTPUT}_$$"

cleanup()
{
   g.remove vect="$TMPMAP" --quiet
}

# what to do in case of user break:
exitprocedure()
{
   g.message -e 'User break!'
   cleanup
   exit 1
}
# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 15

# does map exist?
eval `g.findfile element=vector file="$GIS_OPT_INPUT"`
if [ ! "$file" ] ; then
   g.message -e "Vector map <$GIS_OPT_INPUT> not found"
   exit 1
fi

if [ -z "$GIS_OPT_COLUMN" ] ; then
   v.extract -d input="$GIS_OPT_INPUT" output="$GIS_OPT_OUTPUT" type=area \
             layer="$GIS_OPT_LAYER"
else

    COLTYPE=`v.info -c "$GIS_OPT_INPUT" layer=$GIS_OPT_LAYER --quiet | grep ".*|$GIS_OPT_COLUMN$" | cut -f1 -d'|'`
    if [ "$COLTYPE" != "INTEGER" ] && [ "$COLTYPE" != "SMALLINT" ] && [ "$COLTYPE" != "CHARACTER" ] && [ "$COLTYPE" != "TEXT" ] ; then
       g.message -e "Key column must be of type integer or string"
       exit 1
    fi

   table=`v.db.connect "$GIS_OPT_INPUT" -gl fs="|" layer="$GIS_OPT_LAYER" | cut -f2 -d'|'`
   if [ -z "$table" ] ; then
      g.message -e "Database connection not defined for layer $GIS_OPT_LAYER"
      exit 1
   fi
   v.reclass input="$GIS_OPT_INPUT" output="$TMPMAP" layer="$GIS_OPT_LAYER" \
             column="$GIS_OPT_COLUMN"
   v.extract -d input="$TMPMAP" output="$GIS_OPT_OUTPUT" type=area \
             layer="$GIS_OPT_LAYER"
   cleanup
fi


# write cmd history:
v.support "$GIS_OPT_OUTPUT" cmdhist="${CMDLINE}"

exit 0

