-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathinstall.sh
More file actions
281 lines (234 loc) · 7.84 KB
/
Copy pathinstall.sh
File metadata and controls
281 lines (234 loc) · 7.84 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
#!/bin/sh
# Install script for ZeroFS
set -e
REPO_OWNER="Barre"
REPO_NAME="ZeroFS"
BINARY_NAME="zerofs"
TARBALL_NAME="zerofs-pgo-multiplatform.tar.gz"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
SKIP_CHECKSUM="${SKIP_CHECKSUM:-false}"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() {
printf "${GREEN}[INFO]${NC} %s\n" "$1"
}
warn() {
printf "${YELLOW}[WARN]${NC} %s\n" "$1"
}
error() {
printf "${RED}[ERROR]${NC} %s\n" "$1" >&2
exit 1
}
# Detect OS
detect_os() {
case "$(uname -s)" in
Darwin*)
echo "darwin"
;;
Linux*)
echo "linux"
;;
FreeBSD*)
echo "freebsd"
;;
*)
error "Unsupported operating system: $(uname -s)"
;;
esac
}
detect_arch() {
local os="$1"
arch="$(uname -m)"
case "$arch" in
x86_64|amd64)
if [ "$os" = "darwin" ]; then
echo "x86_64"
else
echo "amd64"
fi
;;
aarch64|arm64)
if [ "$os" = "darwin" ]; then
echo "aarch64"
else
echo "arm64"
fi
;;
*)
error "No prebuilt binary for architecture: $arch (build from source with 'cargo build --release')"
;;
esac
}
get_download_url() {
local version="${1:-latest}"
if [ "$version" = "latest" ]; then
echo "https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/latest/download/${TARBALL_NAME}"
else
echo "https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${version}/${TARBALL_NAME}"
fi
}
get_binary_filename() {
local os="$1"
local arch="$2"
echo "${BINARY_NAME}-${os}-${arch}-pgo"
}
verify_checksum() {
local tarball="$1"
local checksum_url="$2"
local tmp_dir="$3"
if [ "$SKIP_CHECKSUM" = "true" ]; then
warn "Skipping checksum verification (not recommended)"
return 0
fi
local checksum_file="$tmp_dir/checksum.sha256"
info "Downloading checksum file..."
if command -v curl >/dev/null 2>&1; then
if ! curl -fSL "$checksum_url" -o "$checksum_file"; then
warn "Failed to download checksum file, skipping verification"
return 0
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -q "$checksum_url" -O "$checksum_file"; then
warn "Failed to download checksum file, skipping verification"
return 0
fi
fi
info "Verifying checksum..."
cd "$tmp_dir" || exit 1
local base_tarball
base_tarball="$(basename "$tarball")"
if command -v sha256sum >/dev/null 2>&1; then
if sha256sum -c "$checksum_file" --status; then
info "Checksum verification passed"
else
error "Checksum verification failed! The downloaded file may be corrupted or tampered with."
fi
elif command -v shasum >/dev/null 2>&1; then
if shasum -a 256 -c "$checksum_file" --status; then
info "Checksum verification passed"
else
error "Checksum verification failed! The downloaded file may be corrupted or tampered with."
fi
else
warn "No SHA256 tool found (sha256sum or shasum), skipping checksum verification"
fi
cd - >/dev/null || exit 1
}
install_binary() {
local os="$1"
local arch="$2"
local version="${VERSION:-latest}"
info "Detected OS: $os"
info "Detected Architecture: $arch"
info "Installing ${BINARY_NAME}..."
if [ ! -d "$INSTALL_DIR" ]; then
if mkdir -p "$INSTALL_DIR" 2>/dev/null; then
info "Created install directory: $INSTALL_DIR"
else
warn "Cannot create ${INSTALL_DIR}, trying fallback locations..."
if [ -d "/usr/local/bin" ] && [ -w "/usr/local/bin" ]; then
INSTALL_DIR="/usr/local/bin"
info "Using fallback directory: $INSTALL_DIR"
elif [ -d "/usr/bin" ]; then
INSTALL_DIR="/usr/bin"
info "Using fallback directory: $INSTALL_DIR (may require sudo)"
else
error "No suitable install directory found. Please specify with: INSTALL_DIR=\$HOME/.local/bin"
fi
fi
fi
local download_url
download_url="$(get_download_url "$version")"
local checksum_url="${download_url}.sha256"
local binary_filename
binary_filename="$(get_binary_filename "$os" "$arch")"
info "Downloading from: $download_url"
info "Looking for binary: $binary_filename"
local tmp_dir
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT
local tarball="$tmp_dir/${TARBALL_NAME}"
if command -v curl >/dev/null 2>&1; then
if ! curl -fSL "$download_url" -o "$tarball"; then
error "Failed to download tarball. Please check your internet connection."
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -q "$download_url" -O "$tarball"; then
error "Failed to download tarball. Please check your internet connection."
fi
else
error "Neither curl nor wget found. Please install one of them and try again."
fi
verify_checksum "$tarball" "$checksum_url" "$tmp_dir"
info "Extracting tarball..."
if ! tar -xzf "$tarball" -C "$tmp_dir"; then
error "Failed to extract tarball"
fi
if [ ! -f "$tmp_dir/$binary_filename" ]; then
error "Binary $binary_filename not found in tarball. Your platform may not be supported."
fi
chmod +x "$tmp_dir/$binary_filename"
local source_binary="$tmp_dir/$binary_filename"
local dest_binary="$INSTALL_DIR/$BINARY_NAME"
if [ -w "$INSTALL_DIR" ]; then
mv "$source_binary" "$dest_binary"
info "Successfully installed ${BINARY_NAME} to ${dest_binary}"
else
if command -v sudo >/dev/null 2>&1; then
warn "Installation to ${INSTALL_DIR} requires elevated privileges"
info "Using sudo to install..."
if sudo mv "$source_binary" "$dest_binary"; then
info "Successfully installed ${BINARY_NAME} to ${dest_binary}"
else
error "Failed to install with sudo. Please check your permissions."
fi
else
error "No write permission to ${INSTALL_DIR} and sudo is not available. Try: INSTALL_DIR=\$HOME/.local/bin sh install.sh"
fi
fi
}
check_path() {
case "$INSTALL_DIR" in
/usr/local/bin|/usr/bin|/bin)
info "${INSTALL_DIR} is a standard system directory"
;;
*)
# For user directories, check PATH
if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then
warn "${INSTALL_DIR} is not in your PATH"
info "Add the following to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
echo ""
echo " export PATH=\"${INSTALL_DIR}:\$PATH\""
echo ""
fi
;;
esac
}
verify_installation() {
local binary_path="$INSTALL_DIR/$BINARY_NAME"
if [ -f "$binary_path" ] && [ -x "$binary_path" ]; then
info "Installation verified!"
if "$binary_path" --version >/dev/null 2>&1; then
info "Run '${BINARY_NAME} --version' to verify"
else
info "Run '${BINARY_NAME}' to get started"
fi
else
error "Installation verification failed - binary not found or not executable at ${binary_path}"
fi
}
main() {
info "Starting ${BINARY_NAME} installation..."
local os
local arch
os="$(detect_os)"
arch="$(detect_arch "$os")"
install_binary "$os" "$arch"
check_path
verify_installation
echo ""
info "Installation complete!"
}
main