forked from GreenmaskIO/greenmask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·239 lines (191 loc) · 5.73 KB
/
install.sh
File metadata and controls
executable file
·239 lines (191 loc) · 5.73 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/sh
# greenmask installer
# Usage:
# curl -fsSL https://greenmask.io/install.sh | sh
# sh install.sh --bin-dir /usr/local/bin --yes
set -eu
APP="greenmaskio"
REPO="greenmask"
BIN_DIR="/usr/local/bin"
VERSION="latest"
ASSUME_YES=0
DEBUG=0
info() { printf "> %s\n" "$*"; }
warn() { printf "! %s\n" "$*"; }
err() { printf "x %s\n" "$*" >&2; }
die() { err "$*"; exit 1; }
has() { command -v "$1" >/dev/null 2>&1; }
usage() {
cat <<EOF
greenmask installer
Options:
-b, --bin-dir DIR install directory (default: ${BIN_DIR})
-y, --yes skip confirmation (non-interactive)
-v, --version VER version tag (e.g. v1.2.3) (default: ${VERSION})
--debug verbose output
-h, --help show this help
EOF
}
fetch() {
url="$1"; out="$2"
if has curl; then
curl -fsSL --proto '=https' --tlsv1.2 "$url" -o "$out"
elif has wget; then
wget -qO "$out" "$url"
else
die "need 'curl' or 'wget' to download: $url"
fi
}
detect_os() {
case "$(uname -s | tr '[:upper:]' '[:lower:]')" in
linux) printf "linux" ;;
darwin) printf "darwin" ;;
*) die "unsupported OS (linux, darwin only)";;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) printf "amd64" ;;
aarch64|arm64) printf "arm64" ;;
armv7l|armv7) printf "armv7" ;;
armv6l|armv6|arm)printf "armv6" ;;
ppc64le) printf "ppc64le" ;;
riscv64) printf "riscv64" ;;
s390x) printf "s390x" ;;
*) die "unsupported ARCH";;
esac
}
build_base_url() {
if [ "$VERSION" = "latest" ]; then
printf "https://github.com/%s/%s/releases/latest/download" "$APP" "$REPO"
else
printf "https://github.com/%s/%s/releases/download/%s" "$APP" "$REPO" "$VERSION"
fi
}
calc_sha256() {
f="$1"
if has sha256sum; then sha256sum "$f" | awk '{print $1}'; elif has shasum; then shasum -a 256 "$f" | awk '{print $1}'; else die "need sha256sum or shasum"; fi
}
verify_checksum_from_archive() {
tmpdir="$1"
bin="$2"
sha_file="$tmpdir/greenmask.sha256"
[ -f "$sha_file" ] || die "checksum file not found in archive"
expected="$(awk '{print $1}' "$sha_file")"
actual="$(calc_sha256 "$bin")"
if [ "$expected" != "$actual" ]; then
die "checksum mismatch: expected $expected, got $actual"
fi
info "Checksum verified ✓"
}
is_writable() {
dir="$1"
tmp="$dir/.gm_write_test.$$"
( : >"$tmp" ) 2>/dev/null && { rm -f "$tmp"; return 0; }
return 1
}
need_sudo_or_exit() {
if ! has sudo; then
die "need write access to ${BIN_DIR} or 'sudo' for install"
fi
if ! sudo -v; then
die "sudo failed; cannot install to ${BIN_DIR}"
fi
}
check_bin_in_path() {
# remove the trailing /
dir="${1%/}"
OLDIFS="$IFS"
IFS=:
for p in $PATH; do [ "${p%/}" = "$dir" ] && return 0; done
IFS="$OLDIFS"
return 1
}
print_path_tips() {
dir="$1"
warn "Bin directory ${dir} is not in your PATH."
printf "\nAdd it to your shell config:\n\n"
printf "bash: echo 'export PATH=\"%s:\$PATH\"' >> ~/.bashrc && . ~/.bashrc\n" "$dir"
printf "zsh: echo 'export PATH=\"%s:\$PATH\"' >> ~/.zshrc && . ~/.zshrc\n" "$dir"
printf "\n"
}
unpack_tar_to() {
archive="$1"; dest="$2";
has tar || die "need 'tar' to extract archives"
tar xzf "$archive" -C "$dest"
}
install_asset() {
os="$1"; arch="$2"; base="$3"
asset="${REPO}-${os}-${arch}.tar.gz"
url="${base}/${asset}"
has mktemp || die "need 'mktemp' to create temp directories"
tmp="$(mktemp -d)"
# clean the tmp directory at the end
trap 'rm -rf "$tmp"' EXIT INT TERM
info "Downloading: ${url}"
fetch "$url" "$tmp/$asset"
info "Extracting…"
unpack_tar_to "$tmp/$asset" "$tmp"
info "Verifying checksum…"
# find the executable file
bin_path="$(find "$tmp" -type f -name "$REPO*" \( -perm -u+x -o -perm -g+x -o -perm -o+x \) 2>/dev/null | head -n1 || true)"
[ -n "$bin_path" ] || die "binary '$REPO' not found in archive"
verify_checksum_from_archive "$tmp" "$bin_path"
sudo=""
if ! is_writable "$BIN_DIR"; then
warn "Escalated permissions required for ${BIN_DIR}"
need_sudo_or_exit
sudo="sudo"
fi
info "Installing to ${BIN_DIR}…"
if has install; then
$sudo install -m 0755 "$bin_path" "$BIN_DIR/$REPO"
else
$sudo cp "$bin_path" "$BIN_DIR/$REPO"
$sudo chmod 0755 "$BIN_DIR/$REPO"
fi
info "Installed: $BIN_DIR/$REPO"
# Shadowed binary warning
present="$(command -v "$REPO" 2>/dev/null || true)"
if [ -n "$present" ] && [ "$present" != "$BIN_DIR/$REPO" ]; then
warn "'$REPO' on PATH is '$present' (not '$BIN_DIR/$REPO')"
fi
if ! check_bin_in_path "$BIN_DIR"; then
if has realpath; then print_path_tips "$(realpath "$BIN_DIR")"; else print_path_tips "$BIN_DIR"; fi
fi
printf "✓ %s %sinstalled\n" "$APP" "$( [ "$VERSION" = "latest" ] && printf '' || printf "%s " "$VERSION")"
}
main() {
# Parse args
while [ $# -gt 0 ]; do
case "$1" in
-b|--bin-dir) BIN_DIR="$2"; shift 2 ;;
-v|--version) VERSION="$2"; shift 2 ;;
-y|--yes) ASSUME_YES=1;shift;;
--debug) DEBUG=1; shift;;
-h|--help) usage; exit 0 ;;
*) die "unknown option: $1" ;;
esac
done
[ "$DEBUG" -eq 1 ] && set -x
# If we're not connected to a TTY (piped/CI), default to non-interactive
if [ "$ASSUME_YES" -ne 1 ] && [ ! -t 0 ]; then
ASSUME_YES=1
fi
os="$(detect_os)"
arch="$(detect_arch)"
base="$(build_base_url)"
printf "Configuration\n"
info "Bin directory: ${BIN_DIR}"
info "OS/ARCH : ${os}/${arch}"
info "Version : ${VERSION}"
printf "\n"
if [ "$ASSUME_YES" -ne 1 ]; then
printf "? Install %s %s to %s? [y/N] " "$APP" "$VERSION" "$BIN_DIR"
read -r ans || true
case "$ans" in y|Y|yes|YES) ;; *) die "aborted" ;; esac
fi
install_asset "$os" "$arch" "$base"
info "Uninstall: rm -f $BIN_DIR/$REPO"
}
main "$@"