taler-merchant-report-generator-file (2719B)
1 #!/bin/bash 2 # 3 # This file is part of TALER 4 # Copyright (C) 2025 Taler Systems SA 5 # 6 # TALER is free software; you can redistribute it and/or modify it under the 7 # terms of the GNU General Public License as published by the Free Software 8 # Foundation; either version 3, or (at your option) any later version. 9 # 10 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY 11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 # 14 # You should have received a copy of the GNU General Public License along with 15 # TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 16 # 17 18 # 19 # Script to write Taler merchant reports to a file. 20 # Reads report data from stdin and writes it to a file. 21 # 22 # Usage: taler-merchant-report-generator-file -d DESCRIPTION -m MIME_TYPE -t TARGET_ADDRESS 23 # 24 25 set -eu 26 27 DESCRIPTION="" 28 MIME_TYPE="" 29 TARGET_ADDRESS="" 30 TMPDIR="${TMPDIR:-/tmp}" 31 32 while getopts "d:m:t:h" opt; do 33 case $opt in 34 d) 35 DESCRIPTION="$OPTARG" 36 ;; 37 m) 38 MIME_TYPE="$OPTARG" 39 ;; 40 t) 41 TARGET_ADDRESS="$OPTARG" 42 ;; 43 h) 44 echo "Usage: $0 -d DESCRIPTION -m MIME_TYPE -t EMAIL_ADDRESS" 45 echo "" 46 echo "Sends reports via email." 47 echo "" 48 echo "Options:" 49 echo " -d DESCRIPTION Subject line for the email" 50 echo " -m MIME_TYPE MIME type of the report (e.g., text/plain, application/pdf)" 51 echo " -t EMAIL_ADDRESS Email address to send the report to" 52 echo " -h Show this help message" 53 echo "" 54 echo "The report data is read from stdin." 55 exit 0 56 ;; 57 \?) 58 echo "Invalid option: -$OPTARG" >&2 59 echo "Use -h for help" >&2 60 exit 1 61 ;; 62 esac 63 done 64 65 if [ -z "$DESCRIPTION" ]; 66 then 67 echo "Error: Description (-d) is required" >&2 68 exit 1 69 fi 70 71 if [ -z "$MIME_TYPE" ]; 72 then 73 echo "Error: MIME type (-m) is required" >&2 74 exit 1 75 fi 76 77 if [ -z "$TARGET_ADDRESS" ]; 78 then 79 echo "Error: Target address (-t) is required" >&2 80 exit 1 81 fi 82 83 # Normalize MIME type to lowercase for comparison 84 MIME_TYPE=$(echo "$MIME_TYPE" | tr '[:upper:]' '[:lower:]') 85 86 # Handle different MIME types 87 case "$MIME_TYPE" in 88 text/plain) 89 cat - > "$TARGET_ADDRESS.txt" 90 ;; 91 application/pdf) 92 cat - > "$TARGET_ADDRESS.pdf" 93 ;; 94 application/json) 95 cat - > "$TARGET_ADDRESS.json" 96 ;; 97 application/xml) 98 cat - > "$TARGET_ADDRESS.xml" 99 ;; 100 text/csv) 101 cat - > "$TARGET_ADDRESS.csv" 102 ;; 103 image/jpeg) 104 cat - > "$TARGET_ADDRESS.jpeg" 105 ;; 106 image/png) 107 cat - > "$TARGET_ADDRESS.png" 108 ;; 109 *) 110 cat - > "$TARGET_ADDRESS" 111 ;; 112 esac 113 114 exit 0