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

# Test of gettext facilities in the OCaml language.
# Assumes an fr_FR locale is installed.
# Assumes the following packages are installed:
#   - OCaml,
#   - 'opam', the OCaml package manager.
# The following get installed through 'opam':
#   - 'dune', part of the OCaml build system,
#   - the opam package 'gettext-stub'
#     <https://opam.ocaml.org/packages/gettext-stub/>
#     <https://github.com/gildor478/ocaml-gettext/>
#     <https://gildor478.github.io/ocaml-gettext/>

# Test for presence of opam.
(opam --version) >/dev/null 2>/dev/null \
  || { echo "Skipping test: opam not found"; Exit 77; }

# Set environment variables for using opam, including PATH.
eval $(opam env)

# Ensure dune is installed.
opam install dune \
  || { echo "Skipping test: failed to install dune"; Exit 77; }

# Test for presence of dune.
(dune --version) >/dev/null 2>/dev/null \
  || { echo "Skipping test: dune not found"; Exit 77; }

cat <<\EOF > prog.ml
module ConfiguredGettext =
  (* See https://gildor478.github.io/ocaml-gettext/gettext/Gettext/Program/index.html *)
  Gettext.Program
    (* See https://gildor478.github.io/ocaml-gettext/gettext/GettextTypes/module-type-INIT_TYPE/index.html *)
    (struct
      let textdomain = "prog"
      let codeset = None
      let dir = Some "."
      let dependencies = []
    end)
    (* See https://github.com/gildor478/ocaml-gettext/blob/master/doc/reference-manual.md *)
    (GettextStub.Native)

open ConfiguredGettext;;
open Printf;;

let () =
  let arg1 = Sys.argv.(1) in
  let n = int_of_string arg1 in
  print_endline (s_ "'Your command, please?', asked the waiter.");
  print_endline (sprintf (fn_ "%u piece of cake" "%u pieces of cake" n) n)
EOF

: ${XGETTEXT=xgettext}
${XGETTEXT} -o prog.tmp --omit-header --no-location prog.ml \
  || 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 ""

#, ocaml-format
msgid "%u piece of cake"
msgid_plural "%u pieces of cake"
msgstr[0] ""
msgstr[1] ""
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.
#, ocaml-format
msgid "%u piece of cake"
msgid_plural "%u pieces of cake"
msgstr[0] "un morceau de gateau"
msgstr[1] "%u morceaux de gateau"
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} -c -o fr/LC_MESSAGES/prog.mo fr.po

opam install gettext-stub \
  || { echo "Skipping test: failed to install gettext-stub"; Exit 77; }

cat > dune <<\EOF
(executable
  (name prog)
  (libraries gettext.base gettext-stub unix))
EOF

cat > dune-project <<\EOF
(lang dune 3.19)
(name prog)
EOF

dune build --root=. prog.exe \
  || { echo "Build failure" 1>&2; Exit 1; }

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

: ${LOCALE_FR=fr_FR}
: ${LOCALE_FR_UTF8=fr_FR.UTF-8}
if test $LOCALE_FR != none; then
  LC_ALL=$LOCALE_FR LANGUAGE= _build/default/prog.exe 2 > prog.out
  case $? in
    0) ${DIFF} prog.ok prog.out || Exit 1;;
    77) LOCALE_FR=none;;
    *) Exit 1;;
  esac
fi
if test $LOCALE_FR_UTF8 != none; then
  LC_ALL=$LOCALE_FR_UTF8 LANGUAGE= _build/default/prog.exe 2 > prog.out
  case $? in
    0) ${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
