aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: 15bff2a90653a4ed997774f21386177dfe333609 (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
#!/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]:   set file/directory to use for building"
    echo "    -h, help:          print usage information and exit"
    echo "    -o, output=[LOC]:  set output file/directory"
    echo "    -s, safe:          don't delete existing directories;"
    echo "                       throw an error instead."
    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 user to invoke Chromium"
    
    exit $EXIT_STATUS
}

. ./lib_build.sh

BROWSER=''
BUILDDIR=''
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;;
	-o | --output)            OUTPUT="$2"; shift;;
	output=*)  OUTPUT="$(echo "$1" | cut -c 8-)";;
	-s | --safe | safe)                  FORCE=0;;
	-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.
fi


if [ $MAKEZIP = 1 ]; then
    BUILDDIR="${BUILDDIR:-build_$BROWSER}"
    
    if [ "x$OUTPUT" = x ]; then
	case $BROWSER in
	    chromium) OUTPUT=build.crx;;
	    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 "$BUILDDIR"/build.zip "$OUTPUT"
    rm -rf "$BUILDDIR"
elif [ "$BUILDDIR" != "$OUTPUT" ]; then
    mv "$BUILDDIR" "$OUTPUT"
fi