-
Notifications
You must be signed in to change notification settings - Fork 6
/
fetch
executable file
·63 lines (53 loc) · 1.12 KB
/
fetch
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
#!/bin/bash
function usage {
cat <<EOF
Usage
-----
$0 <url> <output-file> <sha256-checksum>
EOF
}
function main {
if [ $# -ne 3 ]; then
usage
return 1
fi
local URL=$1
local OUTFILE=$2
local CHECKSUM=$3
local TMPFILE=$(mktemp fetch.tmp.XXXXXX)
if [ $? -ne 0 ]; then
echo "Error creating temporary file."
return 1
fi
echo "[+] Fetching '$URL' into '$TMPFILE'..."
wget -O "$TMPFILE" "$URL"
if [ $? -ne 0 ]; then
echo "Error downloading '$URL'."
rm -f "$TMPFILE"
return 1
fi
echo "[+] Done."
echo "[+] Checking checksum of '$TMPFILE' ($CHECKSUM)..."
local SUM=$(openssl dgst -sha256 -r < "$TMPFILE" | awk '{print $1}')
if [ $? -ne 0 ]; then
echo "Error computing checksum for '$URL'."
rm -f "$TMPFILE"
return 1
fi
if [ "$SUM" != "$CHECKSUM" ]; then
echo "Checksum mismatch for '$URL': got '$SUM' / expecting '$CHECKSUM'."
rm -f "$TMPFILE"
return 1
fi
echo "[+] Done."
echo "[+] Renaming '$TMPFILE' to '$OUTFILE'..."
mv "$TMPFILE" "$OUTFILE"
if [ $? -ne 0 ]; then
echo "Error renaming '$TMPFILE' to '$OUTFILE'."
rm -f "$TMPFILE"
return 1
fi
echo "[+] Done."
return 0
}
main "$@"