#!/bin/sh
#
############################################################################
#
# MODULE:       r.mask
# AUTHOR(S):	Michael Barton, Arizona State University
#               Markus Neteler
# PURPOSE:	    Facilitates creation of raster MASK using r.reclass
# COPYRIGHT:	(C) 2005-2013 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: Creates 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: no
#% guisection: Create
#%end
#%option
#% key: maskcats
#% type: string
#% description: Category values to use for MASK (format: 1 2 3 thru 7 *)
#% answer: *
#% guisection: Create
#%end
#%flag
#% key: i
#% description: Create inverse MASK from specified 'maskcats' list
#% guisection: Create
#%end
#%flag
#% key: o
#% description: Overwrite existing MASK
#%end
#%flag
#% key: r
#% description: Remove existing MASK (overrides other options)
#% guisection: Remove
#%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 [ -n "$GRASS_OVERWRITE" ] && [ "$GRASS_OVERWRITE" -eq 1 ] ; then
   GIS_FLAG_O=1
fi

if [ -z "$GRASS_VERBOSE" ] || [ "$GRASS_VERBOSE" -le 2 ] ; then
  BEQUIET="--quiet"
else
  BEQUIET=""
fi

# temp file
TEMPRAST="r_mask_$$"

cleanup()
{
 g.remove --q rast="$TEMPRAST"
}

# what to do in case of user break:
exitprocedure()
{
 g.message -e 'User break!'
 cleanup
 exit 1
}

# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 15

# if map is given check if existing
if [ -n "$GIS_OPT_INPUT" ]; then
   eval `g.findfile element=cell file="$GIS_OPT_INPUT"`
   filename="$fullname"
   if [ -z "$filename" ] ; then
      g.message -e "Raster map <$GIS_OPT_INPUT> not found"
      exit 1
   fi
fi

if [ "$GIS_FLAG_R" -eq 0 ] ; then
   if [ -n "$GIS_OPT_MASKCATS" -a "$GIS_OPT_MASKCATS" != '*' ] ; then
      eval `r.info -t $GIS_OPT_INPUT`
      if [ "$datatype" != "CELL" ] ; then
         g.message -e "Raster map <$GIS_OPT_INPUT> must be integer for maskcats parameter"
         exit 1
      fi
   fi
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
 		g.message $BEQUIET "Raster MASK removed"
 	else
 		g.message $BEQUIET -w "No existing MASK to remove"
	fi
else
	if [ -z "$GIS_OPT_INPUT" ]; then
		g.message $BEQUIET -w "No raster map for MASK provided"
		exit 1;
	fi
	if [ "$GIS_FLAG_O" -eq 1 ]; then
		echo "$GIS_OPT_MASKCATS = 1" | r.reclass input="$GIS_OPT_INPUT" output=MASK --o
	else 
		# does map exist in CURRENT mapset?
		eval `g.findfile element=cell file=MASK mapset="$MAPSET"`
		if [ -n "$file" ] ; then
		   g.message -e "MASK already found in current mapset. Delete first or overwrite"
		   exit 1
		fi

		echo "$GIS_OPT_MASKCATS = 1" | r.reclass input="$GIS_OPT_INPUT" output=MASK
	fi

        if [ "$GIS_FLAG_I" -eq 1 ]; then
		g.rename --q rast=MASK,"$TEMPRAST"
		r.mapcalc "MASK = if(isnull($TEMPRAST),1,null())"
		g.remove --q rast="$TEMPRAST"
		g.message $BEQUIET "Inverted MASK created. All subsequent raster operations"
	else
	  g.message $BEQUIET "MASK created. All subsequent raster operations"
	fi
        g.message $BEQUIET "will be limited to MASK area"
        g.message $BEQUIET "Removing or renaming raster file named MASK will"
        g.message $BEQUIET "restore raster operations to normal"
fi

exit 0
