-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·99 lines (87 loc) · 3.11 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·99 lines (87 loc) · 3.11 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
#!/bin/sh
# shctx installer: curl -fsSL https://raw.githubusercontent.com/adrsimon/shctx/main/install.sh | sh
#
# Downloads the latest release binary for this OS/arch, verifies its checksum,
# installs it to ~/.local/bin, and wires an idempotent block into the rc file
# of your login shell. Override the shell with SHCTX_SHELL=zsh|bash|fish.
set -eu
REPO="adrsimon/shctx"
BIN="shctx"
INSTALL_DIR="${SHCTX_INSTALL_DIR:-$HOME/.local/bin}"
say() { printf '%s\n' "$*" >&2; }
die() { say "install: $*"; exit 1; }
# --- detect the user's shell ---
# $SHELL, not the interpreter running this script: curl | sh runs under sh.
shell_name="${SHCTX_SHELL:-$(basename "${SHELL:-zsh}")}"
case "$shell_name" in
zsh) rc="${ZDOTDIR:-$HOME}/.zshrc" ;;
bash) rc="$HOME/.bashrc" ;;
fish) rc="${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/shctx.fish" ;;
*) die "unsupported shell: $shell_name (supported: zsh, bash, fish; override with SHCTX_SHELL=...)" ;;
esac
# --- detect platform ---
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$os" in
darwin|linux) ;;
*) die "unsupported OS: $os" ;;
esac
case "$arch" in
x86_64|amd64) arch=amd64 ;;
arm64|aarch64) arch=arm64 ;;
*) die "unsupported architecture: $arch" ;;
esac
# --- fetch latest release asset ---
asset="${BIN}_${os}_${arch}.tar.gz"
base="https://github.com/$REPO/releases/latest/download"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
say "downloading $asset ..."
curl -fsSL "$base/$asset" -o "$tmp/$asset" || die "download failed ($base/$asset)"
curl -fsSL "$base/checksums.txt" -o "$tmp/checksums.txt" || die "checksum download failed"
# --- verify checksum ---
(
cd "$tmp"
expected=$(grep " $asset\$" checksums.txt | awk '{print $1}')
[ -n "$expected" ] || die "no checksum entry for $asset"
if command -v sha256sum >/dev/null 2>&1; then
actual=$(sha256sum "$asset" | awk '{print $1}')
else
actual=$(shasum -a 256 "$asset" | awk '{print $1}')
fi
[ "$expected" = "$actual" ] || die "checksum mismatch for $asset"
)
say "checksum OK"
# --- install binary ---
mkdir -p "$INSTALL_DIR"
tar -xzf "$tmp/$asset" -C "$tmp" "$BIN"
install -m 0755 "$tmp/$BIN" "$INSTALL_DIR/$BIN"
say "installed $INSTALL_DIR/$BIN"
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*) say "warning: $INSTALL_DIR is not on your PATH, add it to your shell rc" ;;
esac
# --- wire the rc file with an idempotent marked block ---
begin="# >>> shctx >>>"
end="# <<< shctx <<<"
if [ "$shell_name" = fish ]; then
evalline="$BIN init fish | source"
else
evalline="eval \"\$($BIN init $shell_name)\""
fi
mkdir -p "$(dirname "$rc")"
touch "$rc"
if grep -qF "$begin" "$rc"; then
# replace the existing block in place (BSD awk: no newlines in -v values)
awk -v begin="$begin" -v end="$end" -v evalline="$evalline" '
$0 == begin { skipping = 1; print begin; print evalline; print end; next }
$0 == end { skipping = 0; next }
!skipping { print }
' "$rc" > "$rc.shctx.tmp" && mv "$rc.shctx.tmp" "$rc"
say "updated shctx block in $rc"
else
printf '\n%s\n%s\n%s\n' "$begin" "$evalline" "$end" >> "$rc"
say "added shctx block to $rc"
fi
say ""
say "done, restart your shell or run: source $rc"