#!/bin/sh # Copyright 2023 - 2024 Paul Hänsch # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. . $_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 shh=${start_time%:*} shh="${shh#0}" smm=${start_time#*:} smm="${smm#0}" ehh=${end_time%:*} ehh="${ehh#0}" emm=${end_time#*:} emm="${emm#0}" if [ ! "$end_date" -a "$end_time" -a "$start_time" ]; then if [ "$((ehh * 60 + emm))" -gt "$((shh * 60 + smm))" ]; 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 printf '
' "${evid}" # 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