#!/bin/sh
#
############################################################################
#
# MODULE:       v.db.droptable
# AUTHOR(S):   	Markus Neteler 
# PURPOSE:      interface to db.execute to drop an existing table of 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: removes existing attribute table of a vector map
#%  keywords: vector, database, attribute table
#%End

#%flag
#%  key: f
#%  description: force removal (don't ask)
#%end
#%option
#% key: map
#% type: string
#% gisprompt: old,vector,vector
#% description: vector map from which to remove attribute table
#% required : yes
#%end

#%option
#% key: table
#% type: string
#% description: name of existing attribute table (default: vector map name)
#% required : no
#%end

#%option
#% key: layer
#% type: integer
#% description: Layer from which to drop linked attribute table
#% 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

PROG=`basename $0`

#### check if we have awk
if [ ! -x "`which awk`" ] ; then
    echo "$PROG: awk required, please install awk/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

# do some paranoia tests as well:
if [ -z "$GIS_OPT_TABLE" ] ; then
    table=`v.db.connect $GIS_OPT_MAP -g`
    if [ $? -ne 0 ] ; then
       echo "An error occured. Stop." 1>&2
       exit 1
    fi
    table=`v.db.connect $GIS_OPT_MAP -g | grep -w $GIS_OPT_LAYER | awk '{print $2}'`
    if [ -z $table ] ; then
       echo "No table assigned to layer <$GIS_OPT_LAYER>" 1>&2
       exit 1
    fi
    echo "Removing following table name connected to selected layer: ${GIS_OPT_MAP}" 1>&2
else
    echo "Removing user specified table name: $GIS_OPT_TABLE" 1>&2
    existingtable=`v.db.connect $GIS_OPT_MAP -g | grep -w $GIS_OPT_LAYER | awk '{print $2}'`
    table=$GIS_OPT_TABLE
    if [ "$existingtable" != "$table" ] ; then
       echo "User selected table <$table> but the table <$existingtable> is linked to layer <$GIS_OPT_LAYER>" 1>&2
       exit 1
    fi		  
fi

# we use the DB settings selected layer 
database=`v.db.connect $GIS_OPT_MAP -g`
if [ $? -ne 0 ] ; then
   echo "An error occured. Stop." 1>&2
   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}'`

echo "Removing table <$table> linked to layer <$GIS_OPT_LAYER> of vector map <$GIS_OPT_MAP>" 1>&2

# this may not work in GUI mode:
if [ $GIS_FLAG_f -eq 0 ] ; then
  #better?  read -p "Are you sure (y/n)? [n] " ans
  echo "Are you sure (y/n)? [n]"
  read ans

else
  ans="y"
fi

if [ "$ans" = "y" -o "$ans" = "Y" ] ; then
   echo "Dropping table <$table>..." 1>&2
   echo "DROP TABLE $table" | db.execute
   if [ $? -ne 0 ] ; then
      echo "An error occured. Stop." 1>&2
      exit 1
   fi

   v.db.connect -d map=$GIS_OPT_MAP layer=$GIS_OPT_LAYER

   echo 1>&2
   echo "Current attribute table link(s):" 1>&2
   v.db.connect -p $GIS_OPT_MAP
   if [ $? -ne 0 ] ; then
      echo "       (No database links remaining)" 1>&2
   fi

   exit 0
else
   echo "Leaving map/table unchanged." 1>&2
   exit 1
fi
