#!/bin/bash

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

logfile=$LOGDIR/034.status

file=empty-file


function Filter
{
	filt=$1
	exp=$2

	$BINdflt st -C -f $filt $file > $logfile
	if [[ `wc -l < $logfile` -ne $exp ]]
	then
		cat $logfile
		$ERROR "Status output wrong - filter $filt, expected $exp."
	fi
}

function FiltMTOGNDA
{
	Filter meta $1
	Filter text $2
	Filter owner $3
	Filter group $4
	Filter new $5
	Filter deleted $6
	Filter any $7

	$SUCCESS "Trial run $1$2$3$4$5$6$7 ok."
}


# without any change?
FiltMTOGNDA 0 0 0 0 0 0 0


# meta-data change.
touch -t 200101270007 $file
FiltMTOGNDA 1 0 0 0 0 0 1


# set as known state.
$BINdflt ci -m 1


# text change, meta-data same
echo aiikortv > $file
touch -t 200101270007 $file
FiltMTOGNDA 0 1 0 0 0 0 1


# text and meta-data change
echo adehlnor > $file
touch -t 200210291240 $file

FiltMTOGNDA 1 1 0 0 0 0 1


# deleted
rm $file
FiltMTOGNDA 0 1 0 0 0 1 1

# replaced
mkdir $file
FiltMTOGNDA 1 1 0 0 1 1 1


# Test with a removed directory
mkdir -p a/b/c/d a/b/c/e a/b/d a/h/u a/h/j
( cd a/h ; touch -d yesterday some files in dir )
$BINq ci -m 2
rmdir a/b/c/d a/b/c/e a/b/c a/h/u

$BINdflt st -C -o filter=deleted > $logfile
if [[ `wc -l < $logfile` -ne 4 || 
	`grep -w dir < $logfile | wc -l` -ne 4 ||
	`grep a/ < $logfile | wc -l` -ne 4 ]]
then
	cat $logfile
	$ERROR "Status output wrong (deleted directories #1)"
fi

# The parent directories are changed, and that gets counted, too.
$BINdflt st -C -o filter=text > $logfile
if [[ `wc -l < $logfile` -ne 6 || 
	`grep a/ < $logfile | wc -l` -ne 6 ]]
then
	cat $logfile
	$ERROR "Status output wrong (deleted directories #2)"
fi

date > a/h/some
date > a/h/dir

$BINdflt st -C > $logfile
if [[ `wc -l < $logfile` -ne 8 || 
	`grep a/ < $logfile | wc -l` -ne 8 ]]
then
	cat $logfile
	$ERROR "Status output wrong (deleted directories #3)"
fi


$SUCCESS "Ok, filter works."



# set as known state.
$BINq ci -m 2 -o delay=yes
$INFO "Testing sorting"

# Try sorting.
funsort=$logfile.unsort
fsort=$logfile.sort
touch z a y b x c w
for parm in "" "-v"
do
	# We do non-recursive here, because the subdirectories come unsorted.
	$BINdflt st -N ? > $funsort
	$BINdflt st -N ? -o dir_sort=yes > $fsort

	if cmp -s $funsort $fsort
	then
	  $WARN "Sorted equals unsorted?"
	fi

	if sort -k3 $funsort | cmp -s - $fsort
	then
	  echo "Sorting ok"
	else
	  $ERROR "Didn't sort (cmdline='$parm')"
	fi
done
$SUCCESS "Sorting works."


$BINq ci -m1 -o delay=yes

# Test color output.
function HasEscape
{
	# I cannot make grep and egrep understand \x1b.
  if $BINdflt st -o stat_color=yes | perl -e 'exit (0 == grep(/\x1b\[0;0m/, <STDIN>))'
	then
	  $SUCCESS "$1 colorized"
	else
	  $ERROR "$1 not colorized"
	fi
}

echo aaa > hazgr
HasEscape "New"
$BINq ci -m1 -o delay=yes
echo aaar > hazgr
HasEscape "Changed"
$BINq ci -m1 -o delay=yes
rm hazgr
HasEscape "Deleted"


# Check for -N on deleted hierarchies
# Set some known timestamp
touch -d "2008-02-01 12:13" .
$BINq ci -m1 -o delay=yes

function ExpLines
{
  parms="$1"
	exp_cnt="$2"
	if [[ `$BINdflt st $parms | wc -l` -eq $exp_cnt ]]
	then
		$SUCCESS "found $exp_cnt for '$parms'"
	else
		$BINdflt st $parms
		$ERROR "expected $exp_cnt, got "`$BINdflt st $parms | wc -l`
	fi
}


ExpLines "-C" 0
rm -r tree
touch -d "2008-02-01 12:13" .

# With -N -N, no children are looked at.
ExpLines "-N -N -C" 0
# If we don't pass -C, the timestamp is looked at, and if it's still the 
# same no check is done.
ExpLines "-N -N" 0
# We have to touch the directory; even with -C no children are seen.
touch .
ExpLines "-N -N -C" 1
ExpLines "-N -N" 1
ExpLines "-N" 2
ExpLines "" 41

if $BINdflt -o stop_change=true status
then
  $ERROR "Expected an error code - 1"
fi

$BINq ci -m 1
if $BINdflt -o stop_change=true status
then
  $SUCCESS "No error code without change."
else
  $ERROR "Expected no error code - 1"
fi

touch empty-file
if $BINdflt -o stop_change=true status
then
  $ERROR "Expected an error code - 2"
fi
if $BINdflt -o stop_change=true -f text status
then
  $SUCCESS "Filtering for changes, stopping ok"
else
  $ERROR "Expected no error code - 2"
fi




