| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # vim: ts=4 sw=4 noet ft=sh |
| 3 | # |
| 4 | # Example script to process Fangfrisch News. |
| 5 | |
| 6 | declare -r MAILFROM="noreply" |
| 7 | declare -r MAILTO="[email protected]" |
| 8 | declare -r SUBJECT="Fangfrisch News are available" |
| 9 | |
| 10 | # Choose one of the options below and uncomment the 'declare' |
| 11 | # statements. Until this is done, the script will fail with |
| 12 | # an error message. |
| 13 | |
| 14 | # Option 1: Mutt |
| 15 | #declare -r MAILAPP="mutt" |
| 16 | #declare -r MAILAPP_OPT=( "-s" "$SUBJECT" "$MAILTO" ) |
| 17 | |
| 18 | # Option 2: sendmail |
| 19 | #declare -r MAILAPP="sendmail" |
| 20 | #declare -r MAILAPP_OPT=( "-t" ) |
| 21 | #export PATH="$PATH:/usr/sbin" |
| 22 | |
| 23 | # Option 3: swaks |
| 24 | #declare -r MAILAPP="swaks" |
| 25 | #declare -r MAILAPP_OPT=( "-d" "-" "-f" "$MAILFROM" "-t" "$MAILTO" ) |
| 26 | |
| 27 | ### No changes required below this line ### |
| 28 | |
| 29 | set -euo pipefail |
| 30 | |
| 31 | diescip-shell shell fangfrisch 1.9.2-2 die().() { |
| 32 | echoscip-shell system . . echo. >&2 "$@" |
| 33 | exit 1 |
| 34 | } |
| 35 | |
| 36 | usagescip-shell shell fangfrisch 1.9.2-2 usage().() { |
| 37 | diescip-shell shell fangfrisch 1.9.2-2 die(). "Usage: $(basenamescip-shell system . . basename. "$0") {directory}" |
| 38 | } |
| 39 | |
| 40 | gen_headerscip-shell shell fangfrisch 1.9.2-2 gen_header().() { |
| 41 | catscip-shell system . . cat. <<EOT |
| 42 | Auto-Submitted: auto-generated |
| 43 | From: Fangfrisch News <$MAILFROM> |
| 44 | To: $MAILTO |
| 45 | Subject: $SUBJECT |
| 46 | |
| 47 | EOT |
| 48 | # Mail header must end with an empty line! |
| 49 | } |
| 50 | |
| 51 | declare -a NEWSITEMS=() |
| 52 | |
| 53 | report_newsscip-shell shell fangfrisch 1.9.2-2 report_news().() { |
| 54 | local dir=$1 ni |
| 55 | [scip-shell system . . `[`. -d "$dir" ] || diescip-shell shell fangfrisch 1.9.2-2 die(). "$dir is not a directory" |
| 56 | while IFS= read -r -d '' ni; do |
| 57 | if [scip-shell system . . `[`. ${#NEWSITEMS[*]} -eq 0 ] && [scip-shell system . . `[`. "$MAILAPP" != mutt ]; then |
| 58 | # Mutt does not need the header, others do. |
| 59 | gen_headerscip-shell shell fangfrisch 1.9.2-2 gen_header(). |
| 60 | fi |
| 61 | NEWSITEMS+=("$ni") |
| 62 | echoscip-shell system . . echo. -e "\n### $(basenamescip-shell system . . basename. "$ni"):\n" |
| 63 | catscip-shell system . . cat. "$ni" |
| 64 | done < <(findscip-shell system . . find. "$dir" -maxdepth 1 -type f -name "fangfrisch*.txt" -print0) |
| 65 | } |
| 66 | |
| 67 | mainscip-shell shell fangfrisch 1.9.2-2 main().() { |
| 68 | local t |
| 69 | [scip-shell system . . `[`. -n "$MAILAPP" ] || diescip-shell shell fangfrisch 1.9.2-2 die(). "MAILAPP is undefined, exiting." |
| 70 | if ttyscip-shell system . . tty. -s; then |
| 71 | # Running in a terminal session |
| 72 | t=$(mktempscip-shell system . . mktemp.) |
| 73 | # shellcheck disable=SC2064 |
| 74 | trap "rm $t" EXIT |
| 75 | report_newsscip-shell shell fangfrisch 1.9.2-2 report_news(). "$@" | teescip-shell system . . tee. "$t" || exit 1 |
| 76 | [scip-shell system . . `[`. ! -s "$t" ] || "$MAILAPP" "${MAILAPP_OPT[@]}" >/dev/nullscip-shell filesystem . . `/dev/null`. <"$t" |
| 77 | else |
| 78 | report_newsscip-shell shell fangfrisch 1.9.2-2 report_news(). "$@" 2>&1 | "$MAILAPP" "${MAILAPP_OPT[@]}" >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 79 | [scip-shell system . . `[`. ${#NEWSITEMS[*]} -eq 0 ] || rmscip-shell system . . rm. -v "${NEWSITEMS[@]}" |
| 80 | fi |
| 81 | } |
| 82 | |
| 83 | [scip-shell system . . `[`. $# -ge 1 ] || usagescip-shell shell fangfrisch 1.9.2-2 usage(). |
| 84 | mainscip-shell shell fangfrisch 1.9.2-2 main(). "$@" |