]> git.plutz.net Git - confetti/blob - actions/update_course.sh
selectable strokewidth in therapy
[confetti] / actions / update_course.sh
1 #!/bin/zsh
2
3 # Copyright 2014, 2015 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 attfile="$_DATA/mappings/attendance"
26
27 # DURATION:
28 uid="${_POST[UID]}"
29
30 tzid=$(cat /etc/timezone)
31 tstamp=$(TZ="$tzid" date +%Y%m%dT%H%M%S)
32
33 dts_year="${_POST[DTSYEAR]}"
34 dts_month="${_POST[DTSMONTH]}"
35 dts_day="${_POST[DTSDAY]}"
36 [ -n "${_POST[DTSDAY0]}" ] && dts_day="${_POST[DTSDAY0]}"
37 dts_hour="${_POST[DTSHOUR]}"
38 dts_minute="${_POST[DTSMINUTE]}"
39
40 [ -z $dts_year ] && dts_year=$(date +%Y)
41 [ -z $dts_month ] && dts_month=$(date +%m)
42 [ -z $dts_day ] && dts_day=$(date +%d)
43 date -d ${dts_year}-${dts_month}-${dts_day} >/dev/null 2>/dev/null || dts_day="01"
44 [ -z $dts_hour ] && dts_hour=$(date +%H)
45 [ -z $dts_minute ] && dts_minute=$(date +%M)
46
47 dtstart="TZID=${tzid}:${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
48
49 rr_int="${_POST[RRULE_INTERVAL]}"
50 rr_freq="${_POST[RRULE_FREQ]}"
51 rr_limit="${_POST[RRULE_LIMIT]}"
52 case "$rr_limit" in
53   ETERN)
54     rrule="FREQ=$rr_freq;INTERVAL=$rr_int"
55     ;;
56   COUNT)
57     t="${_POST[RRULE_COUNT]}"
58     rrule="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$t"
59     ;;
60   UNTIL)
61     uy="${_POST[RRULE_UYEAR]}"
62     um="${_POST[RRULE_UMONTH]}"
63     ud="${_POST[RRULE_UDAY]}"
64     rrule="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${uy}${um}${ud}T000000Z"
65     ;;
66 esac
67
68 echo "BEGIN:VCALENDAR\r" >"$tempfile"
69 echo "VERSION:2.0\r" >>"$tempfile"
70 echo "PRODID:Berlin RAW Confetti\r" >>"$tempfile"
71 echo "BEGIN:VEVENT\r" >>"$tempfile"
72 echo "UID:$uid\r" >>"$tempfile"
73 echo "DTSTAMP:TZID=${tzid}:${tstamp}\r" >>"$tempfile"
74 echo "DTSTART:${dtstart}\r" >>"$tempfile"
75 echo "RRULE:${rrule}\r" >>"$tempfile"
76 for field in SUMMARY COMMENT; do
77   value="${_POST[$field]}"
78   n=0
79   while [ -n "$value" ]; do
80     value="$(echo "$value" |sed -r ':a;N;$!ba;s:\n:\\\\n:g;s:\r:\\\\r:g')"
81     echo "${field}:${value}\r"
82     value="${_POST[$field$n]}"
83     n=$(($n + 1))
84   done
85 done >>"$tempfile"
86
87 case "${_POST[action]}" in
88   addfield)
89     echo "${_POST[newfield]}:\r" >>"$tempfile"
90     echo "END:VEVENT\r" >>"$tempfile"
91     echo "END:VCALENDAR\r" >>"$tempfile"
92     echo -n "Location: ?p=courses&edit=$course\n\n"
93     ;;
94   update)
95     attendance=()
96     for att in attendance attendance{0..100}; do
97       [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
98     done
99     sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
100       touch "$_DATA/vcard/$card"
101     done
102     sed -i -r '/^'$course'\t(.+)$/d' "$attfile"
103     for each in $attendance; do
104       echo "$course\t$each"
105     done >>"$attfile"
106     sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
107       touch "$_DATA/vcard/$card"
108     done
109
110     echo "END:VEVENT\r" >>"$tempfile"
111     echo "END:VCALENDAR\r" >>"$tempfile"
112     mv "$tempfile" "$coursefile"
113     echo -n "Location: ?p=courses#$course\n\n"
114     ;;
115   cancel)
116     rm "$tempfile"
117     [ -f "$coursefile" ] \
118       && echo -n "Location: ?p=courses#$course\n\n" \
119       || echo -n "Location: ?p=courses\n\n"
120     ;;
121   delete)
122     rm "$tempfile" "$coursefile"
123     echo -n "Location: ?p=courses\n\n"
124     ;;
125   *)
126     echo "END:VEVENT\r" >>"$tempfile"
127     echo "END:VCALENDAR\r" >>"$tempfile"
128     echo -n "Location: ?p=courses&edit=$course\n\n"
129     ;;
130 esac