#!/bin/sh

# g.popregion.sh - restore the previous region from g.pushregion.sh
# 
#   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 files in order
# of newest to oldest.

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

if [ -z "$old" ] ; then
    echo "$0: Region stack is empty."
    exit 1
fi

g.region region=$old
g.remove region=$old >/dev/null 2>&1

exit 0

