#!/bin/sh
############################################################################
#
# MODULE:	d.monsize, v 1.6 for GRASS 6.0 (2004/12/07) 
#		based on d.monsize for GRASS 5
# AUTHOR(S):	Unknown; 
#		updated to GRASS 5.7 by Michael Barton (michael.barton@asu.edu)
# PURPOSE:	selects/starts specified monitor at specified window size
# 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.
#
#############################################################################


# NOTE: This is based on the output pattern of d.mon -p
# Anantha Prasad <aprasad/ne_de@fs.fed.us>
#
# Version 1: Author?. Tue, 25 Jan 2000 10:31:37


#%Module
#%  description: Selects/starts specified monitor at specified size
#%  keywords: display, setup
#%End
#%option
#% key: setmonitor
#% type: string
#% description: Display monitor to start
#% options: x0,x1,x2,x3,x4,x5,x6
#% required : yes
#%end
#%option
#% key: setwidth
#% type: integer
#% description: Width in pixels of new display monitor
#% required : yes
#%end
#%option
#% key: setheight
#% type: integer
#% description: Height in pixels of new display monitor
#% required : yes
#%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

GRASS_WIDTH=$GIS_OPT_SETWIDTH
export GRASS_WIDTH
GRASS_HEIGHT=$GIS_OPT_SETHEIGHT
export GRASS_HEIGHT


# Check the current status of the monitor
st1=`d.mon -p| sed s/'No monitor currently selected for output'//`
st2=`echo $st1 | cut -d ":" -f2`

# STrip leading blanks
st3=`echo $st2|perl -pne 's/^\s+//g'`

if [ -z "$st3" ]
then
  # No monitor running so start a new one....
  echo
  echo "No monitor selected"
  echo "Starting monitor $GIS_OPT_SETMONITOR of size ${GIS_OPT_SETWIDTH}X${GIS_OPT_SETHEIGHT} ..."
  d.mon start=$GIS_OPT_SETMONITOR
else
  if [ "$GIS_OPT_SETMONITOR" = "$st3" ]
  then
    # Monitor is already running...stop it and start new one
    d.mon stop=$GIS_OPT_SETMONITOR
    echo
    echo "Monitor already running and selected"
    echo "Restarting monitor $GIS_OPT_SETMONITORAT size ${GIS_OPT_SETWIDTH}X${GIS_OPT_SETHEIGHT} ..."
    sleep 4
    d.mon start=$GIS_OPT_SETMONITOR
  else
    echo
    echo "A different monitor is selected"
    echo "Selecting and restarting monitor ${GIS_OPT_SETWIDTH}X${GIS_OPT_SETHEIGHT} ..."
    #d.mon select=$GIS_OPT_SETMONITOR
    d.mon stop=$GIS_OPT_SETMONITOR
    sleep 4
    d.mon start=$GIS_OPT_SETMONITOR
  fi
fi
