-
Notifications
You must be signed in to change notification settings - Fork 3
/
wizard.sh
executable file
·192 lines (159 loc) · 5.36 KB
/
wizard.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
#!/bin/bash
DIR=$(pwd)
CLANG=${CLANG:-C.UTF-8}
IMAGE=${IMAGE:-netways/showoff:0.20.4}
CNAME=${CNAME:-showoff}
TRAINING=${TRAINING:-$(basename "$DIR")}
RUNTIME=${RUNTIME:-$(command -v docker)}
GIT=${GIT:-$(command -v git)}
SCRIPT_DIR=$(cd $(dirname $0); pwd -P)
NO_RESET=${NO_RESET:-""}
# Run given command in the showoff container
execdocker () {
if [[ -n $($RUNTIME ps -aq -f name=$CNAME 2> /dev/null) ]]; then
$RUNTIME rm -f $CNAME 2> /dev/null
fi
case $RUNTIME in
*podman*) USERNS=keep-id;;
*) USERNS=host;;
esac
$RUNTIME run \
-it \
--name=$CNAME \
--rm \
-p 9090:9090 \
--user $(id -u) \
--userns=$USERNS \
-v "$DIR:/training:z" \
-e "LANG=$CLANG" \
-e "LANGUAGE=$CLANG" \
-e "LC_ALL=$CANG" \
$IMAGE \
$1
}
# Generate handouts as showoff HTML and convert HTML to PDF
printhandouts () {
echo -e "\n--- RUN SHOWOFF STATIC FOR HANDOUTS ---"
execdocker "showoff static print"
# Removes showoff's Section markers for PDF output
sed -i 's/~~~.*~~~ //g' static/index.html
echo -e "\n--- RUN WKHTMLTOPDF FOR HANDOUTS ---"
execdocker "wkhtmltopdf --enable-local-file-access --load-error-handling ignore -s ${FORMAT} --print-media-type --footer-left [page] --footer-right ©NETWAYS static/index.html ${TRAINING}_${1}_${FORMAT}-handouts.pdf"
}
# Generate exercises as showoff HTML and convert HTML to PDF
printexercises () {
echo -e "\n--- RUN SHOWOFF STATIC FOR EXERCISES ---"
execdocker "showoff static supplemental exercises"
# Removes showoff's Section markers for PDF output
sed -i 's/~~~.*~~~ //g' static/index.html
echo -e "\n--- RUN WKHTMLTOPDF FOR EXERCISES ---"
execdocker "wkhtmltopdf --enable-local-file-access --load-error-handling ignore -s ${FORMAT} --print-media-type --footer-left [page] --footer-right ©NETWAYS static/index.html ${TRAINING}_${1}_${FORMAT}-exercises.pdf"
}
# Generate solutions as showoff HTML and convert HTML to PDF
printsolutions () {
echo -e "\n--- RUN SHOWOFF STATIC FOR SOLUTIONS ---"
execdocker "showoff static supplemental solutions"
# Removes showoff's Section markers for PDF output
sed -i 's/~~~.*~~~ //g' static/index.html
echo -e "\n--- RUN WKHTMLTOPDF FOR SOLUTIONS ---"
execdocker "wkhtmltopdf --enable-local-file-access --load-error-handling ignore -s ${FORMAT} --print-media-type --footer-left [page] --footer-right ©NETWAYS static/index.html ${TRAINING}_${1}_${FORMAT}-solutions.pdf"
}
setlayout () {
find . -maxdepth 1 -type l -name *.css -delete
ln -s global/layouts/$1.css .
}
# Create reference if it doesn't exist
ln -sf . global
if [[ ! -x $RUNTIME ]]; then
echo "Command '${RUNTIME}' not found, exit"
exit 1
fi
if [[ ! -x $GIT ]]; then
echo "Command 'git' not found, exit"
exit 1
fi
# Wizard
clear
echo "###########################"
echo " NETWAYS Training Wizard "
echo "###########################"
echo -e "\n### LAYOUT ###"
echo -e "
[1] NETWAYS
[2] OSMC
[3] OSDC
[4] OSBConf\n"
LAYOUT_DEFAULT=1
read -p "Which Layout? [1-4] (Default: "$LAYOUT_DEFAULT"): " LAYOUT
LAYOUT="${LAYOUT:-$LAYOUT_DEFAULT}"
while [[ $LAYOUT != [1-4] ]]; do
echo "Invalid option, try again..."
read -p "What to print? [1-4] (Default: "$LAYOUT_DEFAULT"): " LAYOUT
LAYOUT="${LAYOUT:-$LAYOUT_DEFAULT}"
done
case "$LAYOUT" in
1) LAYOUT=netways
sed -i 's|^[ \t\s]*"default":.*| "default": "global/layouts/netways.tpl"|' showoff.json;;
2) LAYOUT=osmc
sed -i 's|^[ \t\s]*"default":.*| "default": "global/layouts/osmc.tpl"|' showoff.json;;
3) LAYOUT=osdc
sed -i 's|^[ \t\s]*"default":.*| "default": "global/layouts/osdc.tpl"|' showoff.json;;
4) LAYOUT=osbconf
sed -i 's|^[ \t\s]*"default":.*| "default": "global/layouts/osbconf.tpl"|' showoff.json;;
esac
setlayout $LAYOUT
echo -e "\n### FORMAT ###"
echo -e "
[1] A5
[2] A4\n"
FORMAT_DEFAULT=1
read -p "Which format? [1-2] (Default: "$FORMAT_DEFAULT"): " FORMAT
FORMAT="${FORMAT:-$FORMAT_DEFAULT}"
while [[ $FORMAT != [1-2] ]]; do
echo "Invalid option, try again..."
read -p "Which format? [1-2] (Default: "$FORMAT_DEFAULT"): " FORMAT
FORMAT="${FORMAT:-$FORMAT_DEFAULT}"
done
case "$FORMAT" in
1) FORMAT=A5;;
2) FORMAT=A4;;
esac
echo -e "\n### MODE ###"
echo -e "
[1] serve
[2] print\n"
MODE_DEFAULT=1
read -p "Which mode? [1-2] (Default: "$MODE_DEFAULT"): " MODE
MODE="${MODE:-$MODE_DEFAULT}"
if [[ $MODE == 1 ]]; then
echo "--- RUN SHOWOFF SERVE ---"
execdocker "showoff serve"
elif [[ $MODE == 2 ]]; then
echo -e "\n### PRINT ###"
echo -e "
[1] Handouts
[2] Handouts & Solutions
[3] Handouts & Exercises & Solutions
[4] Exercises & Solutions\n"
PRINT_DEFAULT=2
read -p "What to print? [1-4] (Default: "$PRINT_DEFAULT"): " PRINT
PRINT="${PRINT:-$PRINT_DEFAULT}"
while [[ $PRINT != [1-4] ]]; do
echo "Invalid option, try again..."
read -p "What to print? [1-4] (Default: "$PRINT_DEFAULT"): " PRINT
PRINT="${PRINT:-$PRINT_DEFAULT}"
done
VERSION_DEFAULT=`grep release showoff.json | cut -f2 -d: | tr -d '"' | tr -d " " | tr -d ","`
read -p "Which version? (Default: "$VERSION_DEFAULT"): " VERSION
VERSION="${VERSION:-$VERSION_DEFAULT}"
case "$PRINT" in
1) printhandouts $VERSION;;
2) printhandouts $VERSION
printsolutions $VERSION;;
3) printhandouts $VERSION
printexercises $VERSION
printsolutions $VERSION;;
4) printexercises $VERSION
printsolutions $VERSION;;
esac
fi