#!/bin/bash

set -e 
$PREPARE_CLEAN > /dev/null
$INCLUDE_FUNCS
cd $WC

logfile=$LOGDIR/008.log

filename=update-file.upd
dir=2313
file2=$dir/garble

function VL
{
  if [[ "$VERBOSE" != "" ]]
	then
		# If the files don't exist, we'd stop because 
		# ls doesn't find the given name.
		# This way the output is shorter - only 2 lines compared to 10.
	  ls -la --full-time $WC/* $WC2/* || true
	fi
}

if [[ -e $filename ]]
then
  rm $filename
	$BINq ci -m "delete the test-file"
fi

# this next line has two tabulators - in grep and cut
rev=`$BINdflt up | grep "revision	" | tail -1 | cut -f2 -d"	" | cut -f1 -d"."` 

echo "now at rev. $rev"

echo "A testline #1" > $filename
echo "A testline #2" >> $filename
# We do a directory with an entry here, to see
# whether directories only available in the repository
# are correctly handled.
mkdir $dir
echo $file2 > $file2

echo "     ci1"
VL
$BINq ci -m "new file"

echo "A further line" >> $filename
echo $file2 >> $file2
echo "     ci2"
VL
$BINq ci -m "new file"

VL
$BINq up -r$rev
VL
if [[ -e $filename ]]
then
  $ERROR 'File should not exist!'
else
  $SUCCESS 'Ok, file was deleted'
fi

if [[ `$BINdflt remote-status | grep $filename` == "N... "* ]]
then
  $SUCCESS "remote-status says to-be-done (1)."
else
	$ERROR " remote-status failed (1)!"
fi

$BINq up -r`expr $rev + 1`
VL
if [[ `wc -l < $filename` -ne 2 ]]
then
  $ERROR 'File has wrong data!'
else
  $SUCCESS 'Ok, file was added'
fi

if [[ `$BINdflt remote-status | grep $filename` == .?C?" "* ]]
then
  $SUCCESS " remote-status says to-be-done (2)."
else
	$ERROR " remote-status failed (2)!"
fi

$BINq up -r`expr $rev + 2`
VL
if [[ `wc -l < $filename` -ne 3 ]]
then
  $ERROR 'File has wrong data!'
else
  $SUCCESS 'file was modified'
fi

if [[ `$BINdflt remote-status -r$rev | grep $filename` == "D..."* ]]
then
  $SUCCESS " remote-status to old revision says to-be-deleted."
else
	$ERROR " remote-status -r $rev failed (3)!"
fi

$BINdflt remote-status > $logfile
if grep $filename < $logfile
then
	cat $logfile
	$ERROR " remote-status failed (4)!"
else
  $SUCCESS " remote-status says nothing."
fi

touch -t 200406271837 $filename
$BINdflt remote-status > $logfile
if grep $filename < $logfile
then
	$ERROR " remote-status failed (5)!"
else
  $SUCCESS " remote-status on touched file says nothing."
fi
VL

# No $WC2_UP_ST_COMPARE here, as the file was touched.
# We'd have to update to an earlier version, and return to the present :-)

