#!/bin/sh
############################################################################
#
# MODULE:       d.split.frame
# AUTHOR:       M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
#                 New Zealand
# PURPOSE:      Split the display into quarter frames, etc
#		  (This script is a replacement for d.split from GRASS 5,6)
#
# COPYRIGHT:    (c) 2007 Hamish Bowman, and the GRASS Development Team
#               This program is free software released to the public domain
#
#############################################################################

#%Module
#% description: Split the display into subframes.
#%End
#%option
#% key: frames
#% type: integer
#% description: Number of subframes
#% options: 2,4,6,8,9,12,16
#% answer: 4
#%end
#%flag
#% key: h
#% description: Split horizontally not vertically
#%end

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

# skip parser if no args are given
if [ "$#" -gt 0 ] ; then
  if [ "$1" != "@ARGS_PARSED@" ] ; then
    exec g.parser "$0" "$@"
  fi
else
  GIS_OPT_FRAMES=4
  GIS_FLAG_H=0
fi


if [ `d.mon -p | cut -f1 -d' '` != "Currently" ] ; then
   g.message -e "No monitor currently selected for output"
   exit 1
fi

d.erase -f
if [ $? -ne 0 ] ; then
   g.message -e "Error clearing display monitor"
   exit 1
fi


case "$GIS_OPT_FRAMES" in
   2) if [ $GIS_FLAG_H -eq 0 ] ; then
	#split vert
	d.frame -c frame=uno at=50,100,0,100
	d.frame -c frame=dos at=0,50,0,100
      else
	d.frame -c frame=uno at=0,100,0,50
	d.frame -c frame=dos at=0,100,50,100
      fi
     ;;
   4) d.frame -c frame=uno at=50,100,0,50
      d.frame -c frame=dos at=50,100,50,100
      d.frame -c frame=tres at=0,50,0,50
      d.frame -c frame=cuatro at=0,50,50,100
     ;;
   6) if [ $GIS_FLAG_H -eq 0 ] ; then
	#split vert
	d.frame -c frame=uno at=66.6667,100,0,50
	d.frame -c frame=dos at=66.6667,100,50,100
	d.frame -c frame=tres at=33.3333,66.6667,0,50
	d.frame -c frame=cuatro at=33.3333,66.6667,50,100
	d.frame -c frame=cinco at=0,33.3333,0,50
	d.frame -c frame=seis at=0,33.3333,50,100
      else
	d.frame -c frame=uno at=50,100,0,33.3333
	d.frame -c frame=dos at=50,100,33.3333,66.6667
	d.frame -c frame=tres at=50,100,66.6667,100
	d.frame -c frame=cuatro at=0,50,0,33.3333
	d.frame -c frame=cinco at=0,50,33.3333,66.6667
	d.frame -c frame=seis at=0,50,66.6667,100
      fi
     ;;
   8) if [ $GIS_FLAG_H -eq 0 ] ; then
	#split vert
	d.frame -c frame=uno at=75,100,0,50
	d.frame -c frame=dos at=75,100,50,100
	d.frame -c frame=tres at=50,75,0,50
	d.frame -c frame=cuatro at=50,75,50,100
	d.frame -c frame=cinco at=25,50,0,50
	d.frame -c frame=seis at=25,50,50,100
	d.frame -c frame=siete at=0,25,0,50
	d.frame -c frame=ocho at=0,25,50,100
      else
	d.frame -c frame=uno at=50,100,0,25
	d.frame -c frame=dos at=50,100,25,50
	d.frame -c frame=tres at=50,100,50,75
	d.frame -c frame=cuatro at=50,100,75,100
	d.frame -c frame=cinco at=0,50,0,25
	d.frame -c frame=seis at=0,50,25,50
	d.frame -c frame=siete at=0,50,50,75
	d.frame -c frame=ocho at=0,50,75,100
      fi
     ;;
   9) d.frame -c frame=uno at=66.6667,100,0,33.3333
      d.frame -c frame=dos at=66.6667,100,33.3333,66.6667
      d.frame -c frame=tres at=66.6667,100,66.6667,100
      d.frame -c frame=cuatro at=33.3333,66.6667,0,33.3333
      d.frame -c frame=cinco at=33.3333,66.6667,33.3333,66.6667
      d.frame -c frame=seis at=33.3333,66.6667,66.6667,100
      d.frame -c frame=siete at=0,33.3333,0,33.3333
      d.frame -c frame=ocho at=0,33.3333,33.3333,66.6667
      d.frame -c frame=nueve at=0,33.3333,66.6667,100
     ;;
   12) if [ $GIS_FLAG_H -eq 0 ] ; then
	#split vert
	d.frame -c frame=uno at=75,100,0,33.3333
	d.frame -c frame=dos at=75,100,33.3333,66.6667
	d.frame -c frame=tres at=75,100,66.6667,100
	d.frame -c frame=cuatro at=50,75,0,33.3333
	d.frame -c frame=cinco at=50,75,33.3333,66.6667
	d.frame -c frame=seis at=50,75,66.6667,100
	d.frame -c frame=siete at=25,50,0,33.3333
	d.frame -c frame=ocho at=25,50,33.3333,66.6667
	d.frame -c frame=nueve at=25,50,66.6667,100
	d.frame -c frame=diez at=0,25,0,33.3333
	d.frame -c frame=once at=0,25,33.3333,66.6667
	d.frame -c frame=doce at=0,25,66.6667,100
      else
      	d.frame -c frame=uno at=66.6667,100,0,25
	d.frame -c frame=dos at=66.6667,100,25,50
	d.frame -c frame=tres at=66.6667,100,50,75
	d.frame -c frame=cuatro at=66.6667,100,75,100
	d.frame -c frame=cinco at=33.3333,66.6667,0,25
	d.frame -c frame=seis at=33.3333,66.6667,25,50
	d.frame -c frame=siete at=33.3333,66.6667,50,75
	d.frame -c frame=ocho at=33.3333,66.6667,75,100
	d.frame -c frame=nueve at=0,33.3333,0,25
	d.frame -c frame=diez at=0,33.3333,25,50
	d.frame -c frame=once at=0,33.3333,50,75
	d.frame -c frame=doce at=0,33.3333,75,100
      fi
     ;;
   16) d.frame -c frame=uno at=75,100,0,25
       d.frame -c frame=dos at=75,100,25,50
       d.frame -c frame=tres at=75,100,50,75
       d.frame -c frame=cuatro at=75,100,75,100
       d.frame -c frame=cinco at=50,75,0,25
       d.frame -c frame=seis at=50,75,25,50
       d.frame -c frame=siete at=50,75,50,75
       d.frame -c frame=ocho at=50,75,75,100
       d.frame -c frame=nueve at=25,50,0,25
       d.frame -c frame=diez at=25,50,25,50
       d.frame -c frame=once at=25,50,50,75
       d.frame -c frame=doce at=25,50,75,100
       d.frame -c frame=trece at=0,25,0,25
       d.frame -c frame=catorce at=0,25,25,50
       d.frame -c frame=quince at=0,25,50,75
       d.frame -c frame=dieciseis at=0,25,75,100
     ;;
esac


d.frame -s uno

g.message "Use 'd.erase -f' to clear frames from display monitor."

exit
