#!/bin/bash

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

cd $WC

fn1=file1
fn2=file2
fn3=file3

logfile=$LOGDIR/044.copyfrom
copydb=`$PATH2SPOOL . Copy`


######################################################
# Check copyfrom relation setting logic.
# Overwrite is not allowed - the user would have to revert (TODO).
# We could allow it for non-directories, however.
touch 1 4 5 s
$BINq ci -mx

# If these file had a different timestamp, the revert below would only 
# change this; but we want the copy relations to be removed.
touch -r 1 2 3 6
$BINdflt cp 1 2
$BINdflt cp 5 6
$BINdflt cp ./4 $WC/3
if [[ ! -s $copydb ]]
then
  $ERROR "Expected a copyfrom database at $copydb"
fi

if [[ $(echo `$BINdflt cp dump | sort`) == ". . 2 3 6 $REPURL/1 $REPURL/4 $REPURL/5" ]]
then
  $SUCCESS "Setting and overwriting copyfrom information works."
else
	$BINdflt cp dump
  $ERROR "Setting or overwriting makes mistakes?"
fi


# Remove copyfrom markers
$BINq sync-repos

( 
  echo 5
  echo 6
  echo .
  echo 4
  echo 3
  echo .
  echo 1
  echo 2
) | $BINdflt cp load
# we still have the values of the last copy operations
if [[ $(echo `$BINdflt cp dump | sort`) == ". . 2 3 6 $REPURL/1 $REPURL/4 $REPURL/5" ]]
then
  $SUCCESS "Loading copyfrom information works."
else
	$BINdflt cp dump
  $ERROR "Loading copyfrom fails."
fi

# A "true | fsvs cp load" doesn't work, as the copyfrom information is 
# added, not replaced. So we undo the copy.
$BINdflt uncopy 3 6 2
if [[ `$BINdflt cp dump -v` == "No copyfrom information was written." ]]
then
  $SUCCESS "Purging copyfrom works"
else
  $ERROR "Purging copyfrom fails"
fi

if [[ `true | $BINdflt cp load -v` == "0 copyfrom relations loaded." ]]
then
  $SUCCESS "Nothing loaded."
else
  $ERROR "Loading nothing on empty copy database fails"
fi



######################################################
# Test copyfrom on commits.

seq 1 100 > $fn1
$BINq ci -m "file1" -o delay=yes
# Now the copyfrom relation file should be cleaned up.
if [[ -s $copydb ]]
then
  $ERROR "Expected copyfrom database at $copydb to be deleted"
fi



function Status
{
  $BINdflt st > $logfile
	while [[ "$1" != "" ]]
	do
	  if ! grep -F "$1" < $logfile | grep -F "$2"
		then
		  $ERROR "Expected status '$1' for '$2'"
		fi
		shift
		shift
	done
}


######################################################
# Test simple copying.
# The other file is moved before the commit, so we can check that a commit 
# removed *exactly* the used records, and no others.
cat $fn1 > $fn2
mv $fn1 $fn3
$BINq cp $fn1 $fn2
Status ".m.+" $fn2 "N..." $fn3 "D..." $fn1
$BINq mv $fn1 $fn3
Status ".m.+" $fn2 "...+" $fn3 "D..." $fn1
$BINq ci -m "file2" $fn2
# Status ".m.+" $fn2 "...+" $fn3 "D..." $fn1

if [[ $(echo `$BINdflt cp dump | sort`) == "$fn3 $REPURL/$fn1" ]]
then
  $SUCCESS "On commit correct entries purged."
else
	$BINdflt cp dump
  $ERROR "Commit removed wrong copyfrom entries."
fi


function CopyLog
{
  src="$1"
	dest="$2"
	msg="$3"

	LC_ALL=C svn log -v -r HEAD $REPURL > $logfile
	if grep "   A /$REPSUBDIR/$dest .from /$REPSUBDIR/$src:" $logfile
	then
		# ------------------------------------------------------------------------
		# r5 | flip | 2007-11-22 18:39:20 +0100 (Thu, 22 Nov 2007) | 1 line
		# Changed paths:
		#    A /trunk/file2 (from /trunk/file1:4)
		# 
		# file2
		# ------------------------------------------------------------------------
		$SUCCESS "$msg"
	else
		cat $logfile
		$ERROR "$msg"
	fi
}

# Doing a $WC2_UP_ST_COMPARE wouldn't work here, as the moved file is not 
# yet committed.

CopyLog $fn1 $fn2 "File was copied"



######################################################
# Test file renaming (moving)
# The operations have been done above.
$BINdflt ci -m "move" # -d > $logfile

$WC2_UP_ST_COMPARE

if [[ -s $copydb ]]
then
  $ERROR "Expected copyfrom database at $copydb to be deleted"
fi

CopyLog $fn1 $fn3 "File was moved - 1"
if grep "   D /$REPSUBDIR/$fn1" $logfile
then
	$SUCCESS "File was moved - 2"
else
  cat $logfile
  $ERROR "File not moved? - 2"
fi


######################################################
# Test directory copying
mkdir dir1
date > dir1/date-file
seq 1 600 > dir1/large
$BINq ci -m "dir1"


src=dir1
dest=dir2
cp -a $src $dest
$BINq cp $src $dest
$BINq ci -m "$dest"

$WC2_UP_ST_COMPARE

CopyLog $src $dest "Dir was copied"

if [[ -s $copydb ]]
then
  $ERROR "Expected copyfrom database at $copydb to be deleted"
fi


# 2nd try: new directory without files; the entries are implicitly copied, 
# and have to be removed from therein.
dest=dir3
mkdir $dest
$BINdflt cp $src $dest
$BINq ci -m "$dest"

$WC2_UP_ST_COMPARE

CopyLog $src $dest "Empty dir was copied"


# Now try the same, but not starting in the wc root:
# Copy definition wc/a/b/c => wc/a/e/f
dirname=aergheanrgerkgergwergwergwa
pre=a/b
fulldir=$dirname/$dirname/$dirname/$dirname/$dirname
full=$fulldir/gggg
mkdir -p $pre/d/$fulldir
echo $$ $RANDOM $RANDOM > $pre/c
echo $RANDOM > $pre/d/$full
$BINq ci -m "prep"

mkdir -p a/e a/g
cp $pre/c a/e/f
cp -r $pre/d a/g/d
# copy the file
( cd a ; $BINq cp b/c ../a/e/f )
( cd a ; $BINq cp b/d ../a/g/d )

# Test diff for *long* filenames.
echo $$-$RANDOM > a/g/d/$full
$BINdflt diff a/g/d/$full > $logfile
if [[ `egrep "^[+-]" $logfile | wc -l` -eq 4 ]]
then
  $SUCCESS "Diff for long copied entries with long names works."
else
  $ERROR "Diff for long copied entries with long names"
fi

$BINq ci -m "copy" -o delay=yes

CopyLog a/b/c a/e/f "Copy definition below wc root"

$WC2_UP_ST_COMPARE

cp -r a with_subs
$BINq cp a with_subs

# Currently copying a copied file that's not committed is not possible.
mkdir will_error
if $BINdflt cp with_subs will_error
then
  $ERROR "Unexpected exit status of copying copied, uncommitted entry."
else
  $SUCCESS "Expected exit status of copying copied, uncommitted entry."
fi


$BINq ci -m "subs"

$WC2_UP_ST_COMPARE
