]> git.plutz.net Git - shellwiki/blob - macros/event
4b3881057713f9edeb59abbbebe78544f3f20889
[shellwiki] / macros / event
1 #!/bin/sh
2
3 # Copyright 2023 - 2024 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 . $_EXEC/cgilite/cgilite.sh
18 . $_EXEC/cgilite/storage.sh
19 . $_EXEC/datetime.sh
20
21 _(){ printf %s\\n "$*"; }
22 [ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh"
23
24 start_date= start_time= end_date= end_time=
25 rec_freq= rec_int= error_msg= rec_end=
26 title=
27 start= end= nstart= nend=
28
29 while [ $# -gt 0 ]; do case $1 in
30   --from|from|--start|start)
31     if   isdate "$2" && istime "$3" ; then
32       start_date="$(isdate "$2" )" start_time="$(istime "$3")"
33       shift 3
34     elif isdate "${2%% *}" && istime "${2#* }" ; then
35       start_date="$(isdate "${2%% *}" )" start_time="$(istime "${2#* }")"
36       shift 2
37     elif isdate "$2" ; then
38       start_date="$(isdate "$2")" start_time="00:00"
39       shift 2
40     else
41       error_msg="Event start should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\""
42       shift 1
43     fi >/dev/null
44     ;;
45   --to|to|--end|end)
46     if   isdate "$2" && istime "$3"; then
47       end_date="$(isdate "$2")" end_time="$(istime "$3")"
48       shift 3
49     elif isdate "${2%% *}" && istime "${2#* }"; then
50       end_date="$(isdate "${2%% *}")" end_time="$(istime "${2#* }")"
51       shift 2
52     elif isdate "$2"; then
53       end_date="$(isdate "$2")" end_time="23:59"
54       shift 2
55     elif istime "$2"; then
56       end_time="$(istime "$2")"
57       shift 2
58     else
59       error_msg="Event end should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\" or \"hh:mm\""
60       shift 1
61     fi >/dev/null
62     ;;
63   --repeat|--recur|--recurrence|--every|every)
64     if expr "$2" : '^[0-9]\+$' &&
65        expr "$3" : '^\(days\|nights\|weeks\|months\|years\|weekday\)$'; then
66       rec_freq="$2" rec_int="$3"
67       shift 3
68     elif expr "$2" : '^[0-9]\+ \+\(days\|nights\|weeks\|months\|years\|weekday\)$'; then
69       rec_freq="${2%% *}" rec_int="${2##* }"
70       shift 2
71     elif expr "$2" : '^\(day\|daily\|night\|nightly\|week\|weekly\|month\|monthly\|year\|yearly\|annually\)$'; then
72       rec_freq="1" rec_int="$2"
73       shift 2
74     elif expr "$2" : '^last weekday$'; then
75       rec_freq="-1" rec_int="weekday"
76       shift 2
77     elif expr "$2 $3" : '^last weekday$'; then
78       rec_freq="-1" rec_int="weekday"
79       shift 3
80     elif expr "$2" : '^\(biweekly\|bimonthly\)$'; then
81       rec_freq="2" rec_int="$2"
82       shift 2
83     else
84       error_msg="Recurrence should be \"N days|weeks|months|years\" or \"N|last weekday\""
85       shift 1
86     fi >/dev/null
87     ;;
88   --until|until)
89     if isdate $2; then
90       rec_end="$(isdate "$2")"
91       shift 2
92     else
93       error_msg="Recurrence end should be \"YYYY-MM-DD\""
94       shift 1
95     fi >/dev/null
96     ;;
97   --title)
98     title="$2"
99     shift 2
100     ;;
101   *) shift 1;;
102 esac; done
103
104 if [ ! "$end_time" ]; then
105   end_time="23:59"
106 fi
107
108 if [ ! "$end_date" -a "$end_time" -a "$start_time" ]; then
109   if [ "$((${end_time%:*} * 60 + ${end_time#*:}))" -gt "$((${start_time%:*} * 60 + ${start_time#*:}))" ]; then
110     end_date="$start_date"
111   else
112     end_date="$(date -ud "@$(($(date -ud "$start_date" +%s) + 86400))" +%F)"
113   fi
114 fi
115
116 if [ ! "$start_date" ]; then
117   error_msg="Event needs start date, e.g. --start \"YYYY-MM-DD\""
118 fi
119
120 if [ ! "$title" ]; then
121   error_msg="Event needs title, e.g. --title \"Event Name\""
122 fi
123
124 if [ "$error_msg" ]; then
125   _ "$error_msg"
126   exit 1
127 fi
128
129 case $rec_int in
130   day|daily|days|night|nightly|nights)
131     rec_int="day";;
132   week|weeks|weekly|biweekly)
133     rec_int="week";;
134   month|monthly|months|bimonthly)
135     rec_int="month";;
136   year|yearly|years|annually)
137     rec_int="year";;
138 esac
139
140 [ "$rec_end" ] && rec_end="$(date -ud "$rec_end" +%s)" \
141
142 if LOCK './#events'; then
143   sed -i "/^${_DATE}    /!d" './#events'
144   evid="$(wc -l <'./#events' || printf 0)"
145   printf '%i    %i      %i      %i      %s      %i      %s      %s\n' \
146     "$_DATE" "$(date -ud "$start_date $start_time" +%s)" "$(date -ud "$end_date $end_time" +%s)" \
147     "${rec_freq:-0}" "${rec_int:-\\}" "${rec_end:--1}" "$(STRING "${title}")" "$(STRING "${PATH_INFO}#event${evid}")" \
148     >>'./#events'
149   RELEASE './#events'
150 fi
151
152 printf '<div class="macro event" id="#event%s"></div>' "${evid}"
153
154 # uid="$(timeid)"
155 # tzid="$(cat /etc/timezone || printf 'UTC')"
156
157 # cat >>"#events.ics" <<-EOF
158 # BEGIN:VCALENDAR
159 # VERSION:2.0
160 # PRODID:ShellWiki Event Macro
161 # BEGIN:VEVENT
162 # UID:$uid@$(HEADER Host)
163 # DTSTAMP:TZID=${tzid}:$(date -u +%Y%m%dT%H%M%S)
164 # DTSTART:TZID=${tzid}:$(date -u +%Y%m%dT%H%M%S -d "$start_date $start_time")
165 # DURATION:
166 # RRULE:FREQ=$ec_freq;INTERVAL=$rec_int;UNTIL=$(date -u +%Y%m%dT000000Z -d "$rec_end")
167 # SUMMARY:
168 # COMMENT:
169 # END:VEVENT
170 # END:VCARD
171 # EOF