about summary refs log tree commit diff stats
path: root/misc
blob: 3b46cbd1834ec076e681bb979ef051c4796527ce (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
#!/usr/bin/env bash
# Manually Installed and Source-Compiled

usage() {
	cat >&2 <<EOF
misc: install programs from source
--- Usage:
    misc -h
    misc [-d] [-n] [-r DIR] [-p DIR] REPO ...
--- Parameters:
    REPO    A program to pull from upstream, build, and install locally.
--- Flags:
    -h      Show this help and exit.
    -d      DRY RUN: Don't do anything, just print what would happen.
    -n      NUKE: Delete REPO, then re-download and build.
--- Options:
    -r DIR  Change the misc repo directory to DIR
              (default: $MISC_REPO_ROOT).
    -p DIR  Change the directory to search for install plans
              (default: $MISC_PLAN_ROOT).
    -i DIR  Change the PREFIX to use when installing programs
              (default: $MISC_INSTALL_PREFIX).
EOF
}

## Entry point

main() {
	config "$@"
	shift $((OPTIND - 1))

	(($# > 0)) || usage 1

	[[ "$DEBUG" ]] && set -x

	# Variables
	MISC_PLAN="$1"
	shift

	MISC_FULL_PLAN="$MISC_PLAN_ROOT/$MISC_PLAN"
	MISC_REPO="$MISC_REPO_ROOT/$MISC_PLAN"
	MISC_DONE=

	SOURCE=
	CHECKOUT=master
	CONFIGURE_ARGS=()
	MISC_DEPENDENCIES=()
	APT_DEPENDENCIES=(
		build-essential
		git
	)

	export LD_LIBRARY_PATH="$MISC_INSTALL_PREFIX/lib:$LD_LIBRARY_PATH"
	export PKG_CONFIG_PATH="$MISC_INSTALL_PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"

	# Check that plan exists
	[[ -r "$MISC_FULL_PLAN" ]] ||
		die 2 "Plan not found: \"$MISC_PLAN\""

	_MISC_PREVIOUS_DIR="$PWD"
	trap finish EXIT INT

	if [[ ! "$PWD" == "$MISC_REPO_ROOT" ]]; then
		mkdir -p "$MISC_REPO_ROOT"
		run cd "$MISC_REPO_ROOT"
	fi

	. "$MISC_FULL_PLAN" ||
		die "$?" "Error executing plan: \"$MISC_PLAN\""

	# If the recipe is just variables, run the default plan
	if [[ -z "$MISC_DONE" ]]; then
		install_deps
		repo_pull
		repo_ready
		configure || true # Not always have configure
		make install
	fi
}

config() {
	MISC_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/misc/misc.sh"
	[[ -f "$MISC_CONFIG" ]] && . "$MISC_CONFIG"

	MISC_REPO_ROOT="${MISC_ROOT:-$XDG_DATA_HOME/misc}"
	MISC_PLAN_ROOT="${MISC_PLAN_ROOT:-$XDG_CONFIG_HOME/misc}"
	MISC_INSTALL_PREFIX="${MISC_INSTALL_PREFIX:-$HOME/.local}"
	_ARGS=()
	_DRY_RUN=false
	_NUKE=false
	DEBUG=
	while getopts :hdnvr:p:i: opt; do
		case "$opt" in
			h) usage ;;
			n)
				_NUKE=true
				_ARGS+=(-n)
				;;
			d)
				_DRY_RUN=true
				_ARGS+=(-d)
				;;
			v)
				DEBUG=true
				_ARGS+=(-v)
				;;
			r)
				MISC_REPO_ROOT="$OPTARG"
				_ARGS+=(-r "$OPTARG")
				;;
			p)
				MISC_PLAN_ROOT="$OPTARG"
				_ARGS+=(-p "$OPTARG")
				;;
			i)
				MISC_INSTALL_PREFIX="$OPTARG"
				_ARGS+=(-i "$OPTARG")
				;;
			\?)
				log "Unknown flag \"-$OPTARG\""
				usage 1
				;;
		esac
	done
}

