#!/bin/sh

emptylog() {
    FILENAME=$1
    TESTCASE=$2
    REASON=$3

    cat<<EOF > $FILENAME
<?xml version="1.0" encoding="ISO-8859-1"?>
<TestCase name="${TESTCASE}">
<Environment>
    <QtVersion>4.5.2</QtVersion>
    <QTestVersion>4.5.2</QTestVersion>
</Environment>
<TestFunction name="initTestCase">
<Message type="qdebug" file="" line="0">
    <Description><![CDATA[${REASON}!]]></Description>
</Message>
<Incident type="pass" file="" line="0" />
</TestFunction>
<TestFunction name="NotAvailable">
<Incident type="pass" file="" line="0" />
<Message type="qdebug" file="" line="0">
    <Description><![CDATA[${REASON}!]]></Description>
</Message>
</TestFunction>
<TestFunction name="cleanupTestCase">
<Incident type="pass" file="" line="0" />
<Message type="qdebug" file="" line="0">
    <Description><![CDATA[${REASON}!]]></Description>
</Message>
</TestFunction>
</TestCase>
EOF

}

# please make sure that the en_US.UTF-8 locale is available!
# installing the “libmeegotouch-tests” package should create the en_US.UTF-8 locale
# automatically. Manually it can be done like this:
#   - apt-get install locales
#   - make sure /etc/locale.gen contains the line "en_US.UTF-8 UTF-8"
#   - run /usr/sbin/locale-gen
LC_ALL=en_US.UTF-8
export LC_ALL

CHARMAP=$(locale charmap)
if [ "$CHARMAP" != "UTF-8" ]; then
    echo "UTF-8 locale is missing, skipping all tests"
    echo "Installing the libmeegotouch-tests package should have created the en_US.UTF-8 locale"
    echo "but for some reason it appears to be missing."
    emptylog "ft_mgconfitem.log" "ft_mgconfitem" "UTF-8 locale is missing, skipping all tests. Installing the libmeegotouch-tests package should have created the en_US.UTF-8 locale but for some reason it appears to be missing."
    exit 0
fi

# Setup values expected by the external_values() test.

gconftool-2 -s -t bool /Test/Bool true
VALUE=$(gconftool-2 -g /Test/Bool)
if [ "$VALUE" != "true" ]; then
    emptylog "ft_mgconfitem.log" "ft_mgconfitem" "GConf is not running, skipping all tests"
    exit 0
fi
gconftool-2 -s -t int /Test/Int 123
gconftool-2 -s -t string /Test/String "Hello GConf"
gconftool-2 -s -t float /Test/Double 3.5
gconftool-2 -s -t list --list-type string /Test/StringList "[Hello,GConf,ÄÖÜ]"
gconftool-2 -s -t list --list-type int /Test/IntList "[1,2,3,4]"
gconftool-2 -s -t list --list-type float /Test/DoubleList "[3.5,3.5,3.5]"
gconftool-2 -s -t list --list-type bool /Test/BoolList "[false,true,true,false]"
gconftool-2 -u /Test/UnsetBefore
gconftool-2 -s -t int /Test/UnsetAfter 100
gconftool-2 -s -t int /Test/Dir/Entry 200

$(dirname $0)/ft_mgconfitem_exec $*

# Check what set_external() has left behind.

compare() {
  if [ "$1" != "$2" ]; then
#    Echo to stderr, our output seems to confuse some tools.
     echo >&2 "FAIL: '$1' != '$2'"
    exit 1
  fi
}

compare "`gconftool-2 -g /Test/Bool`" false
compare "`gconftool-2 -g /Test/Int`" 54321
compare "`gconftool-2 -g /Test/String`" "Good bye GConf"
compare "`gconftool-2 -g /Test/Double`" -2.5
compare "`gconftool-2 -g /Test/StringList`" "[Good,bye,GConf,äöü]"
compare "`gconftool-2 -g /Test/IntList`" "[5,4,3,2,1]"
compare "`gconftool-2 -g /Test/DoubleList`" "[-2.5,-2.5]"
compare "`gconftool-2 -g /Test/BoolList`" "[false,false,true,true]"
compare "`gconftool-2 -g /Test/UnsetAfter 2>&1`" 'No value set for `/Test/UnsetAfter'"'"
