-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·135 lines (109 loc) · 4.76 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·135 lines (109 loc) · 4.76 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
#!/usr/bin/env bash
# Install script for Zigzag — TUI project manager for Zellij.
# Usage: curl -fsSL https://raw.githubusercontent.com/arkan/zigzag/main/install.sh | bash
set -euo pipefail
REPO="arkan/zigzag"
BINARY="zigzag"
INSTALL_DIR="${ZIGZAG_INSTALL_DIR:-${HOME}/.local/bin}"
# ── helpers ──────────────────────────────────────────────────────────────────
info() { printf '\033[1;34m%s\033[0m\n' "$*"; }
ok() { printf '\033[1;32m%s\033[0m\n' "$*"; }
err() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; }
need() {
command -v "$1" >/dev/null 2>&1 || err "'$1' is required but not found"
}
# ── detect platform ─────────────────────────────────────────────────────────
detect_target() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux) os="unknown-linux-gnu" ;;
Darwin) os="apple-darwin" ;;
*) err "unsupported OS: $os" ;;
esac
case "$arch" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) err "unsupported architecture: $arch" ;;
esac
echo "${arch}-${os}"
}
# ── resolve version ─────────────────────────────────────────────────────────
resolve_version() {
local version="${ZIGZAG_VERSION:-latest}"
if [ "$version" = "latest" ]; then
need curl
version="$(curl -fsSL -H "Accept: application/json" \
"https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' | head -1 | sed 's/.*"tag_name": *"//;s/".*//')"
[ -n "$version" ] || err "could not determine latest release"
fi
echo "$version"
}
# ── download & verify ───────────────────────────────────────────────────────
download_and_install() {
local version="$1" target="$2"
local tag_version="${version#v}"
local archive="zigzag-v${tag_version}-${target}.tar.gz"
local url="https://github.com/${REPO}/releases/download/${version}/${archive}"
local checksums_url="https://github.com/${REPO}/releases/download/${version}/checksums-sha256.txt"
local tmpdir
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
info "downloading ${archive}…"
curl -fsSL -o "${tmpdir}/${archive}" "$url" \
|| err "download failed — does release ${version} exist for ${target}?"
# verify checksum if sha256sum or shasum is available
if command -v sha256sum >/dev/null 2>&1 || command -v shasum >/dev/null 2>&1; then
info "verifying checksum…"
curl -fsSL -o "${tmpdir}/checksums-sha256.txt" "$checksums_url" 2>/dev/null || true
if [ -f "${tmpdir}/checksums-sha256.txt" ]; then
local expected
expected="$(grep "${archive}" "${tmpdir}/checksums-sha256.txt" | awk '{print $1}')"
if [ -n "$expected" ]; then
local actual
if command -v sha256sum >/dev/null 2>&1; then
actual="$(sha256sum "${tmpdir}/${archive}" | awk '{print $1}')"
else
actual="$(shasum -a 256 "${tmpdir}/${archive}" | awk '{print $1}')"
fi
[ "$actual" = "$expected" ] || err "checksum mismatch (expected ${expected}, got ${actual})"
ok "checksum verified ✓"
fi
fi
fi
info "extracting to ${INSTALL_DIR}…"
mkdir -p "$INSTALL_DIR"
tar xzf "${tmpdir}/${archive}" -C "$tmpdir"
install -m 755 "${tmpdir}/${BINARY}" "${INSTALL_DIR}/${BINARY}"
ok "installed ${BINARY} ${version} to ${INSTALL_DIR}/${BINARY}"
echo ""
echo "Optional short alias: add 'alias z=zigzag' to your shell profile."
}
# ── PATH check ──────────────────────────────────────────────────────────────
check_path() {
case ":${PATH}:" in
*":${INSTALL_DIR}:"*) ;;
*)
printf '\n\033[1;33mwarning:\033[0m %s is not in your PATH.\n' "$INSTALL_DIR"
echo "Add it with:"
echo ""
echo " export PATH=\"${INSTALL_DIR}:\$PATH\""
echo ""
;;
esac
}
# ── main ─────────────────────────────────────────────────────────────────────
main() {
need curl
need tar
local target version
target="$(detect_target)"
version="$(resolve_version)"
info "platform: ${target}"
info "version: ${version}"
download_and_install "$version" "$target"
check_path
}
main