finish() {
	cd "$_MISC_PREVIOUS_DIR"
}

## Utilities

say() { # say WORD ...
	printf '%s\n' "$*"
}

log() { # log WORD ...
	say "$@" >&2
}

die() { # die CODE WORD ...
	code="$1"
	shift
	log "$@"
	exit "$code"
}

run() { # run COMMAND ...
	log "$@"
	if ! "$_DRY_RUN"; then
		eval "$@"
	fi
}

cmd() { # cmd COMMAND ...
	log "$@"
	if ! "$_DRY_RUN"; then
		command "$@"
	fi
}

with_repo() { # with_repo COMMAND ...
	## Run COMMAND in $MISC_REPO.
	run cd "$MISC_REPO" || die 4 "No such directory \"$MISC_REPO\""
	run "$@"
}

## Library

install_deps() { # install_deps
	for dep in "${MISC_DEPENDENCIES[@]}"; do
		run misc "${_ARGS[@]}" "$dep" ||
			die 60 "Error installing dependency: \"$dep\""
	done
	if command -v apt >/dev/null 2>&1; then
		for dep in "${APT_DEPENDENCIES[@]}"; do
			# XXX: I don't like how hacky this is.
			if [[ "$(apt list --installed "$dep" 2>/dev/null | wc -l)" -lt 2 ]]; then
				run sudo apt install "$dep" ||
					die 65 "Error installing dependency: \"$dep\""
			fi
		done
	fi
}

repo_pull() { # repo_pull [CHECKOUT]
	## Pull the latest CHECKOUT from $REPO.
	MISC_DONE=1
	local source checkout
	checkout="${1:-$CHECKOUT}"

	{ [[ -n "$SOURCE" ]] || "$_DRY_RUN"; } ||
		die 10 "No source specified for \"$MISC_PLAN\""

	if [[ -d "$MISC_REPO" ]]; then
		git pull origin "$checkout" ||
			die "$?" "Error running 'git pull'"
		git checkout "$checkout" ||
			die "$?" "Error running 'git checkout'"
	else
		_git clone -b "$checkout" "$SOURCE" "$MISC_REPO" ||
			die "$?" "Error running 'git clone'"
	fi
}

repo_download_extract() { # repo_download_extract
	run 'curl -RL "$SOURCE" | tar -zxf-'
}

repo_ready() { # repo_ready [TARGET ...]
	## Ready $MISC_ for building.
	## If TARGET ... is specified, they're passed to make; otherwise `make
	## clean` is run in the repo directory.
	MISC_DONE=1
	{ [[ -d "$MISC_REPO" ]] || "$_DRY_RUN"; } ||
		die 20 "No directory for \"$MISC_PLAN\""
	make "${@:-clean}"
	if git status >/dev/null 2>&1; then
		git clean -dxf
	fi
}

repo_nuke() { # repo_nuke
	## Fully remove $MISC_FULL_PLAN
	{ [[ -n "$MISC_REPO" ]] || "$_DRY_RUN"; } ||
		die 40 "No directory to remove"
	run rm -rf "$MISC_REPO"
}

## Commands (with arguments)

make() {
	MISC_DONE=1
	cmd make \
	    -C "$MISC_REPO" \
	    PREFIX="$MISC_INSTALL_PREFIX" \
	    "$@"
}
_make() { cmd make "$@"; }

make_install() {
	MISC_DONE=1
	if make "$@"; then
		make install
	else
		die 23 "Make error in \"$MISC_PLAN\": $@"
	fi
}

configure() {
	MISC_DONE=1
	if (($# == 0)); then
		with_repo ./configure "${CONFIGURE_ARGS[@]}"
	else
		with_repo ./configure "$@"
	fi
}

git() {
	cmd git \
	    -C "$MISC_REPO" \
	    "$@"
}
_git() { cmd git "$@"; }

tar() {
	cmd tar \
	    -C "$MISC_REPO" \
	    "$@"
}
_tar() { cmd tar "$@"; }

## Finally ...

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
	main "$@"
fi