#!/bin/sh

############################################################################
#
# MODULE:       db.out.ogr
# AUTHOR(S):   	Markus Neteler
# PURPOSE:      exports attribute tables into various formats
# COPYRIGHT:    (C) 2007 by Markus Neteler 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: Exports attribute tables into various formats.
#%  keywords: database, attribute table
#%End

#%option
#% key: input
#% type: string
#% key_desc : name
#% description: GRASS table name
#% required : yes
#%end

#%option
#% key: dsn
#% type: string
#% key_desc : name
#% gisprompt: new_file,file,input
#% description: Table file to be exported or DB connection string
#% required : yes
#%end

#%option
#% key: format
#% type: string
#% description: Table format
#% required : yes
#% options: CSV,DBF,GML,MySQL,PostgreSQL,SQLite
#% answer: DBF
#%end

#%option
#% key: db_table
#% type: string
#% key_desc : name
#% description: Name for output table
#% required : no
#%end


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

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

PROG=`basename "$0"`

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


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

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

if [ "$GIS_OPT_FORMAT" = "DBF" -o  "$GIS_OPT_FORMAT" = "dbf" ] ; then
  GIS_OPT_FORMAT="ESRI_Shapefile"
fi

if [ "$GIS_OPT_FORMAT" = "CSV" ] ; then
  OLAYER="olayer=`basename "$GIS_OPT_DSN" .csv`"
fi

#is there a simpler way of testing for --overwrite?
if [ -e "$GIS_OPT_INPUT.dbf" ] ; then
   if [ -z "$GRASS_OVERWRITE" ] || [ "$GRASS_OVERWRITE" -ne 1 ] ; then
      g.message -e "File <$GIS_OPT_INPUT.dbf> already exists"
      exit 1
   fi
fi

if [ -n "$OLAYER" ] ; then
   v.out.ogr -c input="$GIS_OPT_INPUT" dsn="$GIS_OPT_DSN" \
      "$OLAYER" format="$GIS_OPT_FORMAT" type=point,line,area --quiet
else
   v.out.ogr -c input="$GIS_OPT_INPUT" dsn="$GIS_OPT_DSN" \
      format="$GIS_OPT_FORMAT" type=point,line,area --quiet
fi

if [ $? -ne 0 ] ;  then
   exit 1
fi

if [ "$GIS_OPT_FORMAT" = "ESRI_Shapefile" ] ; then
  echo "$GIS_OPT_DSN" | grep '\.dbf$' > /dev/null
  if [ $? -eq 0 ] ; then
    OUTNAME="`basename "$GIS_OPT_DSN" .dbf`"
    rm -f "$OUTNAME.shp" "$OUTNAME.shx" "$OUTNAME.prj"
    OUTNAME="$OUTNAME.dbf"
  else
    rm -f "$GIS_OPT_DSN/$GIS_OPT_INPUT.shp" \
          "$GIS_OPT_DSN/$GIS_OPT_INPUT.shx" \
          "$GIS_OPT_DSN/$GIS_OPT_INPUT.prj"
    OUTNAME="$GIS_OPT_DSN/$GIS_OPT_INPUT.dbf"
  fi
else
  if [ "$GIS_OPT_FORMAT" = "CSV" ] ; then
     OUTNAME="$GIS_OPT_DSN".csv
  else
     OUTNAME="$GIS_OPT_INPUT"
  fi
fi

g.message "Exported table <$OUTNAME>"
exit 0

