#!/bin/bash

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

file=diff-file
copy=copy-file
log=$LOGDIR/026.diff-log


echo "line" > $file
$BINq ci -m "repos-vers" -o delay=yes

echo "something else" > $file

if [[ `$BINdflt diff $file | wc -l` -eq 6 ]]
then
	$SUCCESS "We get a diff"
else
	$ERROR "No diff shown."
fi

# Only headerline
if [[ `FSVS_DIFF_PRG=true $BINdflt diff $file | wc -l` -eq 1 ]]
then
	$SUCCESS "FSVS_DIFF_PRG is honored"
else
	$ERROR "FSVS_DIFF_PRG doesn't work?"
fi

FSVS_DIFF_PRG=true $BINdflt diff -v $file > $log
# We cannot be absolutely sure that the mtime doesn't wrap into the next 
# second, so there might be +- lines for meta-data.
if [[ `grep -E '^.(Mode|MTime|Owner|Group): ' $log | wc -l` -lt 4 ]]
then
	$ERROR "Meta-data output missing?"
else
	$SUCCESS "Meta-data is printed"
fi


$BINq revert $file -odelay=yes

if [[ `$BINdflt diff $file | wc -l` -eq 0 ]]
then
	$SUCCESS "No diff if not changed"
else
	$ERROR "Too much output for unchanged files!"
fi

# Tests for diff of copied entries.

cp -a $file $copy
$BINq cp $file $copy
# We don't use -a; because of the sleep above we get a different timestamp, 
# and so the header is printed.
if [[ `$BINdflt diff $copy | wc -l` -le 1 ]]
then
	$SUCCESS "Diff for unchanged, copied, mtime entry"
else
	$ERROR "Diff for unchanged, copied, mtime wrong"
fi

cp -a $file $copy
# Now they should have the same timestamp, so no change.
if [[ `$BINdflt diff $copy | wc -l` -le 1 ]]
then
	$SUCCESS "Diff for unchanged, copied entry"
else
	$BINdflt diff $copy
	$ERROR "Diff for unchanged, copied unexpected"
fi


echo X > $copy
$BINdflt diff -r HEAD $copy > $log
if [[ `wc -l < $log` -eq 6 ]]
then
	$SUCCESS "Diff for copied"
else
  wc -l < $log
	$ERROR "Diff for copied wrong - expected 6 lines"
fi


chmod 700 $copy
$BINq ps a b $copy
$BINq ci -m 1 -o delay=yes

echo Y > $copy
chmod 550 $copy
$BINq ps a c $copy

# TODO: user-defined property diff
$BINdflt diff -v $copy > $log
if perl -e 'undef $/; $_=<>; 
	exit 1 unless /\n-Mode: 0700\n\+Mode: 0550\n/m' < $log
then
	$SUCCESS "Diff for changed copy"
else
	$BINdflt diff -v $copy
	$ERROR "Diff for changed copy, expected mode change"
fi


# Try colordiff auto mode
$BINdflt diff -v $copy -o colordiff="" > /dev/null

# Try error handling
if $BINdflt diff $copy -o colordiff=mustneverexist_invalidbinary.$$.$RANDOM > $log 2>&1
then
  $ERROR "Doesn't error out for an invalid colordiff name?"
else
  $SUCCESS "Reports bad names for colordiff"
fi

# No temporary file may be left behind.
if ls $copy.* 2> /dev/null
then
  $ERROR "Temporary file left behind."
fi


# True immediately exits
if $BINdflt diff $copy -o colordiff=true > /dev/null 2>&1
then
  $ERROR "Doesn't error out for a non-reading colordiff?"
else
  $SUCCESS "Reports stopping colordiffs"
fi

# No temporary file may be left behind.
if ls $copy.* 2> /dev/null
then
  $ERROR "Temporary file left behind."
fi


# EPIPE?
if $BINdflt diff $copy -o colordiff=cat | true
then
  $SUCCESS "Ignores EPIPE"
else
  $ERROR "Doesn't handle EPIPE"
fi

# No temporary file may be left behind.
if ls $copy.* 2> /dev/null
then
  $ERROR "Temporary file left behind."
fi


# Test "diff -rX" against entries in subdirectories, and compare against 
# "live" diff.
# The header lines (current version, timestamp, etc.) are different and 
# made equal for comparision.
$BINq ci -m1 -odelay=yes > $log 
rev=`grep "revision	" $log | tail -1 | cut -f2 -d"	" | cut -f1 -d" "` 
fn=tree/b/2/file-x
equalizer="perl -pe s#($fn).*#filename_and_so_on#"
echo $RANDOM $$ > $fn
$BINdflt diff $fn | $equalizer > $log
$BINq ci -m1
# echo aaa > $fn # for verification that the test mechanism works
if $BINdflt diff -r$rev $fn | $equalizer | diff -u - $log
then
  $SUCCESS "diff -rX"
else
  $ERROR "'diff -rX' gives a different answer"
fi



# Test diff over special entries
ln -s old X
$BINq ci -m1 -odelay=yes > $log 
rev1=`grep "revision	" $log | tail -1 | cut -f2 -d"	" | cut -f1 -d" "` 
ln -sf new X
EXPECT=9
function testdiff
{
	$BINq diff "$@" > $log
# There are additional lines "no linefeed" and "special entry changed".
	if [[ `wc -l < $log` -ne $EXPECT ]]
	then
		cat $log
		$ERROR "'diff "$@"' line count wrong"
	fi

	if grep -F -- '-link old' < $log &&
		grep -F -- '+link new' < $log
	then
		$SUCCESS "'diff "$@"' ok"
	else
		cat $log
		$ERROR "'diff "$@"' output wrong"
	fi
}
testdiff -r$rev1
# Test whether other, non-wanted, entries are diffed.
testdiff X -r$rev1

# "diff x:y" should get rewritten, I think.
# We don't get the "special entry" lines.
$BINq ci -m1 -odelay=yes > $log 
rev2=`grep "revision	" $log | tail -1 | cut -f2 -d"	" | cut -f1 -d" "` 
testdiff -r$rev1:$rev2
testdiff -r$rev1:$rev2 X

# Test how much gets diffed on -rX:Y
date > $fn
$BINq ci -m1 > $log 
rev3=`grep "revision	" $log | tail -1 | cut -f2 -d"	" | cut -f1 -d" "` 
testdiff -r$rev1:$rev3 X

# Test diff on removed entries
# Here we get the message again?
rm X
testdiff -r$rev1:$rev3 X
$BINq ci -m1 > $log 

testdiff -r$rev1:$rev3 X

