aboutsummaryrefslogtreecommitdiff
path: root/contrib/taler-terms-generator
blob: 26cfd775b480edb3d5119cc7abbc8fed8bad16cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/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 <http://www.gnu.org/licenses/>
#
# @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()
{
    cat > "${BUILDDIR}/conf.py" <<EOF
import sys
import os
sys.path.append(os.path.abspath('_exts'))
needs_sphinx = '1.8.5'
extensions = [
    'sphinx.ext.todo',
    'sphinx.ext.imgmath',
    'sphinx_markdown_builder',
]
templates_path = ['_templates']
source_suffix = {
    '.rst': 'restructuredtext',
}
master_doc = '$VERSION_BASENAME'
project = u'$VERSION_BASENAME'
copyright = u'$COPYRIGHT'
version = '$VERSION_BASENAME'
release = '$VERSION_BASENAME'
language = "$LANGUAGE"
exclude_patterns = ['_build', '_exts', 'cf', 'prebuilt']
locale_dirs = ['$LOCALE_DIR/']
gettext_compact = False
pygments_style = 'sphinx'
html_theme = 'epub'
rst_epilog = ""
html_show_sphinx = False
html_theme_options = {
    "relbar1": "false",
    "footer": "false",
}
html_title = "$TITLE"
html_short_title = "$TITLE"
html_use_index = True
html_show_sphinx = False
latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    #'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    #'preamble': '',
}
latex_documents = [
    ('$VERSION_BASENAME', '$VERSION_BASENAME.tex',
     '$VERSION_BASENAME', '$VERSION_BASENAME', 'manual'),
]
epub_basename = "$VERSION_BASENAME"
epub_title = "$TITLE"
EOF
}

# Output file given as first argument to stderr, then exit with a failure.
function failcat ()
{
    cat "$1" 1>&2
    exit 1
}


# defaults
AUTHOR="GNU Taler team"
VERSION="exchange-tos-v0"
LOCALE_DIR=$(taler-config -s "PATHS" -o "LOCALEDIR" -f)
OUTPUT=$(taler-config -s "EXCHANGE" -o "TERMS_DIR" -f)
PAPER="a4"
COPYRIGHT="2014-2023 Taler Systems SA (GPLv3+ or GFDL 1.3+)"
INCREMENTAL=0

# Parse command-line options
while getopts ':a:C:hKi:l:L:o:p:t:' OPTION; do
    case "$OPTION" in
        a)
            AUTHOR="$OPTARG"
            ;;
        C)
            COPYRIGHT="$OPTARG"
            ;;
        h)
            echo 'Supported options:'
            echo '  -a AUTHOR     -- set author header' "(default: $AUTHOR)"
            echo '  -C COPYRIGHT  -- set copyright header' "(default: $COPYRIGHT)"
            echo '  -h            -- print this help'
            echo '  -K            -- rebuild only if input is older than output'
            echo '  -i INPUT      -- input file to convert' "(default: $VERSION)"
            echo '  -l LANGUAGE   -- target language to add'
            echo '  -L LOCALE_DIR -- directory with resources for translation' "(default: $LOCALE_DIR)"
            echo '  -o OUTPUT     -- output directory' "(default: $OUTPUT)"
            echo '  -p PAPER      -- paper format' "(default: $PAPER)"
            echo '  -t TITLE      -- title of the document to generate'
            exit 0
            ;;
        l)
            ADD_LANGUAGE="$OPTARG"
            ;;
        L)
            LOCALE_DIR="$OPTARG"
            ;;
        i)
            VERSION="$OPTARG"
            ;;
        o)
            OUTPUT="$OPTARG"
            ;;
        p)
            PAPER="$OPTARG"
            case "$PAPER" in
                a4|letter)
                ;;
                *)
                    echo "Error: Paper format '$PAPER' invalid (use 'a4' or 'letter')" 1>&2
                    exit 1
                    ;;
            esac
            ;;
        t)
            TITLE="$OPTARG"
            ;;
        K)
            INCREMENTAL=1
            ;;
        ?)
        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

if ! which make > /dev/null
then
    echo "Command 'make' not found, but required. Please install make." 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" \
        || failcat "${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

# As a heuristic for incremental builds, we only check the text output file.
if [[ "$INCREMENTAL" -eq 1 ]]
then
  if [[ "${VERSION}.rst" -ot "${OUTPUT}/buildstamp" ]];
  then
    echo "Not rebuilding, input file $VERSION is older than $OUTPUT/buildstamp."
    exit 0
  fi
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" \
        || failcat "${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" \
        || failcat "${BUILDDIR}/txt-sphinx.log"
    mv "${BUILDDIR}/txt/${VERSION_BASENAME}.txt" "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.txt"

    echo "$VERSION_BASENAME MD ($LANGUAGE)..." 1>&2
    $LBUILD \
        -b markdown \
        "${BUILDDIR}" \
        "${BUILDDIR}/md" \
        &> "${BUILDDIR}/md-sphinx.log" \
        || failcat "${BUILDDIR}/md-sphinx.log"
    mv "${BUILDDIR}/md/${VERSION_BASENAME}.md" "${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" \
        || failcat "${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" \
        || failcat "${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" \
        || failcat "${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" \
        || failcat "${BUILDDIR}/pdf-latex.log"
    mv "${BUILDDIR}/pdf/${VERSION_BASENAME}.pdf" "${OUTPUT}/${LANGUAGE}/${VERSION_BASENAME}.pdf"
done

if [[ "$INCREMENTAL" -eq 1 ]]
then
  touch "${OUTPUT}/buildstamp";
fi

echo "Done" 1>&2
exit 0