#!/bin/bash

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

log=$LOGDIR/017.log


function testfunc
{
	filename=$1

	touch file-$filename
	ln -s file-$filename link-$filename
	ln -s bad-$filename badlink-$filename
	$BINdflt ci -m "locale ci $filename"
	$WC2_UP_ST_COMPARE

# "svn ls" gives characters > \x80 as eg. "?\228".
# With --xml we get the raw string, but *always* in UTF-8 ... so try both ways.
	svn ls --xml $REPURL/ > $log.1
	svn ls $REPURL/ > $log.2
	if [[ `grep -F "$filename" < $log.1 | wc -l` -eq 3 ||
	      `grep -F "$filename" < $log.2 | wc -l` -eq 3 ]]
	then
		echo "Ok, found all 3 entries."
	else
		$ERROR_NB "Expected filename:"
	  echo $filename | od -Ax -tx1 -tc
		$ERROR "En/Decode problem - entries not found."
	fi

# TODO: test whether the entries are correct in the other locale.

	rm *
	$BINq ci -m "locale ci $filename cleanup"
	$WC2_UP_ST_COMPARE
}



# look for UTF8
utf8_locale=`locale -a | grep .utf8 | head -1`
if [[ "$utf8_locale" != "" ]]
then
  echo "Found UTF8-locale '$utf8_locale', using that for testing."
else
  echo "Found no utf8-locale, cannot test"
fi


# look for non-utf8
loc_locale=`locale -a | egrep -v "(POSIX|C|utf8$)" | head -1`
if [[ "$loc_locale" != "" ]]
then
  echo "Found non-UTF8-locale '$loc_locale', using that for testing, too."
else
  echo "Found no non-utf8-locale, cannot test"
fi


# Trivial test with current settings
# We must use only ASCII as we don't know in which locale 
# this script is parsed.
$INFO "testing default locale"
testfunc test12
$SUCCESS "default locale"
	
# Clear environment
unset LC_ALL LC_CTYPE

# Test UTF8
if [[ "$utf8_locale" != "" ]]
then
	$INFO "testing utf8 locale $utf8_locale"
	export LC_ALL=$utf8_locale
	# The bytes here must be \xc2\xa9; in utf8 that's 3 horizontal lines.
	# Use a hex editor.
	testfunc ©
	testfunc $STG_UTF8
	testfunc $STG_LOC
	$SUCCESS "utf8 locale"
fi

# Test non-UTF8
if [[ "$loc_locale" != "" ]]
then
	$INFO "testing non-utf8 locale $loc_locale"
	export LC_ALL=$loc_locale
	# The bytes here must be \xc2\x61, that is an invalid UTF8-sequence.
	# Use a hex editor.
	testfunc a
	testfunc $STG_UTF8
	testfunc $STG_LOC
	$SUCCESS "non-utf8 locale"
fi


# vi: binary
