-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
251 lines (208 loc) · 5.58 KB
/
install.sh
File metadata and controls
251 lines (208 loc) · 5.58 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
#!/usr/bin/env bash
set -euo pipefail
REPO="rasros/lx"
# Allow overriding version: VERSION=v2.0.0-rc.1 ./install.sh
target_version="${VERSION:-latest}"
# Determine install directory
if [ -n "${LX_INSTALL_DIR:-}" ]; then
INSTALL_DIR="$LX_INSTALL_DIR"
elif [ "$(id -u)" -eq 0 ]; then
INSTALL_DIR="/usr/local/bin"
else
INSTALL_DIR="$HOME/.local/bin"
fi
log() {
echo "[lx install] $*" >&2
}
detect_os() {
local u
u="$(uname -s)"
case "$u" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
MINGW*|MSYS*|CYGWIN*) echo "windows" ;;
*)
log "Unsupported OS: $u"
exit 1
;;
esac
}
detect_arch() {
local a
a="$(uname -m)"
case "$a" in
x86_64|amd64) echo "amd64" ;;
aarch64|arm64) echo "arm64" ;;
*)
log "Unsupported architecture: $a"
exit 1
;;
esac
}
ensure_deps() {
if ! command -v curl >/dev/null 2>&1; then
log "curl is required but not found"
exit 1
fi
if ! command -v grep >/dev/null 2>&1; then
log "grep is required but not found"
exit 1
fi
# Detect hashing tool
if command -v sha256sum >/dev/null 2>&1; then
HASHER="sha256sum"
elif command -v shasum >/dev/null 2>&1; then
HASHER="shasum -a 256"
else
log "sha256sum or shasum is required for verification"
exit 1
fi
}
ensure_unpack_tool() {
local os="$1"
if [ "$os" = "windows" ]; then
if ! command -v unzip >/dev/null 2>&1; then
log "unzip is required to extract release archives"
exit 1
fi
else
if ! command -v tar >/dev/null 2>&1; then
log "tar is required to extract release archives"
exit 1
fi
fi
}
get_release_tag() {
if [ "$target_version" != "latest" ]; then
if [[ "$target_version" != v* ]]; then
echo "v$target_version"
else
echo "$target_version"
fi
return
fi
local api_latest="https://api.github.com/repos/${REPO}/releases/latest"
local tag
local raw_output
# We use grep without -m1 to ensure we consume the whole stream.
# This prevents curl from erroring out with code 23 (failed writing body).
raw_output="$(curl -fsSL "$api_latest" | grep '"tag_name"')"
tag="$(echo "$raw_output" | head -n 1 | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')"
if [ -z "$tag" ]; then
log "No stable release found, checking for pre-releases..."
local api_tags="https://api.github.com/repos/${REPO}/tags"
# Same robustness fix for tags endpoint
raw_output="$(curl -fsSL "$api_tags" | grep '"name"')"
tag="$(echo "$raw_output" | head -n 1 | sed -E 's/.*"name": *"([^"]+)".*/\1/')"
fi
if [ -z "$tag" ]; then
log "Failed to determine latest release tag"
exit 1
fi
echo "$tag"
}
ensure_install_dir() {
if [ ! -d "$INSTALL_DIR" ]; then
log "Creating install directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
fi
}
check_path() {
case ":$PATH:" in
*":$INSTALL_DIR:"*) return 0 ;;
*)
log "Warning: $INSTALL_DIR is not in your PATH."
log "Add this to your shell config:"
log " export PATH=\"$INSTALL_DIR:\$PATH\""
;;
esac
}
verify_checksum() {
local file="$1"
local checksums_path="$2"
local filename
filename="$(basename "$file")"
log "Verifying checksum for $filename..."
# Extract expected hash from checksums.txt
# Matches lines like: "hash filename"
local expected
expected="$(grep "$filename" "$checksums_path" | awk '{print $1}')"
if [ -z "$expected" ]; then
log "Error: No checksum found for $filename in checksums.txt"
exit 1
fi
# Calculate actual hash
local actual
actual="$($HASHER "$file" | awk '{print $1}')"
if [ "$actual" != "$expected" ]; then
log "Checksum verification failed!"
log "Expected: $expected"
log "Actual: $actual"
exit 1
fi
log "Checksum verified successfully."
}
main() {
ensure_deps
os="$(detect_os)"
arch="$(detect_arch)"
ensure_unpack_tool "$os"
tag="$(get_release_tag)"
version="${tag#v}"
log "Resolving version: $tag ($os/$arch)"
archive_base="lx_${version}_${os}_${arch}"
ext=".tar.gz"
if [ "$os" = "windows" ]; then
ext=".zip"
fi
archive_file="${archive_base}${ext}"
# Base download URL
download_base="https://github.com/${REPO}/releases/download/${tag}"
url="${download_base}/${archive_file}"
checksum_url="${download_base}/checksums.txt"
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
archive_path="${tmpdir}/${archive_file}"
checksum_path="${tmpdir}/checksums.txt"
# 1. Download Archive
log "Downloading $url"
if ! curl -fL "$url" -o "$archive_path"; then
log "Download failed. Check if version '$tag' exists for $os/$arch."
exit 1
fi
# 2. Download Checksums
log "Downloading checksums..."
if ! curl -fL "$checksum_url" -o "$checksum_path"; then
log "Failed to download checksums.txt. Cannot verify integrity."
exit 1
fi
# 3. Verify
verify_checksum "$archive_path" "$checksum_path"
# 4. Extract
if [ "$os" = "windows" ]; then
(cd "$tmpdir" && unzip -q "$archive_path")
else
(cd "$tmpdir" && tar -xzf "$archive_path")
fi
bin_name="lx-${version}-${os}-${arch}"
if [ "$os" = "windows" ]; then
bin_name="${bin_name}.exe"
fi
src_bin="${tmpdir}/${bin_name}"
if [ ! -f "$src_bin" ]; then
log "Binary not found in archive: $bin_name"
exit 1
fi
ensure_install_dir
chmod +x "$src_bin"
# Check if we need sudo for the move
if [ ! -w "$INSTALL_DIR" ]; then
log "Escalating privileges to write to $INSTALL_DIR (sudo)"
sudo mv "$src_bin" "$INSTALL_DIR/lx"
else
mv "$src_bin" "$INSTALL_DIR/lx"
fi
check_path
log "Successfully installed lx ${version} to $INSTALL_DIR/lx"
}
main "$@"