-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·146 lines (125 loc) · 4.31 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·146 lines (125 loc) · 4.31 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
#!/usr/bin/env bash
set -euo pipefail
repo="stanley2058/gmux"
binary="gmux"
install_dir="${GMUX_INSTALL_DIR:-$HOME/.local/bin}"
install_binary_path="${GMUX_INSTALL_BINARY_PATH:-}"
version="${GMUX_VERSION:-latest}"
log() {
if [ "${GMUX_INSTALL_QUIET:-0}" != "1" ]; then
printf '%s\n' "$*"
fi
}
warn() {
printf 'warning: %s\n' "$*" >&2
}
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
printf 'error: required command not found: %s\n' "$1" >&2
exit 1
fi
}
require_command curl
require_command tar
require_command install
require_command mktemp
if command -v sha256sum >/dev/null 2>&1; then
checksum_command="sha256sum"
elif command -v shasum >/dev/null 2>&1; then
checksum_command="shasum -a 256"
else
printf 'error: required command not found: sha256sum or shasum\n' >&2
exit 1
fi
if [ -n "$install_binary_path" ]; then
install_dir="$(dirname "$install_binary_path")"
binary="$(basename "$install_binary_path")"
fi
case "$(uname -s)" in
Linux) os="linux" ;;
Darwin) os="darwin" ;;
*)
printf 'error: unsupported OS: %s\n' "$(uname -s)" >&2
exit 1
;;
esac
case "$(uname -m)" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*)
printf 'error: unsupported architecture: %s\n' "$(uname -m)" >&2
exit 1
;;
esac
case "$os-$arch" in
linux-x86_64) platform="linux-x86_64" ;;
linux-aarch64) platform="linux-aarch64" ;;
darwin-aarch64) platform="darwin-aarch64" ;;
*)
printf 'error: unsupported platform: %s-%s\n' "$os" "$arch" >&2
printf 'supported platforms: linux-x86_64, linux-aarch64, darwin-aarch64\n' >&2
exit 1
;;
esac
asset="gmux-$platform.tar.gz"
if [ "$version" = "latest" ]; then
url="https://github.com/$repo/releases/latest/download/$asset"
checksums_url="https://github.com/$repo/releases/latest/download/SHA256SUMS"
else
url="https://github.com/$repo/releases/download/$version/$asset"
checksums_url="https://github.com/$repo/releases/download/$version/SHA256SUMS"
fi
tmp_dir="$(mktemp -d)"
cleanup() {
rm -rf "$tmp_dir"
}
trap cleanup EXIT
archive="$tmp_dir/$asset"
checksums="$tmp_dir/SHA256SUMS"
log "downloading $url"
curl -fL --retry 3 --connect-timeout 10 --max-time 120 -o "$archive" "$url"
log "downloading $checksums_url"
curl -fL --retry 3 --connect-timeout 10 --max-time 120 -o "$checksums" "$checksums_url"
expected_checksum="$(awk -v asset="$asset" '$2 == asset { print $1; found = 1 } END { if (!found) exit 1 }' "$checksums")" || {
printf 'error: checksum file did not contain %s\n' "$asset" >&2
exit 1
}
actual_checksum="$($checksum_command "$archive" | awk '{ print $1 }')"
if [ "$actual_checksum" != "$expected_checksum" ]; then
printf 'error: checksum mismatch for %s\n' "$asset" >&2
printf 'expected: %s\n' "$expected_checksum" >&2
printf 'actual: %s\n' "$actual_checksum" >&2
exit 1
fi
tar -xzf "$archive" -C "$tmp_dir"
if [ ! -x "$tmp_dir/gmux" ]; then
printf 'error: archive did not contain executable gmux\n' >&2
exit 1
fi
mkdir -p "$install_dir"
target="$install_dir/$binary"
tmp_target="$install_dir/.$binary.tmp.$$"
install -m 0755 "$tmp_dir/gmux" "$tmp_target"
mv -f "$tmp_target" "$target"
log "installed $binary to $target"
"$target" --version
if [ "${GMUX_INSTALL_COMPLETIONS:-1}" != "0" ]; then
bash_dir="${GMUX_BASH_COMPLETION_DIR:-$HOME/.local/share/bash-completion/completions}"
zsh_dir="${GMUX_ZSH_COMPLETION_DIR:-$HOME/.local/share/zsh/site-functions}"
fish_dir="${GMUX_FISH_COMPLETION_DIR:-$HOME/.config/fish/completions}"
mkdir -p "$bash_dir" "$zsh_dir" "$fish_dir"
"$target" completions bash > "$bash_dir/gmux" || warn "failed to install bash completion"
"$target" completions zsh > "$zsh_dir/_gmux" || warn "failed to install zsh completion"
"$target" completions fish > "$fish_dir/gmux.fish" || warn "failed to install fish completion"
log "installed bash completion to $bash_dir/gmux"
log "installed zsh completion to $zsh_dir/_gmux"
log "installed fish completion to $fish_dir/gmux.fish"
log "zsh users may need to add this before compinit: fpath=($zsh_dir \$fpath)"
fi
case ":$PATH:" in
*":$install_dir:"*) ;;
*)
warn "$install_dir is not on PATH"
warn "add it to PATH or run $target directly"
;;
esac