exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

taler-terms-generator (6348B)


      1 #!/bin/bash
      2 # This file is part of GNU TALER.
      3 # Copyright (C) 2014-2023 Taler Systems SA
      4 #
      5 # TALER is free software; you can redistribute it and/or modify it under the
      6 # terms of the GNU Lesser General Public License as published by the Free Software
      7 # Foundation; either version 2.1, or (at your option) any later version.
      8 #
      9 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU Lesser General Public License along with
     14 # TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 #
     16 # @author Florian Dold
     17 # @author Benedikt Muller
     18 # @author Sree Harsha Totakura
     19 # @author Marcello Stanisci
     20 # @author Christian Grothoff
     21 #
     22 #
     23 # Error checking on
     24 set -eu
     25 
     26 # defaults
     27 AUTHOR="GNU Taler team"
     28 INPUT="exchange-tos-v0"
     29 if taler-exchange-config --version >/dev/null 2>&1
     30 then
     31     OUTPUT=${TALER_EXCHANGE_TERMS_DIR:-$(taler-exchange-config -s "EXCHANGE" -o "TERMS_DIR" -f)}
     32 else
     33     OUTPUT=${TALER_EXCHANGE_TERMS_DIR:-}
     34 fi
     35 PAPER="a4"
     36 COPYRIGHT="2014-2025 Taler Systems SA (GPLv3+ or GFDL 1.3+)"
     37 
     38 # Parse command-line options
     39 while getopts ':a:C:hi:l:o:p:t:' OPTION; do
     40   case "$OPTION" in
     41   a)
     42     AUTHOR="$OPTARG"
     43     ;;
     44   C)
     45     COPYRIGHT="$OPTARG"
     46     ;;
     47   h)
     48     echo 'Supported options:'
     49     echo '  -a AUTHOR     -- set author header' "(default: $AUTHOR)"
     50     echo '  -C COPYRIGHT  -- set copyright header' "(default: $COPYRIGHT)"
     51     echo '  -h            -- print this help'
     52     echo '  -i INPUT      -- input file to convert' "(default: $INPUT)"
     53     echo '  -l LANGUAGE   -- target language to use'
     54     echo '  -o OUTPUT     -- output directory' "(default: $OUTPUT)"
     55     echo '  -p PAPER      -- paper format' "(default: $PAPER)"
     56     echo '  -t TITLE      -- title of the document to generate'
     57     exit 0
     58     ;;
     59   l)
     60     ADD_LANGUAGE="$OPTARG"
     61     ;;
     62   i)
     63     INPUT="$OPTARG"
     64     ;;
     65   o)
     66     OUTPUT="$OPTARG"
     67     ;;
     68   p)
     69     PAPER="$OPTARG"
     70     case "$PAPER" in
     71     a4 | letter) ;;
     72     *)
     73       echo "Error: Paper format '$PAPER' invalid (use 'a4' or 'letter')" 1>&2
     74       exit 1
     75       ;;
     76     esac
     77     ;;
     78   t)
     79     TITLE="$OPTARG"
     80     ;;
     81   ?)
     82     echo "Unrecognized command line option" 1>&2
     83     exit 1
     84     ;;
     85   esac
     86 done
     87 
     88 # Throw away arguments already processed by getopt
     89 shift $(($OPTIND - 1))
     90 
     91 if [ ! -z "${1:-}" ]
     92 then
     93   echo "Error: Superfluous arguments given on command line ($@)." 1>&2
     94   exit 1
     95 fi
     96 
     97 if [ -z "${OUTPUT}" ]
     98 then
     99   echo "Error: '-o' option not given and autodetection failed." 1>&2
    100   exit 1
    101 fi
    102 
    103 if ! which pandoc >/dev/null;
    104 then
    105   echo "Command 'pandoc' not found, but required. Please install pandoc." 1>&2
    106   exit 1
    107 fi
    108 
    109 # FIXME: do we actually need 'gs'?
    110 if ! which gs >/dev/null;
    111 then
    112   echo "Command 'gs' not found, but required. Please install ghostscript." 1>&2
    113   exit 1
    114 fi
    115 
    116 if ! which pdfroff >/dev/null; then
    117   echo "Command 'pdfroff' not found, but required. Please install pdfroff/groff." 1>&2
    118   exit 1
    119 fi
    120 
    121 # Sometimes we just want the basename, not the directory.
    122 INPUT_BASENAME=$(basename "${INPUT}")
    123 
    124 # We append ".$LANG.rst" as needed, remove if given on command-line
    125 # shellcheck disable=SC2001
    126 if [ -z "${ADD_LANGUAGE:-}" ]
    127 then
    128     # Language not specified, check if INPUT provides a hint:
    129     echo $INPUT_BASENAME
    130     if echo "${INPUT_BASENAME}" | grep -e '\...\.rst$' >/dev/null;
    131     then
    132         # Extract language from basename
    133         ADD_LANGUAGE=$(echo "${INPUT_BASENAME}" | awk -F . '{print $2}')
    134     else
    135         # Default to English
    136         ADD_LANGUAGE="en"
    137     fi
    138 fi
    139 
    140 
    141 # We append ".$LANG.rst" as needed, remove to get VERSION if given on command-line
    142 # shellcheck disable=SC2001
    143 VERSION=$(echo "${INPUT_BASENAME}" | sed -e "s/\...\.rst$//")
    144 
    145 # In case absolute path is given for the input, keep that as well
    146 INPUT_DIRNAME=$(dirname "${INPUT}")
    147 
    148 if ! echo "${ADD_LANGUAGE}" | grep -e '^..$' >/dev/null;
    149 then
    150     echo "Error: Invalid language '${ADD_LANGUAGE}'. Two characters (en, de, fr, ...) expected." 1>&2
    151     exit 1
    152 fi
    153 
    154 # Find input file
    155 if [ ! -f "${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst" ];
    156 then
    157     # Try with default installation path
    158     if [ "." = "${INPUT_DIRNAME}" ]
    159     then
    160         # Try again with default system path
    161         INPUT_DIRNAME="/usr/share/taler-exchange/terms"
    162     else
    163         echo "Error: File '${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst' not found. Please check '-i' option." 1>&2
    164         exit 1
    165     fi
    166 
    167     if [ ! -f "${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst" ];
    168     then
    169         echo "Error: File '${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst' not found. Please check '-i' option." 1>&2
    170         exit 1
    171     fi
    172 fi
    173 
    174 BUILDFILE_RST="${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst"
    175 
    176 if [ -z ${TITLE+x} ];
    177 then
    178   TITLE=$(head -n1 "$BUILDFILE_RST")
    179   echo "Title automatically set to '$TITLE'" 1>&2
    180 fi
    181 
    182 # shellcheck disable=SC2086
    183 echo "Generating files at '$OUTPUT' for version '$VERSION' and language '${ADD_LANGUAGE}':" 1>&2
    184 
    185 mkdir -p "${OUTPUT}/${ADD_LANGUAGE}/"
    186 
    187 OUTBASE="${OUTPUT}/${ADD_LANGUAGE}/${VERSION}"
    188 
    189 echo "$VERSION MD ($ADD_LANGUAGE)..." 1>&2
    190 pandoc \
    191     -i "$BUILDFILE_RST" \
    192     -o "${OUTBASE}.md" \
    193     --pdf-engine=pdfroff \
    194     --variable=title:"$TITLE" \
    195     --variable=lang:"$ADD_LANGUAGE" \
    196     --variable=author:"$AUTHOR" \
    197     --variable=rights:"$COPYRIGHT" \
    198     --shift-heading-level-by=-1
    199 
    200 echo "$VERSION PDF ($ADD_LANGUAGE)..." 1>&2
    201 pandoc \
    202     -i "$BUILDFILE_RST" \
    203     -o "${OUTBASE}.pdf" \
    204     --pdf-engine=pdfroff \
    205     --variable=title:"$TITLE" \
    206     --variable=lang:"$ADD_LANGUAGE" \
    207     --variable=author:"$AUTHOR" \
    208     --variable=rights:"$COPYRIGHT" \
    209     --variable=papersize:"$PAPER" \
    210     --shift-heading-level-by=-1
    211 
    212 echo "$VERSION HTML ($ADD_LANGUAGE)..." 1>&2
    213 # FIXME: Newer versions of pandic should use
    214 # --embed-resources --standalone instead of --self-contained
    215 pandoc \
    216     -i "$BUILDFILE_RST" \
    217     -o "${OUTBASE}.html" \
    218     --variable=title:"$TITLE" \
    219     --variable=lang:"$ADD_LANGUAGE" \
    220     --variable=author:"$AUTHOR" \
    221     --variable=rights:"$COPYRIGHT" \
    222     --self-contained \
    223     --shift-heading-level-by=-1
    224 
    225 echo "$VERSION TXT ($ADD_LANGUAGE)..." 1>&2
    226 pandoc \
    227     -i "$BUILDFILE_RST" \
    228     -o "${OUTBASE}.txt"
    229 
    230 echo "Done" 1>&2
    231 exit 0