#!/bin/bash

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

logfile=$LOGDIR/018.various


# Test EPIPE handling.
# I could reproduce it with "strace -o /dev/null $BINdflt log | true", (or 
# "| head -1"), but only in 1 of 10 cases without the strace.
strace_bin=`which strace || true`
strace_cmd=${strace_bin:+$strace_bin -o /dev/null}
for command in log st
do
	ret=$(
		set -o pipefail
		$strace_cmd $BINdflt $command 2>$logfile | true
		echo $?
		set +o pipefail )
	if [[ $ret -eq 0 ]]
	then
		# No errors on STDOUT allowed.
		if [[ `wc -l < $logfile` -eq 0 ]]
		then
			$SUCCESS "EPIPE on $command handled correctly"
		else
			$ERROR "wrong number of output lines on EPIPE $command test"
		fi
	else
		$ERROR "Error code on EPIPE $command"
	fi
done


# Test whether / at the end of an URL are removed
$BINq init $REPURL/////
if $BINdflt st > $logfile
then
	$SUCCESS "slashes at end of URLs are ignored."
else
	$ERROR "slashes make fsvs fail??"
fi

if [[ `$BINq help | wc -l` -gt 10 &&
`$BINq -h | wc -l` -gt 10 &&
`$BINq -V` == "FSVS"*"version "* ]]
then
	$SUCCESS "version and help are given"
else
	$ERROR "version or help are not printed?"
fi

if [[ "$opt_DEBUG" == "1" ]]
then
	if FSVS_DEBUGLEVEL=1 $BINdflt -v -D main | grep LC_ALL > $logfile &&
		test `$BINdflt -v -d -D main | egrep "^[^ ]+ +main" | wc -l` -ge 3
	then
		$SUCCESS "Debug messages seem to work"
	else
		$ERROR "debug doesn't work?"
	fi
fi


if [[ `$BINdflt s 2>&1 ` == *"Action \"s\" is ambiguous."* &&
	`$BINdflt invalid-action-which-will-never-exist 2>&1` == \
		*"Action "*" not found."* ]]
then
	$SUCCESS "actions are checked"
else
	$ERROR "actions are not verified?"
fi


if $BINdflt st /$RANDOM/$RANDOM/$RANDOM/$RANDOM/$RANDOM > $logfile 2>&1
then
	$ERROR "non-existing directories don't fail?"
else
	$SUCCESS "error for non-existing directories"
fi


# Look whether there's a human-readable message (1 line) for non-existing 
# CONF or WAA paths. 
# Has to include the missing path, so that the generic "no working copy" 
# error isn't allowed.
function Check_Path
{
	var=FSVS_$1
	if $BINdflt status /bin > $logfile 2>&1
	then
		cat $logfile
		$ERROR "Invalid $var doesn't stop?"
	else
		if [[ `wc -l < $logfile` -eq 1 &&
			`grep -c "${!var}" < $logfile` -eq 1 ]]
		then
			$SUCCESS "$var checked"
		else
			cat $logfile
			$ERROR "Wrong message on invalid $var."
		fi
	fi
}
FSVS_CONF=$WC/not-here Check_Path CONF
FSVS_WAA=$WC/not-here Check_Path WAA

# Define an empty configuration directory, and try to do a status (without 
# a wc file).
if FSVS_CONF=$WC $BINdflt status -N -N /sbin /bin > $logfile 2>&1
then
	$ERROR "Didn't expect status output"
else
	$SUCCESS "No status output for non-committed WCs"
fi

# make a wc, and retry
( 
	export FSVS_CONF=$WC FSVS_WAA=$WC 
	cd / 
	echo file:/// | $BINq urls load 
	echo './*' | $BINq ignore load 
)
if FSVS_CONF=$WC FSVS_WAA=$WC $BINdflt status -N -N /etc /usr > $logfile 2>&1
then
	if ! grep '^N\.\.\. \+dir \+/\(etc\|usr\)$' $logfile > $logfile.2
	then
	  $ERROR "Wrong status output"
	fi

	if [[ `wc -l < $logfile` -eq 2 &&
				`wc -l < $logfile.2` -eq 2 ]]
	then
		$SUCCESS "Status output for two root entries as expected."
	else
		$ERROR "Wrong status output"
	fi
else
	$ERROR "No status output for non-committed WCs?"
fi


# Test whether an invalid/not existing $FSVS_WAA allows "help" to work
if FSVS_WAA=/tmp/not-existing-$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM $BINdflt help status > $logfile 2>&1
then
	$SUCCESS 'help needs no $FSVS_WAA'
else
	$ERROR 'help tries to access $FSVS_WAA'
fi

if $BINdflt -V | grep version | grep GPL | grep Marek > /dev/null
then
  $SUCCESS "Version is printed"
else
  $ERROR "Version printing error"
fi

if $BINdflt -v -V | grep "compiled .*, with options" > /dev/null
then
  $SUCCESS "Compile options are printed"
else
  $ERROR "Verbose version printing error"
fi

if $BINdflt help | grep "Known commands:" > /dev/null
then
  $SUCCESS "Help gets printed"
else
  $ERROR "No help output?"
fi

if $BINdflt help help | grep 'Help for command "help".' > /dev/null
then
  $SUCCESS "Help for help gets printed"
else
  $ERROR "No help help output?"
fi

if $BINdflt help -h | grep 'Help for command "help".' > /dev/null
then
  $SUCCESS "Help -h gets printed"
else
  $ERROR "No help -h output?"
fi

if $BINdflt help -? | grep 'Help for command "help".' > /dev/null
then
  $SUCCESS "Help -? gets printed"
else
  $ERROR "No help -? output?"
fi



# If we have an invalid charset, ANSI_X3.4-1968 is returned (=ASCII).
# So there should never be an error, unless nl_langinfo fails.
#if ! LC_ALL=invalid LC_CTYPE=invalid $BINdflt -Wcharset-invalid=stop st 2> $logfile
#then
#  $SUCCESS "invalid locales can stop fsvs"
#else
#  $ERROR "invalid locales don't give an error?" 
#fi
#

