]> git.plutz.net Git - confetti/blob - courses/update_course.sh
improved styling for pdf export
[confetti] / courses / update_course.sh
1 #!/bin/zsh
2
3 # Copyright 2014, 2015, 2020 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 . "$_EXEC/pdiread.sh"
21 . "$_EXEC/session_lock.sh"
22 . "$_EXEC/cgilite/storage.sh"
23
24 unset coursefile attfile tempfile
25
26 course="$(POST course |PATH)"; course="${course##*/}"
27 coursefile="$_DATA/ical/$course"
28 attfile="$_DATA/mappings/attendance"
29
30 if ! tempfile="$(CHECK_SLOCK "$coursefile")"; then
31   SET_COOKIE 0 message="NO VALID FILE LOCK"
32   REDIRECT "/courses/?e=${course}"
33   exit 0
34 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
35   SET_COOKIE 0 message="INVALID TRANSACTION ID"
36   REDIRECT "/courses/?e=${course}"
37   exit 0
38 fi
39
40 vcf_escape(){
41   for each in "$@"; do
42     printf %s\\n "$each" \
43     | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
44   done \
45   | sed -E ':X;$!{N;bX}; s;\n;\;;g'
46 }
47
48 ics="$(pdi_load "$coursefile")"
49
50 tzid=$(cat /etc/timezone)
51
52 ics="$(pdi_update_attrib "$ics" DTSTAMP 1 "TZID=${tzid}")"
53 ics="$(pdi_update_value  "$ics" DTSTAMP 1 "$(TZ="$tzid" date +%Y%m%dT%H%M%S)")"
54
55 dts_year="$(  POST DTS_YEAR    |grep -m1 -xE '[0-9]{4}' || date +%Y)"
56 dts_month="$( POST DTS_MONTH   |grep -m1 -xE '0[1-9]|1[012]' || date +%m)"
57 dts_day="$(   POST DTS_DAY     |grep -m1 -xE '0[1-9]|[12][0-9]|3[01]' || date +%d)"
58 dts_hour="$(  POST DTS_HOUR    |grep -m1 -xE '[0-9]|1[0-9]|2[0-3]' || date +%H)"
59 dts_minute="$(POST DTS_MINUTE  |grep -m1 -xE '[0-9]|[1-5][0-9]' || date +%M)"
60 [ ${#dts_hour}   -eq 1 ] && dts_minute="0$dts_hour"
61 [ ${#dts_minute} -eq 1 ] && dts_minute="0$dts_minute"
62 DTSTART="${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
63
64 ics="$(pdi_update_attrib "$ics" DTSTART 1 "TZID=${tzid}")"
65 ics="$(pdi_update_value  "$ics" DTSTART 1 "$DTSTART")"
66
67 rr_int=$(  POST RRULE_INTERVAL |grep -m1 -xE '[0-9]+' || printf 1)
68 rr_count=$(POST RRULE_COUNT    |grep -m1 -xE '[0-9]+' || printf 1)
69 rr_freq=$( POST RRULE_FREQ     |grep -m1 -xE 'DAILY|WEEKLY|MONTHLY|YEARLY' || printf MONTHLY)
70 rr_uy=$(   POST RRULE_UYEAR    |grep -m1 -xE '[0-9]{4}' || date +%Y)
71 rr_um=$(   POST RRULE_UMONTH   |grep -m1 -xE '[1-9]|1[012]' || date +%m)
72 rr_ud=$(   POST RRULE_UDAY     |grep -m1 -xE '[1-9]|[12][0-9]|3[01]' || date +%d)
73 [ ${#rr_um} -eq 1 ] && rr_um="0$rr_um"
74 [ ${#rr_ud} -eq 1 ] && rr_ud="0$rr_ud"
75
76 case $(POST RRULE_LIMIT) in
77   COUNT)   RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$rr_count";;
78   UNTIL)   RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${rr_uy}${rr_um}${rr_ud}T000000Z";;
79   ETERN|*) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int";;
80 esac
81
82 ics="$(pdi_update_value  "$ics" RRULE 1 "$RRULE")"
83
84 for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
85   for cnt in $(seq 1 $(POST_COUNT "$field")); do
86     case "$field" in
87       *)
88          ics="$(pdi_update_value "$ics" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")"
89         ;;
90     esac
91 done; done
92
93 # delete fields, first mark for deletion using delete_key
94 # this way the field enumeration is preserved during the process
95 # finally filter marked lines
96 delete_key="$(randomid)"
97 for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do
98   f="${delete%%_*}"; c="${delete##*_}";
99   [ "$(POST "$delete")" = "true" ] && ics="$(pdi_update_value "$ics" "$f" "$c" "delete=${delete_key}")"
100 done
101 ics="$(printf '%s\n' "$ics" |sed -E "/^[^:]+:delete=${delete_key}\$/d")"
102
103 case "$(POST action)" in
104   addfield)
105     newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
106     ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
107     printf '%s' "$ics" |grep -vx '' >"$tempfile"
108     REDIRECT "/courses/?e=${course}"
109     ;;
110   addfield\ [A-Z]*)
111     newfield="$(POST action |sed -rn '1s;^addfield ([A-Z][A-Z0-9-]*)$;\1;p')"
112     ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
113     printf '%s' "$ics" |grep -vx '' >"$tempfile"
114     REDIRECT "/courses/?e=${course}"
115     ;;
116   update)
117     if LOCK "$attfile"; then
118       grep -F "${course}        " "$attfile" |while read junk card; do
119         touch "$_DATA/vcard/${card}"
120       done
121       sed -i -r "/^${course}    .+\$/d" "$attfile"
122       seq 1 $(POST_COUNT attendance) |while read n; do
123         printf '%s      %s\n' "$course" "$(POST attendance $n)"
124       done >>"$attfile"
125       grep -F "${course}        " "$attfile" |while read junk card; do
126         touch "$_DATA/vcard/${card}"
127       done
128       RELEASE "$attfile"
129     else
130       SET_COOKIE 0 message="COULD NOT UPDATE COURSE MAPPINGS"
131     fi
132
133     printf '%s' "$ics" |grep -vx '' >"${tempfile}.cp"
134     mv "${tempfile}.cp" "$coursefile"
135     RELEASE_SLOCK "$coursefile"
136     REDIRECT "/courses/#${course}"
137     ;;
138   cancel)
139     RELEASE_SLOCK "$coursefile"
140     [ -f "$coursefile" ] \
141     && REDIRECT "/courses/#${course}" \
142     || REDIRECT "/courses/"
143     ;;
144   delete)
145     rm "$coursefile"
146     RELEASE_SLOCK "$coursefile"
147     REDIRECT "/courses/"
148     ;;
149   *)
150     printf '%s' "$ics" |grep -vx '' >"$tempfile"
151     REDIRECT "/courses/?e=${course}"
152     ;;
153 esac