#!/bin/sh
#
############################################################################
#
# MODULE:       r.mask
# AUTHOR(S):	Michael Barton, Arizona State University
# PURPOSE:	    Facilitates creation of raster MASK using r.reclass
# COPYRIGHT:	(C) 2005 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: Create a MASK for limiting raster operation
#%  keywords: raster, mask
#%End
#%option
#% key: input
#% type: string
#% gisprompt: old,cell,raster
#% description: Raster map to use as MASK
#% required : yes
#%END
#%option
#% key: maskcats
#% type: string
#% description: Category values to use for MASK (format: 1 2 3 thru 7 *)
#% answer: *
#%END
#%flag
#% key: o
#% description: Overwrite existing MASK
#%END
#%flag
#% key: r
#% description: Remove existing MASK (overrides other options)
#%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

if [ "$GIS_FLAG_R" -eq 1 ]; then
	if test -f `g.gisenv GISDBASE`/`g.gisenv LOCATION_NAME`/`g.gisenv MAPSET`/cell/MASK ; then
 		g.remove rast=MASK
 		echo " "
		echo "*************************************************"
 		echo "Raster MASK removed"
		echo "*************************************************"
 	else
 		echo " "
		echo "*************************************************"
 		echo "No existing MASK to remove"
		echo "*************************************************"
	fi
else

 	echo " "
	echo "*************************************************"
	echo "MASK created. All subsequent raster operations" 
	echo "will be limited to MASK area"
	echo "Removing or renaming raster file named MASK will"
	echo "restore raster operations to normal"
	echo "*************************************************"

	if [ "$GIS_FLAG_O" -eq 1 ]; then
		echo "$GIS_OPT_MASKCATS = 1" | r.reclass input=$GIS_OPT_INPUT output=MASK --o
	else 
		echo "$GIS_OPT_MASKCATS = 1" | r.reclass input=$GIS_OPT_INPUT output=MASK
	fi
fi
