Merge branch 'master' into develop
[BK-2020-03.git] / user / mw_create_year_journal.sh
1 #!/usr/bin/env bash
2 # Desc: Prints mediawiki code for a journal year in the bk4 wiki.
3 # Usage: /bin/bash mw_create_year_journal.sh [int year]
4 # Input: arg1: year
5 # Example: /bin/bash mw_create_year_journal.sh 2023 > output.txt
6 # Version: 0.0.1
7
8 yyyy="$1";
9
10 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
11 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
12 must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
13
14 # Check input
15 ## Check arg count
16 if [[ $# -ne 1 ]]; then die "FATAL:Invalid arg count:$#"; fi;
17 ## Strip leading zeros
18 yyyy="$((10#${yyyy}))";
19
20 ## Check year
21 re="[0-9]+";
22 if [[ ! $yyyy =~ $re ]]; then die "FATAL:Invalid year string:$yyyy"; fi;
23 if [[ ! $yyyy -ge 1582 ]]; then die "FATAL:Invalid year string:$yyyy"; fi;
24
25 # Calc working vars
26 ## Years
27 yyyy_prev="$((yyyy - 1))";
28 yyyy_next="$((yyyy + 1))";
29
30 # Form and print header
31 printf "{{bk journal header year}}\n";
32 printf "[[Journal]] for the year [[%d]]. " "$yyyy";
33 printf "Preceded by [[%d]]. " "$yyyy_prev";
34 printf "Followed by [[%d]]. " "$yyyy_next";
35 printf "\n\n";
36
37 printf "==Events==\n";
38 printf "\n";
39
40 # Form and print body
41 for (( mm=1; mm <= 12; mm++ )); do
42 printf "==[[%4d-%02d]]==\n" "$yyyy" "$mm";
43 done;
44 printf "\n";
45
46 # Form and print footer
47 s1="==References==";
48 s2="<references>";
49 s3="</references>";
50 printf "%s\n%s\n%s\n" "$s1" "$s2" "$s3";
51 printf "\n";
52
53 printf "==See Also==\n";
54 printf "\n";
55
56 printf "==Ext. Links==\n";
57 printf "\n";
58
59 printf "{{bk journal footer year}}\n";
60
61 s1="[[Category:Journals by year]]";
62 printf "%s\n" "$s1";
63 printf "\n";