-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·179 lines (148 loc) · 4.41 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·179 lines (148 loc) · 4.41 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
#!/usr/bin/env bash
# Garyx installer — downloads the latest release binary with checksum verification.
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/Pyiner/garyx/main/install.sh | bash
#
# Options (env vars):
# GARYX_VERSION — specific version to install (default: latest)
set -euo pipefail
REPO="Pyiner/garyx"
INSTALL_DIR="/usr/local/bin"
main() {
check_deps
local version="${GARYX_VERSION:-}"
local target
target="$(detect_target)"
echo "Detected platform: ${target}"
if [ -z "$version" ]; then
version="$(latest_version)"
fi
if [ -z "$version" ]; then
die "Could not determine latest version. Set GARYX_VERSION or check https://github.com/${REPO}/releases"
fi
echo "Installing garyx ${version}..."
local base_url="https://github.com/${REPO}/releases/download/v${version}"
local archive="garyx-${version}-${target}.tar.gz"
local tmpdir
tmpdir="$(mktemp -d)"
trap 'rm -rf "${tmpdir:-}"' EXIT
echo "Downloading ${archive}..."
curl -fsSL "${base_url}/${archive}" -o "${tmpdir}/${archive}"
curl -fsSL "${base_url}/${archive}.sha256" -o "${tmpdir}/${archive}.sha256"
echo "Verifying checksum..."
verify_checksum "${tmpdir}/${archive}" "${tmpdir}/${archive}.sha256"
tar xzf "${tmpdir}/${archive}" -C "$tmpdir"
install_binary "${tmpdir}/garyx-${version}-${target}/garyx" "${INSTALL_DIR}/garyx"
local installed_cctty=0
if [ -f "${tmpdir}/garyx-${version}-${target}/cctty" ]; then
install_binary "${tmpdir}/garyx-${version}-${target}/cctty" "${INSTALL_DIR}/cctty"
installed_cctty=1
fi
echo ""
echo "Installed garyx to ${INSTALL_DIR}/garyx"
if [ "$installed_cctty" -eq 1 ]; then
echo "Installed cctty to ${INSTALL_DIR}/cctty"
fi
echo ""
if ! path_contains "$INSTALL_DIR"; then
echo "Add to your PATH:"
echo ""
echo " export PATH=\"${INSTALL_DIR}:\$PATH\""
echo ""
fi
echo "Run 'garyx --version' to verify."
}
check_deps() {
for cmd in curl tar mktemp; do
if ! command -v "$cmd" >/dev/null 2>&1; then
die "Required command '${cmd}' not found. Please install it first."
fi
done
}
detect_target() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Darwin)
case "$arch" in
arm64|aarch64) echo "aarch64-apple-darwin" ;;
x86_64) echo "x86_64-apple-darwin" ;;
*) die "Unsupported macOS architecture: ${arch}" ;;
esac
;;
Linux)
case "$arch" in
x86_64|amd64) echo "x86_64-unknown-linux-gnu" ;;
aarch64|arm64) echo "aarch64-unknown-linux-gnu" ;;
*) die "Unsupported Linux architecture: ${arch}" ;;
esac
;;
*) die "Unsupported OS: ${os}" ;;
esac
}
latest_version() {
local ver
ver="$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' \
| head -1 \
| sed 's/.*"v\([^"]*\)".*/\1/')" || true
echo "$ver"
}
verify_checksum() {
local file="$1" sha_file="$2"
local expected actual
expected="$(awk '{print $1}' "$sha_file")"
if [ -z "$expected" ]; then
die "Checksum file is empty or malformed."
fi
if command -v sha256sum >/dev/null 2>&1; then
actual="$(sha256sum "$file" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
actual="$(shasum -a 256 "$file" | awk '{print $1}')"
else
die "sha256sum or shasum is required for checksum verification."
fi
if [ "$expected" != "$actual" ]; then
die "Checksum mismatch!\n Expected: ${expected}\n Actual: ${actual}\nThe download may be corrupted."
fi
echo "Checksum OK."
}
path_contains() {
local dir="$1"
echo "$PATH" | tr ':' '\n' | grep -qx "$dir"
}
can_write_install_dir() {
local dir="$1"
if [ -d "$dir" ]; then
[ -w "$dir" ]
return
fi
local parent
parent="$(dirname "$dir")"
[ -d "$parent" ] && [ -w "$parent" ]
}
install_binary() {
local source="$1" destination="$2" dir
dir="$(dirname "$destination")"
if can_write_install_dir "$dir" && { [ ! -e "$destination" ] || [ -w "$destination" ]; }; then
mkdir -p "$dir"
cp "$source" "$destination"
chmod +x "$destination"
return
fi
if command -v sudo >/dev/null 2>&1; then
echo "Installing to ${dir} requires elevated permissions."
sudo mkdir -p "$dir"
sudo cp "$source" "$destination"
sudo chmod +x "$destination"
return
fi
die "Install directory is not writable and sudo is unavailable: ${dir}"
}
die() {
echo "Error: $*" >&2
exit 1
}
main "$@"