]> git.plutz.net Git - confetti/blob - actions/update_therapy.sh
omit listing of empty courses and vcards
[confetti] / actions / update_therapy.sh
1 #!/bin/zsh
2
3 # Copyright 2016 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 BR='
21 '
22 tpy="${_POST[id]}"
23
24 tpyfile="$_DATA/therapies/$tpy"
25 tempfile="$_DATA/temp/$tpy"
26
27 # serialize POST array into file
28 for key in ${(k)_POST}; do
29   [ "$key" != imagedata ] && printf %s:%s\\n "$key" "${_POST[$key]//$BR/\\n}"
30 done >"$tempfile"
31
32 if [ -n "$_POST[delete_session]" ]; then
33   n="$_POST[delete_session]"
34   sed -i -r '/^session'$n'[_:]/d' "$tempfile"
35   rm "${tpyfile%.tpy}_session${n}.png"
36
37   while grep -Eq '^session'$(($n + 1))'_' "$tempfile"; do
38     sed -i -r 's;^session'$(($n + 1))'(_|:);session'$n'\1;' "$tempfile"
39     mv "${tpyfile%.tpy}_session$(($n+1)).png" "${tpyfile%.tpy}_session${n}.png"
40     n=$(($n+1))
41   done
42
43 elif [ -n "$_POST[new_session]" ]; then
44   sid="$_POST[new_session]"
45
46   identify "$_EXEC/static/therapy_background.png" \
47   | sed -r 's;^.* ([0-9]+x[0-9]+) .*$;\1;' \
48   | read dim
49
50   convert -size "$dim" xc:transparent "${tpyfile%.tpy}_${sid}.png"
51
52   printf '%s:exists\n' "$sid" >>"$tempfile"
53   printf '%s_open:checked\n' "$sid" >>"$tempfile"
54
55 elif [ -n "$_POST[imagedata]" ]; then
56   sed -rn 's;^(session[0-9]+)_open:checked$;\1;p' "$tempfile" \
57   | sort -n \
58   | tail -n1 \
59   | read sid
60
61   convert "${tpyfile%.tpy}_${sid}.png" \
62           -strokewidth 2 -fill '#00000000' \
63           -draw "${_POST[imagedata]}" -transparent white \
64           "${tpyfile%.tpy}_${sid}.png"
65   sync
66 fi
67
68 mv "$tempfile" "$tpyfile"
69
70 redirect "?p=therapy&id=${tpy}"