summaryrefslogtreecommitdiff
path: root/moz-normalize-mozconfig
blob: 62cce3fae515ac6c4da8ed52a0ad8f973cc8f891 (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
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
#!/bin/bash

tempdir="$(mktemp -d --tmpdir ${0##*/}.XXXXXXXXXXX)"

urlencode() {
	perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$1"
}

# outputs in the format
# class-id:line
classify-lines() {
	while read line; do
		_line="$(printf '%s' "$line"|sed -r 's/^#(\S)/\1/')"
		class='other'
		case "$_line" in
			ac_add_options\ --enable-application=*) class=setapp;;
			.\ *mozconfig) class=setapp;;

			*optimiz*) class=vendor;;

			ac_add_options\ --*-dirs) class=vendor;;
			ac_add_options\ --*-home=*) class=vendor;;
			ac_add_options\ --libdir=*) class=vendor;;
			ac_add_options\ --prefix=*) class=vendor;;
			*OFFICIAL*) class=vendor;;

			ac_add_options\ --with-*=*) class=appsetting;;
			*MOZ_PHOENIX*) class=appsetting;;

			ac_add_options\ --*-system-*) class=system;;
			ac_add_options\ --with-pthreads) class=system;;

			ac_add_options\ --build=*) class=platform;;
			ac_add_options\ --host=*) class=platform;;
			*SHORT_LIBNAME*) class=platform;;
			
			ac_add_options\ --*=*) class=other;;
			ac_add_options\ --enable-toolkit-*) class=other;;

			ac_add_options\ --enable-*) class=feature;;
			ac_add_options\ --disable-*) class=feature;;
			ac_add_options\ --with-*) class=feature;;

			'# '*)
				class=comment
				echo "$line" >> "$tempdir/comments"
				;;
		esac
		if [[ $class != comment ]]; then
			ci="$tempdir/comments"
			co="$tempdir/comments-$(urlencode "$line")"
			[[ -f $ci ]] && mv "$ci" "$co"

			class_id=''
			case "$class" in
				setapp)     class_id=0;;
				system)     class_id=1;;
				feature)    class_id=2;;
				vendor)     class_id=3;;
				platform)   class_id=4;;
				appsetting) class_id=5;;
				other)      class_id=6;;
				*) libremessages error "INVALID CLASS: $class"; exit 1;;
			esac
			printf '%s:%s\n' "$class_id" "$line"
		fi
	done
}

format-classes() (
	echo 0 > "$tempdir/prev_id"
	IFS=:
	while read class_id line; do
		prev_id=$(cat "$tempdir/prev_id")
		if [[ "$class_id" != "$prev_id" ]]; then
			echo ''
			case "$class_id" in
				0) echo '# Set the application';;
				1) echo '# System libraries';;
				2) echo '# Features';;
				3) echo '# Vendor settings';;
				4) echo '# Platform settings';;
				5) echo '# Application settings';;
				6) echo '# Other';;
			esac
		fi
		printf '%s\n' "$line"
		echo "$class_id" > "$tempdir/prev_id"
	done
	echo ''
	echo '# Build-generated'
)

add-comments() {
	while read line; do
		file="$tempdir/comments-$(urlencode "$line")"
		if [[ -f $file ]]; then
			cat "$file"
		fi
		echo "$line"
	done
	file="$tempdir/comments"
	if [[ -f $file ]]; then
		cat "$file"
	fi
}

hack() {
	sed -r \
		-e '/^\s*$/d' \
		-e '/#\s*(System libraries|Features|Optimization|Build-generated)/d' \
		-e '/#\s*(Icecat|IceWeasel)/d' \
		-e '/#\s*(ConnochaetOS|Parabola)/d' \
		-e 's/# (ac_add_options)/#\1/'
}

main() {
	cat \
		| hack \
		| classify-lines "$file" \
		| sort  \
		| format-classes \
		| add-comments \

	# above line intentionally left blank
	rm -rf "$tempdir"
}

main "$@"