-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
409 lines (353 loc) · 11.1 KB
/
install.sh
File metadata and controls
409 lines (353 loc) · 11.1 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
#!/bin/bash
# xtop installer
# Installs xtop by building from source with automatic dependency detection and installation
set -euo pipefail
APP_NAME="xtop"
REPO_URL="https://github.com/xscriptor/xtop.git"
INSTALL_DIR="/usr/local/bin"
VERSION="1.0.0"
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Logging functions
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Detect package manager and distribution
detect_distro() {
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO="$ID"
DISTRO_LIKE="${ID_LIKE:-$ID}"
elif [ -f /etc/arch-release ]; then
DISTRO="arch"
DISTRO_LIKE="arch"
elif [ -f /etc/debian_version ]; then
DISTRO="debian"
DISTRO_LIKE="debian"
elif [ -f /etc/redhat-release ]; then
DISTRO="rhel"
DISTRO_LIKE="rhel fedora"
else
DISTRO="unknown"
DISTRO_LIKE="unknown"
fi
# Detect package manager
if command -v pacman &> /dev/null; then
PKG_MANAGER="pacman"
PKG_INSTALL="sudo pacman -S --noconfirm --needed"
PKG_UPDATE="sudo pacman -Sy"
elif command -v apt-get &> /dev/null; then
PKG_MANAGER="apt"
PKG_INSTALL="sudo apt-get install -y"
PKG_UPDATE="sudo apt-get update"
elif command -v dnf &> /dev/null; then
PKG_MANAGER="dnf"
PKG_INSTALL="sudo dnf install -y"
PKG_UPDATE="sudo dnf check-update || true"
elif command -v yum &> /dev/null; then
PKG_MANAGER="yum"
PKG_INSTALL="sudo yum install -y"
PKG_UPDATE="sudo yum check-update || true"
elif command -v zypper &> /dev/null; then
PKG_MANAGER="zypper"
PKG_INSTALL="sudo zypper install -y"
PKG_UPDATE="sudo zypper refresh"
elif command -v apk &> /dev/null; then
PKG_MANAGER="apk"
PKG_INSTALL="sudo apk add"
PKG_UPDATE="sudo apk update"
else
PKG_MANAGER="unknown"
fi
log_info "Detected distribution: $DISTRO (package manager: $PKG_MANAGER)"
}
# Get package names for different distros
get_build_deps() {
case "$PKG_MANAGER" in
pacman)
echo "base-devel git"
;;
apt)
echo "build-essential git pkg-config libssl-dev"
;;
dnf|yum)
echo "gcc gcc-c++ make git pkg-config openssl-devel"
;;
zypper)
echo "gcc gcc-c++ make git pkg-config libopenssl-devel"
;;
apk)
echo "build-base git pkgconfig openssl-dev"
;;
*)
echo ""
;;
esac
}
# Check if running as root
check_root_for_deps() {
if [ "$EUID" -ne 0 ] && ! sudo -n true 2>/dev/null; then
log_warn "Installing dependencies may require sudo password"
fi
}
# Install build dependencies
install_build_deps() {
local deps
deps=$(get_build_deps)
if [ -z "$deps" ]; then
log_warn "Unknown package manager. Please install build dependencies manually."
log_warn "Required: C compiler, git, pkg-config, OpenSSL development headers"
return 1
fi
log_info "Installing build dependencies: $deps"
check_root_for_deps
# Update package database
$PKG_UPDATE 2>/dev/null || true
# Install packages
# shellcheck disable=SC2086
$PKG_INSTALL $deps
log_success "Build dependencies installed"
}
# Check if git is installed
check_git() {
if command -v git &> /dev/null; then
log_success "git is installed"
return 0
else
log_warn "git is not installed"
return 1
fi
}
# Check if Rust/Cargo is installed
check_rust() {
if command -v cargo &> /dev/null; then
local version
version=$(cargo --version)
log_success "Cargo is installed: $version"
return 0
else
log_warn "Cargo (Rust) is not installed"
return 1
fi
}
# Install Rust via rustup
install_rust() {
log_info "Installing Rust via rustup..."
if command -v rustup &> /dev/null; then
log_info "rustup found, updating..."
rustup update stable
else
# Install rustup (handle both direct execution and curl|bash)
# Download the script first, then execute it
local rustup_script
rustup_script=$(mktemp)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o "$rustup_script"
chmod +x "$rustup_script"
# Run with -y for non-interactive mode
sh "$rustup_script" -y --default-toolchain stable
rm -f "$rustup_script"
# Add cargo to PATH for current session (avoid 'source' which can cause issues with curl|bash)
export PATH="$HOME/.cargo/bin:$PATH"
fi
# Verify installation
if command -v cargo &> /dev/null; then
log_success "Rust installed successfully"
return 0
else
log_error "Failed to install Rust"
return 1
fi
}
# Check all dependencies
check_all_deps() {
log_info "Checking dependencies..."
local missing=0
detect_distro
if ! check_git; then
((missing++))
fi
if ! check_rust; then
((missing++))
fi
# Check for C compiler
if command -v gcc &> /dev/null || command -v clang &> /dev/null; then
log_success "C compiler found"
else
log_warn "No C compiler found (gcc or clang)"
((missing++))
fi
# Check for pkg-config
if command -v pkg-config &> /dev/null; then
log_success "pkg-config found"
else
log_warn "pkg-config not found"
((missing++))
fi
if [ $missing -gt 0 ]; then
log_warn "$missing dependencies missing"
return 1
else
log_success "All dependencies satisfied"
return 0
fi
}
# Install all required dependencies
install_all_deps() {
log_info "Installing all dependencies..."
detect_distro
# Install build dependencies first
install_build_deps || log_warn "Could not install some build deps, continuing..."
# Install Rust if needed
if ! check_rust; then
install_rust
fi
log_success "All dependencies installed"
}
# Build and install xtop
do_install() {
local source_dir=""
local cleanup_temp=false
echo -e "${BOLD}${GREEN}"
echo "╔══════════════════════════════════════╗"
echo "║ Installing $APP_NAME ║"
echo "╚══════════════════════════════════════╝"
echo -e "${NC}"
detect_distro
# Check and install dependencies if missing
if ! check_all_deps; then
log_info "Installing missing dependencies..."
install_all_deps
fi
# Verify cargo is available
if ! command -v cargo &> /dev/null; then
log_error "Cargo is still not available after installation attempts"
log_error "Please install Rust manually: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
exit 1
fi
# Determine source directory
if [ -f "Cargo.toml" ] && grep -q "name.*=.*\"$APP_NAME\"" Cargo.toml 2>/dev/null; then
# Running from project directory
log_info "Building from current directory..."
source_dir="$(pwd)"
else
# Clone from remote
log_info "Cloning repository..."
TEMP_DIR=$(mktemp -d)
cleanup_temp=true
if ! git clone --depth 1 "$REPO_URL" "$TEMP_DIR/$APP_NAME"; then
log_error "Failed to clone repository from $REPO_URL"
rm -rf "$TEMP_DIR"
exit 1
fi
source_dir="$TEMP_DIR/$APP_NAME"
fi
# Build the project
log_info "Building $APP_NAME (this may take a moment)..."
cd "$source_dir"
if ! cargo build --release; then
log_error "Build failed"
[ "$cleanup_temp" = true ] && rm -rf "$TEMP_DIR"
exit 1
fi
log_success "Build completed"
# Install binary
log_info "Installing binary to $INSTALL_DIR..."
if [ ! -f "target/release/$APP_NAME" ]; then
log_error "Binary not found at target/release/$APP_NAME"
[ "$cleanup_temp" = true ] && rm -rf "$TEMP_DIR"
exit 1
fi
if [ -w "$INSTALL_DIR" ]; then
cp "target/release/$APP_NAME" "$INSTALL_DIR/$APP_NAME"
chmod +x "$INSTALL_DIR/$APP_NAME"
else
log_info "Requesting sudo to install to $INSTALL_DIR"
sudo cp "target/release/$APP_NAME" "$INSTALL_DIR/$APP_NAME"
sudo chmod +x "$INSTALL_DIR/$APP_NAME"
fi
# Cleanup
[ "$cleanup_temp" = true ] && rm -rf "$TEMP_DIR"
echo ""
log_success "$APP_NAME installed successfully!"
echo -e "Run it with: ${BOLD}$APP_NAME${NC}"
# Verify binary exists (don't run it as it may be interactive)
if [ -x "$INSTALL_DIR/$APP_NAME" ]; then
log_success "Binary installed at $INSTALL_DIR/$APP_NAME"
fi
}
# Uninstall xtop
do_uninstall() {
log_info "Uninstalling $APP_NAME..."
if [ -f "$INSTALL_DIR/$APP_NAME" ]; then
if [ -w "$INSTALL_DIR" ]; then
rm -f "$INSTALL_DIR/$APP_NAME"
else
sudo rm -f "$INSTALL_DIR/$APP_NAME"
fi
log_success "$APP_NAME uninstalled successfully"
else
log_warn "$APP_NAME is not installed in $INSTALL_DIR"
fi
}
# Show help
show_help() {
echo -e "${BOLD}$APP_NAME Installer v$VERSION${NC}"
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --help, -h Show this help message"
echo " --check-deps Check if all dependencies are installed"
echo " --install-deps Install all required dependencies"
echo " --uninstall Uninstall $APP_NAME"
echo " --version, -v Show installer version"
echo ""
echo "Without options, the installer will:"
echo " 1. Detect your distribution and package manager"
echo " 2. Check and install required dependencies"
echo " 3. Clone the repository (if not in project directory)"
echo " 4. Build and install $APP_NAME to $INSTALL_DIR"
echo ""
echo "Supported distributions:"
echo " - Arch Linux (and derivatives like Manjaro, EndeavourOS)"
echo " - Debian/Ubuntu (and derivatives)"
echo " - Fedora/RHEL/CentOS"
echo " - openSUSE"
echo " - Alpine Linux"
}
# Parse arguments
main() {
case "${1:-}" in
--help|-h)
show_help
;;
--version|-v)
echo "$APP_NAME installer v$VERSION"
;;
--check-deps)
check_all_deps
;;
--install-deps)
install_all_deps
;;
--uninstall)
do_uninstall
;;
"")
do_install
;;
*)
log_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
}
main "$@"
exit 0