#!/bin/sh

############################################################################
#
# MODULE:       d.slide.show 
# AUTHOR(S):    CERL, updated by Michael Barton
# PURPOSE:      Slide show of GRASS raster/vector maps
# 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: Slide show of GRASS raster/vector maps
#%  keywords: display, slideshow
#%End
#%flag
#%  key: v
#%  description: show vector maps rather than raster maps
#%END
#%option
#% key: prefix
#% type: string
#% description: map prefix. Specify character(s) to view selected maps only
#% required : no
#%end
#%option
#% key: across
#% type: integer
#% description: map number show across the monitor
#% gisprompt: 4
#% required : no
#%end
#%option
#% key: down
#% type: integer
#% description: map number show down the monitor
#% gisprompt: 3
#% required : no
#%end
#%option
#% key: mapsets
#% type: string
#% description: mapsets to use. Specify multiple mapsets comma separated
#% multiple : yes
#% required : no
#%end
#%option
#% key: delay
#% type: integer
#% description: number of seconds to pause between slides
#% options: 0-60
#% answer: 0
#% 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
     
# set defaults
DOWN=3
ACROSS=4
CWD=`pwd`

# in case of problems or user break:
trap 'cd $CWD ; d.frame -s full_screen ; exit 1' 2 3 15

GISDBASE=`g.gisenv GISDBASE`
LOCATION_NAME=`g.gisenv LOCATION_NAME`
MAPSET=`g.gisenv MAPSET`
LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"

# Get list of current mapsets
if [ -r $LOCATION/SEARCH_PATH ]
then
	MAPSETS=`cat $LOCATION/SEARCH_PATH | tr -s '\012' ' '`
else
	if [ "$MAPSET" = "PERMANENT" ]
	then
		MAPSETS="$MAPSET"
	else
		MAPSETS="$MAPSET PERMANENT"
	fi
fi

if [ -n "$GIS_OPT_PREFIX" ] ; then
   PREFIX=$GIS_OPT_PREFIX
fi
if [ -n "$GIS_OPT_DOWN" ] ; then
   DOWN=$GIS_OPT_DOWN
fi
if [ -n "$GIS_OPT_ACROSS" ] ; then
   ACROSS=$GIS_OPT_ACROSS
fi
if [ -n "$GIS_OPT_MAPSETS" ] ; then
   MAPSETS=`echo $GIS_OPT_MAPSETS | sed 's/,/ /g'`
fi

if [ $GIS_FLAG_V -eq 1 ] ; then
   SHOW_VECT=1
else
   SHOW_VECT=0
fi

# Check if mapset exists
mapsets=""
for mapset in $MAPSETS
do
	if [ -d $LOCATION/../$mapset ]
	then
		mapsets="$mapsets $mapset"
	fi
done
if [ "$mapsets" = "" ]
then
	echo "$MAPSETS: No such mapset(s) found" 1>&2
	exit 1
fi
MAPSETS=$mapsets

drawframes()
{
 d.frame -e
 if [ $? -ne 0 ] ; then
   echo "An error occured." 1>&2
   exit 1
 fi

 # figure height and widths of the windows
 avail_width=`expr 99 - $ACROSS`
 avail_height=`expr 99 - $DOWN`
 map_width=`expr $avail_width / $ACROSS`
 wind_height=`expr $avail_height / $DOWN`
 label_height=`expr $wind_height / 10`
 map_height=`expr $wind_height - $label_height`

 # generate the needed windows
 at_horiz=0
 left=1
 while [ $at_horiz -lt $ACROSS ]
 do
	at_vert=0
	top=99
	right=`expr $left + $map_width`
	while [ $at_vert -lt $DOWN ]
	do
		bottom=`expr $top - $map_height`
		d.frame -c map.$at_horiz.$at_vert at=$bottom,$top,$left,$right
		top=$bottom
		bottom=`expr $top - $label_height`
		d.frame -c lab.$at_horiz.$at_vert at=$bottom,$top,$left,$right
		at_vert=`expr $at_vert + 1`
		top=`expr $bottom - 1`
	done
	at_horiz=`expr $at_horiz + 1`
	left=`expr $right + 1`
 done
}

drawraster()
{
 for mapset in $MAPSETS
 do
	cd $LOCATION/../$mapset
	if [ ! -d cell ]
	then
		continue
	fi
	for i in MASK `ls cell/$PREFIX* 2> /dev/null`
	do
		i=`echo $i | sed 's/cell\///'`
		if [ ! $i = "MASK" ]
		then
			atnum=`expr $atnum % $totmaps`
			at_vert=`expr $atnum % $DOWN`
			at_hori=`expr $atnum / $DOWN`

			d.frame -s lab.$at_hori.$at_vert
			d.erase
			echo $i in $mapset | d.text size=80

			d.frame -s map.$at_hori.$at_vert
			d.erase
#			echo "[$i]"
			d.rast $i 2> /dev/null

			if [ "$GIS_OPT_DELAY" -gt 0 ] ; then
			    sleep "$GIS_OPT_DELAY"
			fi

			atnum=`expr $atnum + 1`
		fi
	done
 done
}


drawvector()
{
 for mapset in $MAPSETS
 do
	cd $LOCATION/../$mapset
	if [ ! -d vector ]
	then
		continue
	fi
	for i in `ls -d vector/$PREFIX* 2> /dev/null`
	do
		i=`echo $i | sed 's/^vector\///'`
		atnum=`expr $atnum % $totmaps`
		at_vert=`expr $atnum % $DOWN`
		at_hori=`expr $atnum / $DOWN`

		d.frame -s lab.$at_hori.$at_vert
		d.erase
		echo $i in $mapset | d.text size=80

		d.frame -s map.$at_hori.$at_vert
		d.erase
#		echo "[$i]"
		d.vect -c $i 2> /dev/null

		if [ "$GIS_OPT_DELAY" -gt 0 ] ; then
		    sleep "$GIS_OPT_DELAY"
		fi

		atnum=`expr $atnum + 1`
	done
 done
}


####################################
# main code

drawframes

# Draw the maps

atnum=0
totmaps=`expr $ACROSS \* $DOWN`

if [ $SHOW_VECT -eq 0 ] ; then
   echo "Displaying raster maps..." 1>&2
   drawraster
else
   echo "Displaying vector maps..." 1>&2
   drawvector
fi

cd $CWD
d.frame -s full_screen
