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 (6130B)


      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 OUTPUT=${TALER_EXCHANGE_TERMS_DIR:-$(taler-exchange-config -s "EXCHANGE" -o "TERMS_DIR" -f)}
     30 PAPER="a4"
     31 COPYRIGHT="2014-2025 Taler Systems SA (GPLv3+ or GFDL 1.3+)"
     32 
     33 # Parse command-line options
     34 while getopts ':a:C:hi:l:o:p:t:' OPTION; do
     35   case "$OPTION" in
     36   a)
     37     AUTHOR="$OPTARG"
     38     ;;
     39   C)
     40     COPYRIGHT="$OPTARG"
     41     ;;
     42   h)
     43     echo 'Supported options:'
     44     echo '  -a AUTHOR     -- set author header' "(default: $AUTHOR)"
     45     echo '  -C COPYRIGHT  -- set copyright header' "(default: $COPYRIGHT)"
     46     echo '  -h            -- print this help'
     47     echo '  -i INPUT      -- input file to convert' "(default: $INPUT)"
     48     echo '  -l LANGUAGE   -- target language to use'
     49     echo '  -o OUTPUT     -- output directory' "(default: $OUTPUT)"
     50     echo '  -p PAPER      -- paper format' "(default: $PAPER)"
     51     echo '  -t TITLE      -- title of the document to generate'
     52     exit 0
     53     ;;
     54   l)
     55     ADD_LANGUAGE="$OPTARG"
     56     ;;
     57   i)
     58     INPUT="$OPTARG"
     59     ;;
     60   o)
     61     OUTPUT="$OPTARG"
     62     ;;
     63   p)
     64     PAPER="$OPTARG"
     65     case "$PAPER" in
     66     a4 | letter) ;;
     67     *)
     68       echo "Error: Paper format '$PAPER' invalid (use 'a4' or 'letter')" 1>&2
     69       exit 1
     70       ;;
     71     esac
     72     ;;
     73   t)
     74     TITLE="$OPTARG"
     75     ;;
     76   ?)
     77     echo "Unrecognized command line option" 1>&2
     78     exit 1
     79     ;;
     80   esac
     81 done
     82 
     83 # Throw away arguments already processed by getopt
     84 shift $(($OPTIND - 1))
     85 
     86 if [ ! -z "${1:-}" ]
     87 then
     88   echo "Error: Superfluous arguments given on command line ($@)." 1>&2
     89   exit 1
     90 fi
     91 
     92 if ! which pandoc >/dev/null; then
     93   echo "Command 'pandoc' not found, but required. Please install pandoc." 1>&2
     94   exit 1
     95 fi
     96 
     97 # FIXME: do we actually need 'gs'?
     98 if ! which gs >/dev/null; then
     99   echo "Command 'gs' not found, but required. Please install ghostscript." 1>&2
    100   exit 1
    101 fi
    102 
    103 if ! which pdfroff >/dev/null; then
    104   echo "Command 'pdfroff' not found, but required. Please install pdfroff/groff." 1>&2
    105   exit 1
    106 fi
    107 
    108 # Sometimes we just want the basename, not the directory.
    109 INPUT_BASENAME=$(basename "${INPUT}")
    110 
    111 # We append ".$LANG.rst" as needed, remove if given on command-line
    112 # shellcheck disable=SC2001
    113 if [ -z "${ADD_LANGUAGE:-}" ]
    114 then
    115     # Language not specified, check if INPUT provides a hint:
    116     echo $INPUT_BASENAME
    117     if echo "${INPUT_BASENAME}" | grep -e '\...\.rst$' >/dev/null;
    118     then
    119         # Extract language from basename
    120         ADD_LANGUAGE=$(echo "${INPUT_BASENAME}" | awk -F . '{print $2}')
    121     else
    122         # Default to English
    123         ADD_LANGUAGE="en"
    124     fi
    125 fi
    126 
    127 
    128 # We append ".$LANG.rst" as needed, remove to get VERSION if given on command-line
    129 # shellcheck disable=SC2001
    130 VERSION=$(echo "${INPUT_BASENAME}" | sed -e "s/\...\.rst$//")
    131 
    132 # In case absolute path is given for the input, keep that as well
    133 INPUT_DIRNAME=$(dirname "${INPUT}")
    134 
    135 if ! echo "${ADD_LANGUAGE}" | grep -e '^..$' >/dev/null;
    136 then
    137     echo "Error: Invalid language '${ADD_LANGUAGE}'. Two characters (en, de, fr, ...) expected." 1>&2
    138     exit 1
    139 fi
    140 
    141 # Find input file
    142 if [ ! -f "${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst" ];
    143 then
    144     # Try with default installation path
    145     if [ "." = "${INPUT_DIRNAME}" ]
    146     then
    147         # Try again with default system path
    148         INPUT_DIRNAME="/usr/share/taler-exchange/terms"
    149     else
    150         echo "Error: File '${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst' not found. Please check '-i' option." 1>&2
    151         exit 1
    152     fi
    153 
    154     if [ ! -f "${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst" ];
    155     then
    156         echo "Error: File '${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst' not found. Please check '-i' option." 1>&2
    157         exit 1
    158     fi
    159 fi
    160 
    161 BUILDFILE_RST="${INPUT_DIRNAME}/${VERSION}.${ADD_LANGUAGE}.rst"
    162 
    163 if [ -z ${TITLE+x} ];
    164 then
    165   TITLE=$(head -n1 "$BUILDFILE_RST")
    166   echo "Title automatically set to '$TITLE'" 1>&2
    167 fi
    168 
    169 # shellcheck disable=SC2086
    170 echo "Generating files at '$OUTPUT' for version '$VERSION' and language '${ADD_LANGUAGE}':" 1>&2
    171 
    172 mkdir -p "${OUTPUT}/${ADD_LANGUAGE}/"
    173 
    174 OUTBASE="${OUTPUT}/${ADD_LANGUAGE}/${VERSION}"
    175 
    176 echo "$VERSION MD ($ADD_LANGUAGE)..." 1>&2
    177 pandoc \
    178     -i "$BUILDFILE_RST" \
    179     -o "${OUTBASE}.md" \
    180     --pdf-engine=pdfroff \
    181     --variable=title:"$TITLE" \
    182     --variable=lang:"$ADD_LANGUAGE" \
    183     --variable=author:"$AUTHOR" \
    184     --variable=rights:"$COPYRIGHT" \
    185     --shift-heading-level-by=-1
    186 
    187 echo "$VERSION PDF ($ADD_LANGUAGE)..." 1>&2
    188 pandoc \
    189     -i "$BUILDFILE_RST" \
    190     -o "${OUTBASE}.pdf" \
    191     --pdf-engine=pdfroff \
    192     --variable=title:"$TITLE" \
    193     --variable=lang:"$ADD_LANGUAGE" \
    194     --variable=author:"$AUTHOR" \
    195     --variable=rights:"$COPYRIGHT" \
    196     --variable=papersize:"$PAPER" \
    197     --shift-heading-level-by=-1
    198 
    199 echo "$VERSION HTML ($ADD_LANGUAGE)..." 1>&2
    200 # FIXME: Newer versions of pandic should use
    201 # --embed-resources --standalone instead of --self-contained
    202 pandoc \
    203     -i "$BUILDFILE_RST" \
    204     -o "${OUTBASE}.html" \
    205     --variable=title:"$TITLE" \
    206     --variable=lang:"$ADD_LANGUAGE" \
    207     --variable=author:"$AUTHOR" \
    208     --variable=rights:"$COPYRIGHT" \
    209     --self-contained \
    210     --shift-heading-level-by=-1
    211 
    212 echo "$VERSION TXT ($ADD_LANGUAGE)..." 1>&2
    213 pandoc \
    214     -i "$BUILDFILE_RST" \
    215     -o "${OUTBASE}.txt"
    216 
    217 echo "Done" 1>&2
    218 exit 0