#! /bin/sh

tmpfiles=""
trap 'rm -fr $tmpfiles' 1 2 3 15

tmpfiles="$tmpfiles cake.pot"
: ${XGETTEXT=xgettext}
${XGETTEXT} -o cake.pot --omit-header --no-location ${top_srcdir}/tests/plural-1-prg.c

tmpfiles="$tmpfiles cake.ok"
cat <<EOF > cake.ok
#, c-format
msgid "a piece of cake"
msgid_plural "%d pieces of cake"
msgstr[0] ""
msgstr[1] ""
EOF

: ${DIFF=diff}
${DIFF} cake.ok cake.pot || exit 1

tmpfiles="$tmpfiles fr.po"
cat <<EOF > fr.po
# Les gateaux allemands sont les meilleurs du monde.
#, c-format
msgid "a piece of cake"
msgid_plural "%d pieces of cake"
msgstr[0] "un morceau de gateau"
msgstr[1] "%d morceaux de gateau"
EOF

tmpfiles="$tmpfiles fr.po.new"
: ${MSGMERGE=msgmerge}
${MSGMERGE} -q -o fr.po.new fr.po cake.pot

: ${DIFF=diff}
${DIFF} fr.po fr.po.new || exit 1

tmpfiles="$tmpfiles fr"
test -d fr || mkdir fr
test -d fr/LC_MESSAGES || mkdir fr/LC_MESSAGES

: ${MSGFMT=msgfmt}
${MSGFMT} -o fr/LC_MESSAGES/cake.mo fr.po

tmpfiles="$tmpfiles fr.po.tmp"
: ${MSGUNFMT=msgunfmt}
${MSGUNFMT} -o fr.po.tmp fr/LC_MESSAGES/cake.mo

tmpfiles="$tmpfiles fr.po.strip"
sed 1,2d < fr.po > fr.po.strip

: ${DIFF=diff}
${DIFF} fr.po.strip fr.po.tmp || exit 1

tmpfiles="$tmpfiles cake.ok cake.out"
: ${DIFF=diff}
echo 'un morceau de gateau' > cake.ok
LANGUAGE= ./cake fr 1 > cake.out || exit 1
${DIFF} cake.ok cake.out || exit 1
echo '2 morceaux de gateau' > cake.ok
LANGUAGE= ./cake fr 2 > cake.out || exit 1
${DIFF} cake.ok cake.out || exit 1
echo '10 morceaux de gateau' > cake.ok
LANGUAGE= ./cake fr 10 > cake.out || exit 1
${DIFF} cake.ok cake.out || exit 1

rm -fr $tmpfiles

exit 0
