-
Notifications
You must be signed in to change notification settings - Fork 9
/
ics
executable file
·121 lines (110 loc) · 2.89 KB
/
ics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
#
# This file is part of badge2019.
# Copyright 2019 Emil Renner Berthing <esmil@labitat.dk>
#
# badge2019 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# badge2019 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with badge2019. If not, see <http://www.gnu.org/licenses/>.
set -e
readonly URL='https://bornhack.dk/bornhack-2019/program/ics/'
time_to_unix() {
local ts="${1//T/ }"
date -d "${ts%??Z} UTC" '+%s'
}
declare -a summary
declare -a location
declare -a tstart
declare -a tend
i=0
while read -r line; do
case "$line" in
'BEGIN:VCALENDAR')
;;
'BEGIN:VEVENT')
;;
'END:VEVENT')
i=$((i+1))
;;
'END:VCALENDAR')
break;;
'SUMMARY:'*)
summary[$i]="$(echo "${line#SUMMARY:}" | sed "s/æ/ae/g;s/ø/oe/g;s/å/aa/g;s/’/'/g;s/–/-/g")";;
'LOCATION:'*)
location[$i]="${line#LOCATION:}";;
'DTSTART:'*)
tstart[$i]="$(time_to_unix "${line#DTSTART:}")";;
'DTEND:'*)
tend[$i]="$(time_to_unix "${line#DTEND:}")";;
'DESCRIPTION:'*)
;;
*)
echo "Don't know '$line'" >&2;;
esac
done < <(curl -s "$URL" | sed -ne ':a;$!N;s/\n //;ta;s/\r//g;s/\\,/,/g;P;D')
readonly N=$i
declare -a sorted
i=0
while read -r e; do
e="${e##* }"
sorted[$i]=$e
i=$((i+1))
done < <(
for ((i=0;i < N; i++)); do
echo "${tstart[$i]} $i"
done | sort
)
echo '/* generated by the ics script from badge2019 */'
echo
for ((i=0; i < N; i++)); do
e=${sorted[$i]}
echo "static const char *event$i[] = {"
LC_TIME='C' printf '\t"%.3(%A)T %(%H:%M)T-%(%H:%M)T",\n' \
"${tstart[$e]}" "${tstart[$e]}" "${tend[$e]}"
printf '\t"%s",\n' "${location[$e]}"
text="${summary[$e]}"
line=''
for word in $text; do
if (( ${#line} + ${#word} + 1 < 21 )); then
line="$line $word"
elif (( ${#word} <= 20 )); then
printf '\t"%s",\n' "${line# }"
line="$word"
else
printf '\t"%s",\n' "${line# }"
while (( ${#word} > 20 )); do
printf '\t"%.20s",\n' "$word"
word="${word#????????????????????}"
done
line="$word"
fi
done
printf '\t"%s",\n' "${line# }"
printf '};\n\n'
done
lday=
for ((i=0; i < N; i++)); do
e=${sorted[$i]}
day=$(printf '%(%d)T' "${tstart[$e]}")
day=${day#0}
day=$(($day - 8))
if [[ "$day" != "$lday" ]]; then
[[ -z "$lday" ]] || printf '};\n\n'
echo "static const struct event day$day[] = {"
lday="$day"
fi
echo -e "\t{ event$i, ARRAY_SIZE(event$i), },"
done
echo '};'
echo
printf '/* %s: set ft=c: */\n' vim
# vim: set ft=sh ts=2 sw=2 et: