summaryrefslogtreecommitdiff
path: root/configure
blob: 3e497b6ad3a467de8fdd679be87bec42b5bcc4c6 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/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_BASE" != "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 "$1" 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