#!/bin/sh

# g.clearregion.sh - clear region stack
# 
#   tom poindexter, march 2000
#

#### Check that user is running GRASS
if test "$GISRC" = ""
then
    echo "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 list of pushed regions, if any

stack=''
comma=''

files=`ls _PUSH_POP_.[0-9] >/dev/null`
for reg in $files
do
    stack="$stack$comma$reg"
    comma=","
done

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

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


exit 0

