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

# Test of gettext facilities in the D language.
# Assumes an fr_FR locale is installed.
# Assumes the following packages are installed: gdc or ldc or dmd.

# Test whether we can build and test D programs.
test "${D_CHOICE}" != no || {
  echo "Skipping test: configured with --disable-d"
  Exit 77
}
test "${BUILDD}" = yes || {
  echo "Skipping test: D compiler not found"
  Exit 77
}

cat <<\EOF > prog.d
// Get writeln.
import std.stdio;
// Get format.
import std.format;
// Get to, text.
import std.conv;
// Get locale constants.
import core.stdc.locale : LC_ALL;
// Get exit.
import core.stdc.stdlib : exit;
// Get textdomain, bindtextdomain, gettext, ngettext, setlocale.
import gnu.libintl : textdomain, bindtextdomain, gettext, ngettext, setlocale;

// Define _() as a shorthand for gettext().
alias _ = gettext;

void main (string[] args)
{
  int n = to!int (args[1]);

  if (!setlocale (LC_ALL, ""))
    /* Couldn't set locale.  */
    exit (77);

  textdomain ("prog");
  bindtextdomain ("prog", ".");

  // Any of these three is valid syntax.
  // Cf. https://dlang.org/spec/function.html#pseudo-member
  //writeln (_("'Your command, please?', asked the waiter."));
  //"'Your command, please?', asked the waiter.".gettext().writeln;
  "'Your command, please?', asked the waiter.".gettext.writeln;

  writeln (format (ngettext ("%.0sa piece of cake", "%s pieces of cake", n), text(n)));
  writeln (format (_ ("%s is replaced by %s."), "FF", "EUR"));
}
EOF

: ${XGETTEXT=xgettext}
${XGETTEXT} -o prog.tmp --omit-header --no-location \
            -k_ --flag=_:1:pass-c-format --flag=_:1:pass-d-format \
            prog.d \
  || Exit 1
LC_ALL=C tr -d '\r' < prog.tmp > prog.pot || Exit 1

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

#, d-format
msgid "%.0sa piece of cake"
msgid_plural "%s pieces of cake"
msgstr[0] ""
msgstr[1] ""

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

: ${DIFF=diff}
${DIFF} prog.ok prog.pot || Exit 1

cat <<\EOF > fr.po
msgid ""
msgstr ""
"Content-Type: text/plain; charset=ISO-8859-1\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

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

# Les gateaux allemands sont les meilleurs du monde.
#, d-format
msgid "%.0sa piece of cake"
msgid_plural "%s pieces of cake"
msgstr[0] "%.0sun morceau de gateau"
msgstr[1] "%s morceaux de gateau"

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

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

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

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

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

${SHELL-/bin/sh} "$top_builddir"/dcomp.sh \
                 -I"$abs_top_srcdir"/../gettext-runtime/intl-d \
                 -c prog.d \
  || Exit 1

: ${CONFIG_SHELL=${SHELL-/bin/sh}}
${CONFIG_SHELL} "$top_builddir"/libtool --quiet --tag=CC --mode=link \
  ${SHELL-/bin/sh} "$top_builddir"/dcomp.sh \
                   -o prog \
                   prog.${OBJEXT} \
                   "$top_builddir"/../gettext-runtime/intl-d/libintl_d.a \
                   ${LTLIBINTL} \
  || Exit 1

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

: ${LOCALE_FR=fr_FR}
: ${LOCALE_FR_UTF8=fr_FR.UTF-8}
if test $LOCALE_FR != none; then
  prepare_locale_ fr $LOCALE_FR
  LC_ALL=$LOCALE_FR LANGUAGE= ./prog 2 > prog.tmp
  case $? in
    0) case "$host_os" in
         mingw*) LC_ALL=C tr -d '\r' < prog.tmp > prog.out || Exit 1 ;;
         *) cp prog.tmp prog.out || Exit 1 ;;
       esac
       ${DIFF} prog.ok prog.out || Exit 1;;
    77) LOCALE_FR=none;;
    *) Exit 1;;
  esac
fi
if test $LOCALE_FR_UTF8 != none; then
  prepare_locale_ fr $LOCALE_FR_UTF8
  LC_ALL=$LOCALE_FR_UTF8 LANGUAGE= ./prog 2 > prog.tmp
  case $? in
    0) case "$host_os" in
         mingw*) LC_ALL=C tr -d '\r' < prog.tmp > prog.out || Exit 1 ;;
         *) cp prog.tmp prog.out || Exit 1 ;;
       esac
       ${DIFF} prog.oku 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
