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