#!/bin/bash

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

logfile=$LOGDIR/024.urls


# clean up
echo "" | $BINq urls load

prio=15
for p in http https svn svn+ssh file svn+some_other_tunnel svn+1
do
	$BINq urls N:$p,prio:$prio,T:$prio,$p://$p/some/path
	# Do pseudo-random
	prio=`perl -e 'print +(shift()*11+41) % 61' $prio`
done
$SUCCESS "Set all known protocols"

if $BINq urls unknown:///void 2> /dev/null
then
  $ERROR "Invalid protocols are accepted"
else
  $SUCCESS "Invalid protocols are rejected"
fi

if $BINq urls svn+:///void 2> /dev/null
then
  $ERROR "'svn+' is wrong, but got accepted"
else
  $SUCCESS "'svn+' rejected"
fi



# Having a invalid sequence
if $BINq urls dump '\\\r\n\t\f\x01\*' > /dev/null 2>&1 ||
	$BINq urls dump '%n%t%u%p%r%%%/' > /dev/null 2>&1 
then
  $ERROR "Parsing escape sequences doesn't work"
fi


# We would have to do the sorting afterwards - if the program fails, 
# we wouldn't get the error.
# But if there's an priority, the output order is defined.
$BINq urls dump > urls.txt
$BINq urls load < urls.txt
$BINq urls dump > urls2.txt

if cmp urls.txt urls2.txt
then
  $SUCCESS "Dump/Load works"
else
  $ERROR "Dump/Load gives different results!"
fi

url=http://egal
echo "name:1qay,P:9812,target:HEAD,$url" | $BINq urls load
if [[ `$BINdflt urls dump '%p'` != 9812 ]]
then
  $ERROR "Dumping priority with '%p'"
fi
if [[ `$BINdflt urls dump '%u'` != $url ]]
then
  $ERROR "Dumping url with '%u'"
fi
if [[ `$BINdflt urls dump '%n'` != 1qay ]]
then
  $ERROR "Dumping name with '%n'"
fi
if [[ `$BINdflt urls dump '%%\x20%%'` != "% %" ]]
then
  $ERROR "Dumping some other format"
fi
if [[ `$BINdflt urls dump '%t'` != "HEAD" ]]
then
	$ERROR "Dumping target revision with '%t'"
fi
$SUCCESS "URL load/dump tests pass"


$BINq urls "prio:22,N:XX,$url"
if [[ `$BINdflt urls dump | wc -l` -ne 1 ]]
then
  $ERROR "URL taken twice"
fi
if [[ `$BINdflt urls dump '%p'` != 22 ]]
then
  $ERROR "Changing priority with '%p'"
fi
if [[ `$BINdflt urls dump '%n'` != XX ]]
then
  $ERROR "Changing name with '%n'"
fi
$SUCCESS "URL change tests pass"


if $BINq urls "N:XX,$url/g" > /dev/null 2>&1
then
	$ERROR "Duplicate names should not be allowed"
else
	$SUCCESS "Duplicate names rejected"
fi


# Test error for non-WC directories
mkdir Xt
cd Xt
if $BINdflt urls dump > $logfile 2>&1
then
	cat $logfile
	$ERROR "No error for non-WC-directories"
else
	if grep $(pwd) $logfile
	then
		$SUCCESS "Error message with path printed"
	else
		cat $logfile
		$ERROR "No or wrong error message"
	fi
fi


# Test keeping the internal numbers
U1=name:1,file:///r/1
U2=name:2,file:///r/2
U3=name:3,file:///r/3
echo -e "$U1\n$U2\n$U3\n" | $BINdflt urls load
$BINdflt urls dump "%u %I\\n" | sort > f-1
echo -e "$U2\n$U3\n$U1\n" | $BINdflt urls load
$BINdflt urls dump "%u %I\\n" | sort > f-2
echo -e "$U3\n$U2\n$U1\n" | $BINdflt urls load
$BINdflt urls dump "%u %I\\n" | sort > f-3
md5sum f-?
if [[ `md5sum f-? | cut -f1 -d" " | sort -u | wc -l` -eq	1 ]]
then
	$SUCCESS "Internal URL numbers are kept."
else
	$ERROR "Internal URL numbers lost."
fi

for a in s sv svn svn: svn:/ svn://
do
	if $BINq urls $a
	then
		$ERROR "invalid URL $a accepted"
	fi
done
$SUCCESS "Cut-off URLs not taken."
if $BINq urls svn://x/t
then
	$SUCCESS "Valid URL taken."
else
	$ERROR "Valid URL not taken."
fi


# Test changes.
function CU
{
	dump="$1"
	exp="$2"
	decl="$3"
	if [[ `$BINdflt urls dump "$dump"` == $exp ]]
	then
		$SUCCESS "$decl"
	else
		$ERROR_NB "expected '$exp', got:"
		$BINdflt urls dump "$dump"
		echo
		$ERROR "$decl"
	fi
}

( echo name:n1,prio:8,target:3,http://localhost/nothing ;
	echo prio:4,name:n2,file:///hopeless ) | $BINq urls load

CU "%n-%p-%t:" 'n2-4-HEAD:n1-8-3:' "Loading with reversed priority"

$BINdflt urls N:n1,prio:3
CU "%n-%p:" 'n1-3:n2-4:' "Reversing priority"

$BINdflt urls N:n3,http://localhost/nothing
CU "%n-%p:" 'n3-3:n2-4:' "Changing name"

$BINdflt urls target:87,http://localhost/nothing
CU "%n-%t:" 'n3-87:n2-HEAD:' "Changing the target revision"


cd $WC1
echo "$REPURL" | $BINq urls load
$BINq ci -m1
$WC2_UP_ST_COMPARE

# Check that nothing below FSVS_CONF gets changed for update/commit - not 
# even the timestamps.
mkdir -p aaa b/c/d b/k
date | tee aaa/2 5g3 b/gg b/c/d/5 > $RANDOM
find $FSVS_CONF -ls | sort > $logfile
$BINq ci -m2
$WC2_UP_ST_COMPARE
find $FSVS_CONF -ls | sort > $logfile.2
if diff -u $logfile $logfile.2
then
	$SUCCESS "FSVS_CONF not changed on commit/update."
else
	$ERROR "FSVS_CONF changed"
fi

