#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - cat        *
#  ###  ##        ####### ####### ##     **      $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
# Prints a table to STDOUT, checking it out from RCS if needed.
#
# Usage:  nosql cat [-d|--date datestring] table
# 
# where "datestring" is the restoration point of table in time, in any
# valid time format, as explained below.
# 
# Prints a NoSQL table the way it was at a point in time. If no datestring
# is supplied, then the latest available version of the table will be
# printed. For "datestring" to be effective the table must have been
# subject to versioning with 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".
# 
##########################################################################

Exit ()
{
  exit $1
}

while [ $# -ge 1 ]
do
  case $1 in                    
	-d|--date)  shift; time_string="$1" ;;
	*)   		tbl_path=$1; break ;;         
  esac
  shift
done

# Check for correctness of required arguments.

case ${tbl_path} in
  ""|-*)
    echo "nosql cat: missing table name" >&2
    Exit 1
    ;;
esac

tbl_name=$(basename ${tbl_path})
tbl_dir=$(dirname ${tbl_path})

# Everything is relative to the target directory.
cd ${tbl_dir} || Exit 2

if test -z "${time_string}"
then
  # Check whether the requested table has to be checked out from RCS.
  if test -f ${tbl_name}
  then
    cat ${tbl_name}
  else
    co -q -p ${tbl_name}
  fi
else
  co -q -p -d"${time_string}" ${tbl_name}
fi

Exit $?

