3 . $_EXEC/cgilite/cgilite.sh
4 . $_EXEC/cgilite/storage.sh
7 _(){ printf %s\\n "$*"; }
8 [ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh"
10 start_date= start_time= end_date= end_time=
11 rec_freq= rec_int= error_msg= rec_end=
13 start= end= nstart= nend=
15 while [ $# -gt 0 ]; do case $1 in
16 --from|from|--start|start)
17 if isdate "$2" && istime "$3" ; then
18 start_date="$(isdate "$2" )" start_time="$(istime "$3")"
20 elif isdate "${2%% *}" && istime "${2#* }" ; then
21 start_date="$(isdate "${2%% *}" )" start_time="$(istime "${2#* }")"
23 elif isdate "$2" ; then
24 start_date="$(isdate "$2")" start_time="00:00"
27 error_msg="Event start should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\""
32 if isdate "$2" && istime "$3"; then
33 end_date="$(isdate "$2")" end_time="$(istime "$3")"
35 elif isdate "${2%% *}" && istime "${2#* }"; then
36 end_date="$(isdate "${2%% *}")" end_time="$(istime "${2#* }")"
38 elif isdate "$2"; then
39 end_date="$(isdate "$2")" end_time="23:59"
41 elif istime "$2"; then
42 end_time="$(istime "$2")"
45 error_msg="Event end should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\" or \"hh:mm\""
49 --repeat|--recur|--recurrence|--every|every)
50 if expr "$2" : '^[0-9]\+$' &&
51 expr "$3" : '^\(days\|nights\|weeks\|months\|years\|weekday\)$'; then
52 rec_freq="$2" rec_int="$3"
54 elif expr "$2" : '^[0-9]\+ \+\(days\|nights\|weeks\|months\|years\|weekday\)$'; then
55 rec_freq="${2%% *}" rec_int="${2##* }"
57 elif expr "$2" : '^\(day\|daily\|night\|nightly\|week\|weekly\|month\|monthly\|year\|yearly\|annually\)$'; then
58 rec_freq="1" rec_int="$2"
60 elif expr "$2" : '^last weekday$'; then
61 rec_freq="-1" rec_int="weekday"
63 elif expr "$2 $3" : '^last weekday$'; then
64 rec_freq="-1" rec_int="weekday"
66 elif expr "$2" : '^\(biweekly\|bimonthly\)$'; then
67 rec_freq="2" rec_int="$2"
70 error_msg="Recurrence should be \"N days|weeks|months|years\" or \"N|last weekday\""
76 rec_end="$(isdate "$2")"
79 error_msg="Recurrence end should be \"YYYY-MM-DD\""
90 if [ ! "$end_time" ]; then
94 if [ ! "$end_date" -a "$end_time" -a "$start_time" ]; then
95 if [ "$((${end_time%:*} * 60 + ${end_time#*:}))" -gt "$((${start_time%:*} * 60 + ${start_time#*:}))" ]; then
96 end_date="$start_date"
98 end_date="$(date -d "@$(($(date -d "$start_date" +%s) + 86400))" +%F)"
102 if [ ! "$start_date" ]; then
103 error_msg="Event needs start date, e.g. --start \"YYYY-MM-DD\""
106 if [ ! "$title" ]; then
107 error_msg="Event needs title, e.g. --title \"Event Name\""
110 if [ "$error_msg" ]; then
116 day|daily|days|night|nightly|nights)
118 week|weeks|weekly|biweekly)
120 month|monthly|months|bimonthly)
122 year|yearly|years|annually)
126 if [ "$rec_end" ]; then
127 rec_end="$(date -d "$rec_end" +%s)"
130 if LOCK './#events'; then
131 sed -i "/^${_DATE} /!d" './#events'
132 evid="$(wc -l './#events' || printf 0)"
133 debug "PAGE: $PATH_INFO $evid"
134 printf '%i %i %i %i %s %i %s\n' \
135 "$_DATE" "$(date -d "$start_date $start_time" +%s)" "$(date -d "$end_date $end_time" +%s)" \
136 "${rec_freq:-0}" "${rec_int:-\\}" "${rec_end:--1}" "$(STRING "${PATH_INFO}#event${evid}")" \
141 start="$(date -d "$start_date $start_time" +%s)"
142 end="$(date -d "$end_date $end_time" +%s)"
144 if [ $_DATE -le $end ]; then
145 nstart="$start" nend="$end"
148 [ $_DATE -gt $end ] && case $rec_int in
150 nend=$(( rec_freq * 86400 - (_DATE - end) % (rec_freq * 86400) + _DATE ))
151 nstart=$(( start - end + nend))
154 nend=$(( rec_freq * 604800 - (_DATE - end) % (rec_freq * 604800) + _DATE ))
155 nstart=$(( start - end + nend))
158 { read _y _m _d; read y m d; } <<-EOF
159 $(date -d @$_DATE +"%Y %_m %_d"
160 date -d @$start +"%Y %_m %_d"
163 _m=$((_y * 12 + _m)) m=$((y * 12 + m))
165 # mod=$(( (_m - m) % rec_freq )); [ $mod -eq 0 ] && mod="$rec_freq";
166 m=$(( rec_freq - ((_m - m - 1) % rec_freq + 1) + _m ))
167 y=$((m / 12)) m=$((m % 12)); [ $m -eq 0 ] && y=$((y - 1)) m=12;
168 nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
169 if isdate "$nstart" && [ $(date -d "$nstart" +%s) -ge $_DATE ]; then
172 m=$((y * 12 + m)) _m="$((_m + rec_freq))"
174 nstart="$(date -d "$nstart $start_time" +%s)"
175 nend="$((end - start + nstart))"
178 { read _y _m _d; read y m d; } <<-EOF
179 $(date -d @$_DATE +"%Y %_m %_d"
180 date -d @$start +"%Y %_m %_d"
184 # mod=$(( ( _y - y ) % rec_freq )); [ $mod -eq 0 ] && mod="$rec_freq"
185 y=$(( rec_freq - ((_y - y - 1) % rec_freq + 1) + _y ))
186 nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
187 if isdate "$nstart" && [ $(date -d "$nstart" +%s) -ge $_DATE ]; then
190 _y="$((_y + rec_freq))"
192 nstart="$(date -d "$nstart $start_time" +%s)"
193 nend="$((end - start + nstart))"
196 nstart="$start" nend="$end"
200 if [ "$nstart" -gt "$rec_end" ]; then
201 nstart="$start" nend="$end"
204 "$_EXEC/cgilite/html-sh.sed" <<-EOF
205 [div .macro .event #event${evid}
206 [label . $(HTML "$title")]
208 [dt $(_ Start:)][dd $(date -d @$nstart +"%F %T")]
209 [dt $(_ End:)][dd $(date -d @$nend +"%F %T")]
210 [dt $(_ Until:)][dd $(date -d @$rec_end +"%F %T")]
216 # tzid="$(cat /etc/timezone || printf 'UTC')"
218 # cat >>"#events.ics" <<-EOF
221 # PRODID:ShellWiki Event Macro
223 # UID:$uid@$(HEADER Host)
224 # DTSTAMP:TZID=${tzid}:$(date +%Y%m%dT%H%M%S)
225 # DTSTART:TZID=${tzid}:$(date +%Y%m%dT%H%M%S -d "$start_date $start_time")
227 # RRULE:FREQ=$ec_freq;INTERVAL=$rec_int;UNTIL=$(date +%Y%m%dT000000Z -d "$rec_end")