#!/bin/sh

############################################################################
#
# MODULE:       v.in.e00
#
# AUTHOR(S):    Markus Neteler, Otto Dassau
#
# PURPOSE:      Import E00 data into a GRASS vector map
#		Imports single and split E00 files (.e00, .e01, .e02 ...)
#
# COPYRIGHT:    (c) 2004, 2005 GDF Hannover bR, http://www.gdf-hannover.de
#
#               This program is free software under the GNU General Public
#               License (>=v2). Read the file COPYING that comes with GRASS
#               for details.
#
#############################################################################
#
# REQUIREMENTS:
#      -  avcimport: http://avce00.maptools.org

#%Module
#%  description: Import of E00 file into a vector map.
#%  keywords: vector, import
#%End
#%flag
#%  key: v
#%  description: verbose mode
#%end
#%option
#% key: file
#% type: string
#% description: E00 file
#% gisprompt: old_file,file,input
#% required : yes
#%end
#%option
#% key: type
#% type: string
#% options: point,line,area
#% description: Input type point, line or area
#% required : yes
#%end
#%option
#% key: vect
#% type: string
#% gisprompt: new,vector,vector
#% description: name for new vector map
#% required : no
#%end

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

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

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

E00TMP=$$

#### check for avcimport 
if [ ! -x "`which avcimport`" ] ; then
    echo "$PROG: 'avcimport' program not found, install it first" 2>&1
    echo "       http://avce00.maptools.org" 2>&1
    exit 1
fi

#### check for e00conv
if [ ! -x "`which e00conv`" ] ; then
    echo "$PROG: 'e00conv' program not found, install it first" 2>&1
    echo "       http://avce00.maptools.org" 2>&1
    exit 1
fi

#### trap ctrl-c so that we can clean up tmp
#### take care to define ${TMP}:
#trap 'rm -f ${TMP}' 2 3 15

# check that the user didn't use all three, which gets past the parser.
if [ "$GIS_OPT_TYPE" != "point" ] && \
   [ "$GIS_OPT_TYPE" != "line" ] && \
   [ "$GIS_OPT_TYPE" != "area" ] ; then
    echo "ERROR: Must specify one of \"point\", \"line\", or \"area\"." 2>&1
    exit 1
fi

if [ -n "$GIS_OPT_FILE" ] ; then
    E00NAME=`basename $GIS_OPT_FILE .e00`
    # to avoid CAPs problem:
    E00NAME=`basename $E00NAME .E00`
    # avcimport only accepts 13 chars:
    E00SHORTNAME="`echo $E00NAME | cut -b1-13`"
fi

#check if this is a split E00 file (.e01, .e02 ...):
MERGING=0
if test -f $E00NAME.e01 ; then
  echo "Found that E00 file is split into pieces (.e01, ...). Merging..."
  MERGING=1
  EXTENSION="NOCAPS" # .e00 .e01 ...
fi
if test -f $E00NAME.E01 ; then
  echo "Found that E00 file is split into pieces (.e01, ...). Merging..."
  MERGING=1
  EXTENSION="CAPS" # .E00 .E01 ...
fi


if [ -z "$GIS_OPT_TYPE" ] ; then
    echo "Input type is missing - choose point, line or area"
    exit 1
fi

if [ -n "$GIS_OPT_VECT" ] ; then
    NAME="$GIS_OPT_VECT"
else
    NAME=$E00NAME
fi

### do import

#make a temporary directory
TMPDIR="`g.tempfile pid=$$`"
if [ $? -ne 0 ] || [ -z "$TMPDIR" ] ; then
    echo "ERROR: unable to create temporary files" 1>&2
    exit 1
fi

rm -f "$TMPDIR"
mkdir "$TMPDIR"

install -m 644 $E00NAME.e* $E00NAME.E* "$TMPDIR" 2>/dev/null

#change to temporary directory to later avoid removal problems (rm -r ...)
cd "$TMPDIR"

#check for binay E00 file (we can just check if import fails):
#avcimport doesn't set exist status :-(

if [ $MERGING -eq 1 ] ; then
  if  [ "$EXTENSION" = "CAPS" ] ; then
     cat `ls -1 $E00NAME.E* | sort` > $E00NAME.cat.$E00TMP.e00
     GIS_OPT_FILE=$E00NAME.cat.$E00TMP.e00
  fi
  if  [ "$EXTENSION" = "NOCAPS" ] ; then
     cat `ls -1 $E00NAME.e* | sort` > $E00NAME.cat.$E00TMP.e00
     GIS_OPT_FILE=$E00NAME.cat.$E00TMP.e00
  fi
fi

echo "An error may appear next which will be ignored..."
avcimport $GIS_OPT_FILE $E00SHORTNAME 2>/dev/null >/dev/null
if [ $? -eq 1 ] ; then
   echo "E00 ASCII found and converted to Arc Coverage in current directory"
else
   echo "E00 Compressed ASCII found. Will uncompress first..."
   rm -rf $E00SHORTNAME info
   e00conv $GIS_OPT_FILE $E00TMP.e00
   echo "...converted to Arc Coverage in current directory"
   avcimport $E00TMP.e00 $E00SHORTNAME 2>/dev/null
fi

## let's import...
## point

#SQL name fix:
NAME=`echo $NAME | sed 's+-+_+g'`

if [ "$GIS_OPT_TYPE" = "point" ] ; then
        echo "Importing points..."
        v.in.ogr -o dsn=$E00SHORTNAME layer=LAB type=point output=$NAME
	if [ $? -eq 1 ] ; then
   	echo "An error occurred. Stop."
   	rm -rf $E00TMP.e00 $E00SHORTNAME info 2>&1 > /dev/null
   	exit 1
	fi
fi

## line

if [ "$GIS_OPT_TYPE" = "line" ] ; then
        echo "Importing lines..."
        v.in.ogr -o dsn=$E00SHORTNAME layer=ARC type=line output=$NAME
	if [ $? -eq 1 ] ; then
   	echo "An error occurred. Stop."
   	rm -rf $E00TMP.e00 $E00SHORTNAME info 2>&1 > /dev/null
   	exit 1
	fi
fi

## area


if [ "$GIS_OPT_TYPE" = "area" ] ; then
        echo "Importing areas..."
        # we must reconstruct topology and cleanup polygons:
        v.in.ogr -o dsn=$E00SHORTNAME layer=LAB,ARC type=centroid,boundary output=$NAME
	if [ $? -eq 1 ] ; then
   	echo "An error occurred. Stop."
   	rm -rf $E00TMP.e00 $E00SHORTNAME info 2>&1 > /dev/null
   	exit 1
	fi
fi

echo ""
echo "Imported $GIS_OPT_TYPE vector map <$NAME>."

#### clean up the mess
rm -f "$TMPDIR"/$E00NAME.e* "$TMPDIR"/$E00NAME.E*
rm -rf $E00TMP.e00 $E00SHORTNAME info $E00NAME.cat.$E00TMP.e00 2>&1 > /dev/null
cd ..
rmdir "$TMPDIR"

#### end
echo 2>&1
echo "Done." 2>&1

exit 0
