3 # Copyright 2023 - 2024 Paul Hänsch
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.
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.
17 . $_EXEC/cgilite/cgilite.sh
18 . $_EXEC/cgilite/storage.sh
21 _(){ printf %s\\n "$*"; }
22 [ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh"
24 start_date= start_time= end_date= end_time=
25 rec_freq= rec_int= error_msg= rec_end=
27 start= end= nstart= nend=
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")"
34 elif isdate "${2%% *}" && istime "${2#* }" ; then
35 start_date="$(isdate "${2%% *}" )" start_time="$(istime "${2#* }")"
37 elif isdate "$2" ; then
38 start_date="$(isdate "$2")" start_time="00:00"
41 error_msg="Event start should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\""
46 if isdate "$2" && istime "$3"; then
47 end_date="$(isdate "$2")" end_time="$(istime "$3")"
49 elif isdate "${2%% *}" && istime "${2#* }"; then
50 end_date="$(isdate "${2%% *}")" end_time="$(istime "${2#* }")"
52 elif isdate "$2"; then
53 end_date="$(isdate "$2")" end_time="23:59"
55 elif istime "$2"; then
56 end_time="$(istime "$2")"
59 error_msg="Event end should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\" or \"hh:mm\""
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"
68 elif expr "$2" : '^[0-9]\+ \+\(days\|nights\|weeks\|months\|years\|weekday\)$'; then
69 rec_freq="${2%% *}" rec_int="${2##* }"
71 elif expr "$2" : '^\(day\|daily\|night\|nightly\|week\|weekly\|month\|monthly\|year\|yearly\|annually\)$'; then
72 rec_freq="1" rec_int="$2"
74 elif expr "$2" : '^last weekday$'; then
75 rec_freq="-1" rec_int="weekday"
77 elif expr "$2 $3" : '^last weekday$'; then
78 rec_freq="-1" rec_int="weekday"
80 elif expr "$2" : '^\(biweekly\|bimonthly\)$'; then
81 rec_freq="2" rec_int="$2"
84 error_msg="Recurrence should be \"N days|weeks|months|years\" or \"N|last weekday\""
90 rec_end="$(isdate "$2")"
93 error_msg="Recurrence end should be \"YYYY-MM-DD\""
104 if [ ! "$end_time" ]; then
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"
112 end_date="$(date -ud "@$(($(date -ud "$start_date" +%s) + 86400))" +%F)"
116 if [ ! "$start_date" ]; then
117 error_msg="Event needs start date, e.g. --start \"YYYY-MM-DD\""
120 if [ ! "$title" ]; then
121 error_msg="Event needs title, e.g. --title \"Event Name\""
124 if [ "$error_msg" ]; then
130 day|daily|days|night|nightly|nights)
132 week|weeks|weekly|biweekly)
134 month|monthly|months|bimonthly)
136 year|yearly|years|annually)
140 [ "$rec_end" ] && rec_end="$(date -ud "$rec_end" +%s)" \
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}")" \
152 printf '<div class="macro event" id="#event%s"></div>' "${evid}"
155 # tzid="$(cat /etc/timezone || printf 'UTC')"
157 # cat >>"#events.ics" <<-EOF
160 # PRODID:ShellWiki Event Macro
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")
166 # RRULE:FREQ=$ec_freq;INTERVAL=$rec_int;UNTIL=$(date -u +%Y%m%dT000000Z -d "$rec_end")