blob: f56639cbf66f926e18eb448e54e79fcd580553a4 (
plain)
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
|
#!/bin/bash
tempdir="$(mktemp -d --tmpdir mozilla-normalize.XXXXXXXXXXX)"
################################################################################
# BUG: can insert empty lines
format_array="
s/\(/& / # force whitespace after the opening 'var=(' bit
s/\)/ &/ # force whitespace after the trailing ')' bit
s/\s+/\n /g # delimit array elements with '\n '
"
format-array() {
local var=$1
local file=$2
sed -ri \
-e "/^$var=\(.*\)/{ $format_array } # single-line" \
-e "/^$var=\(/,/\)/{ $format_array } # multi-line" \
"$file"
}
################################################################################
print_array="
/\(/d # strip the opening 'var=(' line
/\)/d # strip the trailing ')' line
/./p # print everything else
"
sort-array() {
local var=$1
local file=$2
# extract the data
{
# HACK: strip empty lines; to work around bug in format-array
sed -nr "/^$var=\(/,/\)/{ $print_array }" \
| sed '/^\s*$/d' \
| sort -u
} < "$file" > "$tempdir/$var"
# remove it from the file
sed -i -e "/^$var=(/i @$var@" -e "/^$var=/,/)/d" "$file"
# re-insert it into the file
sed -i \
-e "/^@$var@/a$var=(" \
-e "/^@$var@/r$tempdir/$var" \
-e "/^@$var@/a)" \
-e "/^@$var@/d" \
"$file"
}
################################################################################
normalize() {
local file=$1
for var in {make,check,opt,}depends arch license; do
format-array "$var" "$file"
sort-array "$var" "$file"
done
# clean up whitespace
sed -ri 's/\s+$//' "$file"
# generalize
sed -i 's/firefox/$pkgbase/g' "$file"
}
main() {
cat > "$tempdir/PKGBUILD"
normalize "$tempdir/PKGBUILD"
cat "$tempdir/PKGBUILD"
rm -rf "$tempdir"
}
main "$@"
|