]> git.plutz.net Git - confetti/blob - actions/generate_courselist.sh
improved styling for filter input
[confetti] / actions / generate_courselist.sh
1 #!/bin/zsh
2
3 # Copyright 2014, 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 pdflatex="$(where pdflatex |head -n1 || echo false)"
21 course="${_GET[course]}"
22 fromdate="${_GET[fromdate]}"
23 fromdate="$(date -d "$fromdate" +%s)" 2>/dev/null
24 [ -z "$fromdate" ] && fromdate=$(date +%s)
25
26 . ${_EXEC}/pages/courses.sh
27 . ${_EXEC}/pages/cards.sh
28
29 tex_clean() { #in dire need for improvement
30   echo "$*" |tr -d '{&}\\'
31 }
32
33
34 list_attendee() {  #Parameter: Cardfile
35   id="$1"
36   cardfile="$_DATA/vcard/${id}"
37   unset key
38
39   declare -A tags
40   declare -A values
41   vcf_parse "$cardfile" |while read -r line; do
42     declare -A tag
43     case "$line" in
44       value*) eval "$line";;
45       tag*)   eval "$line";;
46       key*)
47         if [ -z "$key" ]; then
48           eval "$line"
49         else
50           values[$key]="$(tex_clean "$value")"
51           for t in ${(k)tag}; do
52             tags[${key}_$t]="$tag[$t]"
53           done
54           eval "$line"
55           if [ -n "$values[$key]" ]; then
56             n=0
57             while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done
58             key=$key$n
59           fi
60           unset value
61           unset tag
62         fi
63       ;;
64     esac
65   done
66
67   n=$(printf %s "$values[N]" \
68       | sed -rn 's:^([^;]*)(;[^;]*)(;[^;]*)?(;[^;]*)?(;[^;]*)?$:\4 \2 \3 \1 \5:gp' \
69       | sed -r 's:,: :;s:;: :g;s: +: :g;s:^ $::;'
70      )
71   fullname="${n:-${values[FN]:-${values[NICKNAME]}}}"
72
73   tel=''
74   for n in TEL TEL{0..10}; do if (echo "$values[$n]" |grep -Eq '[0-9]'); then
75     [ -n "$tel" ] && tel="$tel\\newline $values[$n]" || tel="$values[$n]"
76   fi; done
77
78   note=''
79   for n in NOTE NOTE{0..10}; do if [ -n "$values[$n]" ]; then
80     [ -n "$note" ] && note="$note\\newline $values[$n]" || note="$values[$n]"
81   fi; done
82   echo -E "$fullname & $values[BDAY] & $tel & $note"
83 }
84
85 get_dates() {  #Parameter: Calendarfile
86   calendarfile="$_DATA/ical/$course"
87   unset key
88
89   declare -A tags
90   declare -A values
91   ics_parse "$calendarfile" |while read -r line; do
92     declare -A tag
93     case "$line" in
94       value*) eval "$line";;
95       tag*)   eval "$line";;
96       key*)
97         if [ -z "$key" ]; then
98           eval "$line"
99         else
100           values[$key]="$(tex_clean "$value")"
101           for t in ${(k)tag}; do
102             tags[${key}_$t]="$tag[$t]"
103           done
104           eval "$line"
105           if [ -n "$values[$key]" ]; then
106             n=0
107             while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done
108             key=$key$n
109           fi
110           unset value
111           unset tag
112         fi
113       ;;
114     esac
115   done
116
117   dtstart="$values[DTSTART]"
118   [ -z "$dtstart" ] && dtstart=$(date +%Y%m%dT%H%M%S)
119   echo "$dtstart" |case "$dtstart" in
120     *Z)    sed -rn 's:^([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z$:\1-\2-\3 \4\:\5\:\6 UTC:p';;
121     TZID*) sed -rn 's:^TZID=(.+)\:([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})$:TZ="\1" \2-\3-\4 \5\:\6\:\7:p';;
122     *)     sed -rn 's:^([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})$:\1-\2-\3 \4\:\5\:\6:p';;
123   esac |read dts_date
124   rrule="$values[RRULE]"
125   rr_int="$(echo $rrule |sed -rn 's:^.*INTERVAL=([0-9]+)(;.*)?$:\1:p')"
126   rr_freq="$(echo $rrule |sed -rn 's:^.*FREQ=(YEARLY|MONTHLY|WEEKLY|DAILY)(;.*)?$:\1:p')"
127   case "$rr_freq" in
128     YEARLY) rec="$rr_int year";;
129     MONTHLY) rec="$rr_int month";;
130     DAILY) rec="$rr_int day";;
131     *) rec="$rr_int week";;
132   esac
133
134   next_date="$dts_date"
135   n=10
136   while [ $n -gt 0 ]; do
137     if [ "$(date -d "$next_date" +%s)" -gt "$(date +%s)" ]; then
138       dtlist="$dtlist & $(date -d "$next_date" +"%d. %b.")"
139       n=$(($n - 1))
140     fi
141     next_date="$(date -d "$next_date + $rec" +%Y-%m-%d)"
142   done
143
144   echo "$dtlist"
145 }
146
147 if [ -r "${_DATA}/ical/${course}" ]; then
148   . ${_EXEC}/templates/course_print.sh >"${_DATA}/temp/courselist_${course}.tex"
149   [ -e "${_DATA}/temp/courselist_${course}.pdf" ] && rm "${_DATA}/temp/courselist_${course}.pdf"
150   "$pdflatex" -halt-on-error -output-directory "${_DATA}/temp/" "${_DATA}/temp/courselist_${course}.tex" |debug >/dev/null
151   "$pdflatex" -halt-on-error -output-directory "${_DATA}/temp/" "${_DATA}/temp/courselist_${course}.tex" |debug >/dev/null
152 fi
153 if [ -r "${_DATA}/temp/courselist_${course}.pdf" ]; then
154   echo 'Content-Type: application/x-pdf\n'
155   cat "${_DATA}/temp/courselist_${course}.pdf"
156 fi