#!/bin/sh
#
############################################################################
#
# MODULE:	v.convert.all
# AUTHOR(S):	MN 
# PURPOSE:	converts all old GRASS < V5.7 vector maps to current format
#		in current mapset
# COPYRIGHT:	(C) 2004 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: Converts all older versions of GRASS vector maps in current mapset to current format.
#%  keywords: vector, import
#%end
#%flag
#%  key: r
#%  description: run non-interactively
#%end

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

# the -r flag is a noop, just there to stop the gui coming up.
if [ "$1" != "@ARGS_PARSED@" ] ; then
    exec g.parser "$0" "$@"
fi


eval `g.gisenv`
: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"

CONVERTEDMAPS=0
for i in `g.mlist type=oldvect`
do

  # polish map names to be SQL compliant
  INPUT=$i
  OUTPUT=`echo "$INPUT" | sed 's+\.+_+g'`

  #do it:
  v.convert in="$INPUT" out="$OUTPUT"
  if [ $? -ne 0 ] ; then
    g.message -e "Converting map $INPUT to $OUTPUT" 
  else
    CONVERTEDMAPS=`expr $CONVERTEDMAPS + 1`
  fi
done

if [ "$CONVERTEDMAPS" -lt 1 ] ; then
  g.message -w "No vector maps converted as no old vector maps present in current mapset <$MAPSET>."
else
  g.message "Total $CONVERTEDMAPS number of vector maps in current mapset converted." 
  g.message "Please verify new vector map(s) before deleting old vector map(s)." 
fi

exit 0
