Commit | Line | Data |
---|---|---|
3cc8b063 SBS |
1 | #!/usr/bin/env bash |
2 | # Desc: Prints mediawiki code for a journal month in the bk4 wiki. | |
3 | # Usage: /bin/bash mw_create_month_journal.sh [int year] [int month] | |
4 | # Input: arg1: year | |
5 | # arg2: month | |
6 | # Example: /bin/bash mw_create_month_journal.sh 2023 03 > output.txt | |
338bd5c9 | 7 | # Version: 0.1.0 |
3cc8b063 SBS |
8 | |
9 | yyyy="$1"; | |
10 | mm="$2"; | |
11 | ||
12 | yell() { echo "$0: $*" >&2; } # print script path and all args to stderr | |
13 | die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status | |
14 | must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails | |
15 | ||
16 | # Check input | |
17 | ## Check arg count | |
18 | if [[ $# -ne 2 ]]; then die "FATAL:Invalid arg count:$#"; fi; | |
19 | ## Strip leading zeros (because August/octal) | |
20 | yyyy="$((10#${yyyy}))"; | |
21 | mm="$((10#${mm}))"; | |
22 | ## Check year | |
23 | re="[0-9]+"; | |
24 | if [[ ! $yyyy =~ $re ]]; then die "FATAL:Invalid year string:$yyyy"; fi; | |
25 | if [[ ! $yyyy -ge 1582 ]]; then die "FATAL:Invalid year string:$yyyy"; fi; | |
26 | ## Check month | |
27 | if [[ $mm -lt 1 ]] || [[ $mm -gt 12 ]]; then die "FATAL:Invalid month:$mm"; fi; | |
28 | ||
29 | ||
30 | # Calc working vars | |
31 | ## Years | |
32 | if [[ $mm -gt 1 ]]; then | |
33 | yyyy_prev="$yyyy"; | |
34 | else | |
35 | yyyy_prev="$((yyyy - 1))"; | |
36 | fi; | |
37 | if [[ $mm -lt 12 ]]; then | |
38 | yyyy_next="$yyyy"; | |
39 | else | |
40 | yyyy_next="$((yyyy + 1))"; | |
41 | fi; | |
42 | ## Months | |
43 | mm_prev="$((mm - 1))"; | |
44 | mm_next="$((mm + 1))"; | |
45 | if [[ $mm_prev -lt 1 ]]; then mm_prev="$((mm_prev + 12))"; fi; | |
46 | if [[ $mm_next -gt 12 ]]; then mm_next="$((mm_next - 12))"; fi; | |
47 | ## Leap Years | |
48 | if [[ ! $((yyyy % 4)) -eq 0 ]]; then | |
49 | ### Year is not evenly divisible by 4. | |
50 | leapyear="no"; | |
51 | elif [[ ! $((yyyy % 100)) -eq 0 ]]; then | |
52 | ### Year is not evenly divisible by 100. | |
53 | leapyear="yes"; | |
54 | elif [[ ! $((yyyy % 400)) -eq 0 ]]; then | |
55 | ### year is not evenly divisible by 400. | |
56 | leapyear="no"; | |
57 | else | |
58 | ### year is divisible by 4, 100, and 400 | |
59 | leapyear="yes"; | |
60 | fi; | |
61 | ||
62 | # Form and print header | |
338bd5c9 | 63 | printf "{{bk journal header month}}\n"; |
3cc8b063 SBS |
64 | printf "Journal for [[%d]]-%02d. " "$yyyy" "$mm"; |
65 | printf "Preceded by [[%d-%02d]]. " "$yyyy_prev" "$mm_prev"; | |
66 | printf "Followed by [[%d-%02d]]. " "$yyyy_next" "$mm_next"; | |
67 | printf "\n\n"; | |
68 | ||
69 | printf "==Events==\n"; | |
70 | printf "\n"; | |
71 | ||
72 | printf "==Tasks==\n"; | |
73 | printf "\n"; | |
74 | ||
75 | # Form and print body | |
76 | for (( dd=1; dd <= 31; dd++ )); do | |
77 | if [[ $mm -eq 2 ]] && [[ $dd -eq 29 ]] && \ | |
78 | [[ $leapyear == "no" ]]; then break; fi; | |
79 | if [[ $mm -eq 2 ]] && [[ $dd -ge 30 ]]; then break; fi; | |
80 | if [[ $mm -eq 4 ]] && [[ $dd -ge 31 ]]; then break; fi; | |
81 | if [[ $mm -eq 6 ]] && [[ $dd -ge 31 ]]; then break; fi; | |
82 | if [[ $mm -eq 9 ]] && [[ $dd -ge 31 ]]; then break; fi; | |
83 | if [[ $mm -eq 11 ]] && [[ $dd -ge 31 ]]; then break; fi; | |
84 | printf "==%4d-%02d-%02d==\n" "$yyyy" "$mm" "$dd"; | |
85 | fs_iso8601_alt="+%G-W%V-%u, %Y-%j, %a"; # alternate ISO-8601 dates | |
86 | date_alt="$(date --date="$yyyy-$mm-$dd" "$fs_iso8601_alt")"; | |
87 | printf "%s\n" "$date_alt"; | |
88 | printf "\n"; | |
89 | done; | |
90 | printf "\n"; | |
91 | ||
92 | # Form and print footer | |
93 | s1="==References=="; | |
94 | s2="<references>"; | |
95 | s3="</references>"; | |
96 | printf "%s\n%s\n%s\n" "$s1" "$s2" "$s3"; | |
97 | printf "\n"; | |
98 | ||
99 | printf "==See Also==\n"; | |
100 | printf "\n"; | |
101 | ||
102 | printf "==Ext. Links==\n"; | |
103 | printf "\n"; | |
104 | ||
338bd5c9 SBS |
105 | printf "{{bk journal footer month}}\n"; |
106 | ||
3cc8b063 SBS |
107 | s1="[[Category:Journals by month]]"; |
108 | printf "%s\n" "$s1"; | |
109 | printf "\n"; |