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 shh=${start_time%:*} shh="${shh#0}"
109 smm=${start_time#*:} smm="${smm#0}"
110 ehh=${end_time%:*} ehh="${ehh#0}"
111 emm=${end_time#*:} emm="${emm#0}"
113 if [ ! "$end_date" -a "$end_time" -a "$start_time" ]; then
114 if [ "$((ehh * 60 + emm))" -gt "$((shh * 60 + smm))" ]; then
115 end_date="$start_date"
117 end_date="$(date -ud "@$(($(date -ud "$start_date" +%s) + 86400))" +%F)"
121 if [ ! "$start_date" ]; then
122 error_msg="Event needs start date, e.g. --start \"YYYY-MM-DD\""
125 if [ ! "$title" ]; then
126 error_msg="Event needs title, e.g. --title \"Event Name\""
129 if [ "$error_msg" ]; then
135 day|daily|days|night|nightly|nights)
137 week|weeks|weekly|biweekly)
139 month|monthly|months|bimonthly)
141 year|yearly|years|annually)
145 [ "$rec_end" ] && rec_end="$(date -ud "$rec_end" +%s)" \
147 if LOCK './#events'; then
148 sed -i "/^${_DATE} /!d" './#events'
149 evid="$(wc -l <'./#events' || printf 0)"
150 printf '%i %i %i %i %s %i %s %s\n' \
151 "$_DATE" "$(date -ud "$start_date $start_time" +%s)" "$(date -ud "$end_date $end_time" +%s)" \
152 "${rec_freq:-0}" "${rec_int:-\\}" "${rec_end:--1}" "$(STRING "${title}")" "$(STRING "${PATH_INFO}#event${evid}")" \
157 printf '<div class="macro event" id="#event%s"></div>' "${evid}"
160 # tzid="$(cat /etc/timezone || printf 'UTC')"
162 # cat >>"#events.ics" <<-EOF
165 # PRODID:ShellWiki Event Macro
167 # UID:$uid@$(HEADER Host)
168 # DTSTAMP:TZID=${tzid}:$(date -u +%Y%m%dT%H%M%S)
169 # DTSTART:TZID=${tzid}:$(date -u +%Y%m%dT%H%M%S -d "$start_date $start_time")
171 # RRULE:FREQ=$ec_freq;INTERVAL=$rec_int;UNTIL=$(date -u +%Y%m%dT000000Z -d "$rec_end")