--- /dev/null
+#!/bin/zsh
+
+# Copyright 2014, 2015 Paul Hänsch
+#
+# This file is part of Confetti.
+#
+# Confetti is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Confetti is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Confetti. If not, see <http://www.gnu.org/licenses/>.
+
+cgi_post
+
+course="${_POST[course]}"
+tempfile="temp/$course"
+coursefile="ical/$course"
+attfile="$_DATA/mappings/attendance"
+
+# DURATION:
+uid="${_POST[UID]}"
+
+tzid=$(cat /etc/timezone)
+tstamp=$(TZ="$tzid" date +%Y%m%dT%H%M%S)
+
+dts_year="${_POST[DTSYEAR]}"
+dts_month="${_POST[DTSMONTH]}"
+dts_day="${_POST[DTSDAY]}"
+[ -n "${_POST[DTSDAY0]}" ] && dts_day="${_POST[DTSDAY0]}"
+dts_hour="${_POST[DTSHOUR]}"
+dts_minute="${_POST[DTSMINUTE]}"
+
+[ -z $dts_year ] && dts_year=$(date +%Y)
+[ -z $dts_month ] && dts_month=$(date +%m)
+[ -z $dts_day ] && dts_day=$(date +%d)
+date -d ${dts_year}-${dts_month}-${dts_day} >/dev/null 2>/dev/null || dts_day="01"
+[ -z $dts_hour ] && dts_hour=$(date +%H)
+[ -z $dts_minute ] && dts_minute=$(date +%M)
+
+dtstart="TZID=${tzid}:${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
+
+rr_int="${_POST[RRULE_INTERVAL]}"
+rr_freq="${_POST[RRULE_FREQ]}"
+rr_limit="${_POST[RRULE_LIMIT]}"
+case "$rr_limit" in
+ ETERN)
+ rrule="FREQ=$rr_freq;INTERVAL=$rr_int"
+ ;;
+ COUNT)
+ t="${_POST[RRULE_COUNT]}"
+ rrule="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$t"
+ ;;
+ UNTIL)
+ uy="${_POST[RRULE_UYEAR]}"
+ um="${_POST[RRULE_UMONTH]}"
+ ud="${_POST[RRULE_UDAY]}"
+ rrule="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${uy}${um}${ud}T000000Z"
+ ;;
+esac
+
+echo "BEGIN:VCALENDAR\r" >"$tempfile"
+echo "VERSION:2.0\r" >>"$tempfile"
+echo "PRODID:Berlin RAW Confetti\r" >>"$tempfile"
+echo "BEGIN:VEVENT\r" >>"$tempfile"
+echo "UID:$uid\r" >>"$tempfile"
+echo "DTSTAMP:TZID=${tzid}:${tstamp}\r" >>"$tempfile"
+echo "DTSTART:${dtstart}\r" >>"$tempfile"
+echo "RRULE:${rrule}\r" >>"$tempfile"
+for field in SUMMARY COMMENT; do
+ value="${_POST[$field]}"
+ n=0
+ while [ -n "$value" ]; do
+ value="$(echo "$value" |sed -r ':a;N;$!ba;s:\n:\\\\n:g;s:\r:\\\\r:g')"
+ echo "${field}:${value}\r"
+ value="${_POST[$field$n]}"
+ n=$(($n + 1))
+ done
+done >>"$tempfile"
+
+case "${_POST[action]}" in
+ addfield)
+ echo "${_POST[newfield]}:\r" >>"$tempfile"
+ echo "END:VEVENT\r" >>"$tempfile"
+ echo "END:VCALENDAR\r" >>"$tempfile"
+ echo -n "Location: ?p=courses&edit=$course\n\n"
+ ;;
+ update)
+ attendance=()
+ for att in attendance attendance{0..100}; do
+ [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
+ done
+ sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
+ touch "$_DATA/vcard/$card"
+ done
+ sed -i -r '/^'$course'\t(.+)$/d' "$attfile"
+ for each in $attendance; do
+ echo "$course\t$each"
+ done >>"$attfile"
+ sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
+ touch "$_DATA/vcard/$card"
+ done
+
+ echo "END:VEVENT\r" >>"$tempfile"
+ echo "END:VCALENDAR\r" >>"$tempfile"
+ mv "$tempfile" "$coursefile"
+ echo -n "Location: ?p=courses#$course\n\n"
+ ;;
+ cancel)
+ rm "$tempfile"
+ [ -f "$coursefile" ] \
+ && echo -n "Location: ?p=courses#$course\n\n" \
+ || echo -n "Location: ?p=courses\n\n"
+ ;;
+ delete)
+ rm "$tempfile" "$coursefile"
+ echo -n "Location: ?p=courses\n\n"
+ ;;
+ *)
+ echo "END:VEVENT\r" >>"$tempfile"
+ echo "END:VCALENDAR\r" >>"$tempfile"
+ echo -n "Location: ?p=courses&edit=$course\n\n"
+ ;;
+esac