aboutsummaryrefslogtreecommitdiff
#!/bin/sh

# Copyright (C) 2021 Wojtek Kosior
# Redistribution terms are gathered in the `copyright' file.

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 "$@"