#!/bin/sh
#
############################################################################
#
# MODULE:       v.db.update
# AUTHOR(S):   	Moritz Lennert 
# PURPOSE:      interface to db.execute to update a column in the attribute table connected to a given map
# COPYRIGHT:    (C) 2005 by 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: Allows to assign a new value to a column in the attribute table connected to a given map
#%  keywords: vector, database, attribute table
#%End

#%option
#% key: map
#% type: string
#% gisprompt: old,vector,vector
#% description: Vector map for which to edit attribute table
#% required : yes
#%end

#%option
#% key: layer
#% type: integer
#% description: Layer where to add column
#% answer: 1
#% required : no
#%end

#%option
#% key: column
#% type: string
#% description: Column to update
#% required : yes
#%end

#%option
#% key: value
#% type: string
#% description: New value to assign to the column (varchar values have to be in single quotes, ex: 'grass')
#% required : yes
#%end

#%option
#% key: where
#% type: string
#% description: Condition for update (everything after the 'where' sql statement; ex: cat=1 or col1/col2>1) 
#% required : no
#%end

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

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

PROG=`basename $0`

#### check if we have awk
if [ ! -x "`which awk`" ] ; then
    echo "$PROG: awk required, please install awk or gawk first" 1>&2
    exit 1
fi

# setting environment, so that awk works properly in all languages
unset LC_ALL
LC_NUMERIC=C
export LC_NUMERIC

### setup enviro vars ###
eval `g.gisenv`
: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}

# does map exist in CURRENT mapset?
eval `g.findfile element=vector file=$GIS_OPT_MAP mapset=$MAPSET`
if [ ! "$file" ] ; then
   echo "Vector map '$GIS_OPT_MAP' not found in mapset search path"
   exit 1
fi

table=`v.db.connect $GIS_OPT_MAP -g | grep -w $GIS_OPT_LAYER | awk '{print $2}'`
if [ -z "$table" ] ; then
   echo 'ERROR: There is no table connected to this map!'
   echo "Run v.db.connect or v.db.addtable first."
   exit 1
fi
database=`v.db.connect $GIS_OPT_MAP -g | grep -w $GIS_OPT_LAYER | awk '{print $4}'`
driver=`v.db.connect $GIS_OPT_MAP -g | grep -w $GIS_OPT_LAYER | awk '{print $5}'`

# checking column types
coltype=`v.info -c map=$GIS_OPT_MAP | grep -w $GIS_OPT_COLUMN | cut -d'|' -f1`
# we insert a value
if [ "$coltype" = "INTEGER" -o "$coltype" = "DOUBLE PRECISION" ] ; then
    value=$GIS_OPT_VALUE
else
    value=\'$GIS_OPT_VALUE\'
fi

if [ -n "$GIS_OPT_WHERE" ]
  then
       echo "UPDATE $table SET $GIS_OPT_COLUMN=$value WHERE $GIS_OPT_WHERE" | db.execute database=$database driver=$driver
else
       echo "UPDATE $table SET $GIS_OPT_COLUMN=$value" | db.execute database=$database driver=$driver
fi

exit 0
