summaryrefslogtreecommitdiffstats
path: root/bootstrap.sh
blob: 3bd46a7508770bf035ca89a0bda1af1e38e3f789 (plain) (blame)
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
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# region header
# Copyright Torben Sickert (info["~at~"]torben.website) 29.10.2015
#           Janosch Dobler (info["~at~"]jandob.com) 29.10.2015
#           Jonathan Bauer (jonathan.bauer@rz.uni-freiburg.de) 19.09.2019
#           Thiago Abdo    (tjabdo@inf.ufpr.br) 06-11-2019

# License
# -------
# This library written by Torben Sickert and Janosch Dobler stand under a
# creative commons naming 3.0 unported license.
# see http://creativecommons.org/licenses/by/3.0/deed.de
## endregion

declare -g modules="modules.conf"

print_help_message() {
	echo "Bootstrapper script for systemd-init."
	echo ""
	echo "$0: [--help|-h] [--verbose|-v] <modules_config>"
	exit "${1:-0}"
}

parse_command_line() {
	while [ $# -gt 0 ]; do
		case "$1" in
			--help|-h)
				shift
				print_help_message 0
				;;
			--verbose|-v)
				shift
				verbose='yes'
				set -x
				;;
			*)
				modules="$1"
				shift
				;;
		esac
	done
	return 0
}

vecho() {
	[ -n "$verbose" ] && echo $@
}

handler_git() {
	local path="$1"
	local url="$2"
	local branch="$3"
	local commit="$4"

	rm -rf "$path"
	mkdir -p "$path"

	local gitargs=( \
		"--depth" "1" \
		"--single-branch" \
	)
	if [ -n "$branch" ]; then
		gitargs+=("--branch" "$branch")
	fi
	git clone "${gitargs[@]}" "${url}" "${path}"

	[ -z "$commit" ] && return 0
	
	# make sure given commit in in the fetched history
	local revision="$branch"
	if [ -n "$commit" ]; then
		revision="$commit"
	fi

	# manual "shallow clone" since not all server allow it...
	pushd "$path"
	local i=50
	while ! git rev-parse --quiet --verify $revision^{commit}; do
		git fetch --depth=$(( i+=50 ))
	done
	git reset --hard "$revision"
	popd
}

handler_http() {
	local path="$1"
	local url="$2"

	rm -rf "$path"
	mkdir --parents "$path"

	curl \
		--location \
		--max-redirs 5 \
		--max-time 7 \
		--connect-timeout 2 \
		--retry 3 \
		--retry-max-time 12 \
		"$url" \
		| tar \
			--extract \
			--gzip \
			--directory "$path" \
			--strip-components 1
}

parse_command_line $@

if [ ! -f "$modules" ]; then
	echo "Missing modules definition file: $modules"
	print_help_message 1
fi

. $modules

vecho "Modules file: $modules"
vecho "Modules:      ${!module_*}"
vecho ""

# checkout modules, starting with systemd-init defined in '_core'.
for module in _core "${!module_@}"; do
	declare -n _ref="$module"
	echo "#########################  $module  #########################"
	vecho "Handler: ${_ref[handler]}"
	vecho "    URL: ${_ref[url]}"
	vecho " Branch: ${_ref[branch]}"
	vecho " Commit: ${_ref[commit]}"
	vecho "   Path: ${_ref[path]}"
	handler_${_ref[handler]} \
		"${_ref[path]}" \
		"${_ref[url]}" \
		"${_ref[branch]}" \
		"${_ref[commit]}"
done

# look for patches and apply them to their module's path
for _dir in $(find $PWD/${_core[path]}/builder/patches/* -maxdepth 0 -type d); do
	patches=( ${_dir}/*.patch )
	target_dir="${_dir##*/}"
	declare -n target_module="module_${target_dir//-/_}"
	pushd "${target_module[path]}"
	for patch in "${patches[@]}"; do
		patch -p1  < "$patch"
	done
	popd
done