#!/bin/bash
############################################################################
#
# MODULE:	r.mapcalculator v.1.0 for GRASS 5.7 (2004/05/15)
#		based on mapcalculator.sh for GRASS 5
# AUTHOR(S):	R. Brunzema (r.brunzema@web.de); 
#		updated to GRASS 5.7 by Michael Barton (michael.barton@asu.edu)
# PURPOSE:	Provides GUI front-end to r.mapcalc
# COPYRIGHT:	(C) 2004 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: r.mapcalculator - Calculates new raster map from r.mapcalc expression
#%End
#%option
#% key: amap
#% type: string
#% gisprompt: old,cell,raster
#% description: A: 
#% required : no
#%end
#%option
#% key: bmap
#% type: string
#% gisprompt: old,cell,raster
#% description: B: 
#% required : no
#%end
#%option
#% key: cmap
#% type: string
#% gisprompt: old,cell,raster
#% description: C: 
#% required : no
#%end
#%option
#% key: dmap
#% type: string
#% gisprompt: old,cell,raster
#% description: D: 
#% required : no
#%end
#%option
#% key: emap
#% type: string
#% gisprompt: old,cell,raster
#% description: E: 
#% required : no
#%end
#%option
#% key: fmap
#% type: string
#% gisprompt: old,cell,raster
#% description: F: 
#% required : no
#%end
#%option
#% key: formula
#% type: string
#% description: Formula (e.g. A-B or A*C+B):
#% required : yes
#%end
#%option
#% key: outfile
#% type: string
#% gisprompt: new,cell,raster
#% description: Resulting output map 
#% required : yes
#%end
#%option
#% key: help
#% type: string
#% description: Show help 
#% options: -,help,man
#% answer: -
#% required : no
#%end
#%flag
#% key: e
#% description: Expert mode (enter a set of r.mapcalc expressions)
#%end
#%flag
#% key: o
#% description: Don't overwrite existing map 
#%end


display_help(){
	echo "" >&2
	echo "BRIEF HELP" >&2
	echo "" >&2
	echo "Enter an r.mapcalc expression in the" >&2
	echo "formula field using this format:" >&2
	echo "A+C or (more complex:) exp(A+C)+(B-2)*7" >&2
	echo "Where A, B, C are raster maps entered" >&2
	echo "in the A field, B field, and C field." >&2
	echo "" >&2
	echo "Do not enter output file in the formula field:" >&2
	echo "Correct: A+B" >&2
	echo "Incorrect: newfile = A+B" >&2
	echo "Use no blanks!" >&2
	echo "" >&2
	echo "For details on creating an r.mapcalc expression," >&2
	echo "select 'man' in the help field to see the r.mapcalc manual page" >&2
	echo "(or type g.manual r.mapcalc from the command line)." >&2
}

if  [ -z "$GISBASE" ]
then
	echo ""
	echo "You must be in GRASS GIS to run this program"
	echo ""
	exit 1
fi

if   [ "$1" != "@ARGS_PARSED@" ]
then
	exec g.parser "$0" "$@"
fi

case $GIS_OPT_HELP in
	help)
	display_help
	exit 0
	;;
	man)
	g.manual r.mapcalc
	exit 0
	;;
esac

# use old style mapcalc

if [ $GIS_FLAG_E = 1 ] ; then
	exec "$GISBASE/etc/grass-xterm-wrapper" -e r.mapcalc &
	exit 0
fi

	
# Check for required arguments
if [ -z "$GIS_OPT_FORMULA" ]; then
	echo "ERROR: Missing formula!"
	echo "Please enter a formula in the field formula"
	exit 1
fi

if [ -z "$GIS_OPT_OUTFILE" ]; then
	echo "ERROR: Missing name of outputfile!"
	echo "Please enter a name for the resulting map and try again."
	exit 1
elif [ $GIS_FLAG_O = 1 ]; then
		echo `g.list type=rast | grep -w "$GIS_OPT_OUTFILE" `
		outtest=`g.list type=rast | grep -w $GIS_OPT_OUTFILE`
		if [ -n "$outtest" ]; then
			echo "File $GIS_OPT_OUTFILE exists. Exiting."
			exit 0
		fi		
fi

typeset -i form_length=${#GIS_OPT_FORMULA}
typeset -i count=0

# Get the parts of the formula,
while [ $count -le $form_length ] 
	do
	compart[$count]="${GIS_OPT_FORMULA:$count:1}"
	# ... get the rasterfile names
	case ${compart[$count]} in
		A)
		# ... replace them
		compart[count]=" \"$GIS_OPT_AMAP\" "
		;;
		B)
		compart[count]=" \"$GIS_OPT_BMAP\" "
		;;
		C)
		compart[count]=" \"$GIS_OPT_CMAP\" "
		;;
		D)
		compart[count]=" \"$GIS_OPT_DMAP\" "
		;;
		E)
		compart[count]=" \"$GIS_OPT_EMAP\" "
		;;
		F)
		compart[count]=" \"$GIS_OPT_FMAP\" "
		;;
	esac
	# .. and put the formula together again
	command="$command${compart[$count]}"
	let count=count+1
done

echo r.mapcalc "$GIS_OPT_OUTFILE" ="($command)" # Show the resulting commandline

eval `r.mapcalc "$GIS_OPT_OUTFILE" ="($command)"` # Start the command

echo ""
echo ""
echo "Done"

# Check for errors
if [ $? -ne 0 ]; then
	echo "Error calculating $GIS_OPT_OUTFILE. Try expert mode."
fi

exit	
