#!/bin/bash

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

cd $WC


logfile=$LOGDIR/090.spec


mv tree gt
mkdir 4rg
for a in `seq 1 200`
do
  echo $a > 4rg/$a
done


# Commit a file that's actively changing
$BINdflt ci -m1 > 4rg/file

$BINdflt ci -m1 > 4rg/file2
$BINdflt ci -m1 > 4rg/file3
$BINq ci -m1

$WC2_UP_ST_COMPARE


###############################################################
# 
#  Test commit delay.
#  From common sense this should be done as soon as possible,
#  as most other tests need that to work; but in 001 it would
#  slow down even single case testing, and as 002 it might be
#  run after all other tests (with RANDOM_ORDER=1).
#  So we hope that it just works - and try to get a test
#  summary. 

$INFO "Testing commit delay"
function CT
{
	delay_start=`date +%s`
# Make sure the files have different lengths
	msecs=0
	for a in 1 22 333 4444
	do
		echo $a > file
		$BINq ci -m$a $1 > /dev/null
		nsec=`date +%_9N`
		c_msec=${nsec:0:3}
		msecs=$(($msecs + ${c_msec:=0}))
	done
# We get an error here if the result is 0 ... so make it at least 1.
	delta=$(expr `date +%s` - $delay_start + 1)
}

CT -odelay=yes
with_delay=$delta
wd_msec=$msecs
echo "With delay that took $delta seconds (dm=$wd_msec)"

CT -odelay=no
normal=$delta
n_msec=$msecs
echo "Without delay that took $delta seconds (dm=$n_msec)"

diff=$(expr $with_delay - $normal)
factor=$(expr $n_msec / $wd_msec)
$INFO "Difference $diff, factor $factor"

# At least two seconds difference should be here.
if [[ $diff -ge 2 ]]
then
  $SUCCESS "The delay option seems to work (runtime)."
else
	if [[ $factor -ge 4 ]]
	then
		$SUCCESS "The delay option seems to work (msec-diff)."
	else
		$ERROR "The delay option is too fast"
	fi
fi


$INFO "Testing delay command."
dirfile=`$PATH2SPOOL . dir`
touch -d "now + 2 seconds" $dirfile
a=`date +%s`
$BINdflt delay
b=`date +%s`
diff=`echo $b - $a | bc`
if [ $diff -ge 1 ]
then
	$SUCCESS "Delay command works."
else
	$ERROR "Delay command took only diff=$diff"
fi


# Test having *many* subdirectories in parallel in waa__output_dir(), 
# triggering reallocating the directory array.
# On tmpfs the inode numbers are given in ascending order; so creating many 
# directories, and then putting files there might do the trick.
# We do that as 50 * 50 directories, with each having a file.
$INFO "Creating many directories"
list=`seq 1 50`
declare -a entries
for a in $list
do
	mkdir $a
	for b in $list
	do
	  entries+=($a/$b/file)
		mkdir $a/$b
	done
done


# create files
touch "${entries[@]}"
opt=""
if [[ "$opt_DEBUG" == "1" ]]
then
  opt="-d -D waa__output_tree"
fi

$BINq ci -m1 $opt > $logfile
$WC2_UP_ST_COMPARE

if [[ "$opt_DEBUG" == "1" ]]
then
	if grep "reallocated directory pointers to " $logfile
	then
		$SUCCESS "Many parallel directories in output_tree"
	else
	# Should that only be a warning? Might not work on other filesystems.
		$ERROR "No realloc message found for many parallel directories"
	fi
else
  $WARN "no debug info compiled in"
fi

# If writing the tree didn't work, we will find out when we do a status or 
# another commit.
G=40/32/file
touch $G
if [[ `$BINdflt st` == ".m.."*" 0 "*"$G" ]]
then
	$SUCCESS "Entry list seems to be correct"
else
	$ERROR "Status wrong - entry list destroyed?"
fi
if $BINq ci -m1
then
	$SUCCESS "Commit ok"
else
	$ERROR "Commit barks"
fi
$WC2_UP_ST_COMPARE
