aboutsummaryrefslogtreecommitdiff
#!/bin/sh

# This file is part of Haketilo
#
# Copyright (C) 2021 Jahoti
# Copyright (C) 2021, 2022 Wojtek Kosior <koszko@koszko.org>
#
# 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

TARGET_NAME=''
BROWSERPATH=''
TARGET=''
SRCDIR=''
BROWSER_BINARY=''
CLEAN_PROFILE=''
DRIVER=''
PYTHON=''

SOFT_FAILURES=n

ARG0="$0"

# Parse command line options
while [ "x$1" != x ]; do
    OPT="$1"
    case "$OPT" in
	chromium | chrome | google-chrome | mozilla | firefox | \
	    librewolf | icecat | iceweasel | abrowser | tor-browser)
            TARGET_NAME=$1
	    shift
	    continue
	    ;;
    esac

    if [ "x$(printf %s "$OPT" | cut -c -2)" = "x--" ]; then
	# Process a '--' option
	OPT="${OPT#--}"
	OPT_NAME="${OPT%%=*}"

	if [ "x$OPT_NAME" != "x$OPT" ]; then
	    OPT_VAL="${OPT#*=}"
	else
	    shift
	    OPT_VAL="$1"
	fi

	case "$OPT_NAME" in
	    srcdir)
                SRCDIR="$OPT_VAL";;
	    browser-binary)
		BROWSER_BINARY="$OPT_VAL";;
	    clean-profile)
		CLEAN_PROFILE="$OPT_VAL";;
	    driver)
                DRIVER="$OPT_VAL";;
	    python)
		PYTHON="$OPT_VAL";;
	    srcdir)
                SRCDIR="$OPT_VAL";;
	    destdir|dstdir)
	        DESTDIR="$OPT_VAL";;
	    update-url)
		UPDATE_URL="$OPT_VAL";;
	    host)
		TARGET_NAME="$OPT_VAL";;
	    *)
		printf "Unknown option '--%s'\n" "$OPT_NAME" >&2
		exit 1
		;;
	esac

	shift
	continue
    fi

    # Process a non-'--' option
    OPT_NAME="${OPT%%=*}"
    OPT_VAL="${OPT#*=}"

    case "$OPT_NAME" in
	BROWSER_BINARY)
            BROWSER_BINARY="$OPT_VAL";;
	CLEAN_PROFILE)
            CLEAN_PROFILE="$OPT_VAL";;
	DRIVER)
            DRIVER="$OPT_VAL";;
	PYTHON)
            PYTHON="$OPT_VAL";;
	SRCDIR)
            SRCDIR="$OPT_VAL";;
	DESTDIR|DSTDIR)
            DESTDIR="$OPT_VAL";;
	UPDATE_URL)
            UPDATE_URL="$OPT_VAL";;
	HOST)
            TARGET_NAME="$OPT_VAL";;
	*)
	    printf "Unknown variable '%s'\n" "$OPT_NAME" >&2
	    exit 1
	    ;;
    esac

    shift
done

# Check TARGET_NAME
if [ "x$TARGET_NAME" = x ]; then
    printf 'Target not specified. Please use `--host=<browser-name>`.\n' >&2
    exit 1
fi

# Check and standardize target
case "$TARGET_NAME" in
    mozilla | firefox | abrowser | icecat | librewolf | iceweasel | tor-browser)
	TARGET=mozilla
	;;
    ungoogled-chromium | chromium | chrome | google-chrome | brave | iridium | \
	bromite)
	TARGET=chromium
	printf "Tests won't yet run on a Chromium-based browser.\n"
	SOFT_FAILURES=y
	;;
    *)
	printf 'Invalid target: %s\n' "$TARGET_NAME" >&2
	exit 1
	;;
esac

# Autodetect srcdir
if [ "x$SRCDIR" = x ]; then
    if which "$ARG0" >/dev/null 2>&1; then
	SRCDIR="$(which "$ARG0")"
	SRCDIR="$(dirname "$SRCDIR")"
	SRCDIR="$(realpath "$SRCDIR" || true)"
    fi

    if [ "x$SRCDIR" = x ]; then
	SRCDIR=..
	if [ -f manifest.json ] && [ -f write_makefile.sh ]; then
	    SRCDIR=.
	fi
    fi

    printf 'Guessing SRCDIR: %s\n' "$SRCDIR"
fi

# Check srcdir
for FILE in manifest.json write_makefile.sh version; do
    if [ -f "$SRCDIR"/"$FILE" ]; then
	continue
    fi

    printf "Invalid source directory '%s': missing %s\n" "$SRCDIR" $FILE >&2
    exit 1
done

BROWSERPATH="$(realpath "$(which $TARGET_NAME)" 2>/dev/null || true)"

