#!/bin/sh

# Markus Neteler 2006
# SHAPE -> GPX
# needs: http://www.gpsbabel.org
#
# This script is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details (http://grass.itc.it/grass62/source/COPYING)


usage(){
 echo ""
 echo "SHAPE -> GPX for GPS upload (e.g. via QGIS)"
 echo "If there's a column called 'NAME' in the shapefile, it'll  "
 echo "automatically be used as the description for the waypoints."
 echo ""
 echo "Usage:"
 echo "       $0 shapefile [namecolumn]"
}

if [ $# -lt 1 -o "$1" = "-h" -o "$1" = "help" -o "$1" = "-help" ] ; then
	usage
	exit 1
fi

#### check if we have awk
if [ ! -x "`which gpsbabel`" ] ; then
	echo "$PROG: gpsbabel required, please install first (http://www.gpsbabel.org)" 1>&2
	exit 1
fi

SHAPE=$1
OUTGPX=`basename $SHAPE .shp`.gpx
if [ $# -eq 2 ] ; then
	echo "Using $2 as NAMECOL"
	NAMECOL=",name=$2"
else
	usage
	exit 1
fi


gpsbabel -i shape$NAMECOL -f $SHAPE -o gpx -F $OUTGPX
if [ $? -ne 0 ] ; then
	echo "An error occured"
	exit 1
fi
echo "Written $OUTGPX"


