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