#!/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
	# These must be the bytes c3 b6, which is an &ouml;
	mkdir -p dir-$filename/dir-ö/f-$filename
	echo $filename | od -Ax -tc -tx1 > $log
	$BINdflt ci -m "locale ci $filename" >> $log
	$WC2_UP_ST_COMPARE > /dev/null

# "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 -R --xml $REPURL/ > $log.1
	svn ls -R $REPURL/ > $log.2
	if [[ `grep -F "$filename" < $log.1 | wc -l` -eq 6 ||
	      `grep -F "$filename" < $log.2 | wc -l` -eq 6 ]]
	then
		echo "Ok, found all 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.
	if [[ "$other" != "" ]]
	then
		# Remove all entries
		$BINq up -r1 $WC2 > /dev/null
		# Checkout
		LC_ALL=$other $BINq up $WC2  > /dev/null
		if $BINdflt st $WC2 | grep .
		then
			$ERROR "Didn't expect any output after update"
		fi
		LC_ALL=$other $BINq sync $WC2 > /dev/null
		if $BINdflt st $WC2 | grep .
		then
			$ERROR "Didn't expect any output after sync"
		fi
	fi

	echo "Cleaning up"
	rm -rf *
	$BINdflt ci -m "locale ci $filename cleanup" > $log
	LC_ALL=$other $WC2_UP_ST_COMPARE > $log
}



# 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
	export other=$loc_locale
	# The bytes here must be c3 a4; in utf8 that's an &auml;
	# Use a hex editor.
	testfunc ä
	testfunc $STG_UTF8
	# This sequence of bytes in $STG_LOC is in latin1 or something similar, which 
	# is not a valid UTF-8 sequence, so we can't use that.
	$SUCCESS "utf8 locale"
fi

# Test non-UTF8
if [[ "$loc_locale" != "" ]]
then
	$INFO "testing non-utf8 locale $loc_locale"
	export LC_ALL=$loc_locale
	export other=$utf8_locale
	# The bytes here must be \xc2\x61, that is an invalid UTF8-sequence.
	# Use a hex editor.
	testfunc a
	# In non-UTF8 locales all byte sequences are valid - they have no 
	# interdependencies.
	testfunc $STG_UTF8
	testfunc $STG_LOC
	$SUCCESS "non-utf8 locale"
fi


# vi: binary
