#!/bin/sh

# g.pushregion.sh - save the current region
# 
#   tom poindexter, march 2000
#

#### Check that user is running GRASS
if test "$GISRC" = ""
then
    echo "$0: You must be running GRASS to execute $0" >&2
    exit 1
fi

#### Check GRASS variables are set
eval `g.gisenv`

: $GISDBASE $LOCATION

# check that a WIND file exists

if [ ! -f $LOCATION/WIND ] ; then
    echo "$0: No current region set (missing $LOCATION/WIND)"
    exit 1
fi

# check that a windows directory exists

if [ ! -d $LOCATION/windows ] ; then
    mkdir $LOCATION/windows
fi

cd $LOCATION/windows

# get last pushed file, if any.  relies on "ls -r" to list file in order
# of newest to oldest

old=`( ls -r _PUSH_POP_.[0-9] 2>/dev/null ) | head -1`

if [ -z "$old" ] ; then
    seq=0
else
    SAVEIFS="$IFS"
    IFS='.'
    set $old
    IFS="$SAVEIFS"
    seq=$2
    if [ "$seq" = "9" ] ; then
        echo "$0: Cannot push more than 10 regions."
        exit 1
    fi
    seq=`expr $seq + 1`
fi

g.region save=_PUSH_POP_.$seq

exit 0

