aboutsummaryrefslogtreecommitdiff
#!/bin/sh

# Copyright (C) 2021 jahoti <jahoti@tilde.team>
# Redistribution terms are gathered in the `copyright' file.

print_usage() {
    EXIT_STATUS=${1:-0}
    if [ "x$1" != x ]; then
	shift
	errcho "$@"
    fi
    
    echo "usage:  $0 [OPTION]... BROWSER"
    echo
    echo "long options can be provided either in the form"
    echo "'[OPTION](=[VALUE])' or '--[OPTION] (VALUE)'."
    echo "browsers:"
    echo "    -C, chromium:      build for Chromium and derivatives"
    echo "    -M, mozilla:       build for Firefox and derivatives"
    echo "options:"
    echo "    -b, build=[LOC]:   file/directory to use for building"
    echo "    -h, help:          print usage information and exit"
    echo "    -o, output=[LOC]:  output file/directory"
    echo "    -k, key=[LOC]:     the key to use in signing a CRX;"
    echo "                       builds a CRX instead of a ZIP"
    echo "    -s, safe:          don't delete existing directories;"
    echo "                       throw an error instead"
    echo "    -u, url=[URL]:     URL to check for updates"
    echo "    -z, zip:           pack the extension as a file"
    echo
    echo "environment variables (optional)"
    echo "    HAKETILO_ZIP:      command to use for generating ZIP files"
    echo "    HAKETILO_CHROMIUM: command to use for invoking Chromium"
    
    exit $EXIT_STATUS
}

. ./lib_build.sh

BROWSER=''
BUILDDIR=''
KEY=''
UPDATE_URL=''
MAKEZIP=0
FORCE=1

while [ "x$1" != x ]; do
    case "$1" in
	-C | --chromium | chromium) BROWSER=chromium;;
	-M | --mozilla | mozilla)    BROWSER=mozilla;;
	-b | --build)           BUILDDIR="$2"; shift;;
	build=*) BUILDDIR="$(echo "$1" | cut -c 7-)";;
	-h | --help | help)              print_usage;;
	-k | --key)                  KEY="$2"; shift;;
	key=*)        KEY="$(echo "$1" | cut -c 5-)";;
	-o | --output)            OUTPUT="$2"; shift;;
	output=*)  OUTPUT="$(echo "$1" | cut -c 8-)";;
	-s | --safe | safe)                  FORCE=0;;
	-u | --url)           UPDATE_URL="$2"; shift;;
	url=*) UPDATE_URL="$(echo "$1" | cut -c 5-)";;
	-z | --zip | zip_ext)              MAKEZIP=1;;
	*) print_usage 2 Unrecognized option "'$1'.";;
    esac
    shift
done

if [ "x$BROWSER" = x ]; then
    print_usage 1 No browser was specified.
elif [ "x$KEY" != x ]; then
    if [ $BROWSER != chromium ]; then
	print_usage 4 The "'key'" option applies only to builds 'for' Chromium.
    elif [ $MAKEZIP = 0 ]; then
	print_usage 4 The "'key'" option must be used in conjunction with the "'zip'" option.
    elif [ ! -e "$KEY" ]; then
	errcho "The specified key file '$KEY' does not exist."
	exit 5
    fi
    
    KEY="$(realpath "$KEY")"
fi

if [ "x$KEY" != x ]; then
    PACKDIR="${BUILDDIR:-build_$BROWSER}"
    BUILDDIR="$PACKDIR"/inner
    OUTPUT="${OUTPUT:-build.crx}"
    
    mkdir "$PACKDIR"
elif [ $MAKEZIP = 1 ]; then
    BUILDDIR="${BUILDDIR:-build_$BROWSER}"
    PACKDIR="$BUILDDIR"
    
    if [ "x$OUTPUT" = x ]; then
	case $BROWSER in
	    chromium)  OUTPUT=build.zip;;
	    mozilla)   OUTPUT=build.xpi;;
	esac
    fi
else
    if [ "x$BUILDDIR" = x ]; then
	BUILDDIR="${OUTPUT:-build_$BROWSER}"
    fi
    
    OUTPUT="${OUTPUT:-$BUILDDIR}"
fi

if [ -e "$BUILDDIR" ]; then
    if [ $FORCE = 0 ]; then
	errcho "Build directory '$BUILDDIR' exists."
	exit 3
    else
	rm -rf "$BUILDDIR"
    fi
fi

if [ -e "$OUTPUT" ]; then
    if [ $FORCE = 0 ]; then
	errcho "Output location '$OUTPUT' exists."
	exit 3
    else
	rm -rf "$BUILDDIR"
    fi
fi

mkdir "$BUILDDIR"
main
if [ $MAKEZIP = 1 ]; then
    make_zip
    mv "$PACKDIR"/build.zip "$OUTPUT"
    rm -rf "$PACKDIR"
elif [ "$BUILDDIR" != "$OUTPUT" ]; then
    mv "$BUILDDIR" "$OUTPUT"
fi