-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathbeam-docs-library.sh
57 lines (48 loc) · 1.39 KB
/
beam-docs-library.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
#! /bin/bash
beam_doc_status() {
echo "$@" 1>&2
}
sha256() {
if which sha256sum 2>/dev/null >/dev/null; then
SHA256SUM="sha256sum $1 | awk '{print \$1}'"
elif which openssl 2>/dev/null >/dev/null; then
SHA256SUM="openssl dgst -sha256 $1 | awk '{print \$2}'"
else
beam_doc_status "Cannot find sha256sum or openssl"
exit 1
fi
/bin/sh -c "$SHA256SUM"
}
download () {
CACHED_FILE=$1
URL=$2
EXPECTED_SHA256=$3
CONV=$4
if [ ! -f $CACHED_FILE ]; then
TMP_FILE="${CACHED_FILE}.tmp"
beam_doc_status "Downloading $URL to $CACHED_FILE.tmp..."
DIR=$(dirname $TMP_FILE)
mkdir -p $DIR
curl $URL > $TMP_FILE
ACTUAL_SUM=$(sha256 $TMP_FILE)
if [ "$ACTUAL_SUM" != "$EXPECTED_SHA256" ]; then
beam_doc_status "Sum mismatch, got $ACTUAL_SUM, expected $EXPECTED_SHA256"
exit 1
else
if [ -z $CONV ]; then
cat $TMP_FILE | sed -e 's/\r$//' > $CACHED_FILE
else
cat $TMP_FILE | bash -c "$CONV" | sed -e 's/\r$//' > $CACHED_FILE
fi
rm $TMP_FILE
fi
beam_doc_status "Finished downloading"
fi
}
if ! which pv >/dev/null 2>/dev/null; then
beam_doc_status "No 'pv' command found, no progress indication available"
pv () {
beam_doc_status "Starting upload of $@"
cat "$@"
}
fi