#!/bin/ksh
# Program to reset GNATS to a pristine condition.
# Use with CAUTION!
# Written by Brian Abernathy.

GNATS_ROOT=/usr/local/lib/gnats/gnats-db
PATH=$PATH:$GNATS_ROOT/gnats-bin

echo Warning!  This script will empty the GNATS database of 
echo ALL PRs.  
echo
echo Do you want to do this? \(Y/N\)
read ans

if [ "$ans" != "Y" ]
then
   echo exiting.
   exit
fi

echo
echo Are you REALLY SURE? \(Y/N\)
read ans

if [ "$ans" != "Y" ]
then
   echo I didn\'t think so.\ \ Bye!
   exit
fi

CATEGORIES=`grep -v '^#' $GNATS_ROOT/gnats-adm/categories | sed -e 's/:.*//g'`

# Obtain a database lock.
pr-edit --lockdb
if [ $? -ne 0 ]
then
   echo Sorry - cannot obtain a database lock.  Try later.
   exit 1
fi

# Reset the 'current' number.
echo 0 >$GNATS_ROOT/gnats-adm/current

# Clear out all the waiting PRs.

for files in `echo $CATEGORIES`
do
   rm $GNATS_ROOT/$files/* 2>/dev/null
done

# Regenerate the index.
rm $GNATS_ROOT/gnats-adm/index
touch $GNATS_ROOT/gnats-adm/index
gen-index

# Release database lock.
pr-edit --unlockdb

echo Done!
# Release lock.
pr-edit --unlockdb

echo The GNATS database has been reset.
