aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: abaed4781b7a00d92ee73d71b6520a09cf240548 (about) (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
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
#!/bin/sh

# This file is part of Haketilo
#
# Copyright (C) 2021, Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the CC0 1.0 Universal License as published by
# the Creative Commons Corporation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# CC0 1.0 Universal License for more details.

set -e

. ./shell_utils.sh

as_json_list() {
    while true; do
	if [ "x" = "x$2" ]; then
	    printf '\\n\t\t"%s"\\n\t' "$1"
	    return
	fi
	printf '\\n\t\t"%s",' "$1"
	shift
    done
}

as_html_list() {
    while [ "x" != "x$1" ]; do
	printf '\\n    <script src="/%s"></script>' "$1"
	shift
    done
}

compute_scripts() {
    local DIRS="$1"
    local ROOT_SCRIPT="$2"

    local AVAILABLE="$(find $DIRS -name '[^.#]*.js')"

    awk -f compute_scripts.awk script_dependencies "$ROOT_SCRIPT" $AVAILABLE
}

build_main() {
    local ALL_SCRIPTDIRS='background html common content'

    local ALL_SCRIPTS_AVAILABLE="$(find $ALL_SCRIPTDIRS -name '[^.#]*.js')"

    local SCRIPT
    for SCRIPT in $ALL_SCRIPTS_AVAILABLE; do
	map_set SCRIPTS_UNUSED $(sanitize $SCRIPT) yes
    done

    local ROOT=background/main.js
    local SCRIPTS_BG="$(      compute_scripts 'common/ background/' $ROOT)"

    local ROOT=content/main.js
    local SCRIPTS_CONTENT="$( compute_scripts 'common/ content/'    $ROOT)"

    local ROOT=html/display-panel.js
    local SCRIPTS_POPUP="$(   compute_scripts 'common/ html/'       $ROOT)"

    local ROOT=html/options_main.js
    local SCRIPTS_OPTIONS="$( compute_scripts 'common/ html/'       $ROOT)"

    local BGSCRIPTS="$(      as_json_list $SCRIPTS_BG      )"
    local CONTENTSCRIPTS="$( as_json_list $SCRIPTS_CONTENT )"
    local POPUPSCRIPTS="$(   as_html_list $SCRIPTS_POPUP   )"
    local OPTIONSSCRIPTS="$( as_html_list $SCRIPTS_OPTIONS )"

    for SCRIPT in $SCRIPTS_BG $SCRIPTS_CONTENT $SCRIPTS_POPUP $SCRIPTS_OPTIONS
    do
	map_del SCRIPTS_UNUSED $(sanitize $SCRIPT)
    done

    for DIR in $(find $ALL_SCRIPTDIRS -type d); do
	mkdir -p "$BUILDDIR"/$DIR
    done

    CHROMIUM_UPDATE_URL=''
    GECKO_APPLICATIONS=''

    if [ "x$UPDATE_URL" != x ]; then
	UPDATE_URL=",\n    \"update_url\": \"$UPDATE_URL\""
    fi

    if [ "$BROWSER" = "chromium" ]; then
	CHROMIUM_UPDATE_URL="$UPDATE_URL"
    else
	GECKO_APPLICATIONS="\n\
    \"applications\": {\n\
	\"gecko\": {\n\
	    \"id\": \"{6fe13369-88e9-440f-b837-5012fb3bedec}\",\n\
	    \"strict_min_version\": \"60.0\"$UPDATE_URL\n\
	}\n\
    },"
    fi

    sed "\
s^_GECKO_APPLICATIONS_^$GECKO_APPLICATIONS^
s^_CHROMIUM_UPDATE_URL_^$CHROMIUM_UPDATE_URL^
s^_BGSCRIPTS_^$BGSCRIPTS^
s^_CONTENTSCRIPTS_^$CONTENTSCRIPTS^" \
	< manifest.json > "$BUILDDIR"/manifest.json

    ./process_html_file.sh html/display-panel.html |
	sed "s^_POPUPSCRIPTS_^$POPUPSCRIPTS^" \
	    > "$BUILDDIR"/html/display-panel.html

    ./process_html_file.sh html/options.html |
	sed "s^_OPTIONSSCRIPTS_^$OPTIONSSCRIPTS^" \
	    > "$BUILDDIR"/html/options.html

    for FILE in $ALL_SCRIPTS_AVAILABLE; do
	FILEKEY=$(sanitize "$FILE")
	if [ "x$(map_get SCRIPTS_UNUSED $FILEKEY)" = "xyes" ]; then
	    printf 'WARNING! %s not used\n' "$FILE" >&2
	else
	    awk -f compute_scripts.awk wrapped_code "$FILE" > "$BUILDDIR"/$FILE
	fi
    done

    # A hack to insert the contents of default_settings.json at the appropriate
    # location in background/main.js. Uses an internal sed expression to escape
    # and indent the JSON file for use in the external sed expression.
    sed -i 's/^        `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/        /; s/$/\\/' < default_settings.json) "/g "$BUILDDIR"/background/main.js

    if [ "$BROWSER" = "chromium" ]; then
	cp CHROMIUM_exports_init.js "$BUILDDIR"/exports_init.js
    else
	cp MOZILLA_exports_init.js "$BUILDDIR"/exports_init.js
    fi

    cp -r copyright licenses/ "$BUILDDIR"
    cp dummy "$BUILDDIR"
    cp html/*.css "$BUILDDIR"/html
    mkdir "$BUILDDIR"/icons
    cp icons/*.png "$BUILDDIR"/icons

    if [ "$BROWSER" = "chromium" ]; then
	for MOZILLA_FILE in $(find "$BUILDDIR" -name "MOZILLA_*"); do
	    printf '\n' > "$MOZILLA_FILE"
	done
    fi
    if [ "$BROWSER" = "mozilla" ]; then
	for CHROMIUM_FILE in $(find "$BUILDDIR" -name "CHROMIUM_*"); do
	    printf '\n' > "$CHROMIUM_FILE"
	done
    fi
}

print_usage() {
    printf 'usage:  %s mozilla|chromium [source directory] [update url]\n' \
	   "$0" >&2
}

main() {
    if [ "x$1" = "xmozilla" -o "x$1" = "xchromium" ]; then
	BROWSER=$1
    else
	print_usage
	exit 1
    fi

    SRCDIR="${2:-.}"
    if [ -d "$SRCDIR" ]; then
	BUILDDIR="$(realpath $BROWSER-unpacked)"
	rm -rf "$BUILDDIR"
	mkdir "$BUILDDIR"
	cd "$SRCDIR"
    else
	print_usage
	exit 2
    fi

    UPDATE_URL="$3"

    build_main
}

main "$@"