#! /bin/sh
. "${srcdir=.}/init.sh"; path_prepend_ . ../src

# Test of gettext facilities in the Vala language.
# Assumes an fr_FR locale is installed.
# Assumes the following packages are installed: vala.

cat <<\EOF > l-vala-prog.vala
int main (string[] args) {
    Intl.setlocale (LocaleCategory.ALL, "");
    Intl.textdomain ("prog");
    Intl.bindtextdomain ("prog", "l-vala");
    stdout.printf ("%s\n", _("'Your command, please?', asked the waiter."));
    stdout.printf ("%s\n", _("%s is replaced by %s.").printf ("FF", "EUR"));
    return 0;
}
EOF

# Test for presence of valac.
(valac --version) >/dev/null 2>/dev/null
test $? -le 1 \
  || { echo "Skipping test: valac not found"; exit 77; }

: ${VALAC=valac}
${VALAC} --Xcc=-DGETTEXT_PACKAGE=\"prog\" l-vala-prog.vala 2>l-vala-prog.err \
  || { cat l-vala-prog.err 1>&2; exit 1; }

: ${XGETTEXT=xgettext}
${XGETTEXT} -o l-vala-prog.tmp --omit-header --no-location l-vala-prog.vala || exit 1
LC_ALL=C tr -d '\r' < l-vala-prog.tmp > l-vala-prog.pot || exit 1

cat <<EOF > l-vala-prog.ok
msgid "'Your command, please?', asked the waiter."
msgstr ""

#, c-format
msgid "%s is replaced by %s."
msgstr ""
EOF

: ${DIFF=diff}
${DIFF} l-vala-prog.ok l-vala-prog.pot || exit 1

cat <<\EOF > l-vala-fr.po
msgid ""
msgstr "Content-Type: text/plain; charset=ISO-8859-1\n"

msgid "'Your command, please?', asked the waiter."
msgstr "Votre commande, s'il vous plait, dit le garon."

# Reverse the arguments.
#, c-format
msgid "%s is replaced by %s."
msgstr "%2$s remplace %1$s."
EOF

: ${MSGMERGE=msgmerge}
${MSGMERGE} -q -o l-vala-fr.po.tmp l-vala-fr.po l-vala-prog.pot || exit 1
LC_ALL=C tr -d '\r' < l-vala-fr.po.tmp > l-vala-fr.po.new || exit 1

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

test -d l-vala || mkdir l-vala
test -d l-vala/fr || mkdir l-vala/fr
test -d l-vala/fr/LC_MESSAGES || mkdir l-vala/fr/LC_MESSAGES

: ${MSGFMT=msgfmt}
${MSGFMT} -o l-vala/fr/LC_MESSAGES/prog.mo l-vala-fr.po

: ${DIFF=diff}
cat <<\EOF > l-vala-prog.ok
Votre commande, s'il vous plait, dit le garon.
EUR remplace FF.
EOF
cat <<\EOF > l-vala-prog.oku
«Votre commande, s'il vous plait», dit le garçon.
EUR remplace FF.
EOF

: ${LOCALE_FR=fr_FR}
: ${LOCALE_FR_UTF8=fr_FR.UTF-8}
if test $LOCALE_FR != none; then
  LANGUAGE= LC_ALL=$LOCALE_FR ./l-vala-prog > l-vala-prog.out
  case $? in
    0) ${DIFF} l-vala-prog.ok l-vala-prog.out || exit 1;;
    77) LOCALE_FR=none;;
    *) exit 1;;
  esac
fi
if test $LOCALE_FR_UTF8 != none; then
  LANGUAGE= LC_ALL=$LOCALE_FR_UTF8 ./l-vala-prog > l-vala-prog.out
  case $? in
    0) ${DIFF} l-vala-prog.oku l-vala-prog.out || exit 1;;
    77) LOCALE_FR_UTF8=none;;
    *) exit 1;;
  esac
fi
if test $LOCALE_FR = none && test $LOCALE_FR_UTF8 = none; then
  if test -f /usr/bin/localedef; then
    echo "Skipping test: no french locale is installed"
  else
    echo "Skipping test: no french locale is supported"
  fi
  exit 77
fi

exit 0
