#!/bin/sh
#
############################################################################
#
# MODULE:       v.db.addcolumn
# AUTHOR(S):   	Moritz Lennert 
# PURPOSE:      interface to db.execute to add a column to the attribute table
#               connected to a given vector 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: Adds one or more columns to the attribute table connected to a given vector map.
#%  keywords: vector, database, attribute table
#%End

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

#%option
#% key: layer
#% type: integer
#% gisprompt: old_layer,layer,layer
#% label: Layer number where to add column(s)
#% description: A single vector map can be connected to multiple database tables. This number determines which table to use.
#% answer: 1
#% required : no
#%end

#%option
#% key: columns
#% type: string
#% label: Name and type of the new column(s) ('name type [,name type, ...]')
#% description: Data types depend on database backend, but all support VARCHAR(), INT, DOUBLE PRECISION and DATE
#% required : yes
#%end


if  [ -z "$GISBASE" ] ; then
    echo "You must be in GRASS GIS to run this program." >&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

PROG=`basename "$0"`

#### check if we have awk
if [ ! -x "`which awk`" ] ; then
    g.message -e "awk required, please install awk or gawk first"
    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 ###
MAPSET=`g.gisenv get=MAPSET`

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

table=`v.db.connect "$GIS_OPT_MAP" -gl layer="$GIS_OPT_LAYER" fs="|" | cut -f2 -d'|'`
if [ -z "$table" ] ; then
   g.message -e 'There is no table connected to this map! Run v.db.connect or v.db.addtable first.'
   exit 1
fi

database=`v.db.connect "$GIS_OPT_MAP" -gl fs="|" layer="$GIS_OPT_LAYER" | cut -f4 -d'|'`
driver=`v.db.connect "$GIS_OPT_MAP" -gl fs="|" layer="$GIS_OPT_LAYER" | cut -f5 -d'|'`

colnum=`echo "$GIS_OPT_COLUMNS" | awk -F, '{print NF}'`

n=1
while [ "$n" -le "$colnum" ]
  do
    col=`echo "$GIS_OPT_COLUMNS" | cut -d',' -f$n`
    
    if [ -z "$col" ] ; then
      g.message -e 'There is an empty column! Did you leave a trailing comma?'
      exit 1
    fi

    echo "ALTER TABLE $table ADD COLUMN $col" | db.execute database="$database" driver="$driver"
    if [ $? -eq 1 ] ; then
      g.message -e "Cannot continue (problem adding column)."
      exit 1
    fi
    n=`expr $n + 1`
  done


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

exit 0

