#!/bin/sh . $_EXEC/cgilite/cgilite.sh . $_EXEC/cgilite/storage.sh . $_EXEC/datetime.sh _(){ printf %s\\n "$*"; } [ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh" start_date= start_time= end_date= end_time= rec_freq= rec_int= error_msg= rec_end= title= start= end= nstart= nend= while [ $# -gt 0 ]; do case $1 in --from|from|--start|start) if isdate "$2" && istime "$3" ; then start_date="$(isdate "$2" )" start_time="$(istime "$3")" shift 3 elif isdate "${2%% *}" && istime "${2#* }" ; then start_date="$(isdate "${2%% *}" )" start_time="$(istime "${2#* }")" shift 2 elif isdate "$2" ; then start_date="$(isdate "$2")" start_time="00:00" shift 2 else error_msg="Event start should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\"" shift 1 fi >/dev/null ;; --to|to|--end|end) if isdate "$2" && istime "$3"; then end_date="$(isdate "$2")" end_time="$(istime "$3")" shift 3 elif isdate "${2%% *}" && istime "${2#* }"; then end_date="$(isdate "${2%% *}")" end_time="$(istime "${2#* }")" shift 2 elif isdate "$2"; then end_date="$(isdate "$2")" end_time="23:59" shift 2 elif istime "$2"; then end_time="$(istime "$2")" shift 2 else error_msg="Event end should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\" or \"hh:mm\"" shift 1 fi >/dev/null ;; --repeat|--recur|--recurrence|--every|every) if expr "$2" : '^[0-9]\+$' && expr "$3" : '^\(days\|nights\|weeks\|months\|years\|weekday\)$'; then rec_freq="$2" rec_int="$3" shift 3 elif expr "$2" : '^[0-9]\+ \+\(days\|nights\|weeks\|months\|years\|weekday\)$'; then rec_freq="${2%% *}" rec_int="${2##* }" shift 2 elif expr "$2" : '^\(day\|daily\|night\|nightly\|week\|weekly\|month\|monthly\|year\|yearly\|annually\)$'; then rec_freq="1" rec_int="$2" shift 2 elif expr "$2" : '^last weekday$'; then rec_freq="-1" rec_int="weekday" shift 2 elif expr "$2 $3" : '^last weekday$'; then rec_freq="-1" rec_int="weekday" shift 3 elif expr "$2" : '^\(biweekly\|bimonthly\)$'; then rec_freq="2" rec_int="$2" shift 2 else error_msg="Recurrence should be \"N days|weeks|months|years\" or \"N|last weekday\"" shift 1 fi >/dev/null ;; --until|until) if isdate $2; then rec_end="$(isdate "$2")" shift 2 else error_msg="Recurrence end should be \"YYYY-MM-DD\"" shift 1 fi >/dev/null ;; --title) title="$2" shift 2 ;; *) shift 1;; esac; done if [ ! "$end_time" ]; then end_time="23:59" fi if [ ! "$end_date" -a "$end_time" -a "$start_time" ]; then if [ "$((${end_time%:*} * 60 + ${end_time#*:}))" -gt "$((${start_time%:*} * 60 + ${start_time#*:}))" ]; then end_date="$start_date" else end_date="$(date -ud "@$(($(date -ud "$start_date" +%s) + 86400))" +%F)" fi fi if [ ! "$start_date" ]; then error_msg="Event needs start date, e.g. --start \"YYYY-MM-DD\"" fi if [ ! "$title" ]; then error_msg="Event needs title, e.g. --title \"Event Name\"" fi if [ "$error_msg" ]; then _ "$error_msg" exit 1 fi case $rec_int in day|daily|days|night|nightly|nights) rec_int="day";; week|weeks|weekly|biweekly) rec_int="week";; month|monthly|months|bimonthly) rec_int="month";; year|yearly|years|annually) rec_int="year";; esac [ "$rec_end" ] && rec_end="$(date -ud "$rec_end" +%s)" \ if LOCK './#events'; then sed -i "/^${_DATE} /!d" './#events' evid="$(wc -l <'./#events' || printf 0)" printf '%i %i %i %i %s %i %s %s\n' \ "$_DATE" "$(date -ud "$start_date $start_time" +%s)" "$(date -ud "$end_date $end_time" +%s)" \ "${rec_freq:-0}" "${rec_int:-\\}" "${rec_end:--1}" "$(STRING "${title}")" "$(STRING "${PATH_INFO}#event${evid}")" \ >>'./#events' RELEASE './#events' fi start="$(date -ud "$start_date $start_time" +%s)" end="$(date -ud "$end_date $end_time" +%s)" if [ $_DATE -le $end ]; then nstart="$start" nend="$end" fi [ $_DATE -gt $end ] && case $rec_int in day) nend=$(( rec_freq * 86400 - (_DATE - end) % (rec_freq * 86400) + _DATE )) nstart=$(( start - end + nend)) ;; week) nend=$(( rec_freq * 604800 - (_DATE - end) % (rec_freq * 604800) + _DATE )) nstart=$(( start - end + nend)) ;; month) { read _y _m _d; read y m d; } <<-EOF $(date -ud @$_DATE +"%Y %_m %_d" date -ud @$start +"%Y %_m %_d" ) EOF _m=$((_y * 12 + _m)) m=$((y * 12 + m)) while :; do # mod=$(( (_m - m) % rec_freq )); [ $mod -eq 0 ] && mod="$rec_freq"; m=$(( rec_freq - ((_m - m - 1) % rec_freq + 1) + _m )) # y=$(((m - 1) / 12)) m=$(((m - 1) % 12 + 1)); # [ $m -eq 0 ] && y=$((y - 1)) m=12; nstart="$(printf '%04i-%02i-%02i' "$(( (m - 1) / 12 ))" "$(( (m - 1) % 12 + 1 ))" "$d")" if isdate "$nstart" && [ "$(date -ud "$nstart" +%s)" -ge "$_DATE" ]; then break fi >/dev/null # m=$((y * 12 + m)) _m="$((_m + rec_freq))" done nstart="$(date -ud "$nstart $start_time" +%s)" nend="$((end - start + nstart))" ;; year) { read _y _m _d; read y m d; } <<-EOF $(date -ud @$_DATE +"%Y %_m %_d" date -ud @$start +"%Y %_m %_d" ) EOF while :; do # mod=$(( ( _y - y ) % rec_freq )); [ $mod -eq 0 ] && mod="$rec_freq" y=$(( rec_freq - ((_y - y - 1) % rec_freq + 1) + _y )) nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")" if isdate "$nstart" && [ $(date -ud "$nstart" +%s) -ge $_DATE ]; then break fi >/dev/null _y="$((_y + rec_freq))" done nstart="$(date -ud "$nstart $start_time" +%s)" nend="$((end - start + nstart))" ;; '') nstart="$start" nend="$end" ;; esac if [ "$nstart" -gt "$rec_end" ] 2>/dev/null; then nstart="$start" nend="$end" fi "$_EXEC/cgilite/html-sh.sed" <<-EOF [div .macro .event #event${evid} [label . $(HTML "$title")] [dl [dt $(_ Start:)][dd $(date -ud @$nstart +"%F %T")] [dt $(_ End:)][dd $(date -ud @$nend +"%F %T")] [dt $(_ Until:)][dd $(date -ud @$rec_end +"%F %T")] ] ] EOF # uid="$(timeid)" # tzid="$(cat /etc/timezone || printf 'UTC')" # # cat >>"#events.ics" <<-EOF # BEGIN:VCALENDAR # VERSION:2.0 # PRODID:ShellWiki Event Macro # BEGIN:VEVENT # UID:$uid@$(HEADER Host) # DTSTAMP:TZID=${tzid}:$(date -u +%Y%m%dT%H%M%S) # DTSTART:TZID=${tzid}:$(date -u +%Y%m%dT%H%M%S -d "$start_date $start_time") # DURATION: # RRULE:FREQ=$ec_freq;INTERVAL=$rec_int;UNTIL=$(date -u +%Y%m%dT000000Z -d "$rec_end") # SUMMARY: # COMMENT: # END:VEVENT # END:VCARD # EOF