aboutsummaryrefslogtreecommitdiff
path: root/scripts/common.sh
blob: 9efa3d5e6c663bf22b64ba0f671471c43e7a5e9b (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
platform='unknown'
unameval=`uname`
if [ "$unameval" = 'Linux' ]; then
	platform='linux'
elif [ "$unameval" = 'FreeBSD' ]; then
	platform='freebsd'
fi

sha1sumcmd='sha1sum'
if [ "$platform" = 'freebsd' ]; then
	sha1sumcmd='shasum'
fi

pkgroot="$(pwd)"
deps="$pkgroot/.deps"
prefix="$deps/usr"
export PATH="$prefix/bin:$PATH"

download() {
	local url=$1
	local tgt=$2
	local sha1=$3

	if [ ! -d "$tgt" ]; then
		mkdir -p "$tgt"
		local download_command=""
		if which wget > /dev/null 2>&1; then
			# -O - to send output to stdout
			download_command="wget --no-verbose $url -O -"
		elif which curl >/dev/null 2>&1; then
			# -L to follow the redirects that github will send us
			# -sS to supress the progress bar, but show errors
			# curl sends output to stdout by default
			download_command="curl -L -sS $url"
		else
			echo "Missing wget utility and curl utility"
			exit 1
		fi
		local tmp_dir=$(mktemp -d "/tmp/download_sha1check_XXXXXXX")
		local fifo="$tmp_dir/fifo"
		mkfifo "$fifo"
		echo "Downloading $url..."
		# download, untar and calculate sha1 sum in one pass
		($download_command | tee "$fifo" | \
			(cd "$tgt";  tar --strip-components=1 -xzf -)) &
		local sum=$("$sha1sumcmd" < "$fifo" | cut -d ' ' -f1)
		rm -rf "$tmp_dir"
		if [ "$sum" != "$sha1" ]; then
			echo "SHA1 sum doesn't match, expected '$sha1' got '$sum'"
			exit 1
		else
			echo "Download complete."
		fi
	fi
}

github_download() {
	local repo=$1
	local ver=$2
	download "https://github.com/${repo}/archive/${ver}.tar.gz" "$3" "$4"
}