-
Notifications
You must be signed in to change notification settings - Fork 15
/
fetch.sh
executable file
·70 lines (55 loc) · 1.51 KB
/
fetch.sh
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
#!/bin/sh
set -e
if test "$#" -ne 1 ; then
echo 'usage: fetch.sh <pkg>'
exit 1
fi
opam_cache() {
local chksum=$1
hash_fun=$(echo "$chksum" | cut -d= -f 1)
archive_hash=$(echo "$chksum" | cut -d= -f 2)
first_two=${archive_hash:0:2}
echo "https://opam.ocaml.org/cache/${hash_fun}/${first_two}/${archive_hash}"
}
checksum_aux() {
local file=$1
local bin=$2
local hash=$3
output=$("$bin" "$file")
if ! test "$output" = "$hash $file" ; then
echo "The file '$file' didn't match its checksum. Please remove it."
echo
fi
}
checksum() {
local file=$1
local chksum=$2
hash_fun=$(echo "$chksum" | cut -d= -f 1)
archive_hash=$(echo "$chksum" | cut -d= -f 2)
case "$hash_fun" in
md5) checksum_aux "$file" "md5sum" "$archive_hash" ;;
sha1) checksum_aux "$file" "sha1sum" "$archive_hash" ;;
sha256) checksum_aux "$file" "sha256sum" "$archive_hash" ;;
sha512) checksum_aux "$file" "sha512sum" "$archive_hash" ;;
*) echo "Could not detect checksum type '${hash_fun}'"; exit 1 ;;
esac
}
fetch() {
local pkg=$1
file=$(opam show -furl.src: "$pkg")
file=$(echo "$file" | sed -e 's,.*/,,' -e 's/"$//')
chksum=$(opam show -furl.checksum: "$pkg")
chksum=$(echo "$chksum" | head -n 1 | sed -e 's/^"//' -e 's/"$//')
url=$(opam_cache "$chksum")
echo "Fetching ${pkg}..."
curl -sLo "$file" "$url"
checksum "$file" "$chksum"
}
pkg=$1
if echo "$pkg" | grep -qF . ; then
fetch "${pkg}"
else
for version in $(opam show -fall-versions "$pkg") ; do
fetch "${pkg}.${version}"
done
fi