-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinstall.sh
More file actions
202 lines (179 loc) · 4.2 KB
/
install.sh
File metadata and controls
202 lines (179 loc) · 4.2 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
#!/bin/sh
set -eu
REPO="pproenca/agent-tui"
BIN_NAME="agent-tui"
SKIP_PM="${AGENT_TUI_SKIP_PM:-0}"
SKIP_VERIFY="${AGENT_TUI_SKIP_VERIFY:-0}"
if [ -n "${AGENT_TUI_INSTALL_DIR:-}" ]; then
INSTALL_DIR="$AGENT_TUI_INSTALL_DIR"
else
if [ -z "${HOME:-}" ]; then
printf '%s\n' "error: HOME is not set; set AGENT_TUI_INSTALL_DIR to install." >&2
exit 1
fi
INSTALL_DIR="$HOME/.local/bin"
fi
log() {
printf '%s\n' "$*"
}
err() {
printf '%s\n' "$*" >&2
}
die() {
err "error: $*"
exit 1
}
have() {
command -v "$1" >/dev/null 2>&1
}
normalize_npm_version() {
version=$1
case "$version" in
v*) printf '%s' "${version#v}" ;;
*) printf '%s' "$version" ;;
esac
}
try_package_manager() {
if [ "$SKIP_PM" = "1" ]; then
return 1
fi
version="${AGENT_TUI_VERSION:-}"
if [ -n "$version" ]; then
npm_version=$(normalize_npm_version "$version")
package="agent-tui@$npm_version"
else
package="agent-tui"
fi
if have npm; then
log "Installing via npm..."
if npm i -g "$package"; then
return 0
fi
err "npm install failed; falling back to binary install."
return 1
fi
if have pnpm; then
log "Installing via pnpm..."
if pnpm add -g "$package"; then
return 0
fi
err "pnpm install failed; falling back to binary install."
return 1
fi
if have bun; then
log "Installing via bun..."
if bun add -g "$package"; then
return 0
fi
err "bun install failed; falling back to binary install."
return 1
fi
return 1
}
download() {
url=$1
dest=$2
if have curl; then
curl -fsSL "$url" -o "$dest"
return
fi
if have wget; then
wget -qO "$dest" "$url"
return
fi
die "curl or wget is required"
}
hash_file() {
file=$1
if have sha256sum; then
sha256sum "$file" | awk '{print $1}'
return
fi
if have shasum; then
shasum -a 256 "$file" | awk '{print $1}'
return
fi
if have openssl; then
openssl dgst -sha256 "$file" | awk '{print $2}'
return
fi
die "sha256sum, shasum, or openssl is required to verify downloads"
}
is_musl() {
if have ldd; then
if ldd --version 2>&1 | grep -qi musl; then
return 0
fi
if ldd /bin/sh 2>&1 | grep -qi musl; then
return 0
fi
fi
if ls /lib/ld-musl-*.so* >/dev/null 2>&1; then
return 0
fi
return 1
}
detect_platform() {
os=$(uname -s 2>/dev/null | tr '[:upper:]' '[:lower:]')
case "$os" in
linux | darwin) platform="$os" ;;
*) die "Unsupported OS: $os (macOS and Linux only)" ;;
esac
arch=$(uname -m 2>/dev/null)
case "$arch" in
x86_64 | amd64) arch="x64" ;;
arm64 | aarch64) arch="arm64" ;;
*) die "Unsupported architecture: $arch" ;;
esac
musl_suffix=""
if [ "$platform" = "linux" ] && is_musl; then
musl_suffix="-musl"
fi
}
main() {
if try_package_manager; then
log "Done."
exit 0
fi
detect_platform
asset="${BIN_NAME}-${platform}-${arch}${musl_suffix}"
version="${AGENT_TUI_VERSION:-}"
if [ -n "$version" ]; then
case "$version" in
v*) tag="$version" ;;
*) tag="v$version" ;;
esac
base_url="https://github.com/$REPO/releases/download/$tag"
else
base_url="https://github.com/$REPO/releases/latest/download"
fi
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t agent-tui.XXXXXX)
trap 'rm -rf "$tmpdir"' EXIT HUP INT TERM
bin_tmp="$tmpdir/$asset"
sums_tmp="$tmpdir/checksums-sha256.txt"
log "Downloading $asset..."
download "$base_url/$asset" "$bin_tmp"
if [ "$SKIP_VERIFY" = "1" ]; then
log "Skipping checksum verification (AGENT_TUI_SKIP_VERIFY=1)."
else
log "Verifying checksum..."
download "$base_url/checksums-sha256.txt" "$sums_tmp"
expected=$(awk -v file="$asset" '$2 == file {print $1; exit}' "$sums_tmp")
if [ -z "$expected" ]; then
die "Checksum not found for $asset"
fi
actual=$(hash_file "$bin_tmp")
if [ "$expected" != "$actual" ]; then
die "Checksum mismatch for $asset"
fi
fi
mkdir -p "$INSTALL_DIR"
cp "$bin_tmp" "$INSTALL_DIR/$BIN_NAME"
chmod 755 "$INSTALL_DIR/$BIN_NAME"
log "Installed $BIN_NAME to $INSTALL_DIR/$BIN_NAME"
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*) log "Add to PATH: export PATH=\"$INSTALL_DIR:\$PATH\"" ;;
esac
}
main "$@"