#!/bin/sh
#
# Prints an earlier version of an rdbtable.
#

RCS_ID='$Id: nsq-rollback,v 1.1 1998/05/29 20:34:18 carlos Exp $'

my_name=$(basename $0)

while [ $# -ge 1 ] ; do
  case $1 in
    -h*) cat <<_EOH_

        NoSQL operator: ${my_name}

Usage:  ${my_name}  [options]  rdbtable

Options:
    -d "xxx"  Restoration point of table in any valid time format.
              "xxx" is a time/date string, as explained below.
    -h        Print this help info.

Prints an rdbtable the way it was at a point in time, by using RCS.
The table must have been subject to versioning with 'nsq-tee -RCS'.

Please refer to co(1) for details about the acceptable date/time
formats. If the time string contains spaces, then it must be quoted,
following the usual Unix shell quoting/escaping rules. Examples of
time specifications which are acceptable for RCS are:

"1998/05/21 10:20:00"      (Default time zone is UTC)

"1998/05/21 10:20:00 GMT"  (The same as UTC)

"1998/05/21 10:20:00 LT"   (Local time zone)

"1998/05/21"               (equivalent to: "1998/05/21 00:00:00 UTC" )

A fairly common specification is "1998/05/21 LT".

This operator reads an rdbtable from a file and prints the requested
version of the same rdbtable to STDOUT. The output stream can then
easily be used to overwrite the original rdbtable on disk with the
command :

   ${my_name} [options] rdbtable | nsq-tee [options] rdbtable

This will effectively roll-back the rdbtable to the desired point
in time.


$RCS_ID

            ----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

You should have received a copy of the GNU General Public License
along with this program;  if not, write to the Free Software
Foundation, Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
            ----------------------

_EOH_
        exit 0
        ;;
    -d)    shift; time_string="$1"; shift ;;
    *)     tbl_name=$1; break         ;;
  esac
done

# Check for correctness of required arguments.

case ${tbl_name} in
  ""|-*)
    echo "Usage: ${my_name} [options] rdbtable" >&2
    exit 1
    ;;
esac

tbl_name="$(basename ${tbl_name})"
out_dir="$(dirname ${tbl_name})"

# Everything is relative to the target directory.
cd ${out_dir} || exit 2

if [ -z "${time_string}" ]
then
  co -q -p ${tbl_name}
else
  co -q -p -d"${time_string}" ${tbl_name}
fi

exit $?

