-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
546 lines (480 loc) · 16 KB
/
Copy pathinstall.sh
File metadata and controls
546 lines (480 loc) · 16 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/usr/bin/env bash
set -euo pipefail
APP="btch"
REPO="hostinger-bot/btch-cli"
RELEASES_API="https://api.github.com/repos/${REPO}/releases"
USER_DIR="${HOME}/.btch"
INSTALL_DIR="${USER_DIR}/bin"
METADATA_PATH="${USER_DIR}/install.json"
PATH_MARKER="# btch"
requested_version=""
binary_path=""
no_modify_path=false
written_config_file=""
written_path_command=""
usage() {
cat <<'EOF'
Install btch from GitHub Releases.
Usage:
curl -fsSL https://raw.githubusercontent.com/hostinger-bot/btch-cli/main/install.sh | bash
curl -fsSL https://raw.githubusercontent.com/hostinger-bot/btch-cli/main/install.sh | bash -s -- --version 3.0.15
bash install.sh --binary /path/to/btch
Options:
-v, --version <version> Install a specific version
-b, --binary <path> Install from a local binary instead of downloading
--no-modify-path Do not edit shell config files
-h, --help Show this help text
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
if [[ -z "${2:-}" ]]; then
echo "Error: --version requires a version argument" >&2
exit 1
fi
requested_version="$2"
shift 2
;;
-b|--binary)
if [[ -z "${2:-}" ]]; then
echo "Error: --binary requires a path argument" >&2
exit 1
fi
binary_path="$2"
shift 2
;;
--no-modify-path)
no_modify_path=true
shift
;;
*)
echo "Warning: Unknown option '$1'" >&2
shift
;;
esac
done
mkdir -p "$INSTALL_DIR"
chmod 700 "$USER_DIR" "$INSTALL_DIR"
is_termux() {
# Android/Termux uses the bionic libc, and Android's seccomp filter blocks
# syscalls Bun needs ("Bad system call"), so Termux installs run btch inside
# a proot-distro Debian environment instead (full glibc, no seccomp).
[[ -n "${TERMUX_VERSION:-}" ]] && return 0
[[ -d /data/data/com.termux ]] && return 0
[[ "$(uname -o 2>/dev/null)" == "Android" ]] && return 0
return 1
}
install_on_termux() {
cat <<'EOF'
Termux (Android) detected.
Android's seccomp filter blocks syscalls Bun needs ("Bad system call"), so
btch is installed inside a proot-distro Debian environment where it runs
normally with the full interactive TUI. This takes a few minutes on first
install (it downloads the Debian rootfs).
EOF
# 1) Make sure proot-distro is available.
if ! command -v proot-distro >/dev/null 2>&1; then
echo "Installing proot-distro..."
pkg install -y proot-distro || {
echo "Failed to install proot-distro. Run 'pkg install proot-distro' manually, then re-run this script." >&2
exit 1
}
fi
local rootfs="${PREFIX}/var/lib/proot-distro/installed-rootfs/debian"
# 2) Install the Debian rootfs on first run.
if [[ ! -d "$rootfs" ]]; then
echo "Installing Debian environment (first run downloads ~200MB, be patient)..."
proot-distro install debian || {
echo "Debian install failed. Run 'proot-distro install debian' manually, then re-run this script." >&2
exit 1
}
fi
# Remove leftovers from previous installs (npm route, old binary, wrapper, stale PATH).
rm -f "$HOME/.btch/bin/btch"
rm -f "/usr/local/bin/btch"
if [[ -n "${PREFIX:-}" ]]; then
rm -f "${PREFIX}/bin/btch"
fi
rm -rf "$HOME/.btch"
mkdir -p "$INSTALL_DIR"
# Clean up old PATH entries pointing to ~/.btch/bin from shell config
# files (v3.0.13 and earlier). These would shadow the wrapper at
# $PREFIX/bin/btch and cause the glibc binary to be executed directly
# in Termux, producing "cannot execute: required file not found".
for config in "$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.profile" "$HOME/.zshrc" "$HOME/.zshenv"; do
if [[ -f "$config" ]]; then
sed -i "\|.btch/bin|d" "$config" 2>/dev/null || true
sed -i "\|# btch|d" "$config" 2>/dev/null || true
fi
done
# 3) Inside Debian the full glibc + no seccomp means the standalone arm64
# binary works. Resolve the version and download to ~/.btch/runtime/ —
# NOT ~/.btch/bin/ (which might be on PATH from old installs and would
# shadow the wrapper). The wrapper bind-mounts the runtime dir into
# Debian at /usr/local/btch.
local RUNTIME_DIR="${USER_DIR}/runtime"
mkdir -p "$RUNTIME_DIR"
ASSET_NAME="btch-linux-arm64"
BINARY_NAME="btch"
resolve_release_version
local tmp_dir binary_file checksum_file
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/btch-install.XXXXXX")
binary_file="${tmp_dir}/${ASSET_NAME}"
checksum_file="${tmp_dir}/checksums.txt"
echo "Downloading ${ASSET_NAME} (${RESOLVED_VERSION})..."
curl -fSL "${RELEASE_BASE_URL}/${ASSET_NAME}" -o "$binary_file"
curl -fsSL "${RELEASE_BASE_URL}/checksums.txt" -o "$checksum_file"
verify_checksum "$binary_file" "$checksum_file"
cp "$binary_file" "${RUNTIME_DIR}/${BINARY_NAME}"
chmod 755 "${RUNTIME_DIR}/${BINARY_NAME}"
rm -rf "$tmp_dir"
# Best-effort: also copy into the Debian rootfs so `btch` works when
# logging into Debian directly. The wrapper below does not depend on this.
if [[ -d "$rootfs" ]]; then
mkdir -p "$rootfs/usr/local/bin" 2>/dev/null || true
cp "$binary_file" "$rootfs/usr/local/bin/${BINARY_NAME}" 2>/dev/null || true
chmod 755 "$rootfs/usr/local/bin/${BINARY_NAME}" 2>/dev/null || true
fi
# 4) Wrapper: bind-mount ~/.btch/runtime into Debian and run the binary
# there. The wrapper lives at $PREFIX/bin/btch (already on PATH), so
# typing `btch` finds the wrapper, not the glibc binary.
# `uninstall` is handled here in Termux (the binary runs inside Debian
# with HOME=/root, so it cannot see Termux's install.json).
local wrapper="${PREFIX}/bin/btch"
cat > "$wrapper" <<WRAPPER
#!${PREFIX}/bin/bash
if [[ "\${1:-}" == "uninstall" ]]; then
rm -f "${RUNTIME_DIR}/btch"
rm -f "${wrapper}"
rm -rf "${USER_DIR}"
echo "btch uninstalled."
echo "To also remove the Debian environment, run: proot-distro remove debian"
exit 0
fi
if ! proot-distro login --bind "${RUNTIME_DIR}:/usr/local/btch" debian -- /usr/local/btch/btch "\$@"; then
echo "btch failed to start inside Debian." >&2
echo "Make sure your Debian environment is working: proot-distro login debian" >&2
exit 1
fi
WRAPPER
chmod 755 "$wrapper"
# Verify the binary actually starts through the wrapper.
if ! "$wrapper" --version >/dev/null 2>&1; then
echo ""
echo "Warning: btch did not start inside Debian yet. Check that the Debian"
echo "environment exists (proot-distro list) and works (proot-distro login debian)." >&2
fi
# 5) Metadata so `btch uninstall` cleans up the wrapper + binary.
mkdir -p "$USER_DIR"
cat > "$METADATA_PATH" <<METAEOF
{
"schemaVersion": 1,
"installMethod": "script",
"version": "$(json_escape "$RESOLVED_VERSION")",
"repo": "$(json_escape "$REPO")",
"binaryPath": "$(json_escape "${RUNTIME_DIR}/${BINARY_NAME}")",
"installDir": "$(json_escape "$RUNTIME_DIR")",
"assetName": "$(json_escape "$ASSET_NAME")",
"target": "android-arm64",
"installedAt": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"shellConfigPath": null,
"pathCommand": null,
"globalBinPath": "$(json_escape "$wrapper")"
}
METAEOF
chmod 600 "$METADATA_PATH"
echo ""
echo "btch ${RESOLVED_VERSION} installed inside a proot-distro Debian environment."
echo ""
echo "Run:"
echo " btch --help"
echo ""
echo "If 'btch' is not found right away, run: hash -r"
echo ""
echo "To uninstall later:"
echo " btch uninstall"
echo " proot-distro remove debian # optional: also remove the Debian environment"
exit 0
}
resolve_target() {
local raw_os arch
raw_os=$(uname -s)
if is_termux; then
OS="android"
else
case "$raw_os" in
Darwin*) OS="darwin" ;;
Linux*) OS="linux" ;;
MINGW*|MSYS*|CYGWIN*) OS="windows" ;;
*)
echo "Unsupported OS: ${raw_os}" >&2
exit 1
;;
esac
fi
arch=$(uname -m)
case "$arch" in
arm64|aarch64) ARCH="arm64" ;;
x86_64|amd64) ARCH="x64" ;;
*)
echo "Unsupported architecture: ${arch}" >&2
exit 1
;;
esac
case "${OS}-${ARCH}" in
darwin-arm64|linux-x64|linux-arm64|android-arm64|windows-x64) ;;
*)
echo "Unsupported platform: ${OS}-${ARCH}" >&2
exit 1
;;
esac
TARGET="${OS}-${ARCH}"
# Older x64 CPUs (pre-AVX2, e.g. Sandy Bridge) crash with SIGILL on the
# regular build, so pick the baseline variant automatically. Modern CPUs
# get the faster regular build. (ARM64 builds are always baseline-safe.)
BASELINE_SUFFIX=""
if [[ "$TARGET" == "linux-x64" ]] && ! cpu_supports_avx2; then
BASELINE_SUFFIX="-baseline"
fi
if [[ "$TARGET" == windows-* ]]; then
ASSET_NAME="btch-${TARGET}${BASELINE_SUFFIX}.exe"
BINARY_NAME="btch.exe"
else
ASSET_NAME="btch-${TARGET}${BASELINE_SUFFIX}"
BINARY_NAME="btch"
fi
}
cpu_supports_avx2() {
if [[ -r /proc/cpuinfo ]] && grep -q '\bavx2\b' /proc/cpuinfo; then
return 0
fi
if command -v sysctl >/dev/null 2>&1; then
# macOS/BSD fallback; btch only ships a baseline for linux-x64, so this
# is only consulted on non-Linux platforms where it returns 0 anyway.
sysctl -a 2>/dev/null | grep -qi 'avx2' && return 0
fi
return 1
}
json_escape() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
}
json_or_null() {
if [[ -n "${1:-}" ]]; then
printf '"%s"' "$(json_escape "$1")"
else
printf 'null'
fi
}
write_metadata() {
local version="$1"
cat > "$METADATA_PATH" <<METAEOF
{
"schemaVersion": 1,
"installMethod": "script",
"version": "$(json_escape "$version")",
"repo": "$(json_escape "$REPO")",
"binaryPath": "$(json_escape "${INSTALL_DIR}/${BINARY_NAME}")",
"installDir": "$(json_escape "$INSTALL_DIR")",
"assetName": "$(json_escape "$ASSET_NAME")",
"target": "$(json_escape "$TARGET")",
"installedAt": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"shellConfigPath": $(json_or_null "$written_config_file"),
"pathCommand": $(json_or_null "$written_path_command"),
"globalBinPath": $(json_or_null "$GLOBAL_BIN_PATH")
}
METAEOF
chmod 600 "$METADATA_PATH"
}
setup_global_symlink() {
# Create a symlink in a system bin dir that is already on PATH (e.g.
# /usr/local/bin, or $PREFIX/bin on Termux) so `btch` works immediately in
# the current session without needing to source shell config. Only done
# when possible; the script falls back to the PATH hint otherwise.
GLOBAL_BIN_PATH=""
[[ "$TARGET" == windows-* ]] && return 0
local bin_dir=""
if is_termux && [[ -n "${PREFIX:-}" ]] && [[ -w "${PREFIX}/bin" ]]; then
# Termux keeps $PREFIX/bin (e.g. /data/data/com.termux/files/usr/bin) on PATH.
bin_dir="${PREFIX}/bin"
elif [[ -w /usr/local/bin ]]; then
bin_dir="/usr/local/bin"
fi
[[ -n "$bin_dir" ]] || return 0
if [[ -e "${bin_dir}/${BINARY_NAME}" ]] && [[ ! -L "${bin_dir}/${BINARY_NAME}" ]]; then
# A real file (not our symlink) already exists; don't clobber it.
return 0
fi
if ln -sf "${INSTALL_DIR}/${BINARY_NAME}" "${bin_dir}/${BINARY_NAME}" 2>/dev/null; then
GLOBAL_BIN_PATH="${bin_dir}/${BINARY_NAME}"
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
if command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "$1" | awk '{ print $NF }'
return
fi
echo "No SHA-256 tool found (expected sha256sum, shasum, or openssl)." >&2
exit 1
}
verify_checksum() {
local binary_file="$1" checksum_file="$2"
local expected actual
expected=$(awk -v asset="$ASSET_NAME" '$2 == asset || $2 == "*"asset { print $1 }' "$checksum_file")
if [[ -z "$expected" ]]; then
echo "Missing checksum for ${ASSET_NAME}" >&2
exit 1
fi
actual=$(sha256_file "$binary_file")
if [[ "$actual" != "$expected" ]]; then
echo "Checksum mismatch for ${ASSET_NAME}" >&2
exit 1
fi
}
add_to_path() {
local config_file="$1" command="$2"
if grep -Fxq "$command" "$config_file" 2>/dev/null; then
written_config_file="$config_file"
written_path_command="$command"
return
fi
printf '\n%s\n%s\n' "$PATH_MARKER" "$command" >> "$config_file"
written_config_file="$config_file"
written_path_command="$command"
}
maybe_update_path() {
if [[ "$no_modify_path" == true ]]; then return; fi
if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then return; fi
local current_shell config_files config_file path_command
local xdg_config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
current_shell=$(basename "${SHELL:-bash}")
case "$current_shell" in
fish)
config_files="$xdg_config_home/fish/config.fish"
path_command="fish_add_path $INSTALL_DIR"
;;
zsh)
config_files="${ZDOTDIR:-$HOME}/.zshrc ${ZDOTDIR:-$HOME}/.zshenv"
path_command="export PATH=$INSTALL_DIR:\$PATH"
;;
*)
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile"
path_command="export PATH=$INSTALL_DIR:\$PATH"
;;
esac
config_file=""
for file in $config_files; do
if [[ -f "$file" ]]; then
config_file="$file"
break
fi
done
if [[ -z "$config_file" ]]; then
echo "Add this to your shell config manually:" >&2
echo " export PATH=$INSTALL_DIR:\$PATH" >&2
return
fi
add_to_path "$config_file" "$path_command"
}
warn_if_prerelease() {
local version="$1"
case "$version" in
*-rc*|*-alpha*|*-beta*|*-pre*)
cat >&2 <<EOF
Warning: Installing pre-release version ${version}.
Pre-releases may be unstable. For the latest stable release, re-run with:
curl -fsSL https://raw.githubusercontent.com/${REPO}/main/install.sh | bash -s -- --version STABLE_VERSION
See https://github.com/${REPO}/releases for available versions.
EOF
;;
esac
}
resolve_release_version() {
if [[ -n "$requested_version" ]]; then
RESOLVED_VERSION="${requested_version}"
RELEASE_BASE_URL="https://github.com/${REPO}/releases/download/btch-cli@${RESOLVED_VERSION}"
return
fi
local tag
tag=$(curl -fsSL "${RELEASES_API}/latest" \
| sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' \
| head -n 1)
RESOLVED_VERSION="${tag#btch-cli@}"
if [[ -z "$RESOLVED_VERSION" ]]; then
echo "Failed to resolve the latest btch release version." >&2
exit 1
fi
RELEASE_BASE_URL="https://github.com/${REPO}/releases/latest/download"
warn_if_prerelease "$RESOLVED_VERSION"
}
install_downloaded_release() {
local tmp_dir binary_file checksum_file
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/btch-install.XXXXXX")
trap "rm -rf \"$tmp_dir\"" EXIT
binary_file="${tmp_dir}/${ASSET_NAME}"
checksum_file="${tmp_dir}/checksums.txt"
echo "Downloading ${ASSET_NAME}..."
curl -fSL "${RELEASE_BASE_URL}/${ASSET_NAME}" -o "$binary_file"
curl -fsSL "${RELEASE_BASE_URL}/checksums.txt" -o "$checksum_file"
verify_checksum "$binary_file" "$checksum_file"
cp "$binary_file" "${INSTALL_DIR}/${BINARY_NAME}"
[[ "$TARGET" != windows-* ]] && chmod 755 "${INSTALL_DIR}/${BINARY_NAME}"
}
install_local_binary() {
if [[ ! -f "$binary_path" ]]; then
echo "Binary not found at ${binary_path}" >&2
exit 1
fi
cp "$binary_path" "${INSTALL_DIR}/${BINARY_NAME}"
[[ "$TARGET" != windows-* ]] && chmod 755 "${INSTALL_DIR}/${BINARY_NAME}"
}
resolve_installed_version() {
INSTALLED_VERSION=$("${INSTALL_DIR}/${BINARY_NAME}" --version 2>/dev/null | tr -d '\r') || true
: "${INSTALLED_VERSION:=unknown}"
}
if is_termux; then
install_on_termux
fi
resolve_target
if [[ -n "$binary_path" ]]; then
install_local_binary
else
resolve_release_version
install_downloaded_release
fi
setup_global_symlink
maybe_update_path
resolve_installed_version
write_metadata "$INSTALLED_VERSION"
echo ""
echo "btch ${INSTALLED_VERSION} installed to ${INSTALL_DIR}/${BINARY_NAME}"
echo ""
echo "Run:"
echo " btch --help"
echo ""
if [[ "$TARGET" != windows-* ]] && ! command -v "${BINARY_NAME}" >/dev/null 2>&1; then
echo "Note: '${BINARY_NAME}' is not on your current shell's PATH yet."
echo "Either open a new terminal, or make it available right now with:"
echo ""
if [[ -n "$written_config_file" ]]; then
echo " source ${written_config_file}"
else
echo " export PATH=${INSTALL_DIR}:\$PATH"
fi
echo ""
fi
echo "To uninstall later:"
echo " btch uninstall"
echo ""