]> git.plutz.net Git - lobster/blob - therapies/update_therapy.sh
Merge commit '8456138268c26b78936c0c2b22a7cc6ab9a7dd14' into cgilite
[lobster] / therapies / update_therapy.sh
1 #!/bin/zsh
2
3 # Copyright 2016, 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 tpy=$(POST id)
22
23 tpyfile="$_DATA/therapies/${tpy}.tpy"
24 tempfile="$_DATA/temp/${tpy}.tpy"
25
26 # serialize POST array into file
27 for key in $(POST_KEYS); do
28   case "$key" in
29     imagedata) : ;;
30     session*_date)
31       value="$(POST "$key")"
32       y=0 mon=0 dom=0
33       case $value in
34         *.*.*) IFS=. read dom mon y <<-END
35                 ${value}
36                 END
37           ;;
38         *.*.) IFS=. read dom mon <<-END
39                 ${value}
40                 END
41           ;;
42         */*/*) IFS=/ read mon dom y <<-END
43                 ${value}
44                 END
45           ;;
46         */*) IFS=/ read mon dom <<-END
47                 ${value}
48                 END
49           ;;
50         *-*-*) IFS=- read y mon dom <<-END
51                 ${value}
52                 END
53           ;;
54       esac
55       [ ! "$y" ] && y="$(date +%Y)"
56       [ "$y" -gt 0 -a "$y" -lt 100 ] && y="$((y + 2000))"
57       date -d "${y}-${mon}-${dom}" + && printf %s:%s\\n "$key" "$(date -d "${y}-${mon}-${dom}" +%F)" \
58                                      || printf %s:\\n "$key"
59       ;;
60     *) printf %s:%s\\n "$key" "$(pdi_escape "$(POST "$key")")" ;;
61   esac
62 done >"$tempfile" 2>&-
63
64 if [ "$(POST delete_session)" ]; then
65   n="$(POST delete_session)"
66   sed -Ei '/^session'$n'[_:]/d' "$tempfile"
67   rm "${tpyfile%.tpy}_session${n}.png"
68
69   while grep -Eq '^session'$(($n + 1))'_' "$tempfile"; do
70     sed -Ei 's;^session'$(($n + 1))'(_|:);session'$n'\1;' "$tempfile"
71     mv "${tpyfile%.tpy}_session$(($n+1)).png" "${tpyfile%.tpy}_session${n}.png"
72     n=$(($n+1))
73   done
74
75 elif [ "$(POST new_session)" ]; then
76   sid="$(POST new_session)"
77
78   read junkx junky dim junkz <<-E_READ
79         $(identify "$_EXEC/therapies/therapy_background.png")
80         E_READ
81
82   convert -size "$dim" xc:transparent "${tpyfile%.tpy}_${sid}.png"
83
84   printf '%s:exists\n' "$sid" >>"$tempfile"
85   printf '%s_open:checked\n' "$sid" >>"$tempfile"
86
87 elif [ "$(POST imagedata)" ]; then
88   sid="$(sed -En 's;^(session[0-9]+)_open:checked$;\1;p' "$tempfile" \
89          | sort -n \
90          | tail -n1
91        )"
92
93   convert "${tpyfile%.tpy}_${sid}.png" \
94           -draw "$(POST imagedata)" -transparent white \
95           "${tpyfile%.tpy}_${sid}.png"
96   sync
97 fi
98
99 mv "$tempfile" "$tpyfile"
100 rm -f "${_DATA}/cache/${tpy%%.*}.vcf.cache"
101
102 if [ "$(POST autosubmit)" = "true" ]; then
103   printf 'HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n'
104 else
105   REDIRECT "/therapies/${tpy%.*}/${tpy#*.}"
106 fi