-
Notifications
You must be signed in to change notification settings - Fork 9
/
make.sh
executable file
·335 lines (271 loc) · 6.87 KB
/
make.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/bash
#
# (P) & (C) 2011 - 2011 by Dr. Peter Bieringer <pb@bieringer.de>
#
# Wrapper script for HOWTO generator script
#
# Requires: "generate.sh"
#
# Changelog
#
#
# 20110511/PB: use absolute paths, extend error checking
URL_BASE="http://cvs.tldp.org/go.to/LDP/LDP/users/Peter-Bieringer/"
FILE_EN="Linux+IPv6-HOWTO.sgml"
FILE_DE="Linux+IPv6-HOWTO.de.sgml"
FILE_FR="Linux+IPv6-HOWTO.fr.sgml"
FILE_PT_BR="Linux+IPv6-HOWTO.pt_BR.sgml"
options_wget="--quiet"
DIR_BASE="$HOME/howtos"
DIR_DOWNLOAD="$HOME/howtos/download"
DIR_DEST_BASE="/var/www/html/howtos"
## logging
log() {
local level="$1"
shift
printf "%-6s: %s\n" "$level" "$*"
}
## download file
download() {
local url_base="$1"
local file="$2"
if [ -z "$url_base" ]; then
log "ERROR" "'url_base' empty"
return 1
fi
if [ -z "$file" ]; then
log "ERROR" "'file' empty"
return 1
fi
if [ -z "$DIR_DOWNLOAD" ]; then
log "ERROR" "no download directory given"
return 1
fi
if [ ! -d "$DIR_DOWNLOAD" ]; then
log "ERROR" "download directory doesn't exist: $DIR_DOWNLOAD"
return 1
fi
if [ "$force_download" != "1" ]; then
log "NOTICE" "skip download $url_base/$file (use option -d)"
return 0
fi
log "INFO" "start download $url_base/$file to directory $DIR_DOWNLOAD"
wget $options_wget -N -P "$DIR_DOWNLOAD" "$url_base/$file"
local result=$?
if [ $result -eq 0 ]; then
log "INFO" "download successful: $url_base/$file (stored to $DIR_DOWNLOAD)"
else
log "ERROR" "download not successful: $url_base/$file"
fi
return $result
}
## check, whether processing should be started
check() {
local file_download="$1"
local file="$2"
local dir_html="$3"
if [ -z "$file" ]; then
log "ERROR" "'file' empty"
return 1
fi
if [ -z "$file_download" ]; then
log "ERROR" "'file_download' empty"
return 1
fi
if [ -z "$dir_html" ]; then
log "ERROR" "'dir_html' empty"
return 1
fi
log "INFO" "check file: $file"
if [ ! -f "$file_download" ]; then
log "WARN" "no downloaded file available: $file_download"
return 1
fi
if [ ! -f "$file" ]; then
log "NOTICE" "no 'old' file available: $file (copy now)"
cp -p "$file_download" "$file"
if [ $? -ne 0 ]; then
log "ERROR" "can't copy 'new' file to 'old' file: $file_download -> $file"
return 1
fi
return 0
else
if cmp -s "$file_download" "$file"; then
log "INFO" "'old' is identical with 'new' file: $file"
else
log "NOTICE" "'old' is not equal with 'new' file: $file"
cp -p "$file_download" "$file"
if [ $? -ne 0 ]; then
log "ERROR" "can't copy 'new' file to 'old' file: $file_download -> $file"
return 1
fi
return 0
fi
# check contents of destination directory
log "INFO" "check directory: $dir"
if [ ! -d "$dir" ]; then
log "NOTICE" "directory still not exist: $dir"
return 0
fi
local num_files="`find "$dir" -mindepth 1 -maxdepth 1 -type f | wc -l`"
if [ $num_files -eq 0 ]; then
# directory is empty
log "NOTICE" "directory is empty: $dir"
return 0
fi
local num_files_old="`find "$dir" -mindepth 1 -maxdepth 1 -type f ! -newer "$file" | wc -l`"
if [ $num_files_old -ne 0 ]; then
# force regeneration
log "NOTICE" "directory contains old files: $dir ($num_files_old)"
return 0
fi
# 'old' file already exists
if [ "$file" -nt "$file_download" ]; then
log "WARN" "'old' is newer than 'new' file: $file"
return 1
fi
fi
if [ "$force_generate" = "1" ]; then
log "NOTICE" "force regeneration of: $file"
return 0
fi
# nothing to do
log "DEBUG" "no indication for start processing file found: $file"
return 1
}
## process/generate output
process() {
log "INFO" "start processing file: $file"
pushd $DIR_BASE >/dev/null || return 1
./generate.sh "$file"
local result=$?
popd >/dev/null
if [ $result -ne 0 ]; then
log "ERROR" "processing of file not successful: $file"
return 1
fi
log "INFO" "processing of file successful: $file"
}
## copy output
copy() {
local file="$1"
if [ -z "$DIR_DEST_BASE" ]; then
log "ERROR" "destination directory for output not given 'DIR_DEST_BASE'"
fi
if [ ! -d "$DIR_DEST_BASE" ]; then
log "ERROR" "destination directory for output doesn't exist: $DIR_DEST_BASE"
return 1
fi
log "INFO" "start syncing files of master file: $file"
local file_pdf="${file/.sgml/.pdf}"
local file_html="${file/.sgml/.html}"
local dir_html="${file/.sgml}"
local file_base="`basename "${file/.sgml}"`"
local file_dest_pdf="$DIR_DEST_BASE/${file_base}.pdf"
local file_dest_html="$DIR_DEST_BASE/${file_base}.html"
local dir_dest_html="$DIR_DEST_BASE/$file_base"
local file_dest_status="$DIR_DEST_BASE/${file_base}.last"
log "INFO" "copy PDF file: $file_pdf"
cp -p "$file_pdf" "$file_dest_pdf"
if [ $? -ne 0 ]; then
log "ERROR" "can't copy PDF file: $file_pdf"
return 1
fi
if [ ! -f "$file_dest_pdf" ]; then
log "ERROR" "destination PDF file is not a file or doesn't exist: $file_dest_pdf"
return 1
fi
chmod 644 "$file_dest_pdf"
if [ $? -ne 0 ]; then
log "ERROR" "can't adjust permissions of PDF file: $file_dest_pdf"
return 1
fi
log "INFO" "copy HTML file: $file_html"
cp -p "$file_html" "$file_dest_html"
if [ $? -ne 0 ]; then
log "ERROR" "can't copy HTML file: $file_html"
return 1
fi
if [ ! -f "$file_dest_html" ]; then
log "ERROR" "destination HTML file is not a file or doesn't exist: $file_dest_html"
return 1
fi
chmod 644 "$file_dest_html"
if [ $? -ne 0 ]; then
log "ERROR" "can't adjust permissions of HTML file: $file_dest_html"
return 1
fi
if [ ! -d "$dir_html" ]; then
log "ERROR" "source directory for HTML doesn't exist: $dir_html"
return 1
fi
if [ ! -d "$dir_dest_html" ]; then
log "ERROR" "destination directory for HTML doesn't exist: $dir_dest_html"
return 1
fi
if [ ! -w "$dir_dest_html" ]; then
log "ERROR" "destination directory for HTML isn't writable: $dir_dest_html"
return 1
fi
log "INFO" "copy HTML directory: $dir_html -> $dir_dest_html"
rsync --delete -r "$dir_html/" "$dir_dest_html/"
if [ $? -ne 0 ]; then
log "ERROR" "can't sync HTML dir: $dir_html"
return 1
fi
log "INFO" "update status file: $file_dest_status"
touch "$file_dest_status"
if [ $? -ne 0 ]; then
log "ERROR" "can't update status file: $file_dest_status"
return 1
fi
}
## main
main() {
for file in $*; do
download "$URL_BASE" "$file"
if [ $? -ne 0 ]; then
continue
fi
dir="$DIR_BASE/${file/.sgml}"
check "$DIR_DOWNLOAD/$file" "$DIR_BASE/$file" "$dir"
if [ $? -eq 0 ]; then
process "$DIR_BASE/$file"
fi
copy "$DIR_BASE/$file"
done
}
# parse options
while getopts "dg" opt; do
case $opt in
d)
force_download=1
;;
g)
force_download=1
force_generate=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $((OPTIND-1))
case $1 in
'de')
files="$FILE_DE"
;;
'en')
files="$FILE_EN"
;;
'fr')
files="$FILE_FR"
;;
'pt')
files="$FILE_PT_BR"
;;
'all')
files="$FILE_EN $FILE_DE $FILE_FR $FILE_PT_BR"
;;
esac
main $files