aboutsummaryrefslogtreecommitdiff
#!/bin/sh

# This file is part of Haketilo
#
# Copyright (C) 2021, jahoti
#
# 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

# Set BROWSERPATH appropriately
set_browserpath () {
    BROWSERPATH="${BROWSERPATH:-$(realpath "$1")}"
}

SRCDIR=''
TARGET=''

# Parse command line options
while [ "x$1" != x ]; do
    case "$1" in
	--srcdir=*)           SRCDIR="$(echo "$1" | cut -c 10-)";;
	--srcdir)                             SRCDIR="$2"; shift;;
	BROWSER=*)        BROWSERPATH="$(echo "$1" | cut -c 9-)";;
	DESTDIR=*)            DESTDIR="$(echo "$1" | cut -c 9-)";;
	PYTEST=*)              PYTEST="$(echo "$1" | cut -c 8-)";;
	PYTHON=*)              PYTHON="$(echo "$1" | cut -c 8-)";;
	TEST_BROWSER=*) TEST_BROWSER="$(echo "$1" | cut -c 14-)";;
	TEST_PORT=*)       TEST_PORT="$(echo "$1" | cut -c 11-)";;
	TEST_PROFILE=*) TEST_PROFILE="$(echo "$1" | cut -c 14-)";;
	UPDATE_URL=*)     UPDATE_URL="$(echo "$1" | cut -c 12-)";;
	--host=*)              TARGET="$(echo "$1" | cut -c 8-)";;
	--host)                               TARGET="$2"; shift;;

	# browsers
	chromium | chrome | google-chrome | mozilla |\
	firefox | librewolf | icecat | iceweasel | abrowser |\
	iceweasel-uxp | tor-browser)                   TARGET=$1;;
	*)                           echo Ignoring option "'$1'";;
    esac
    shift
done

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

# Check srcdir
if [ ! -f "$SRCDIR"/manifest.json ]; then
    echo Invalid source directory "'$SRCDIR'": missing manifest.json >&2
    exit 1
elif [ ! -f "$SRCDIR"/write_makefile.sh ]; then
    echo Invalid source directory "'$SRCDIR'": missing write_makefile.sh >&2
    exit 1
fi

# Autodetect target
if [ "x$TARGET" = x ]; then
    echo Detecting target automatically.
    if [ -h /etc/alternatives/x-www-browser ]; then
	set_browserpath /etc/alternatives/x-www-browser
	TARGET="$("$BROWSERPATH" --version 2> /dev/null |
	    tail -n 1 | awk '{ print $1 }' | tr [A-Z] [a-z])"
    else
    	echo Warning: could not find target automatically. >&2
    	echo Some make rules may fail. >&2
    fi
else
    set_browserpath "$(which $TARGET)"
fi

TEST_BROWSER="${TEST_BROWSER:-$BROWSERPATH}"

# Check and standardize target
case "$TARGET" in
    mozilla | firefox | abrowser | icecat | iceweasel-uxp |\
    librewolf | iceweasel | gnu | tor-browser)   TARGET=mozilla;;
    chromium | chrome | google-chrome | google) TARGET=chromium;;
    "")                                                        ;;
    *)              echo Invalid target "'$TARGET'" >&2; exit 2;;
esac

# Autodetect DESTDIR (no check needed)
if [ "x$DESTDIR" = x ]; then
    echo Guessing installation directory.
    if [ -n "$BROWSERPATH" ] && [ -n "$TARGET" ]; then
	DESTDIR="$(dirname "$BROWSERPATH")" # TODO: a hack for Debian?
	if [ $TARGET = mozilla ]; then
	    DESTDIR="$DESTDIR"/browser
	fi
	DESTDIR="$DESTDIR"/extensions
    else
    	echo Warning: could not guess installation directory. >&2
    	echo Some make rules may fail. >&2
    fi
fi

# Write record.conf (LEAVE SRCDIR FIRST)
echo srcdir = "$SRCDIR" > record.conf
echo default_target = "$TARGET" >> record.conf
echo DESTDIR = "$DESTDIR" >> record.conf
echo PYTEST = "${PYTEST:-$(which pytest)}" >> record.conf
echo PYTHON = "${PYTHON:-$(which python3)}" >> record.conf
echo UPDATE_URL = "$UPDATE_URL" >> record.conf

# (Re-)write testing.conf
rm -f testing.conf
[ "x$TEST_BROWSER" != x ] && echo BINARY "$TEST_BROWSER" >> testing.conf
[ "x$TEST_PROFILE" != x ] && echo TEST_PROFILE "$TEST_PROFILE" >> testing.conf
[ "x$TEST_PORT" != x ] && echo TEST_PORT "$TEST_PORT" >> testing.conf
[ "$srcdir" != '.' ] && echo CERT_DIR "$PWD/test/certs" >> testing.conf

# Prepare and run write_makefile.sh (as config.status)
if [ ! -e config.status ]; then
    cp "$SRCDIR"/write_makefile.sh config.status
fi

./config.status