-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.sh
200 lines (161 loc) · 5.84 KB
/
parser.sh
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
# Darius Mihai, University Politehnica of Bucharest
#
# Parser to create badges based on the template
# Please change variables below to fit your needs
#
# By default, the script expects the directory it's placed in to be a fitting
# work directory
#
# Relevant variables:
# -> *_REF_* variables values the script will look for in the template file to
# replace with actual values
# -> GUESTS_FILE is the name of a CSV file that contains the names, emails, and
# other information for the guests
# TEMPLATE_DOC is the badge template file
set -eu
SCRIPT_NAME="`readlink -f $0`"
WORK_DIR="${SCRIPT_NAME%/*}"
GUESTS_FILE="${WORK_DIR}/tickets.csv"
TEMPLATE_DOC="${WORK_DIR}/EuroBSDCon-2018-Badge_Template.docx"
PARAM_NAME_IDX=1
PARAM_MAIL_IDX=2
PARAM_ROLE_IDX=3
PARAM_FIRST_EVENT_IDX=4
GUEST_ROLE_REF_COLOUR="ccddee"
GUEST_EVENTS_REF_COLOUR="CFE2F3"
GUEST_TUTORIALS_REF_COLOUR="FFF2CC"
BADGE_NUM=0
BADGES_DIR="${WORK_DIR}/badges"
TMP_DIR="${WORK_DIR}/tmp"
GUEST_EVENTS=""
GUEST_EVENTS_COLOUR="FFFFFF"
GUEST_TUTORIALS=""
GUEST_TUTORIALS_COLOUR="FFFFFF"
declare -A EVENTS
EVENTS=(
["Conference"]="CONF"
["Social event"]="SOC"
["NetBSD devsummit"]="NETBSD"
["FreeBSD devsummit (Thursday)"]="FBSD (T)"
["FreeBSD devsummit (Friday)"]="FBSD (F)"
["Tutorial: Advanced container management with libiocage"]="T1"
["Tutorial: Ports and Poudriere"]="T3"
["Tutorial: Managing BSD Systems with Ansible"]="T2"
["Tutorial: LibTLS Tutorial for TLS beginners"]="T4"
["Tutorial: Introduction to BGP for developers and sysadmins"]="T5"
["Tutorial: An Introduction to the FreeBSD Open-Source Operating System"]="T6"
)
SORTED_EVENTS=("FBSD (T)" "FBSD (F)" "NETBSD" "CONF" "SOC")
SORTED_TUTORIALS=("T1" "T2" "T3" "T4" "T5" "T6")
get_best_event_map() {
local EVENT="${1}"
if [ -z "$(echo -e ${EVENT} | tr -d '[:space:]')" ]; then
return
fi
if [ ! -z "${EVENTS[${EVENT}]+exists}" ]; then
echo "${EVENTS[${EVENT}]}"
return
else
for EV in "${!EVENTS[@]}"; do
if [[ "${EVENT}" = "${EV}"* ]]; then
echo "${EVENTS[${EV}]}"
return
fi
done
fi
exit 1
}
sort_events_and_tutorials() {
local EVTS=""
local TUTS=""
for EVT in "${SORTED_EVENTS[@]}"; do
if [[ "${GUEST_EVENTS}" = *"${EVT}"* ]]; then
if [ -z "${EVTS}" ]; then
EVTS="${EVT}"
else
EVTS+=" ${EVT}"
fi
fi
done
# Special clause for a second Social event invitation
if [[ "${GUEST_EVENTS}" = *"SOC"*"SOC"* ]]; then
if [ -z "${EVTS}" ]; then
EVTS="SOC2"
else
EVTS+=" SOC2"
fi
fi
for TUT in "${SORTED_TUTORIALS[@]}"; do
if [[ "${GUEST_TUTORIALS}" = *"${TUT}"* ]]; then
if [ -z "${TUTS}" ]; then
TUTS="${TUT}"
else
TUTS+=" ${TUT}"
fi
fi
done
GUEST_EVENTS="${EVTS}"
GUEST_TUTORIALS="${TUTS}"
}
mkdir -p "${BADGES_DIR}"
rm -rf "${BADGES_DIR}"/*
PROGRESS=0
while IFS='' read -r LINE || [[ -n "${LINE}" ]]; do
IFS=','; PARAMS=(${LINE}); unset IFS
printf "Parsing... [%d]: %s\n" $((++PROGRESS)) "${PARAMS[${PARAM_NAME_IDX}]}"
#MAIL="${PARAMS[$PARAM_MAIL_IDX]%@*}"
#if [ "${MAIL%+*}" = "${PARAMS[$PARAM_NAME_IDX]}" ]; then
# echo "${PARAMS[$PARAM_MAIL_IDX]}"
#fi
mkdir -p "${TMP_DIR}"; rm -rf "${TMP_DIR}"/*
unzip "${TEMPLATE_DOC}" -d "${TMP_DIR}" 2>&1 >/dev/null
# Set parameters for document depending on the guest type and other parameters
case "${PARAMS[${PARAM_ROLE_IDX}]^^}" in
SPEAKER) GUEST_ROLE_COLOUR="ff0000" ;;
ATTENDEE) GUEST_ROLE_COLOUR="439f4c" ;;
WHEEL) GUEST_ROLE_COLOUR="4a86e8" ;;
esac
GUEST_EVENTS=""
GUEST_EVENTS_COLOUR="FFFFFF"
GUEST_TUTORIALS=""
GUEST_TUTORIALS_COLOUR="FFFFFF"
for IDX in $(seq ${PARAM_FIRST_EVENT_IDX} $((${#PARAMS[@]}-1))); do
EVENT="$(get_best_event_map "${PARAMS[${IDX}]}")"
if [[ "${EVENT}" = "T"[1-6] ]]; then
GUEST_TUTORIALS+=" | ${EVENT}"
GUEST_TUTORIALS_COLOUR="${GUEST_TUTORIALS_REF_COLOUR}"
else
GUEST_EVENTS+=" | ${EVENT}"
GUEST_EVENTS_COLOUR="${GUEST_EVENTS_REF_COLOUR}"
fi
done
# Sort the events and tutorials in an expected order
sort_events_and_tutorials
# For empty registrations leave the lines coloured, and fill the text later in pen
if [ -z "${GUEST_EVENTS}" -a -z "${GUEST_TUTORIALS}" ]; then
GUEST_TUTORIALS_COLOUR="${GUEST_TUTORIALS_REF_COLOUR}"
GUEST_EVENTS_COLOUR="${GUEST_EVENTS_REF_COLOUR}"
fi
# Set the values in the document
sed -i -e "s|GUEST_MAIL|${PARAMS[${PARAM_MAIL_IDX}]}|g" \
-e "s|GUEST_NAME|${PARAMS[${PARAM_NAME_IDX}]}|g" \
-e "s|GUEST_ROLE|${PARAMS[${PARAM_ROLE_IDX}]^^}|g" \
-e "s|${GUEST_ROLE_REF_COLOUR}|${GUEST_ROLE_COLOUR}|g" \
-e "s|GUEST_EVENTS|${GUEST_EVENTS}|g" \
-e "s|${GUEST_EVENTS_REF_COLOUR}|${GUEST_EVENTS_COLOUR}|g" \
-e "s|GUEST_TUTORIALS|${GUEST_TUTORIALS}|g" \
-e "s|${GUEST_TUTORIALS_REF_COLOUR}|${GUEST_TUTORIALS_COLOUR}|g" \
${TMP_DIR}/word/document.xml
# Create the new document with the values inserted in it
cd "${TMP_DIR}"
DOC_NAME="${BADGES_DIR}/EuroBSDCon_Badge_$((++BADGE_NUM)).docx"
zip -rq "${DOC_NAME}" *
rm -rf "${TMP_DIR}"
done < "${GUESTS_FILE}"
sync
# Convert everything to PDFs (cd is required since we might be in tmp which was removed)
cd "${BADGES_DIR}"
lowriter --headless --convert-to pdf $(ls "${BADGES_DIR}"/*.docx | sort -V)
# Merge PDFs for convenience
pdfunite $(ls "${BADGES_DIR}"/*.pdf | sort -V) ${BADGES_DIR}/merged.pdf