-
Notifications
You must be signed in to change notification settings - Fork 8
/
extract-manual
executable file
·237 lines (213 loc) · 5.74 KB
/
extract-manual
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/sh -x
#
# extract-manual - get ahold of the latest manual and unpack it for use
#
# Look for built files, then built tar-file in each of these in turn
locations="/home/web/firehol/download/firehol/unsigned/master/ ../firehol"
scriptname=extract-manual
if ! MYTMP=$(mktemp -d -t $scriptname-XXXXXX)
then
echo >&2
echo >&2
echo >&2 "Cannot create temporary directory."
echo >&2
exit 1
fi
cleanup() {
status=$?
rm -rf "${MYTMP}"
exit $status
}
# clean up if we get stopped by Crtl-C or forced logout or normal exit
trap cleanup INT
trap cleanup HUP
trap cleanup 0
set -e
mode=single
src=
for l in $locations
do
if [ -f $l/doc/firehol-manual.html ]
then
cur=`pwd`
cd $l/doc
src=`pwd`
cd $cur
echo "Extracting manual from $src"
break
fi
if [ -f $l/doc/firehol/firehol-manual.html ]
then
cur=`pwd`
cd $l/doc
src=`pwd`
cd $cur
mode=separate
echo "Extracting manual from $l/doc"
break
fi
done
local=
for l in $locations
do
if [ $(ls $l/firehol-*.tar.gz 2> /dev/null | wc -l) -eq 1 ]
then
local=
fi
done
if [ ! "$local" ]
then
if [ $(ls ./firehol-*.tar.gz 2> /dev/null | wc -l) -gt 0 ]
then
echo "Using cached local .tar.gz - remove to update from github"
else
set +x
if [ -f .travis/read_only_api_key ]
then
echo Using decrypted read_only_api_key
token="Authorization: token $(cat .travis/read_only_api_key)"
token_opt="-H"
else
echo read_only_api_key not decrypted - attempting to proceed without
token=""
token_opt=""
fi
curl "$token_opt" "$token" -s https://api.github.com/repos/firehol/firehol/releases/latest > $MYTMP/tag.txt
set -x
set +e
tar=$(sed -ne '/"name":/{s/^[^:]*: "//;s/".*//;p}' $MYTMP/tag.txt | grep '.tar.gz$')
tag=$(sed -ne '/"tag_name":/{s/^[^:]*: "//;s/".*//;p}' $MYTMP/tag.txt)
set -e
if [ ! "$tar" -o ! "$tag" ]
then
echo "no local copy and unable to detemine download location - exiting"
echo "----------------------------------------------------------------"
cat $MYTMP/tag.txt
exit 1
fi
echo "downloading manual from latest release"
curl -s -L -o "$tar" "https://github.com/firehol/firehol/releases/download/$tag/$tar"
fi
locations=.
fi
for l in $locations
do
if [ $(ls $l/firehol-*.tar.gz 2> /dev/null | wc -l) -eq 1 ]
then
echo "Extracting manual from" $l/firehol-*.tar.gz
tar xfzC $l/firehol-*.tar.gz $MYTMP
if [ -f $MYTMP/firehol*/doc/firehol-manual.html ]
then
src=$(ls -d $MYTMP/firehol*/doc 2> /dev/null)
break
elif [ -f $MYTMP/firehol*/doc/firehol/firehol-manual.html ]
then
src=$(ls -d $MYTMP/firehol*/doc 2> /dev/null)
mode=separate
break
else
# Docs not in this tar, continue looking
rm -rf $MYTMP/firehol*
fi
fi
done
if [ ! "$src" ]
then
1>&2 echo "No single tar.gz or built docs found in expected locations"
1>&2 echo " ($locations)"
exit 1
fi
if [ $mode = "single" -a ! -f $src/contents.md ]
then
echo "Old-style manual: no longer supported"
exit 1
fi
if [ $mode = "separate" -a ! -f $src/firehol/contents.md ]
then
echo "New-style manual: missing firehol contents"
exit 1
fi
if [ $mode = "separate" -a ! -f $src/fireqos/contents.md ]
then
echo "New-style manual: missing fireqos contents"
exit 1
fi
if [ $mode = "separate" -a ! -f $src/vnetbuild/contents.md ]
then
echo "New-style manual: missing vnetbuild contents"
exit 1
fi
process() {
echo "Importing pandoc directly to website: $2 .. $3"
base="$1"
doc="$2"
dest="$3"
if [ "$4" ]
then
v=" (v$4)"
else
v=""
fi
rm -rf content/$dest
mkdir -p content/$dest
cd content/$dest || exit 1
for i in $doc/*.md
do
o=$(basename $i)
# Remove manpage headers and change references into the final website form
sed -e '1i---' \
-e '1isubmenu: documentation' \
-e '4i---' \
-e "1s/^% \([^ ]*\) .*/title: Reference Manual$v - \1\ndescription: \1 Reference Manual\nkeywords: firewall, linux, free, gpl, manual, reference, \1/p" \
-e '1,3d' \
-e '/^%/d' \
-e 's;\.[0-9]*\.md;.md;g' \
-e "s;(\([^)]*\)\.md\([^)]*\));(/$dest/\1/\2);" \
-e 's;http://firehol.org/;/;g' $i > $o
# Import cleaned links into each page
cat ../../tmp/links* ../../tmp/service-links >> $o
done
mv contents.md ../$dest.md
cd ../..
# Copy unpacked manuals
rm -rf content/files/$dest
mkdir -p content/files/$dest/css
mkdir -p content/files/$dest/html
mkdir -p content/files/$dest/pdf
cp $doc/../*.css content/files/$dest/css/.
cp $doc/*.html content/files/$dest/html/.
cp $doc/*.pdf content/files/$dest/pdf/.
}
mkdir -p tmp
for i in $src/links* $src/service-links
do
j=$(basename $i)
dest=firehol-manual
case $i in
*fireqos)
dest=fireqos-manual
;;
*vnetbuild)
dest=vnetbuild-manual
;;
esac
# Import links from manual, cleaning up anchors that are not available
# in the separate-page versions.
sed -e 's;\.[0-9]*\.md;.md;g' \
-e "s;: \(.*\)\.md\(.*\);: /$dest/\1/\2;" $i | \
awk -F/ '/^[[]/ { anchor=$NF; page=$(NF-1); a=anchor; sub(/^#/, "", a);
sub(/[0-9]*$/, "", a); gsub(/[^a-z0-9_-]/, "-", a);
if (a == page) { sub(anchor "$", "") }
}
{ print }' > tmp/$j
done
if [ $mode = separate ]
then
version=$(sed -n -e 's/^define(\[VERSION_.*\], \[\(.*\)\]).*/\1./p' $src/../configure.ac |tr -d '\n'|sed -e 's/\.$//' -e 's/\._/./')
process $src $src/firehol firehol-manual "$version"
process $src $src/fireqos fireqos-manual "$version"
process $src $src/vnetbuild vnetbuild-manual "$version"
else
process $src $src manual
rm -f content/manual/README.md
fi