]> git.plutz.net Git - confetti/blob - actions/update_course.sh
replace dbus-uuidgen by /dev/urandom based solution
[confetti] / actions / update_course.sh
1 #!/bin/zsh
2
3 # Copyright 2014 Paul Hänsch
4 #
5 # This file is part of Confetti.
6
7 # Confetti is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # Confetti is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Confetti.  If not, see <http://www.gnu.org/licenses/>. 
19
20 cgi_post
21
22 course="$_POST[\"course\"]"
23 tempfile="temp/$course"
24 coursefile="ical/$course"
25
26 # DURATION:
27 uid="$_POST[\"UID\"]"
28
29 tzid=$(cat /etc/timezone)
30 tstamp=$(TZ="$tzid" date +%Y%m%dT%H%M%S)
31
32 dts_year="$_POST[\"DTSYEAR\"]"
33 dts_month="$_POST[\"DTSMONTH\"]"
34 dts_day="$_POST[\"DTSDAY\"]"
35 [ -n "$_POST[\"DTSDAY0\"]" ] && dts_day="$_POST[\"DTSDAY0\"]"
36 dts_hour="$_POST[\"DTSHOUR\"]"
37 dts_minute="$_POST[\"DTSMINUTE\"]"
38
39 [ -z $dts_year ] && dts_year=$(date +%Y)
40 [ -z $dts_month ] && dts_month=$(date +%m)
41 [ -z $dts_day ] && dts_day=$(date +%d)
42 date -d ${dts_year}-${dts_month}-${dts_day} >/dev/null 2>/dev/null || dts_day="01"
43 [ -z $dts_hour ] && dts_hour=$(date +%H)
44 [ -z $dts_minute ] && dts_minute=$(date +%M)
45
46 dtstart="TZID=${tzid}:${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
47
48 rr_int="$_POST[\"RRULE_INTERVAL\"]"
49 rr_freq="$_POST[\"RRULE_FREQ\"]"
50 rr_limit="$_POST[\"RRULE_LIMIT\"]"
51 case "$rr_limit" in
52   ETERN)
53     rrule="FREQ=$rr_freq;INTERVAL=$rr_int"
54     ;;
55   COUNT)
56     t="$_POST[\"RRULE_COUNT\"]"
57     rrule="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$t"
58     ;;
59   UNTIL)
60     uy="$_POST[\"RRULE_UYEAR\"]"
61     um="$_POST[\"RRULE_UMONTH\"]"
62     ud="$_POST[\"RRULE_UDAY\"]"
63     rrule="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${uy}${um}${ud}T000000Z"
64     ;;
65 esac
66
67 echo "BEGIN:VCALENDAR\r" >"$tempfile"
68 echo "VERSION:2.0\r" >>"$tempfile"
69 echo "PRODID:Berlin RAW Confetti\r" >>"$tempfile"
70 echo "BEGIN:VEVENT\r" >>"$tempfile"
71 echo "UID:$uid\r" >>"$tempfile"
72 echo "DTSTAMP:TZID=${tzid}:${tstamp}\r" >>"$tempfile"
73 echo "DTSTART:${dtstart}\r" >>"$tempfile"
74 echo "RRULE:${rrule}\r" >>"$tempfile"
75 for field in SUMMARY COMMENT; do
76   value="$_POST[\"$field\"]"
77   n=0
78   while [ -n "$value" ]; do
79     value="$(echo "$value" |sed -r ':a;N;$!ba;s:\n:\\\\n:g;s:\r:\\\\r:g')"
80     echo "${field}:${value}\r"
81     value="$_POST[\"$field$n\"]"
82     n=$(($n + 1))
83   done
84 done >>"$tempfile"
85
86 case "$_POST[\"action\"]" in
87   addfield)
88     echo "$_POST[\"newfield\"]:\r" >>"$tempfile"
89     echo "END:VEVENT\r" >>"$tempfile"
90     echo "END:VCALENDAR\r" >>"$tempfile"
91     echo -n "Location: ?page=courses&edit=$course\n\n"
92     ;;
93   update)
94     echo "END:VEVENT\r" >>"$tempfile"
95     echo "END:VCALENDAR\r" >>"$tempfile"
96     mv "$tempfile" "$coursefile"
97     echo -n "Location: ?page=courses#$course\n\n"
98     ;;
99   cancel)
100     rm "$tempfile"
101     [ -f "$coursefile" ] \
102       && echo -n "Location: ?page=courses#$course\n\n" \
103       || echo -n "Location: ?page=courses\n\n"
104     ;;
105   delete)
106     rm "$tempfile" "$coursefile"
107     echo -n "Location: ?page=courses\n\n"
108     ;;
109   *)
110     echo "END:VEVENT\r" >>"$tempfile"
111     echo "END:VCALENDAR\r" >>"$tempfile"
112     echo -n "Location: ?page=courses&edit=$course\n\n"
113     ;;
114 esac