# Guess browser binary (needed for Selenium)
if [ "x$BROWSER_BINARY" = x ]; then
    if [ "x$TARGET_NAME" = xabrowser ]; then
	# Trisquel's path to Abrowser
	BROWSER_BINARY=/usr/lib/abrowser/abrowser
    elif [ "x$TARGET_NAME" = xlibrewolf ]; then
	# Debian's path to Librewolf
	BROWSER_BINARY=/usr/share/librewolf/librewolf
    elif [ "x$TARGET_NAME" = xiceweasel ]; then
	# Parabola's path to Iceweasel
	BROWSER_BINARY=/usr/lib/iceweasel/iceweasel
    elif [ "x$TARGET_NAME" = xicecat ]; then
	# Parabola's path to IceCat
	BROWSER_BINARY=/usr/lib/icecat/icecat
    fi

    if [ "x$BROWSER_BINARY" != x ]; then
	printf 'Guessing BROWSER_BINARY: %s\n' "$BROWSER_BINARY"
    elif [ "$TARGET" != "chromium" ]; then
	printf 'Cannot guess BROWSER_BINARY for %s.\n' "$TARGET_NAME"
	SOFT_FAILURES=y
    fi
fi

# Autodetect Selenium driver
if [ "x$DRIVER" = x ]; then
    if [ "x$TARGET" = xmozilla ]; then
	DRIVER="$(which geckodriver 2>/dev/null || true)"
	if [ -n "$DRIVER" ]; then
	    printf 'Guessing DRIVER: %s\n' "$DRIVER"
	else
	    printf 'Cannot guess DRIVER for %s.\n' "$TARGET_NAME"
	fi
    fi
fi

# Autodetect clean profile directory for use in selenium tests
if [ "x$CLEAN_PROFILE" = x ]; then
    if [ "x$TARGET" = xmozilla ]; then
	CLEAN_PROFILE="$SRCDIR"/test/default_profile/icecat_empty
    fi
fi

# Autodetect python
if [ "x$TARGET" = xmozilla -a "x$PYTHON" = x ]; then
    PYTHON2_SKIPPED=n
    for PYTHON_GUESS in python3 python-3 python; do
	if [ "x$PYTHON" = x ]; then
	    PYTHON="$(which $PYTHON_GUESS 2>/dev/null || true)"
	fi

	if [ -n "$PYTHON" ]; then
	    if [ "$PYTHON_GUESS" = python ]; then
		if python --version 2>&1 | grep '^Python 2' >/dev/null; then
		    PYTHON=''
		    PYTHON2_SKIPPED=y
		fi
	    fi
	fi

	if [ -n "$PYTHON" ]; then
	    break
	fi
    done

    if [ -n "$PYTHON" ]; then
	printf 'Guessing PYTHON: %s\n' "$PYTHON"
    else
	printf 'Cannot guess PYTHON'
	if [ "$PYTHON2_SKIPPED" = y ]; then
	    printf ' %s' \
		   '(skipped Python 2 executable in PATH; Python 3 is required)'
	fi
	printf '.\n'
	SOFT_FAILURES=y
    fi
fi

# Autodetect DESTDIR (no check needed)
if [ "x$DESTDIR" = x ]; then
    if [ "x$TARGET" = xmozilla ]; then
	DESTDIR=/usr/share/mozilla/extensions/
    elif test "x$TARGET_NAME" = xchromium -o \
	      "x$TARGET_NAME" = xungoogled-chromium; then
	DESTDIR=/usr/share/chromium/extensions
    fi

    if [ -n "$DESTDIR" ]; then
	printf 'Guessing DESTDIR: %s\n' "$DESTDIR"
    else
	printf 'Cannot guess DESTDIR.\n'
	SOFT_FAILURES=y
    fi
fi

# Write record.conf (LEAVE SRCDIR FIRST)
printf '%s\n' "srcdir = $SRCDIR" > record.conf
printf '%s\n' "default_target = $TARGET" >> record.conf
printf '%s\n' "DESTDIR = $DESTDIR" >> record.conf
printf '%s\n' "UPDATE_URL = $UPDATE_URL" >> record.conf
printf '%s\n' "DRIVER = $DRIVER" >> record.conf
printf '%s\n' "BROWSER_BINARY = $BROWSER_BINARY" >> record.conf
printf '%s\n' "CLEAN_PROFILE = $CLEAN_PROFILE" >> record.conf
printf '%s\n' "PYTHON = $PYTHON" >> record.conf

# Prepare and run write_makefile.sh (as config.status)
cp "$SRCDIR"/write_makefile.sh config.status

./config.status

if [ "$SOFT_FAILURES" = "y" ]; then
    printf 'Some make rules may fail.\n'
fi