#!/bin/sh
############################################################################
#
# MODULE:	d.mvmon
# AUTHOR(S):	Huidae Cho - grass4u@gmail.com
# PURPOSE:	To move current displayed maps to another monitor
# COPYRIGHT:	(C) 2005 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: Moves displayed maps to another monitor
#%  keywords: display, setup
#%End
#%flag
#% key: c
#% description: clear target monitor before moving
#%end
#%flag
#% key: s
#% description: stay with source monitor
#%end
#%flag
#% key: k
#% description: kill source monitor after moving
#%end
#%option
#% key: to
#% type: string
#% required: yes
#% description: Target monitor
#%end
#%option
#% key: from
#% type: string
#% required: no
#% description: Source monitor
#%end

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

to=$GIS_OPT_TO

if [ -n "$GIS_OPT_FROM" ]
then
	from=$GIS_OPT_FROM
else
	from=`d.mon -p | sed 's/^.*: *//'`
fi

if [ "$from" = "$to" ]
then
        g.message -e "Target monitor cannot be the same as source monitor" 
	exit 1
fi

if ! d.mon -L | grep "^$from .*  running" > /dev/null 2>&1
then
        g.message -e "$from: No such monitor or not running" 
	exit 1
fi

if ! d.mon -l | grep "^$to " > /dev/null 2>&1
then
        g.message -e "$to: No such monitor" 
	exit 1
fi

d.mon select=$from
dsave=`d.save -o`
if d.mon -L | grep "^$to .* not running" > /dev/null 2>&1
then
	d.mon start=$to
else
	d.mon select=$to
	if [ $GIS_FLAG_C -eq 1 ]
	then
		d.erase -x
	fi
fi

eval "$dsave"

if [ $GIS_FLAG_S -eq 1 ]
then
	d.mon select=$from
elif [ $GIS_FLAG_K -eq 1 ]
then
	d.mon stop=$from
fi

exit 0
