#!/bin/sh

set -e

LOCALES_DIR="${DESTDIR}/usr/share/locale"

run() {
  echo "$@"
  "$@"
}

for filename in $(find po/ -name '*.po' -type f); do
  language="$(basename "${filename}" .po)"
  domain="$(basename "$(dirname "$filename")")"
  output_dir="${LOCALES_DIR}/${language}/LC_MESSAGES"
  output_file="${output_dir}/${domain}.mo"
  if [ ! -d "${output_dir}" ]; then
    run install -d -m 0755 "${output_dir}"
  fi
  # --endianness => Ensure consistent output even if built on a big-endian host
  run msgfmt --statistics --endianness=little -c "${filename}" -o "${output_file}"
done
