#!/bin/bash
# This file is part of GNU TALER.
# Copyright (C) 2014-2023 Taler Systems SA
#
# TALER is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 2.1, or (at your option) any later version.
#
# TALER is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with
# TALER; see the file COPYING. If not, see
#
# @author Florian Dold
# @author Benedikt Muller
# @author Sree Harsha Totakura
# @author Marcello Stanisci
# @author Christian Grothoff
#
#
# Error checking on
set -eu
# Call with target language as first argument.
function make_config()
{
XLOC=$(echo "$LOCALE_DIR" | sed -e "s/\//\\\\\\//g")
sed -e "s/%VERSION%/$VERSION/g" \
-e "s/%TITLE%/$TITLE/g" \
-e "s/%AUTHOR%/$AUTHOR/g" \
-e "s/%LOCALE_DIR%/$XLOC/g" \
-e "s/%COPYRIGHT%/$COPYRIGHT/g" \
-e "s/%LANGUAGE%/$1/g" \
> "${BUILDDIR}/conf.py" <&2
exit 1
;;
esac
;;
t)
TITLE="$OPTARG"
;;
?)
echo "Unrecognized command line option" 1>&2
exit 1
;;
esac
done
if ! which sphinx-build > /dev/null
then
echo "Command 'sphinx-build' not found, but required. Please install sphinx." 1>&2
exit 1
fi
if ! which latexmk > /dev/null
then
echo "Command 'latexmk' not found, but required. Please install latexmk." 1>&2
exit 1
fi
# We append ".rst" if needed, remove if given on command-line
# shellcheck disable=SC2001
VERSION=$(echo "${VERSION}" | sed -e "s/\.rst$//")
# Sometimes we just want the basename, not the directory.
VERSION_BASENAME=$(basename "${VERSION}")
BUILDDIR=$(mktemp -d /tmp/taler-terms-XXXXXX)
if [ ! -f "${VERSION}.rst" ]
then
echo "Error: File '${VERSION}.rst' not found. Please check '-i' option." 1>&2
exit 1
fi
cp "${VERSION}.rst" "${BUILDDIR}/"
if [ -z ${TITLE+x} ]
then
TITLE=$(head -n1 "${VERSION}.rst")
echo "Title automatically set to '$TITLE'" 1>&2
fi
if [ -n "${ADD_LANGUAGE+x}" ]
then
if ! echo "${ADD_LANGUAGE}" | grep -e '^..$' > /dev/null
then
echo "Error: Invalid language '${ADD_LANGUAGE}'. Two characters (en, de, fr, ...) expected." 1>&2
exit 1
fi
echo "Adding language files for translations to '${ADD_LANGUAGE}'" 1>&2
make_config "${ADD_LANGUAGE}"
sphinx-build \
-b gettext \
-D language="${ADD_LANGUAGE}" \
-d "${BUILDDIR}/.doctrees" \
"${BUILDDIR}" \
"${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/" \
&> "${BUILDDIR}/add-language.log"
if [ -f "${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.po" ]
then
msgmerge --lang="${ADD_LANGUAGE}" \
--no-location \
-o "${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.mrg" \
"${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.po" \
"${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.pot"
mv "${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.mrg" \
"${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.po"
else
mv "${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.pot" \
"${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.po"
fi
rm -f "${LOCALE_DIR}/${ADD_LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.pot"
echo "Done" 1>&2
exit 0
fi
# shellcheck disable=SC2086
for d in en $(ls -d ${LOCALE_DIR}/?? | grep -v "en" 2> /dev/null || true)
do
LANGUAGE=$(basename "$d")
if [ "en" != "${LANGUAGE}" ] && [ ! -f "${LOCALE_DIR}/${LANGUAGE}/LC_MESSAGES/${VERSION_BASENAME}.po" ]
then
echo "Skipping language ${LANGUAGE}: no translation for ${VERSION_BASENAME} found."
continue
fi
echo "Generating files at '$OUTPUT' for ETag '$VERSION_BASENAME' and language '${LANGUAGE}' in '${BUILDDIR}':" 1>&2
make_config "$LANGUAGE"
mkdir -p "${OUTPUT}/${LANGUAGE}/"
LBUILD="sphinx-build -D language=${LANGUAGE} -d ${BUILDDIR}/.doctrees"
echo "$VERSION_BASENAME XML ($LANGUAGE)..." 1>&2
# shellcheck disable=SC2090
$LBUILD \
-b xml \
"${BUILDDIR}" \
"${BUILDDIR}/xml" \
&> "${BUILDDIR}/xml-sphinx.log"
mv "${BUILDDIR}/xml/${VERSION_BASENAME}.xml" "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.xml"
echo "$VERSION_BASENAME TXT ($LANGUAGE)..." 1>&2
# shellcheck disable=SC2090
$LBUILD \
-b text \
"${BUILDDIR}" \
"${BUILDDIR}/txt" \
&> "${BUILDDIR}/txt-sphinx.log"
mv "${BUILDDIR}/txt/${VERSION_BASENAME}.txt" "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.txt"
cp "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.txt" "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.md"
echo "$VERSION_BASENAME HTML ($LANGUAGE)..." 1>&2
# shellcheck disable=SC2090
$LBUILD \
-b html \
"${BUILDDIR}" \
"${BUILDDIR}/html" \
&> "${BUILDDIR}/html-sphinx.log"
htmlark \
-o "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.html" \
"${BUILDDIR}/html/${VERSION_BASENAME}.html"
echo "$VERSION_BASENAME EPUB ($LANGUAGE)..." 1>&2
# shellcheck disable=SC2090
$LBUILD \
-b epub \
"${BUILDDIR}" \
"${BUILDDIR}/epub" \
&> "${BUILDDIR}/epub-sphinx.log"
mv "${BUILDDIR}/epub/${VERSION_BASENAME}.epub" "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.epub"
echo "$VERSION_BASENAME PDF ($LANGUAGE)..." 1>&2
# shellcheck disable=SC2090
$LBUILD \
-b latex \
-D latex_paper_size="${PAPER}" \
"${BUILDDIR}" \
"${BUILDDIR}/pdf" \
&> "${BUILDDIR}/pdf-sphinx.log"
# We pipe in /dev/null in case latexmk
# asks for input and would hang otherwise.
make \
-C "${BUILDDIR}/pdf" \
all-pdf \
< /dev/null &> "${BUILDDIR}/pdf-latex.log"
mv "${BUILDDIR}/pdf/${VERSION_BASENAME}.pdf" "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.pdf"
done
echo "Done" 1>&2
exit 0