-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstall-prebuilt.sh
More file actions
executable file
·558 lines (478 loc) · 18.6 KB
/
Copy pathinstall-prebuilt.sh
File metadata and controls
executable file
·558 lines (478 loc) · 18.6 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
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/env bash
# xfetch - cross-platform system information fetcher
# Prebuilt installer — downloads a precompiled binary from GitHub Releases.
# Fast: no Rust toolchain, no compilation. Requires only curl (and tar/unzip).
# Usage: curl -fsSL https://raw.githubusercontent.com/xfetch-cli/xfetch/main/install-prebuilt.sh | bash
# bash install-prebuilt.sh --version 0.7.0
# bash install-prebuilt.sh --prefix /usr/local --yes
set -euo pipefail
IFS=$'\n\t'
# ──────────────────────────────────────────────
# Configuration
# ──────────────────────────────────────────────
REPO="xfetch-cli/xfetch"
REPO_URL="https://github.com/${REPO}"
REPO_API="https://api.github.com/repos/${REPO}"
REPO_RAW="https://raw.githubusercontent.com/${REPO}/main"
PROJECT="xfetch"
PROJECT_DESC="cross-platform system information fetcher (prebuilt binary)"
# Default paths (may be overridden by flags)
PREFIX="${PREFIX:-${HOME}/.local}"
BIN_DIR="${BIN_DIR:-${PREFIX}/bin}"
CONFIG_DIR="${CONFIG_DIR:-${HOME}/.config/${PROJECT}}"
# Behavior flags
FLAG_VERBOSE=1
FLAG_MODIFY_PATH=1
FLAG_YES=0
FLAG_SKIP_CONFIG=0
FLAG_NO_CHECKSUM=0
VERSION="" # empty = latest release
# Runtime
TEMP_DIR=""
EXISTING_CONFIG=0
OS_NAME=""
ARCH_NAME=""
TARGET_TRIPLE=""
# ──────────────────────────────────────────────
# Utility functions
# ──────────────────────────────────────────────
log() { printf '\033[1;34m[%s]\033[0m %s\n' "${PROJECT}" "$*"; }
ok() { printf '\033[1;32m[%s]\033[0m %s\n' "${PROJECT}" "$*"; }
warn() { printf '\033[1;33m[%s]\033[0m %s\n' "${PROJECT}" "$*" >&2; }
error() { printf '\033[1;31m[%s]\033[0m %s\n' "${PROJECT}" "$*" >&2; }
die() { error "$*"; exit 1; }
has_tty() { [ -t 0 ]; }
cleanup() {
local exit_code=$?
if [ -n "${TEMP_DIR}" ] && [ -d "${TEMP_DIR}" ]; then
rm -rf "${TEMP_DIR}" 2>/dev/null || true
fi
if [ "${exit_code}" -ne 0 ]; then
error "Installation failed (exit code: ${exit_code}). See errors above."
fi
exit "${exit_code}"
}
trap cleanup EXIT INT TERM HUP
# ──────────────────────────────────────────────
# OS / Architecture detection
# ──────────────────────────────────────────────
detect_os() {
case "$(uname -s)" in
Linux) echo "linux" ;;
Darwin) echo "macos" ;;
CYGWIN*|MINGW*|MSYS*) echo "windows" ;;
*) echo "unknown" ;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo "x86_64" ;;
aarch64|arm64) echo "aarch64" ;;
*) echo "unknown" ;;
esac
}
# True on musl-based Linux (Alpine, ...), where the glibc build cannot run.
is_musl() {
if [ -e /lib/ld-musl-x86_64.so.1 ] || [ -e /lib/ld-musl-aarch64.so.1 ]; then
return 0
fi
ldd --version 2>&1 | grep -qi musl
}
# Map detected OS/arch to the rust target triple used in release assets
resolve_target() {
case "${OS_NAME}-${ARCH_NAME}" in
linux-x86_64)
if is_musl; then
TARGET_TRIPLE="x86_64-unknown-linux-musl"
else
TARGET_TRIPLE="x86_64-unknown-linux-gnu"
fi
;;
linux-aarch64) TARGET_TRIPLE="aarch64-unknown-linux-gnu" ;;
macos-x86_64) TARGET_TRIPLE="x86_64-apple-darwin" ;;
macos-aarch64) TARGET_TRIPLE="aarch64-apple-darwin" ;;
windows-x86_64) die "On Windows use PowerShell: irm ${REPO_RAW}/install-prebuilt.ps1 | iex" ;;
*) die "No prebuilt binary for ${OS_NAME}-${ARCH_NAME}. Use install.sh (builds from source)." ;;
esac
[ "${FLAG_VERBOSE}" -eq 1 ] && log "Target: ${TARGET_TRIPLE}"
}
detect_shell_rc() {
local shell_name
shell_name="$(basename "${SHELL:-${HOME}}" 2>/dev/null || echo "bash")"
shell_name="${shell_name%%[0-9]*}"
case "${shell_name}" in
zsh)
if [ -n "${ZDOTDIR:-}" ]; then
echo "${ZDOTDIR}/.zshrc"
else
echo "${HOME}/.zshrc"
fi
;;
bash)
if [ "${OS_NAME}" = "macos" ]; then
echo "${HOME}/.bash_profile"
else
echo "${HOME}/.bashrc"
fi
;;
fish) echo "${HOME}/.config/fish/config.fish" ;;
*)
for rc in "${HOME}/.zshrc" "${HOME}/.bashrc" "${HOME}/.profile"; do
if [ -f "${rc}" ]; then
echo "${rc}"
return 0
fi
done
echo "${HOME}/.profile"
;;
esac
}
# ──────────────────────────────────────────────
# Argument parsing
# ──────────────────────────────────────────────
usage() {
cat <<EOF
${PROJECT} — ${PROJECT_DESC}
Usage:
curl -fsSL ${REPO_RAW}/install-prebuilt.sh | bash
bash install-prebuilt.sh [options]
Options:
-h, --help Show this help message and exit
-p, --prefix <dir> Installation prefix (default: \${HOME}/.local)
-b, --bin-dir <dir> Binary install directory (default: \${PREFIX}/bin)
-c, --config-dir <dir> Config directory (default: \${HOME}/.config/${PROJECT})
-n, --no-modify-path Do not modify shell config files to add to PATH
-y, --yes Automatic yes to all prompts
-s, --skip-config Skip generating the default config
-q, --quiet Quiet mode (minimal output)
--version <ver> Install a specific version (e.g. 0.7.0); default: latest release
--no-checksum Skip SHA256 checksum verification
Environment variables:
PREFIX Same as --prefix
BIN_DIR Same as --bin-dir
CONFIG_DIR Same as --config-dir
Examples:
# Quick install (latest release, no sudo needed)
curl -fsSL ${REPO_RAW}/install-prebuilt.sh | bash
# Install a specific version
curl -fsSL ${REPO_RAW}/install-prebuilt.sh | bash -s -- --version 0.7.0
# Non-interactive install
bash install-prebuilt.sh --yes
# System-wide install
bash install-prebuilt.sh --prefix /usr/local --yes
# Install without PATH modification
bash install-prebuilt.sh --no-modify-path
Report issues: ${REPO_URL}/issues
EOF
exit 0
}
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) usage ;;
-n|--no-modify-path) FLAG_MODIFY_PATH=0 ;;
-y|--yes) FLAG_YES=1 ;;
-s|--skip-config) FLAG_SKIP_CONFIG=1 ;;
-q|--quiet) FLAG_VERBOSE=0 ;;
--no-checksum) FLAG_NO_CHECKSUM=1 ;;
--version)
shift; VERSION="$1"
;;
-p|--prefix)
shift; PREFIX="$1"
[ -z "${BIN_DIR_OVERRIDE:-}" ] && BIN_DIR="${PREFIX}/bin"
;;
-b|--bin-dir)
shift; BIN_DIR="$1"; BIN_DIR_OVERRIDE=1
;;
-c|--config-dir)
shift; CONFIG_DIR="$1"
;;
--) shift; break ;;
-*)
die "Unknown option: $1. Use --help for usage."
;;
*) break ;;
esac
shift
done
}
# ──────────────────────────────────────────────
# Download and checksum
# ──────────────────────────────────────────────
resolve_asset_url() {
local release_url
if [ -z "${VERSION}" ]; then
release_url="${REPO_API}/releases/latest"
else
release_url="${REPO_API}/releases/tags/v${VERSION}"
fi
log "Resolving ${PROJECT} release..."
local json
json="$(curl -fsSL --max-time 30 "${release_url}")" || die "Cannot reach ${release_url}"
ASSET_URL="$(printf '%s' "${json}" \
| grep -o "\"browser_download_url\": *\"[^\"]*${TARGET_TRIPLE}[^\"]*\"" \
| head -n 1 | sed 's/.*"browser_download_url": *"//; s/"$//')"
if [ -z "${ASSET_URL}" ]; then
die "No prebuilt binary for ${TARGET_TRIPLE} in this release. Use install.sh (builds from source)."
fi
ASSET_FILE="${ASSET_URL##*/}"
if [ -z "${VERSION}" ]; then
VERSION="$(printf '%s' "${ASSET_FILE}" \
| sed "s/^${PROJECT}-//; s/-${TARGET_TRIPLE}\\.\\(tar\\.gz\\|zip\\)$//")"
fi
log "Found: ${ASSET_FILE} (v${VERSION})"
}
download_and_verify() {
TEMP_DIR="$(mktemp -d)"
log "Downloading ${ASSET_FILE}..."
curl -fSL --max-time 300 "${ASSET_URL}" -o "${TEMP_DIR}/${ASSET_FILE}" \
|| die "Download failed: ${ASSET_URL}"
if [ "${FLAG_NO_CHECKSUM}" -eq 1 ]; then
warn "Skipping checksum verification (--no-checksum)."
return 0
fi
local sums_url="${REPO_URL}/releases/download/v${VERSION}/SHA256SUMS"
if curl -fsSL --max-time 30 "${sums_url}" -o "${TEMP_DIR}/SHA256SUMS" 2>/dev/null; then
local expected actual
expected="$(grep "${ASSET_FILE}" "${TEMP_DIR}/SHA256SUMS" | awk '{print $1}')"
if [ -z "${expected}" ]; then
warn "No checksum found for ${ASSET_FILE} in SHA256SUMS."
return 0
fi
actual="$(sha256sum "${TEMP_DIR}/${ASSET_FILE}" | awk '{print $1}')"
if [ "${expected}" != "${actual}" ]; then
die "Checksum mismatch for ${ASSET_FILE}. Refusing to install."
fi
ok "Checksum verified (${actual:0:12}...)."
else
warn "Could not fetch ${sums_url}. Skipping checksum verification."
fi
}
extract_binary() {
local archive="${TEMP_DIR}/${ASSET_FILE}"
case "${ASSET_FILE}" in
*.tar.gz) tar -xzf "${archive}" -C "${TEMP_DIR}" ;;
*.zip) unzip -q "${archive}" -d "${TEMP_DIR}" ;;
*) die "Unsupported archive format: ${ASSET_FILE}" ;;
esac
BINARY_SRC="${TEMP_DIR}/${PROJECT}"
if [ ! -f "${BINARY_SRC}" ]; then
die "Binary not found inside ${ASSET_FILE}. Expected: ${PROJECT}"
fi
}
# ──────────────────────────────────────────────
# Install
# ──────────────────────────────────────────────
install_binary() {
mkdir -p "${BIN_DIR}"
# Stage next to the destination and rename: a failed copy must never
# truncate an existing installation.
local tmp_dest="${BIN_DIR}/.${PROJECT}.xfetch-tmp-$$"
if command -v install >/dev/null 2>&1; then
install -m 755 "${BINARY_SRC}" "${tmp_dest}" || { rm -f "${tmp_dest}"; die "Failed to copy the binary into ${BIN_DIR}."; }
else
cp "${BINARY_SRC}" "${tmp_dest}" || { rm -f "${tmp_dest}"; die "Failed to copy the binary into ${BIN_DIR}."; }
chmod 755 "${tmp_dest}"
fi
mv -f "${tmp_dest}" "${BIN_DIR}/${PROJECT}"
ok "Installed binary: ${BIN_DIR}/${PROJECT}"
}
install_config() {
if [ "${FLAG_SKIP_CONFIG}" -eq 1 ]; then
log "Skipping config generation (--skip-config)."
return 0
fi
mkdir -p "${CONFIG_DIR}"
if [ -f "${CONFIG_DIR}/config.jsonc" ]; then
EXISTING_CONFIG=1
warn "Config already exists at ${CONFIG_DIR}/config.jsonc — not overwriting."
else
if "${BIN_DIR}/${PROJECT}" --gen-config >/dev/null 2>&1; then
ok "Generated config at ${CONFIG_DIR}/config.jsonc"
else
warn "Could not generate config; skipping."
fi
fi
if [ "${OS_NAME}" = "macos" ]; then
local mac_support="${HOME}/Library/Application Support/${PROJECT}"
if [ ! -e "${mac_support}" ]; then
ln -sf "${CONFIG_DIR}" "${mac_support}"
ok "Created macOS config symlink: ${mac_support} -> ${CONFIG_DIR}"
fi
fi
}
# ──────────────────────────────────────────────
# PATH modification
# ──────────────────────────────────────────────
ensure_path_in_file() {
local file="$1"
local path_line="$2"
local comment="$3"
if [ ! -f "${file}" ]; then
mkdir -p "$(dirname "${file}")"
touch "${file}"
fi
if grep -qsF "# ${comment}" "${file}" 2>/dev/null; then
[ "${FLAG_VERBOSE}" -eq 1 ] && ok "PATH already configured in ${file}"
return 0
fi
printf '\n# %s\n%s\n' "${comment}" "${path_line}" >> "${file}"
ok "Added ${BIN_DIR} to PATH in ${file}"
}
modify_path() {
local primary_rc
primary_rc="$(detect_shell_rc)"
local fish_rc="${HOME}/.config/fish/config.fish"
local -a rc_list=()
if [ "${OS_NAME}" = "macos" ]; then
rc_list=("${HOME}/.bash_profile" "${HOME}/.zprofile" "${HOME}/.zshrc")
[ -f "${HOME}/.bashrc" ] && rc_list+=("${HOME}/.bashrc")
else
rc_list=("${HOME}/.bashrc" "${HOME}/.zshrc" "${HOME}/.profile")
[ -f "${HOME}/.bash_profile" ] && rc_list+=("${HOME}/.bash_profile")
fi
if [ -f "${fish_rc}" ] || [ "${primary_rc}" = "${fish_rc}" ]; then
rc_list+=("${fish_rc}")
fi
local comment="${PROJECT} path"
local path_line
for rc in "${rc_list[@]}"; do
if [ -f "${rc}" ] || [ "${rc}" = "${primary_rc}" ]; then
if [ "${rc}" = "${fish_rc}" ]; then
path_line="fish_add_path ${BIN_DIR}"
else
path_line="export PATH=\"${BIN_DIR}:\$PATH\""
fi
ensure_path_in_file "${rc}" "${path_line}" "${comment}"
fi
done
if [ "${primary_rc}" = "${fish_rc}" ]; then
ok "To add ${BIN_DIR} to your current session, run: fish_add_path ${BIN_DIR}"
else
ok "To add ${BIN_DIR} to your current session, run: source ${primary_rc}"
fi
}
# ──────────────────────────────────────────────
# Verification / Summary
# ──────────────────────────────────────────────
verify_installation() {
local binary="${BIN_DIR}/${PROJECT}"
if [ ! -f "${binary}" ]; then
error "Binary not found at ${binary}"
return 1
fi
if [ ! -x "${binary}" ]; then
error "Binary is not executable: ${binary}"
return 1
fi
if "${binary}" --version >/dev/null 2>&1; then
local version_output
version_output="$("${binary}" --version 2>&1)"
ok "Verified: ${version_output}"
else
warn "Binary installed at ${binary} but could not verify version."
fi
return 0
}
print_summary() {
cat <<EOF
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
${PROJECT} — Installation Complete (prebuilt)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
OS: ${OS_NAME} (${ARCH_NAME})
Version: v${VERSION}
Binary: ${BIN_DIR}/${PROJECT}
Config: ${CONFIG_DIR}/
EOF
if [ "${EXISTING_CONFIG}" -eq 1 ]; then
cat <<EOF
Existing config preserved at ${CONFIG_DIR}/config.jsonc
EOF
fi
if [ "${FLAG_MODIFY_PATH}" -eq 1 ]; then
local shell_rc
shell_rc="$(detect_shell_rc)"
if [ "${shell_rc}" = "${HOME}/.config/fish/config.fish" ]; then
cat <<EOF
PATH updated in: ${shell_rc}
Restart your terminal (or run 'fish_add_path ${BIN_DIR}') to use ${PROJECT}.
EOF
else
cat <<EOF
PATH updated in: ${shell_rc}
Restart your terminal or run 'source ${shell_rc}' to use ${PROJECT}.
EOF
fi
else
cat <<EOF
PATH not modified. Add ${BIN_DIR} to your PATH manually:
export PATH="${BIN_DIR}:\$PATH"
EOF
fi
cat <<EOF
${PROJECT} is ready! Run it:
${PROJECT}
For configuration help:
${PROJECT} --help
${REPO_URL}
To uninstall:
rm -f "${BIN_DIR}/${PROJECT}"
rm -rf "${CONFIG_DIR}"
Uninstall script available at:
${REPO_RAW}/uninstall.sh
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EOF
}
# ──────────────────────────────────────────────
# Main
# ──────────────────────────────────────────────
main() {
parse_args "$@"
OS_NAME="$(detect_os)"
ARCH_NAME="$(detect_arch)"
resolve_target
log "Installing ${PROJECT} (prebuilt) on ${OS_NAME} (${ARCH_NAME})"
if [ "${EUID:-${UID}}" = "0" ] && [ "${FLAG_YES}" -eq 0 ]; then
warn "Running as root is not recommended for local installs."
if has_tty; then
printf "[%s] Continue as root? [y/N]: " "${PROJECT}"
read -r response
case "${response}" in
y|Y|yes) ;;
*) die "Aborted. Run as a normal user, or use --yes to skip this check." ;;
esac
else
die "Running as root without an interactive terminal. Re-run with --yes to skip this check."
fi
fi
if ! command -v curl >/dev/null 2>&1; then
die "curl is required. Install it and try again."
fi
if ! command -v sha256sum >/dev/null 2>&1; then
warn "sha256sum not found; checksum verification will be skipped."
FLAG_NO_CHECKSUM=1
fi
case "${OS_NAME}" in
linux) command -v tar >/dev/null 2>&1 || die "tar is required." ;;
macos) command -v tar >/dev/null 2>&1 || die "tar is required." ;;
esac
if ! mkdir -p "${BIN_DIR}" 2>/dev/null; then
die "Cannot write to ${BIN_DIR}. Check HOME permissions."
fi
resolve_asset_url
download_and_verify
extract_binary
install_binary
install_config
if [ "${FLAG_MODIFY_PATH}" -eq 1 ]; then
modify_path
else
log "Skipping PATH modification (--no-modify-path)."
fi
rm -rf "${TEMP_DIR}" 2>/dev/null || true
TEMP_DIR=""
verify_installation || die "Installation verification failed."
print_summary
}
main "$@"