forked from shidel/fd-nls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.sh
executable file
·514 lines (426 loc) · 12.6 KB
/
status.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/bin/bash
url_main=http://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/repositories/latest/listing.csv
url_aux=http://fd.lod.bz/repos/current/listing.csv
INDENTING=''
DEBUG=
function setIndent () {
local denting=' '
if [[ "${1}" == '+' ]] ; then
INDENTING="${INDENTING}${denting}"
else
INDENTING="${INDENTING:${#denting}}"
fi
}
function dented () {
echo "${INDENTING}${@}"
}
function errorlog () {
local flag=true
while [[ ${flag} ]] ; do
read -r line || flag=
[[ ! ${flag} ]] && [[ "${line}" == '' ]] && continue
dented "${line}" >&2
echo "$(date +'%Y-%m-%d %H:%m:%S') (${CSV_FILE}) ${line}">>error.log
done
}
UPPER_CHARS='ABCDEFGHIJKLMNOPQRSTUFWXYZ'
LOWER_CHARS='abcdefghijklmnopqrstufwxyz'
function upperCase () {
# unfortunately cannot use the simple ${var^^} on OS-X to convert to upper case
local out c i
for (( i=0;i<${#1};i++ )) ; do
c="${1:${i}:1}"
if [[ "${c//[a-z]}" != "${c}" ]] ; then
c="${LOWER_CHARS%${c}*}"
c="${UPPER_CHARS:${#c}:1}"
fi
out="${out}${c}"
done
echo "${out}"
}
function lowerCase () {
# unfortunately cannot use the simple ${var,,} on OS-X to convert to lower case
local out c i
for (( i=0;i<${#1};i++ )) ; do
c="${1:${i}:1}"
if [[ "${c//[A-Z]}" != "${c}" ]] ; then
c="${UPPER_CHARS%${c}*}"
c="${LOWER_CHARS:${#c}:1}"
fi
out="${out}${c}"
done
echo "${out}"
}
function download () {
local server="${1#*//}'"
server="${server%%/*}"
setIndent +
dented "download listing.csv from ${1%%//*}//${server} as ${2}"
setIndent -
local cmd="$(command -v wget)"
if [[ -e "${cmd}" ]] ; then
"${cmd}" -q "${1}" -O "${2}" && return 0 || return $?
fi
cmd="$(command -v curl)"
if [[ -e "${cmd}" ]] ; then
"${cmd}" -s -o "${2}" "${1}" && return 0 || return $?
fi
echo "Unable to locate either wget or curl, cannot download file." | errorlog
return 1
}
function fetch_csv () {
echo Retrieving latest translation CSV files.
download "${url_main}" ibiblio.csv
if [[ $? -ne 0 ]] ; then
echo "problem downloading ${url_main}"
return 1
fi;
download "${url_aux}" fd-lod.csv
if [[ $? -ne 0 ]] ; then
echo "problem downloading ${url_aux}"
return 1
fi;
return 0
}
MASTER_HEADER=
MASTER_FIELDS=0
MASTER_IGNORE=";md5;group;crc;versions;version;entered-date;modified-date;"\
"changes;primary-site;alternate-site;original-site;wiki-site;mirror-site;"\
"author;maintained-by;"
MASTER_TOTAL=0
# MASTER_ID[*]=
function set_master_header () {
dented "Defining master CSV from current CSV"
setIndent +
MASTER_HEADER="${1}"
MASTER_FIELDS=0
unset MASTER_FIELD
local line="${MASTER_HEADER}"
local item field
while [[ "${line}" ]] ; do
item="${line%%,*}"
line="${line:$(( ${#item} + 1 ))}"
[[ "${item}" == "" ]] && continue
if [[ "${MASTER_IGNORE//;${item};}" != "${MASTER_IGNORE}" ]] ; then
dented "Ignore master field \`${item}'"
continue
fi
(( MASTER_FIELDS++ ))
MASTER_FIELD[${MASTER_FIELDS}]="${item}"
done
setIndent -
return 0
}
CSV_HEADER=
CSV_FIELDS=
CSV_TOTAL=0
# CSV_ID[*]=
CSV_MASTER=true
function set_header_fields () {
local header="$(lowerCase ${1})"
CSV_HEADER="${header}"
CSV_FIELDS=0
unset CSV_FIELD
unset CSV_MAP
if [[ ${MASTER_FIELDS} -eq 0 ]] ; then
set_master_header "${1}"
fi
dented "Mapping CSV header to master CSV"
setIndent +
local line="${header}"
local item field index
while [[ "${line}" ]] ; do
item="${line%%,*}"
line="${line:$(( ${#item} + 1 ))}"
[[ "${item}" == "" ]] && continue
index=0
(( CSV_FIELDS++ ))
CSV_FIELD[${CSV_FIELDS}]="${item}"
while [[ ${index} -lt ${MASTER_FIELDS} ]] ; do
((index++))
if [[ "${item}" == "${MASTER_FIELD[${index}]}" ]] ; then
CSV_MAP[${CSV_FIELDS}]=${index}
break
fi
done
if [[ "${CSV_FIELD[${CSV_FIELDS}]}" = "" ]] ; then
dented "CSV field \`${item}' will be ignored"
fi
done
setIndent -
return 0
}
function id_in_master () {
local i=0
for (( i=1;i<=${MASTER_TOTAL};i++ )) ; do
[[ "${MASTER_ID[${i}]}" == "${1}" ]] && return 0
done
return 1
}
function id_in_csv () {
local i=0
for (( i=1;i<=${CSV_TOTAL};i++ )) ; do
[[ "${CSV_ID[${i}]}" == "${1}" ]] && return 0
done
return 1
}
function get_csv_id () {
local i=0
for (( i=1;i<=${CSV_TOTAL};i++ )) ; do
[[ "${CSV_ID[${i}]}" == "${1}" ]] && echo "${i}" && return 0
done
return 1
}
function process_line () {
local line="${1}"
# echo "?? ${line}"
local index=0
local id sha field
local data storage finished piece xpiece
unset data
local data
unset RECENT_ID
while [[ ${#line} -gt 0 ]] && [[ ! ${finished} ]]; do
(( index++ ))
if [[ ${#line} -eq 0 ]] ; then
break
fi
field=
if [[ "${line:0:1}" == '"' ]] ; then
line="${line:1}"
xpiece="${line%%\"*}"
xpiece=${#xpiece}
while [[ ${xpiece} -lt ${#line} ]] ; do
piece="${line:${xpiece}:2}"
# echo "? ${piece}"
if [[ "${piece}" == '""' ]] ; then
(( xpiece++ ))
elif [[ "${piece:0:1}" == '"' ]] ; then
break
fi
(( xpiece++ ))
done
piece="${line:0:${xpiece}}"
line="${line:$((${xpiece} + 1 ))}"
field="${field}${piece}"
fi
piece="${line%%,*}"
line="${line:$(( ${#piece} + 1))}"
field="${field}${piece}"
field="${field//\"\"/\"}"
# echo ">> ${field}"
if [[ "${CSV_FIELD[${index}]}" == 'id' ]] ; then
id="${field}"
if [[ "${CSV_MASTER}" == "true" ]] ; then
id_in_master "${id}" && return 0
fi
elif [[ "${CSV_FIELD[${index}]}" == 'sha' ]] ; then
sha="${field//[![:xdigit]]}"
fi
data[${index}]="${field}"
done
# return 0
RECENT_ID="${id}"
if [[ ! ${index} -eq ${CSV_FIELDS} ]] ; then
echo "warning: field count ${index}/${CSV_FIELDS} missmatch for \`${id}'" | errorlog
fi;
if [[ ! ${#sha} -eq 64 ]] ; then
echo "error: invalid sha hash for \`${id}', entry excluded" | errorlog
return 0
fi
[[ ${DEBUG} ]] && [[ ${CSV_TOTAL} -gt ${DEBUG} ]] && return 1
if [[ "${CSV_MASTER}" == "true" ]] ; then
((MASTER_TOTAL++))
MASTER_ID[${MASTER_TOTAL}]="${id}"
else
id_in_master "${id}"
if [[ $? -eq 1 ]] ; then
echo "warning: no master entry for \`${id}', entry excluded" | errorlog
return 42
fi
fi
((CSV_TOTAL++))
CSV_ID[${CSV_TOTAL}]="${id}"
index=0
storage="CSV_${CSV_TOTAL}"
unset $storage
while [[ ${index} -lt ${CSV_FIELDS} ]] ; do
((index++))
if [[ "${CSV_MAP[${index}]}" != "" ]] && [[ "${data[${index}]}" != '' ]]; then
storage="CSV_${CSV_TOTAL}[${CSV_MAP[${index}]}]"
read -r ${storage}<<<"${data[${index}]}"
if [[ "${CSV_MASTER}" == "true" ]]; then
storage="MASTER_${MASTER_TOTAL}[${CSV_MAP[${index}]}]"
read -r ${storage}<<<"${data[${index}]}"
fi
fi
done
}
function import_csv () {
CSV_FILE="${1}"
CSV_HEADER=
CSV_FIELDS=
CSV_TOTAL=0
unset CSV_ID
dented "Import ${1}"
local flag line header ret
setIndent +
flag=first
while [[ ${flag} ]] ; do
read -r line || flag=
line="${line//[$'\t\r\n']}"
[[ "${line}" == "" ]] && continue
if [[ "${header}" == "" ]] ; then
header="${line}"
set_header_fields "${header}"
else
if [[ "${flag}" == "first" ]] ; then
flag=true
dented "Processing CSV table"
fi
process_line "${line}"
ret=$?
if [[ ${ret} -eq 42 ]] && [[ "${RECENT_ID}" != "" ]] ; then
[[ -d "${CSV_FILE%/*}/excluded" ]] || mkdir -p "${CSV_FILE%/*}/excluded"
echo "${header}">"${CSV_FILE%/*}/excluded/${RECENT_ID}.csv"
echo "${line}">>"${CSV_FILE%/*}/excluded/${RECENT_ID}.csv"
elif [[ ${ret} -ne 0 ]] ; then
break
fi
fi
done< "${1}"
dented "${CSV_TOTAL} total entries processed"
setIndent -
return 0
}
function print_master () {
CSV_FILE="master.csv"
dented "Export master.csv"
local i x v out
for (( i=1;i<=${MASTER_FIELDS};i++ )) ; do
[[ "${out}" != '' ]] && out="${out},"
out="${out}${MASTER_FIELD[${i}]}"
done
echo "${out}">master.csv
for (( i=1;i<=${MASTER_TOTAL};i++ )) ; do
out=
for (( x=1;x<=${MASTER_FIELDS};x++ )) ; do
v="MASTER_${i}[${x}]"
v="${!v}"
# [[ "${v/\"}" != "${v}" ]] && echo "${v}"
[[ "${v/,}" != "${v}" ]] && v="\"${v}\""
[[ "${out}" != '' ]] && out="${out},"
out="${out}${v}"
done
echo "${out}"
done | LC_ALL='C' sort >>master.csv
}
function print_csv () {
CSV_FILE="${1}"
dented "Export ${CSV_FILE}"
local i x v out
for (( i=1;i<=${MASTER_FIELDS};i++ )) ; do
[[ "${out}" != '' ]] && out="${out},"
out="${out}${MASTER_FIELD[${i}]}"
done
echo "${out}">"${CSV_FILE}"
for (( i=1;i<=${CSV_TOTAL};i++ )) ; do
out=
for (( x=1;x<=${MASTER_FIELDS};x++ )) ; do
v="CSV_${i}[${x}]"
v="${!v}"
# [[ "${v/\"}" != "${v}" ]] && echo "${v}"
[[ "${v/,}" != "${v}" ]] && v="\"${v}\""
[[ "${out}" != '' ]] && out="${out},"
out="${out}${v}"
done
echo "${out}"
done | LC_ALL='C' sort >>"${CSV_FILE}"
}
function print_diff () {
CSV_FILE="${1}"
dented "Export ${1}, ${CSV_TOTAL} known entries"
setIndent +
local i x v out mixed csv
[[ "${1/mixed}" != "${1}" ]] && mixed=true
for (( i=1;i<=${MASTER_FIELDS};i++ )) ; do
[[ "${out}" != '' ]] && out="${out},"
out="${out}${MASTER_FIELD[${i}]}"
[[ $i -eq 1 ]] && [[ ${mixed} ]] && out="${out},new"
done
echo "${out}">"${1}"
for (( i=1;i<=${MASTER_TOTAL};i++ )) ; do
out=
csv=$( get_csv_id "${MASTER_ID[${i}]}" )
if [[ "${csv}" == "" ]] ; then
if [[ ${mixed} ]] ; then
echo "missing ${MASTER_ID[${i}]}, adding from master.csv" | errorlog
fi
for (( x=1;x<=${MASTER_FIELDS};x++ )) ; do
v="MASTER_${i}[${x}]"
v="${!v}"
# [[ "${v/\"}" != "${v}" ]] && echo "${v}"
[[ "${v/,}" != "${v}" ]] && v="\"${v}\""
[[ "${out}" != '' ]] && out="${out},"
out="${out}${v}"
[[ $x -eq 1 ]] && [[ ${mixed} ]] && out="${out},*"
done
echo "${out}"
else
[[ ! ${mixed} ]] && continue
for (( x=1;x<=${MASTER_FIELDS};x++ )) ; do
v="CSV_${csv}[${x}]"
v="${!v}"
# [[ "${v/\"}" != "${v}" ]] && echo "${v}"
[[ "${v/,}" != "${v}" ]] && v="\"${v}\""
[[ "${out}" != '' ]] && out="${out},"
out="${out}${v}"
[[ $x -eq 1 ]] && [[ ${mixed} ]] && out="${out},"
done
echo "${out}"
fi
done | LC_ALL='C' sort >>"${1}"
setIndent -
}
function create_master_csv () {
dented "Building master.csv"
setIndent +
while [[ "${1}" ]] ; do
import_csv "${1}" || return $?
shift
done
print_master
dented "${MASTER_TOTAL} total master entries"
setIndent -
}
function main () {
[[ "${1}" = "master" ]] && echo "Update MasterCSV only"
[[ -e "error.log" ]] && rm "error.log"
local f o
fetch_csv || return $?
create_master_csv ibiblio.csv fd-lod.csv || return $?
[[ "${1}" = "master" ]] && return 0
CSV_MASTER=
for f in */listing.csv ; do
[[ ! -e "${f}" ]] && continue
import_csv "${f}"
o="${f%/*}"
# o="${o//\//-}"
setIndent +
print_diff "${o}/missing.csv"
print_diff "${o}/mixed.csv"
# print_csv "export.csv"
setIndent -
done
}
function maintest () {
local line
header=$(head -n 1 de/listing.csv)
set_header_fields "${header}"
line="$(grep -i 'vmsmount' de/listing.csv)"
line="${line//[$'\t\r\n']}"
process_line "${line}"
}
main ${@}
# maintest
exit $?