-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·296 lines (247 loc) · 7.65 KB
/
install.sh
File metadata and controls
executable file
·296 lines (247 loc) · 7.65 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
#!/bin/bash
# Installation script for splash-cli-utils
# This script builds and installs the utilities
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
PREFIX="/usr/local"
PROFILE="release"
INTERACTIVE=true
FORCE=false
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}$1${NC}"
}
# Function to show help
show_help() {
cat << EOF
splash-cli-utils Installation Script
USAGE:
./install.sh [OPTIONS]
OPTIONS:
-p, --prefix PATH Installation prefix (default: /usr/local)
-d, --debug Build in debug mode instead of release
-y, --yes Non-interactive mode, assume yes to all prompts
-f, --force Force installation even if binaries exist
-u, --uninstall Uninstall the tools instead of installing
-h, --help Show this help message
EXAMPLES:
./install.sh # Install to /usr/local/bin
./install.sh -p ~/.local # Install to ~/.local/bin
./install.sh -d # Build in debug mode
./install.sh -y -f # Force install without prompts
./install.sh -u # Uninstall
Note: You may need sudo privileges for system-wide installation.
EOF
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check dependencies
check_dependencies() {
print_header "Checking dependencies..."
if ! command_exists cargo; then
print_error "Rust/Cargo is not installed!"
print_error "Please install Rust from https://rustup.rs/ and try again."
exit 1
fi
print_status "Rust/Cargo found: $(cargo --version)"
# Check if we're in the right directory
if [[ ! -f "Makefile" ]] || [[ ! -d "mkdev" ]] || [[ ! -d "signals" ]]; then
print_error "This script must be run from the splash-cli-utils root directory!"
exit 1
fi
print_status "Project structure verified"
}
# Function to build the tools
build_tools() {
print_header "Building tools..."
print_status "Building with profile: $PROFILE"
if ! make build PROFILE="$PROFILE"; then
print_error "Build failed!"
exit 1
fi
print_status "Build completed successfully"
}
# Function to check if binaries already exist
check_existing() {
local bindir="$PREFIX/bin"
local mkdev_exists=false
local sig_exists=false
if [[ -f "$bindir/mkdev" ]]; then
mkdev_exists=true
fi
if [[ -f "$bindir/sig" ]]; then
sig_exists=true
fi
if [[ "$mkdev_exists" == true ]] || [[ "$sig_exists" == true ]]; then
if [[ "$FORCE" == false ]]; then
print_warning "Some binaries already exist:"
[[ "$mkdev_exists" == true ]] && print_warning " - $bindir/mkdev"
[[ "$sig_exists" == true ]] && print_warning " - $bindir/sig"
if [[ "$INTERACTIVE" == true ]]; then
echo -n "Do you want to overwrite them? (y/N): "
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
print_status "Installation cancelled"
exit 0
fi
else
print_error "Binaries exist and --force not specified"
exit 1
fi
fi
fi
}
# Function to install the tools
install_tools() {
print_header "Installing tools..."
local bindir="$PREFIX/bin"
print_status "Installing to: $bindir"
# Check if we need sudo
local use_sudo=false
if [[ ! -w "$(dirname "$bindir")" ]] && [[ ! -w "$bindir" ]] 2>/dev/null; then
if [[ "$PREFIX" == "/usr/local" ]] || [[ "$PREFIX" =~ ^/usr ]]; then
use_sudo=true
print_warning "Root privileges required for installation to $PREFIX"
else
print_error "No write permission to $bindir"
exit 1
fi
fi
# Create bin directory if it doesn't exist
if [[ "$use_sudo" == true ]]; then
sudo mkdir -p "$bindir"
else
mkdir -p "$bindir"
fi
# Install binaries
local install_cmd="make install PREFIX=\"$PREFIX\" PROFILE=\"$PROFILE\""
if [[ "$use_sudo" == true ]]; then
# Find cargo path to pass to sudo environment
local cargo_path=$(which cargo)
local cargo_dir=$(dirname "$cargo_path")
if ! sudo env PATH="$cargo_dir:$PATH" bash -c "$install_cmd"; then
print_error "Installation failed!"
exit 1
fi
else
if ! eval "$install_cmd"; then
print_error "Installation failed!"
exit 1
fi
fi
print_status "Installation completed successfully!"
print_status ""
print_status "Installed tools:"
print_status " mkdev -> $bindir/mkdev"
print_status " sig -> $bindir/sig"
print_status ""
print_status "You can now use:"
print_status " mkdev <source> <target> # Write disk images to devices"
print_status " sig <signal> <process> # Send signals to processes"
}
# Function to uninstall the tools
uninstall_tools() {
print_header "Uninstalling tools..."
local bindir="$PREFIX/bin"
local use_sudo=false
# Check if we need sudo
if [[ ! -w "$bindir" ]] 2>/dev/null; then
if [[ "$PREFIX" == "/usr/local" ]] || [[ "$PREFIX" =~ ^/usr ]]; then
use_sudo=true
print_warning "Root privileges required for uninstallation from $PREFIX"
fi
fi
local uninstall_cmd="make uninstall PREFIX=\"$PREFIX\""
if [[ "$use_sudo" == true ]]; then
# Find cargo path to pass to sudo environment (needed for potential rebuilds)
local cargo_path=$(which cargo)
local cargo_dir=$(dirname "$cargo_path")
if ! sudo env PATH="$cargo_dir:$PATH" bash -c "$uninstall_cmd"; then
print_error "Uninstallation failed!"
exit 1
fi
else
if ! eval "$uninstall_cmd"; then
print_error "Uninstallation failed!"
exit 1
fi
fi
print_status "Uninstallation completed successfully!"
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-p|--prefix)
PREFIX="$2"
shift 2
;;
-d|--debug)
PROFILE="debug"
shift
;;
-y|--yes)
INTERACTIVE=false
shift
;;
-f|--force)
FORCE=true
shift
;;
-u|--uninstall)
check_dependencies
uninstall_tools
exit 0
;;
-h|--help)
show_help
exit 0
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
# Main installation process
print_header "splash-cli-utils Installation"
print_status "Configuration:"
print_status " Install prefix: $PREFIX"
print_status " Build profile: $PROFILE"
print_status " Interactive: $INTERACTIVE"
print_status ""
# Check dependencies
check_dependencies
# Check for existing binaries
check_existing
# Confirm installation if interactive
if [[ "$INTERACTIVE" == true ]]; then
echo -n "Proceed with installation? (Y/n): "
read -r response
if [[ "$response" =~ ^[Nn]$ ]]; then
print_status "Installation cancelled"
exit 0
fi
fi
# Build and install
build_tools
install_tools
print_header "Installation Complete!"
print_status "Run './install.sh --help' for more options"