-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·643 lines (538 loc) · 15.4 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·643 lines (538 loc) · 15.4 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/env bash
#
# install.sh - Install rbite from GitHub releases
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/requestbite/rbite/main/install.sh | bash
# curl -fsSL https://raw.githubusercontent.com/requestbite/rbite/main/install.sh | bash -s -- --prefix=$HOME/bin
# curl -fsSL https://raw.githubusercontent.com/requestbite/rbite/main/install.sh | bash -s -- --version 0.0.1
#
set -euo pipefail
# Configuration
BINARY_NAME="rbite"
PROXY_NAME="rbite-proxy"
GITHUB_REPO="requestbite/rbite"
DEFAULT_INSTALL_DIR="$HOME/.local/bin"
# Parse command line arguments
VERSION=""
PREFIX=""
while [[ $# -gt 0 ]]; do
case $1 in
--version)
VERSION="$2"
shift 2
;;
--prefix)
PREFIX="$2"
shift 2
;;
--help)
cat <<EOF
rbite - Installation Script
Usage:
install.sh [options]
Options:
--version VERSION Install specific version (e.g., 0.0.1)
--prefix PATH Install to PATH (default: ~/.local/bin)
--help Show this help message
Examples:
# Install latest version to default location
./install.sh
# Install specific version
./install.sh --version 0.0.1
# Install to custom location
./install.sh --prefix \$HOME
# One-line install from GitHub
curl -fsSL https://raw.githubusercontent.com/requestbite/rbite/main/install.sh | bash
EOF
exit 0
;;
*)
echo "Unknown option: $1"
echo "Run with --help for usage information"
exit 1
;;
esac
done
# Colors (only if terminal supports it)
if [ -t 1 ]; then
COLOR_RESET='\033[0m'
COLOR_BOLD='\033[1m'
COLOR_GREEN='\033[32m'
COLOR_BLUE='\033[34m'
COLOR_RED='\033[31m'
COLOR_YELLOW='\033[33m'
else
COLOR_RESET=''
COLOR_BOLD=''
COLOR_GREEN=''
COLOR_BLUE=''
COLOR_RED=''
COLOR_YELLOW=''
fi
# Utility functions
info() {
echo -e "\n${COLOR_BOLD}${COLOR_BLUE}==>${COLOR_RESET} ${COLOR_BOLD}$*${COLOR_RESET}"
}
success() {
echo -e "${COLOR_GREEN}✓${COLOR_RESET} $*"
}
error() {
echo -e "${COLOR_RED}✗ Error:${COLOR_RESET} $*" >&2
}
warning() {
echo -e "${COLOR_YELLOW}⚠${COLOR_RESET} $*"
}
die() {
error "$*"
exit 1
}
# Check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Cleanup function
TEMP_DIR=""
cleanup() {
if [ -n "$TEMP_DIR" ] && [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
fi
}
trap cleanup EXIT INT TERM
# Check prerequisites
check_prerequisites() {
local missing=()
if ! command_exists curl; then
missing+=("curl")
fi
if ! command_exists tar; then
missing+=("tar")
fi
if ! command_exists shasum && ! command_exists sha256sum; then
missing+=("shasum or sha256sum")
fi
if [ ${#missing[@]} -gt 0 ]; then
error "Missing required tools:"
for tool in "${missing[@]}"; do
echo " - $tool"
done
exit 1
fi
}
# Detect operating system
detect_os() {
local os
os="$(uname -s)"
case "$os" in
Linux*)
echo "linux"
;;
Darwin*)
echo "darwin"
;;
*)
die "Unsupported operating system: $os (supported: Linux, macOS)"
;;
esac
}
# Detect architecture
detect_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64)
echo "amd64"
;;
arm64 | aarch64)
echo "arm64"
;;
*)
die "Unsupported architecture: $arch (supported: x86_64, arm64)"
;;
esac
}
# Get latest version from GitHub
get_latest_version() {
info "Fetching latest version from GitHub..." >&2
local version
version=$(curl -fsSL -H "User-Agent: rbite-installer" "https://api.github.com/repos/$GITHUB_REPO/releases/latest" |
grep '"tag_name"' |
sed -E 's/.*"tag_name": *"v?([^"]+)".*/\1/' || echo "")
if [ -z "$version" ]; then
die "Failed to fetch latest version from GitHub API"
fi
echo "$version"
}
# Determine installation directory
determine_install_dir() {
local install_dir
if [ -n "$PREFIX" ]; then
install_dir="$PREFIX"
elif [ -d "$DEFAULT_INSTALL_DIR" ] || mkdir -p "$DEFAULT_INSTALL_DIR" 2>/dev/null; then
install_dir="$DEFAULT_INSTALL_DIR"
else
install_dir="/usr/local/bin"
warning "Cannot create $DEFAULT_INSTALL_DIR, will try system-wide installation"
warning "This may require sudo privileges"
fi
echo "$install_dir"
}
# Check if directory is writable
is_writable() {
local dir="$1"
if [ -d "$dir" ]; then
[ -w "$dir" ]
else
# Check parent directory
local parent
parent="$(dirname "$dir")"
[ -w "$parent" ]
fi
}
# Download and verify archive
download_and_verify() {
local url="$1"
local archive_name="$2"
local checksum_url="$3"
if ! curl -fsSL --progress-bar "$url" -o "$archive_name"; then
die "Failed to download $url"
fi
success "Downloaded $archive_name"
if ! curl -fsSL "$checksum_url" -o SHA256SUMS 2>/dev/null; then
return 0
fi
local expected_checksum
expected_checksum=$(grep "$archive_name" SHA256SUMS | awk '{print $1}')
if [ -z "$expected_checksum" ]; then
return 0
fi
local actual_checksum
if command_exists shasum; then
actual_checksum=$(shasum -a 256 "$archive_name" | awk '{print $1}')
else
actual_checksum=$(sha256sum "$archive_name" | awk '{print $1}')
fi
if [ "$expected_checksum" != "$actual_checksum" ]; then
die "Checksum verification failed!
Expected: $expected_checksum
Actual: $actual_checksum"
fi
}
# Extract archive contents
extract_binary() {
local archive_name="$1"
if [[ "$archive_name" == *.tar.gz ]]; then
tar -xzf "$archive_name"
if [ ! -f "$BINARY_NAME/$BINARY_NAME" ]; then
die "Binary not found in archive"
fi
mv "$BINARY_NAME/$BINARY_NAME" "${BINARY_NAME}.tmp"
if [ -d "$BINARY_NAME/completions" ]; then
mv "$BINARY_NAME/completions" completions
fi
if [ -d "$BINARY_NAME/man" ]; then
mv "$BINARY_NAME/man" man
fi
rm -rf "$BINARY_NAME"
mv "${BINARY_NAME}.tmp" "$BINARY_NAME"
else
die "Unsupported archive format: $archive_name"
fi
}
# Install binary
install_binary() {
local install_dir="$1"
local use_sudo=false
# Create install directory if needed
if [ ! -d "$install_dir" ]; then
if ! mkdir -p "$install_dir" 2>/dev/null; then
use_sudo=true
fi
fi
# Check if we need sudo
if ! is_writable "$install_dir"; then
use_sudo=true
fi
info "Installing $BINARY_NAME $VERSION:"
if [ "$use_sudo" = true ]; then
warning "Installation requires elevated privileges"
if ! command_exists sudo; then
die "sudo is required but not available"
fi
sudo mkdir -p "$install_dir"
sudo rm -f "$install_dir/$BINARY_NAME"
sudo cp "$BINARY_NAME" "$install_dir/"
sudo chmod +x "$install_dir/$BINARY_NAME"
else
mkdir -p "$install_dir"
rm -f "$install_dir/$BINARY_NAME"
cp "$BINARY_NAME" "$install_dir/"
chmod +x "$install_dir/$BINARY_NAME"
fi
success "Installed $BINARY_NAME to $install_dir"
}
# Install shell completions and man page
install_completions() {
local os="$1"
# Fish completion — user-level, works on both Linux and macOS
local fish_completion_dir="$HOME/.config/fish/completions"
if command_exists fish; then
mkdir -p "$fish_completion_dir"
if [ -f "completions/rbite.fish" ]; then
cp "completions/rbite.fish" "$fish_completion_dir/rbite.fish"
success "Installed Fish completion to $fish_completion_dir/rbite.fish"
fi
fi
# Zsh completion
local zsh_completion_dir="$HOME/.local/share/zsh/site-functions"
if [ "$os" = "darwin" ] && command_exists brew; then
local brew_prefix
brew_prefix="$(brew --prefix 2>/dev/null)"
if [ -d "$brew_prefix/share/zsh/site-functions" ]; then
zsh_completion_dir="$brew_prefix/share/zsh/site-functions"
fi
fi
if [ -f "completions/_rbite" ]; then
mkdir -p "$zsh_completion_dir"
cp "completions/_rbite" "$zsh_completion_dir/_rbite"
success "Installed Zsh completion to $zsh_completion_dir/_rbite"
fi
# Man page
local man_dir="$HOME/.local/share/man/man1"
if [ -f "man/rbite.1" ]; then
mkdir -p "$man_dir"
cp "man/rbite.1" "$man_dir/rbite.1"
success "Installed man page to $man_dir/rbite.1"
if command_exists mandb; then
mandb -q "$HOME/.local/share/man" 2>/dev/null || true
elif command_exists makewhatis; then
makewhatis "$HOME/.local/share/man" 2>/dev/null || true
fi
fi
# Bash completion
local bash_completion_dir="$HOME/.local/share/bash-completion/completions"
if [ "$os" = "darwin" ] && command_exists brew; then
local brew_prefix
brew_prefix="$(brew --prefix 2>/dev/null)"
if [ -d "$brew_prefix/share/bash-completion/completions" ]; then
bash_completion_dir="$brew_prefix/share/bash-completion/completions"
fi
fi
if [ -f "completions/rbite.bash" ]; then
mkdir -p "$bash_completion_dir"
cp "completions/rbite.bash" "$bash_completion_dir/rbite"
success "Installed Bash completion to $bash_completion_dir/rbite"
fi
}
# Download and extract rbite-proxy (sets PROXY_VERSION; stores binary as rbite-proxy.bin)
PROXY_VERSION=""
download_companion() {
local os="$1"
local arch="$2"
local companion="rbite-proxy"
local companion_repo="requestbite/proxy"
PROXY_VERSION=$(curl -fsSL -H "User-Agent: rbite-installer" \
"https://api.github.com/repos/${companion_repo}/releases/latest" |
grep '"tag_name"' |
sed -E 's/.*"tag_name": *"v?([^"]+)".*/\1/' || echo "")
if [ -z "$PROXY_VERSION" ]; then
warning "Could not fetch latest version of $companion — skipping"
return 0
fi
local archive="${companion}-${PROXY_VERSION}-${os}-${arch}.tar.gz"
local base_url="https://github.com/${companion_repo}/releases/download/${PROXY_VERSION}"
if ! curl -fsSL --progress-bar "${base_url}/${archive}" -o "$archive"; then
warning "Failed to download $companion — skipping"
PROXY_VERSION=""
return 0
fi
success "Downloaded $archive"
if curl -fsSL "${base_url}/SHA256SUMS" -o "SHA256SUMS.proxy" 2>/dev/null; then
local expected actual
expected=$(grep "$archive" SHA256SUMS.proxy | awk '{print $1}')
if [ -n "$expected" ]; then
if command_exists shasum; then
actual=$(shasum -a 256 "$archive" | awk '{print $1}')
else
actual=$(sha256sum "$archive" | awk '{print $1}')
fi
if [ "$expected" != "$actual" ]; then
warning "$companion checksum mismatch — skipping"
PROXY_VERSION=""
return 0
fi
fi
fi
tar -xzf "$archive"
if [ ! -f "$companion/$companion" ]; then
warning "$companion binary not found in archive — skipping"
PROXY_VERSION=""
return 0
fi
mv "$companion/$companion" "${companion}.bin"
rm -rf "$companion"
}
# Install pre-downloaded rbite-proxy binary
install_companion() {
local install_dir="$1"
[ -z "$PROXY_VERSION" ] && return 0
local companion="rbite-proxy"
info "Installing $companion $PROXY_VERSION:"
local use_sudo=false
if ! is_writable "$install_dir"; then
use_sudo=true
fi
if [ "$use_sudo" = true ]; then
sudo cp "${companion}.bin" "$install_dir/$companion"
sudo chmod +x "$install_dir/$companion"
else
cp "${companion}.bin" "$install_dir/$companion"
chmod +x "$install_dir/$companion"
fi
success "Installed $companion to $install_dir"
}
# Verify installation
verify_installation() {
local install_dir="$1"
local binary_path="$install_dir/$BINARY_NAME"
if [ ! -f "$binary_path" ]; then
die "Installation verification failed: $binary_path not found"
fi
if [ ! -x "$binary_path" ]; then
die "Installation verification failed: $binary_path is not executable"
fi
# Try to run --version
if "$binary_path" --version >/dev/null 2>&1; then
success "Installation verified"
else
warning "Binary exists but --version check failed"
fi
}
# Detect the user's shell config file
detect_shell_config() {
if [ -n "${FISH_VERSION:-}" ] || [[ "${SHELL:-}" == */fish ]]; then
echo "$HOME/.config/fish/config.fish"
elif [ -n "${ZSH_VERSION:-}" ] || [[ "${SHELL:-}" == */zsh ]]; then
echo "$HOME/.zshrc"
elif [ -n "${BASH_VERSION:-}" ] || [[ "${SHELL:-}" == */bash ]]; then
# Prefer .bash_profile on macOS (login shell), .bashrc on Linux
if [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
else
echo "$HOME/.bashrc"
fi
else
echo ""
fi
}
# Check if install directory is in PATH; offer to add it interactively
check_path() {
local install_dir="$1"
# Already in PATH — nothing to do
if echo ":${PATH}:" | grep -q ":${install_dir}:"; then
return 0
fi
local shell_config
shell_config="$(detect_shell_config)"
local config_display="${shell_config/#$HOME/\~}"
if [ -z "$shell_config" ]; then
warning "$install_dir is not in your PATH"
echo "Add the following line to your shell configuration file:"
echo " export PATH=\"$install_dir:\$PATH\""
echo ""
return 0
fi
# Determine the export/set line for the detected shell
local export_line
if [[ "$shell_config" == */fish/config.fish ]]; then
export_line="fish_add_path $install_dir"
else
export_line="export PATH=\"$install_dir:\$PATH\""
fi
# Ask the user (default yes)
echo ""
printf "${COLOR_YELLOW}⚠${COLOR_RESET} ${COLOR_BOLD}rbite${COLOR_RESET} is not in your PATH."
echo ""
printf " Should I add ${COLOR_BOLD}$install_dir${COLOR_RESET} to ${COLOR_BOLD}$config_display${COLOR_RESET}? [Y/n] "
# Read from /dev/tty so it works even when script is piped from curl
local answer
if read -r answer </dev/tty 2>/dev/null; then
: # got input
else
answer="n"
fi
case "${answer:-Y}" in
[Yy]* | "")
echo "" >>"$shell_config"
echo "# Added by rbite installer" >>"$shell_config"
echo "$export_line" >>"$shell_config"
success "Added to $config_display"
# Update PATH in the current shell session
export PATH="$install_dir:$PATH"
success "Updated PATH for this session — $BINARY_NAME is ready to use now"
;;
*)
warning "Skipped. Add the following line to $config_display manually:"
echo " $export_line"
echo "Then restart your shell or run: source $shell_config"
echo ""
;;
esac
}
# Main installation function
main() {
info "rbite - Installation Script"
echo ""
# Check prerequisites
check_prerequisites
# Detect platform
local os
local arch
os=$(detect_os)
arch=$(detect_arch)
echo "Detected platform: $os/$arch"
# Get version
if [ -z "$VERSION" ]; then
VERSION=$(get_latest_version)
fi
# Determine installation directory
local install_dir
install_dir=$(determine_install_dir)
echo "Install directory: $install_dir"
# Construct download URL
local archive_name="${BINARY_NAME}-${VERSION}-${os}-${arch}.tar.gz"
local base_url="https://github.com/${GITHUB_REPO}/releases/download/${VERSION}"
local download_url="${base_url}/${archive_name}"
local checksum_url="${base_url}/SHA256SUMS"
# Create temporary directory
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
# Download everything first
info "Downloading from GitHub releases..."
download_and_verify "$download_url" "$archive_name" "$checksum_url"
download_companion "$os" "$arch"
# Extract rbite
extract_binary "$archive_name"
# Install rbite (binary + completions + man page)
install_binary "$install_dir"
install_completions "$os"
# Install rbite-proxy
install_companion "$install_dir"
# Verify
verify_installation "$install_dir"
echo ""
success "Installation complete!"
echo ""
# Check PATH
check_path "$install_dir"
# Show usage
echo "Usage:"
echo " $BINARY_NAME --help"
echo " $BINARY_NAME --version"
echo ""
echo " $PROXY_NAME --help"
echo " $PROXY_NAME --version"
echo ""
}
# Run main function
main