]> git.plutz.net Git - confetti/blob - courses/export_pdf.sh
update naming convention for exported files
[confetti] / courses / export_pdf.sh
1 #!/bin/sh
2
3 . "${_EXEC}/pdiread.sh"
4 . "$_EXEC/cards/l10n.sh"
5
6 coursefile="${_DATA}/ical/$(GET course)"
7
8 if [ ! -r "$coursefile" ]; then
9   SET_COOKIE 0 message="Cannot read course file"
10   REDIRECT /courses/
11   return 0
12 elif ! mkdir -p "$_DATA/export"; then
13   SET_COOKIE 0 message="Cannot create export directory"
14   REDIRECT /courses/
15   return 0
16 fi
17
18 ics="$(pdi_load "$coursefile")"
19 htmlfile="${_DATA}/export/$(pdi_value "$ics" SUMMARY |unescape |tr \\n/ __).html"
20 pdffile=${htmlfile%.html}.pdf
21
22 pdi_date() {
23   local pdt y m d H M S Z
24   [ $# -eq 0 ] && read pdt || pdt="$*"
25
26   case $pdt in
27     *T*Z)
28       Z=UTC; pdt="${pdt%Z}";;
29     TZID=*:*T*)
30       Z="${pdt%%:*}"; Z=${Z#TZID=}; pdt=${pdt#TZID=*:};;
31   esac
32
33   y="${pdt%%????T*}" pdt=${pdt#????}
34   m="${pdt%%??T*}" pdt=${pdt#??}
35   d="${pdt%%T*}" pdt=${pdt#??T}
36   H="${pdt%%????}" pdt=${pdt#??}
37   M="${pdt%%??}" pdt=${pdt#??}
38   S="${pdt}" pdt=''
39
40   case Z in 
41     UTC) date -d "${y}-${m}-${d} ${H}:${M}:${S} UTC" +%s;;
42      '') date -d "${y}-${m}-${d} ${H}:${M}:${S}" +%s;;
43       *) date -d "TZ=\"${Z}\" ${y}-${m}-${d} ${H}:${M}:${S}" +%s;;
44   esac
45 }
46
47 get_dates() {
48   local dts_date rrule rr_int rr_freq rec today="$(date +%Y%m%d)"
49
50   dts_date="$(pdi_value "$ics" DTSTART || printf %s "$today")"
51   dts_date="${dts_date#TZID=*:}" dts_date="${dts_date%%T*}"
52   rrule="$(pdi_value "$ics" RRULE)"
53   rr_int="${rrule##*INTERVAL=}" rr_int="${rr_int%%;*}"
54   rr_freq="${rrule##*FREQ=}" rr_freq="${rr_freq%%;*}"
55
56   [ "$rr_int" -ge 0 ] || rr_int=1 2>/dev/null
57   case "$rr_freq" in
58     YEARLY)  rec="$rr_int year";;
59     MONTHLY) rec="$rr_int month";;
60     DAILY)   rec="$rr_int day";;
61     WEEKLY)  rec="$rr_int week";;
62     *) rec="$rr_int week";;
63   esac
64
65   while [ "$dts_date" -lt "$today" ]; do dts_date="$(date -d "${dts_date} + ${rec}" +%Y%m%d)"; done
66   for n in 1 2 3 4 5 6 7 8 9 10; do
67     LANG=de_DE.UTF-8 date -d "$dts_date" +"%d. %b."
68     dts_date="$(date -d "${dts_date} + ${rec}" +%Y%m%d)"
69   done
70 }
71
72 # some table styles need to be inline, because this is how libreoffice works
73 style_td='style="border: 1pt solid; padding: 1mm 2mm; vertical-align: top;"'
74
75 "$_EXEC/cgilite/html-sh.sed" <<-EOF >"$htmlfile"
76 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
77 [html [head
78   [meta http-equiv="content-type" content="text/html; charset=utf-8"]
79   [title]
80   [meta name="generator" content="Confetti"]
81   [meta name="created" content="$(date +%FT%T)"]
82   [meta name="changed" content="$(date +%FT%T)"]
83   [style type="text/css"
84     @page { size: 29.7cm 21cm; margin: 1.5cm; }
85     * { background: inherit; }
86     body { background: transparent; font-family: Liberation Sans, Sans-Serif; }
87
88     th { white-space: pre; }
89     th, td { text-align: left; }
90   ]
91 ][body lang="de_DE"
92   [table width="100%" style="page-break-after: always;"
93     [col width=10*] [col width=5*] [col width=10*] [col width=15*]
94     [thead
95       [tr [th $style_td . $(l10n N)] [th $style_td . $(l10n BDAY)] [th $style_td . $(l10n TEL)] [th $style_td . $(l10n NOTE)]]
96     ][tbody
97       $(grep -F "${coursefile##*/}      " "$_DATA/mappings/attendance" |while read discard each; do
98         vcf="$(pdi_load "$_DATA/vcard/$each")"
99         tel="$( seq 1 $(pdi_count "$vcf" TEL) |while read n; do
100                   type="$(pdi_attrib "$vcf" TEL $n TYPE)"
101                   [ "$type" ] && type="$(l10n "TYPE=$type"):"
102                   printf '%s %s<br>' "$type" "$(pdi_value "$vcf" TEL $n)"
103                 done  )"
104         printf '[tr valign=top [td %s .N . %s] [td %s .BDAY . %s] [td %s .TEL . %s] [td %s .NOTE . %s]]\n' \
105                "$style_td" "$(pdi_value "$vcf" FN   |unescape |HTML)" \
106                "$style_td" "$(pdi_value "$vcf" BDAY |unescape |HTML)" \
107                "$style_td" "$tel" \
108                "$style_td" "$(pdi_value "$vcf" NOTE |unescape |HTML)"
109       done |sort)]
110   ]
111   [table width="100%"
112     [col width=30*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*]
113     [thead
114       [tr [th $style_td ] $(get_dates |xargs -d\\n printf "[th $style_td . %s]")]
115     ][tbody
116     $(grep -F "${coursefile##*/}        " "$_DATA/mappings/attendance" |while read discard each; do
117       vcf="$(pdi_load "$_DATA/vcard/$each")"
118       printf '[tr [td %s .N . %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s]]\n' \
119              "$style_td" "$(pdi_value "$vcf" FN |unescape |HTML)" \
120              "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td"
121     done |sort)]
122   ]
123 ]]
124 EOF
125
126 export            HOME="$_DATA"
127 export XDG_CONFIG_HOME="$_DATA/xdg_config"
128 export  XDG_CACHE_HOME="$_DATA/xdg_cache"
129 export   XDG_DATA_HOME="$_DATA/xdg_local"
130
131 lowriter --convert-to pdf --outdir "$_DATA/export/" "$htmlfile" >/dev/null
132 REDIRECT "$(URL "/export/${pdffile##*/}")"