#!/bin/sh

##############################################################################
# d.rast.leg	(GRASS Shell Script)
#
# displays a raster map and its legend on a graphics window. 
#
# Usage: 	d.rast.leg
#	or	d.rast.leg help
#	or	d.rast.leg rast_map [num_of_lines]
#
# Description:	d.rast.leg clears the entire screen, divides it into a main
#		(left) and a minor (right) frames, and then display a raster 
#		map in the main frame and the map legend in the minor frame.
#		The user can run the program interactively or 
#		non-interactively.
#
# Parameters: 	rast_map 	A raster map to be displayed.
#
#	 	num_of_lines 	Number of lines to appear in the legend. 
#				If this number is not given, the legend frame 
#				will display as many lines as number of 
#				categories in the map, otherwise, it will
#				display the first num_of_lines minus 1  
#				categories with the rest being truncated. 
# 
# Note:		The user may adjust the num_of_lines parameter or the size of 
#		graphics window to get an appropriate result.
#
# See also:	d.rast, d.legend.
#
# Jianping Xu and Scott Madry, Rutgers University. October 19, 1993
# Markus Neteler 8/2002: added simple d.legend logic
# Markus Neteler 10/2003: added g.parser
# Michael Barton 12/2004: remove reference to (null)
##############################################################################

#%Module
#%  description: Displays a raster map and its legend on a graphics window
#%  keywords: display
#%End
#%flag
#%  key: f
#%  description: Flip legend
#%end
#%flag
#%  key: n
#%  description: Omit entries with missing label
#%end
#%flag
#%  key: s
#%  description: Draw smooth gradient
#%end
#%option
#% key: map
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of raster map to display
#% required : yes
#%end
#%option
#% key: num_of_lines
#% type: integer
#% description: Number of lines to appear in the legend
#% required : no
#%end
#%option
#% key: rast
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of raster map to generate legend from
#% required : no
#%end
#%option
#% key: title
#% type: string
#% description: Name of raster map to print in legend
#% required : no
#%end
#%option
#% key: position
#% type: double
#% description: Position of vertical map-legend separator (in percent)
#% answer: 80
#% required : no
#%end

if test "$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_N -eq 1 ] ; then
  OMIT="-n"
else
  OMIT=""
fi

if [ $GIS_FLAG_F -eq 1 ] ; then
  FLIP="-f"
fi

if [ $GIS_FLAG_S -eq 1 ] ; then
  SMOOTH="-s"
else
  SMOOTH=""
fi

if [ ! -z "$GIS_OPT_TITLE" ] ; then
   MAPNAME="$GIS_OPT_TITLE"
else
   MAPNAME="$GIS_OPT_MAP"
fi

#for -n flag of d.legend
eval `g.findfile el=cell file="$GIS_OPT_MAP"`
if [ ! "$file" ] ; then
   g.message -e "Raster map '$GIS_OPT_MAP' not found in mapset search path"
   exit 1
fi

eval `r.info -t "$GIS_OPT_MAP"`

# for rast=
if [ -n "$GIS_OPT_RAST" ] ; then
    eval `g.findfile el=cell file="$GIS_OPT_RAST"`
    if [ ! "$file" ] ; then
	g.message -e "Raster map '$GIS_OPT_RAST' not found in mapset search path"
	exit 1
    else
	eval `r.info -t "$GIS_OPT_RAST"`
    fi

    eval `g.findfile ele=cell_misc file="$GIS_OPT_RAST" | grep '^file='`
    HSTGRM_DIR="$file"
else
    eval `g.findfile ele=cell_misc file="$GIS_OPT_MAP" | grep '^file='`
    HSTGRM_DIR="$file"
fi


# save defined font setting
FONTCMD=`d.frame -l | grep d.font | sed 's+"++1'| sed 's+"++1'`
d.frame -e

# restore font setting
$FONTCMD

XPOS=$GIS_OPT_POSITION

#at=bottom,top,left,right
if [ "$datatype" = "CELL" ] ; then
    # using at= turns off automatic font resize to fit in frame
    LEG_AT=""
else
    LEG_AT="at=7,93,3,18"
fi


if [ -z "$GIS_OPT_NUM_OF_LINES" ] ; then
       #draw title
	d.frame -s at=90,100,$XPOS,100  frame=title
	echo "$MAPNAME" | d.text col=black size=30

       #draw legend
    	d.frame -s at=0,90,$XPOS,100 frame=right
	if [ -z "$GIS_OPT_RAST" ] ; then
		g.message -d "No lines; own legend"
		# HB: I'm not sure this works as written. e.g. try erode.index@spearfish
		# would it be better to search for cats/ file ??
		if test -f "$HSTGRM_DIR/histogram" ; then
	  	     d.legend $FLIP $SMOOTH -n map="$GIS_OPT_MAP" $LEG_AT
        	else
	             d.legend $FLIP $SMOOTH $OMIT map="$GIS_OPT_MAP" $LEG_AT
        	fi
	else
		g.message -d "No lines; external legend"
		if test -f "$HSTGRM_DIR/histogram" ; then
		     d.legend $FLIP $SMOOTH -n map="$GIS_OPT_RAST" $LEG_AT
		else
		     d.legend $FLIP $SMOOTH $OMIT map="$GIS_OPT_RAST" $LEG_AT
		fi
	fi

       #draw map
	d.frame -s at=0,100,0,$XPOS frame=left
    	d.rast map=$GIS_OPT_MAP

else

       #draw title
	d.frame -s at=90,100,$XPOS,100 frame=title
	echo "$MAPNAME" | d.text col=black size=30

       #draw legend
    	d.frame -s at=0,90,$XPOS,100 frame=right
	if [ -z "$GIS_OPT_RAST" ] ; then
		g.message -d message="Lines=$GIS_OPT_NUM_OF_LINES; own legend"
		if test -f "$HSTGRM_DIR/histogram" ; then
	             d.legend $FLIP -n map="$GIS_OPT_MAP" \
			lines="$GIS_OPT_NUM_OF_LINES" $LEG_AT
        	else
	             d.legend $FLIP $OMIT map="$GIS_OPT_MAP" $LEG_AT
        	fi
	else
		g.message -d message="Lines=$GIS_OPT_NUM_OF_LINES; external legend"
		if test -f "$HSTGRM_DIR/histogram" ; then
		     d.legend $FLIP -n map="$GIS_OPT_RAST" \
			lines="$GIS_OPT_NUM_OF_LINES" $LEG_AT
		else
		     d.legend $FLIP $OMIT map="$GIS_OPT_RAST" $LEG_AT
		fi
	fi

       #draw map
	d.frame -s at=0,100,0,$XPOS frame=left
    	d.rast map="$GIS_OPT_MAP"
fi


#d.frame -s frame=full_screen
g.message "Clear screen with:"
g.message "d.erase -f"
