#!/bin/sh
#  r.pack   --  pack up a raster map:
#     collect raster map elements => gzip
#
#   (c) 2004-2013 GRASS Development Team
#   AUTHOR: Hamish Bowman, Otago University, New Zealand
#
#   This program is free software under the GNU General Public License
#   (>=v2). Read the file COPYING that comes with GRASS for details.
#

#%module
#% description: Packs up a raster map and support files for copying.
#% keywords: raster, copying
#%end
#%option
#% key: input
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of raster map to pack up
#% key_desc: name
#% required : yes
#%end
#%option
#% key: output
#% type: string
#% gisprompt: new_file,file,output
#% description: Name for output file (default is input-map.pack)
#% key_desc: name
#% 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

# check for tar  (is there a better way?)
if [ -z "`which tar`" ] ; then
   echo "ERROR: tar must be installed to use this program."
   exit 1
fi
# check for gzip
if [ -z "`which gzip`" ] ; then
   echo "ERROR: gzip must be installed to use this program."
   exit 1
fi

if [ -n "$GIS_OPT_OUTPUT" ] ; then
   outfile="$GIS_OPT_OUTPUT"
else
   outfile="`echo $GIS_OPT_INPUT | cut -d'@' -f1`.pack"
fi

#be overly helpful
outfile="`basename "$outfile" .pack`.pack"


eval `g.findfile elem=cell file="$GIS_OPT_INPUT"`
if [ -z "$name" ] ; then
   g.message -e "Map not found"
   exit 1
else
   # strip off mapset to remove fully qualified input
   name=`echo "$name" | cut -d'@' -f1`
fi


# create temporary directory to hold bits
TMP="`g.tempfile pid=$$`"
if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
   g.message -e "Unable to create temporary files"
fi
rm -f "$TMP"
mkdir "$TMP"
TMP_DIR="$TMP/$name"
mkdir "$TMP_DIR"

if [ ! -d "$TMP_DIR" ] ; then
   g.message -e "Unable to create temporary directory"
   exit 1
fi


BASEDIR=`echo "$file" | sed -e 's+/cell/.*$++'`
OLDDIR=`pwd`

for ELEMENT in cats cell cellhd colr fcell grid3 hist ; do
    if [ -e "$BASEDIR/$ELEMENT/$name" ] ; then
	cp "$BASEDIR/$ELEMENT/$name" "$TMP_DIR/$ELEMENT"
    fi
done

if [ -d "$BASEDIR/cell_misc/$name/" ] ; then
    mkdir "$TMP_DIR/cell_misc/"
    cp "$BASEDIR/cell_misc/$name/"* "$TMP_DIR/cell_misc/"
fi


if [ -z "`ls $TMP_DIR`" ] ; then
   g.message -e "No map components found"
   exit 1
fi

# copy projection info
#   (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
LOCATION_NAME=`g.gisenv get=LOCATION_NAME`
GISDBASE=`g.gisenv get=GISDBASE`

for SUPPORT in INFO UNITS EPSG ; do
   if [ -e "$GISDBASE/$LOCATION_NAME/PERMANENT/PROJ_$SUPPORT" ] ; then
       cp "$GISDBASE/$LOCATION_NAME/PERMANENT/PROJ_$SUPPORT" "$TMP_DIR"
   fi
done


# pack it all up
cd "$TMP"
tar czf "$outfile" *

# clean up
mv "$outfile" "$OLDDIR/"
if [ $? -ne 0 ] ; then
   g.message -e "Saving file to <$OLDDIR/$outfile>"
   rm -rf "$TMP"
   exit 1
fi

cd "$OLD_DIR"
rm -rf "$TMP"

g.message -v "Done. Map saved to <$OLDDIR/$outfile>"
exit 0
