1
|
#!/bin/sh
|
2
|
|
3
|
# Copyright (C) 2021 Wojtek Kosior
|
4
|
# Redistribution terms are gathered in the `copyright' file.
|
5
|
|
6
|
set -e
|
7
|
|
8
|
. ./shell_utils.sh
|
9
|
|
10
|
as_json_list() {
|
11
|
while true; do
|
12
|
if [ "x" = "x$2" ]; then
|
13
|
printf '\\n\t\t"%s"\\n\t' "$1"
|
14
|
return
|
15
|
fi
|
16
|
printf '\\n\t\t"%s",' "$1"
|
17
|
shift
|
18
|
done
|
19
|
}
|
20
|
|
21
|
as_html_list() {
|
22
|
while [ "x" != "x$1" ]; do
|
23
|
printf '\\n <script src="/%s"></script>' "$1"
|
24
|
shift
|
25
|
done
|
26
|
}
|
27
|
|
28
|
compute_scripts() {
|
29
|
local DIRS="$1"
|
30
|
local ROOT_SCRIPT="$2"
|
31
|
|
32
|
local AVAILABLE="$(find $DIRS -name '[^.#]*.js')"
|
33
|
|
34
|
awk -f compute_scripts.awk script_dependencies "$ROOT_SCRIPT" $AVAILABLE
|
35
|
}
|
36
|
|
37
|
build_main() {
|
38
|
local ALL_SCRIPTDIRS='background html common content'
|
39
|
|
40
|
local ALL_SCRIPTS_AVAILABLE="$(find $ALL_SCRIPTDIRS -name '[^.#]*.js')"
|
41
|
|
42
|
local SCRIPT
|
43
|
for SCRIPT in $ALL_SCRIPTS_AVAILABLE; do
|
44
|
map_set SCRIPTS_UNUSED $(sanitize $SCRIPT) yes
|
45
|
done
|
46
|
|
47
|
local ROOT=background/main.js
|
48
|
local SCRIPTS_BG="$( compute_scripts 'common/ background/' $ROOT)"
|
49
|
|
50
|
local ROOT=content/main.js
|
51
|
local SCRIPTS_CONTENT="$( compute_scripts 'common/ content/' $ROOT)"
|
52
|
|
53
|
local ROOT=html/display-panel.js
|
54
|
local SCRIPTS_POPUP="$( compute_scripts 'common/ html/' $ROOT)"
|
55
|
|
56
|
local ROOT=html/options_main.js
|
57
|
local SCRIPTS_OPTIONS="$( compute_scripts 'common/ html/' $ROOT)"
|
58
|
|
59
|
local BGSCRIPTS="$( as_json_list $SCRIPTS_BG )"
|
60
|
local CONTENTSCRIPTS="$( as_json_list $SCRIPTS_CONTENT )"
|
61
|
local POPUPSCRIPTS="$( as_html_list $SCRIPTS_POPUP )"
|
62
|
local OPTIONSSCRIPTS="$( as_html_list $SCRIPTS_OPTIONS )"
|
63
|
|
64
|
for SCRIPT in $SCRIPTS_BG $SCRIPTS_CONTENT $SCRIPTS_POPUP $SCRIPTS_OPTIONS
|
65
|
do
|
66
|
map_del SCRIPTS_UNUSED $(sanitize $SCRIPT)
|
67
|
done
|
68
|
|
69
|
for DIR in $(find $ALL_SCRIPTDIRS -type d); do
|
70
|
mkdir -p "$BUILDDIR"/$DIR
|
71
|
done
|
72
|
|
73
|
CHROMIUM_UPDATE_URL=''
|
74
|
GECKO_APPLICATIONS=''
|
75
|
|
76
|
if [ "x$UPDATE_URL" != x ]; then
|
77
|
UPDATE_URL=",\n \"update_url\": \"$UPDATE_URL\""
|
78
|
fi
|
79
|
|
80
|
if [ "$BROWSER" = "chromium" ]; then
|
81
|
CHROMIUM_UPDATE_URL="$UPDATE_URL"
|
82
|
else
|
83
|
GECKO_APPLICATIONS="\n\
|
84
|
\"applications\": {\n\
|
85
|
\"gecko\": {\n\
|
86
|
\"id\": \"{6fe13369-88e9-440f-b837-5012fb3bedec}\",\n\
|
87
|
\"strict_min_version\": \"60.0\"$UPDATE_URL\n\
|
88
|
}\n\
|
89
|
},"
|
90
|
fi
|
91
|
|
92
|
sed "\
|
93
|
s^_GECKO_APPLICATIONS_^$GECKO_APPLICATIONS^
|
94
|
s^_CHROMIUM_UPDATE_URL_^$CHROMIUM_UPDATE_URL^
|
95
|
s^_BGSCRIPTS_^$BGSCRIPTS^
|
96
|
s^_CONTENTSCRIPTS_^$CONTENTSCRIPTS^" \
|
97
|
< manifest.json > "$BUILDDIR"/manifest.json
|
98
|
|
99
|
./process_html_file.sh html/display-panel.html |
|
100
|
sed "s^_POPUPSCRIPTS_^$POPUPSCRIPTS^" \
|
101
|
> "$BUILDDIR"/html/display-panel.html
|
102
|
|
103
|
./process_html_file.sh html/options.html |
|
104
|
sed "s^_OPTIONSSCRIPTS_^$OPTIONSSCRIPTS^" \
|
105
|
> "$BUILDDIR"/html/options.html
|
106
|
|
107
|
for FILE in $ALL_SCRIPTS_AVAILABLE; do
|
108
|
FILEKEY=$(sanitize "$FILE")
|
109
|
if [ "x$(map_get SCRIPTS_UNUSED $FILEKEY)" = "xyes" ]; then
|
110
|
printf 'WARNING! %s not used\n' "$FILE" >&2
|
111
|
else
|
112
|
awk -f compute_scripts.awk wrapped_code "$FILE" > "$BUILDDIR"/$FILE
|
113
|
fi
|
114
|
done
|
115
|
|
116
|
# A hack to insert the contents of default_settings.json at the appropriate
|
117
|
# location in background/main.js. Uses an internal sed expression to escape
|
118
|
# and indent the JSON file for use in the external sed expression.
|
119
|
sed -i 's/^ `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/ /; s/$/\\/' < default_settings.json) "/g "$BUILDDIR"/background/main.js
|
120
|
|
121
|
if [ "$BROWSER" = "chromium" ]; then
|
122
|
cp CHROMIUM_exports_init.js "$BUILDDIR"/exports_init.js
|
123
|
else
|
124
|
cp MOZILLA_exports_init.js "$BUILDDIR"/exports_init.js
|
125
|
fi
|
126
|
|
127
|
cp -r copyright licenses/ "$BUILDDIR"
|
128
|
cp dummy "$BUILDDIR"
|
129
|
cp html/*.css "$BUILDDIR"/html
|
130
|
mkdir "$BUILDDIR"/icons
|
131
|
cp icons/*.png "$BUILDDIR"/icons
|
132
|
|
133
|
if [ "$BROWSER" = "chromium" ]; then
|
134
|
for MOZILLA_FILE in $(find "$BUILDDIR" -name "MOZILLA_*"); do
|
135
|
printf '\n' > "$MOZILLA_FILE"
|
136
|
done
|
137
|
fi
|
138
|
if [ "$BROWSER" = "mozilla" ]; then
|
139
|
for CHROMIUM_FILE in $(find "$BUILDDIR" -name "CHROMIUM_*"); do
|
140
|
printf '\n' > "$CHROMIUM_FILE"
|
141
|
done
|
142
|
fi
|
143
|
}
|
144
|
|
145
|
print_usage() {
|
146
|
printf 'usage: %s mozilla|chromium [source directory] [update url]\n' \
|
147
|
"$0" >&2
|
148
|
}
|
149
|
|
150
|
main() {
|
151
|
if [ "x$1" = "xmozilla" -o "x$1" = "xchromium" ]; then
|
152
|
BROWSER=$1
|
153
|
else
|
154
|
print_usage
|
155
|
exit 1
|
156
|
fi
|
157
|
|
158
|
SRCDIR="${2:-.}"
|
159
|
if [ -d "$SRCDIR" ]; then
|
160
|
BUILDDIR="$(realpath $BROWSER-unpacked)"
|
161
|
rm -rf "$BUILDDIR"
|
162
|
mkdir "$BUILDDIR"
|
163
|
cd "$SRCDIR"
|
164
|
else
|
165
|
print_usage
|
166
|
exit 2
|
167
|
fi
|
168
|
|
169
|
UPDATE_URL="$3"
|
170
|
|
171
|
build_main
|
172
|
}
|
173
|
|
174
|
main "$@"
|