-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
112 lines (93 loc) · 3.07 KB
/
install.sh
File metadata and controls
112 lines (93 loc) · 3.07 KB
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
#!/usr/bin/env sh
set -eu
REPO="lucaspiritogit/wallbit-cli"
BIN_NAME="wallbit-cli"
INSTALL_DIR="/usr/local/bin"
fail() {
printf "%s\n" "$1" >&2
exit 1
}
if command -v curl >/dev/null 2>&1; then
FETCH_TOOL="curl"
elif command -v wget >/dev/null 2>&1; then
FETCH_TOOL="wget"
else
fail "curl or wget is required"
fi
if ! command -v tar >/dev/null 2>&1; then
fail "tar is required"
fi
sha256_file() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
return
fi
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
return
fi
fail "sha256sum or shasum is required"
}
VERSION="${WALLBIT_CLI_VERSION:-}"
if [ -z "$VERSION" ]; then
if [ "$FETCH_TOOL" = "curl" ]; then
VERSION="$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | awk -F '"' '/"tag_name":/ { print $4 }')"
else
VERSION="$(wget -qO- "https://api.github.com/repos/${REPO}/releases/latest" | awk -F '"' '/"tag_name":/ { print $4 }')"
fi
fi
[ -n "$VERSION" ] || fail "Could not resolve wallbit-cli version"
UNAME_S="$(uname -s)"
UNAME_M="$(uname -m)"
case "$UNAME_S" in
Linux) OS="Linux" ;;
Darwin) OS="Darwin" ;;
*) fail "Unsupported operating system: $UNAME_S" ;;
esac
case "$UNAME_M" in
x86_64|amd64) ARCH="amd64" ;;
arm64|aarch64) ARCH="arm64" ;;
*) fail "Unsupported CPU architecture: $UNAME_M" ;;
esac
ASSET="${BIN_NAME}_${OS}_${ARCH}.tar.gz"
CHECKSUMS_ASSET="SHA256SUMS"
BASE_URL="https://github.com/${REPO}/releases/download/${VERSION}"
ASSET_URL="${BASE_URL}/${ASSET}"
CHECKSUMS_URL="${BASE_URL}/${CHECKSUMS_ASSET}"
TMP_DIR="$(mktemp -d 2>/dev/null || mktemp -d -t wallbit-cli)"
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM
ARCHIVE_PATH="${TMP_DIR}/${ASSET}"
CHECKSUMS_PATH="${TMP_DIR}/${CHECKSUMS_ASSET}"
if [ "$FETCH_TOOL" = "curl" ]; then
curl -fsSL "$ASSET_URL" -o "$ARCHIVE_PATH"
curl -fsSL "$CHECKSUMS_URL" -o "$CHECKSUMS_PATH"
else
wget -qO "$ARCHIVE_PATH" "$ASSET_URL"
wget -qO "$CHECKSUMS_PATH" "$CHECKSUMS_URL"
fi
EXPECTED_HASH="$(awk -v asset="$ASSET" '$2 == asset { print $1 }' "$CHECKSUMS_PATH")"
[ -n "$EXPECTED_HASH" ] || fail "Could not find checksum for ${ASSET}"
ACTUAL_HASH="$(sha256_file "$ARCHIVE_PATH")"
[ "$ACTUAL_HASH" = "$EXPECTED_HASH" ] || fail "Checksum verification failed for ${ASSET}"
tar -xzf "$ARCHIVE_PATH" -C "$TMP_DIR"
[ -f "${TMP_DIR}/${BIN_NAME}" ] || fail "Archive did not contain ${BIN_NAME}"
if [ ! -d "$INSTALL_DIR" ]; then
if [ -w "$(dirname "$INSTALL_DIR")" ]; then
mkdir -p "$INSTALL_DIR"
elif command -v sudo >/dev/null 2>&1; then
sudo mkdir -p "$INSTALL_DIR"
else
fail "Cannot create ${INSTALL_DIR}. Run with sufficient permissions."
fi
fi
TARGET_PATH="${INSTALL_DIR}/${BIN_NAME}"
if [ -w "$INSTALL_DIR" ]; then
mv "${TMP_DIR}/${BIN_NAME}" "$TARGET_PATH"
chmod +x "$TARGET_PATH"
elif command -v sudo >/dev/null 2>&1; then
sudo mv "${TMP_DIR}/${BIN_NAME}" "$TARGET_PATH"
sudo chmod +x "$TARGET_PATH"
else
fail "No write access to ${INSTALL_DIR} and sudo is unavailable."
fi
printf "Installed %s %s to %s\n" "$BIN_NAME" "$VERSION" "$TARGET_PATH"