summaryrefslogtreecommitdiff
path: root/moz-normalize-PKGBUILD
blob: c254d198818d8ad3c037253978d78f2b22fb8851 (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
77
78
79
80
81
82
#!/bin/bash

tempdir="$(mktemp -d --tmpdir ${0##*/}.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,}depends arch license; do
		format-array "$var" "$file"
		sort-array "$var" "$file"
	done

	# clean up whitespace
	sed -ri 's/\s+$//' "$file"

	# generalize
	sed -ri \
		-e 's/([^=])firefox/\1$pkgbase/g' \
		-e 's/\$pkgbase-(install-dir\.patch)/firefox-\1/g' \
		"$file"
	if ! grep -q pkgbase= "$file"; then
		sed -i '/pkgdesc=/iif [[ -z $pkgbase ]]\; then pkgbase=$pkgname\; fi' "$file"
	fi
}

main() {
	cat > "$tempdir/PKGBUILD"
	normalize "$tempdir/PKGBUILD"
	cat "$tempdir/PKGBUILD"
	rm -rf "$tempdir"
}

main "$@"