-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall
More file actions
executable file
·424 lines (359 loc) · 9.91 KB
/
install
File metadata and controls
executable file
·424 lines (359 loc) · 9.91 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
#!/bin/env bash
# LazyShell Install Script
# Usage: curl -fsSL https://raw.githubusercontent.com/bernoussama/lazyshell/main/install.sh | bash
# this line is used to ensure the script exits on any error
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
# Script configuration
REPO="bernoussama/lazyshell"
BINARY_NAME="lsh"
ALIAS_NAME="lazyshell"
INSTALL_DIR=$HOME/.lazyshell/bin
mkdir -p "$INSTALL_DIR"
TEMP_DIR="/tmp/lazyshell-install"
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_message() {
local level=$1
local message=$2
local color=""
case $level in
info) color="${GREEN}" ;;
warning) color="${YELLOW}" ;;
error) color="${RED}" ;;
esac
echo -e "${color}${message}${NC}"
}
# Cleanup function
cleanup() {
if [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
fi
}
# Set up trap for cleanup
trap cleanup EXIT
# Check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Detect operating system
detect_os() {
local os
case "$(uname -s)" in
Linux*)
os="linux"
;;
Darwin*)
os="darwin"
;;
CYGWIN* | MINGW* | MSYS*)
os="win32"
;;
*)
log_error "Unsupported operating system: $(uname -s)"
exit 1
;;
esac
echo "$os"
}
# Detect architecture
detect_arch() {
local arch
case "$(uname -m)" in
x86_64 | amd64)
arch="x64"
;;
arm64 | aarch64)
arch="arm64"
;;
*)
log_error "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
echo "$arch"
}
# Get binary name based on OS and architecture
get_binary_name() {
local os="$1"
local arch="$2"
local binary_name
case "$os" in
linux)
binary_name="lsh-linux-${arch}"
;;
darwin)
binary_name="lsh-darwin-${arch}"
;;
win32)
binary_name="lsh-win32-${arch}.exe"
;;
*)
log_error "Unsupported OS: $os"
exit 1
;;
esac
echo "$binary_name"
}
# Check required commands
check_requirements() {
local missing_commands=()
if ! command_exists curl; then
missing_commands+=("curl")
fi
if ! command_exists jq; then
if command_exists python3; then
log_warning "jq not found, using python3 for JSON parsing"
elif command_exists python; then
log_warning "jq not found, using python for JSON parsing"
else
missing_commands+=("jq or python")
fi
fi
if [ ${#missing_commands[@]} -ne 0 ]; then
log_error "Missing required commands: ${missing_commands[*]}"
log_error "Please install them and try again"
exit 1
fi
}
# Parse JSON without jq (fallback)
parse_json() {
local json_data="$1"
local key="$2"
if command_exists jq; then
echo "$json_data" | jq -r "$key"
elif command_exists python3; then
echo "$json_data" | python3 -c "import sys, json; print(json.load(sys.stdin)$key)"
elif command_exists python; then
echo "$json_data" | python -c "import sys, json; print(json.load(sys.stdin)$key)"
else
log_error "Cannot parse JSON without jq or python"
exit 1
fi
}
# Get latest release info from GitHub API
get_latest_release() {
local api_url="https://api.github.com/repos/${REPO}/releases"
local release_data
release_data=$(curl -fsSL "$api_url")
echo "$release_data"
}
# Download and install binary
install_binary() {
local binary_name="$1"
local download_url="$2"
local install_path="$INSTALL_DIR/$BINARY_NAME"
# Create temp directory
mkdir -p "$TEMP_DIR"
local temp_file="$TEMP_DIR/$binary_name"
print_message info "Downloading ${GREEN}lazyshell ..."
if ! curl -# -L -o "$temp_file" "$download_url"; then
log_error "Failed to download binary"
exit 1
fi
# Verify download
if [ ! -f "$temp_file" ]; then
log_error "Downloaded file not found"
exit 1
fi
# Check if install directory is writable
if [ ! -w "$INSTALL_DIR" ]; then
log_warning "Cannot write to $INSTALL_DIR, trying with sudo..."
if ! sudo mkdir -p "$INSTALL_DIR"; then
log_error "Failed to create install directory"
exit 1
fi
if ! sudo cp "$temp_file" "$install_path"; then
log_error "Failed to install binary"
exit 1
fi
if ! sudo chmod +x "$install_path"; then
log_error "Failed to make binary executable"
exit 1
fi
else
# Install without sudo
if ! cp "$temp_file" "$install_path"; then
log_error "Failed to install binary"
exit 1
fi
if ! chmod +x "$install_path"; then
log_error "Failed to make binary executable"
exit 1
fi
fi
log_success "Binary installed to $install_path"
}
# Verify installation
verify_installation() {
local install_path="$INSTALL_DIR/$BINARY_NAME"
if [ ! -f "$install_path" ]; then
log_error "Installation verification failed: binary not found"
exit 1
fi
if [ ! -x "$install_path" ]; then
log_error "Installation verification failed: binary not executable"
exit 1
fi
# Check if binary is in PATH
if ! command_exists "$BINARY_NAME"; then
log_warning "$INSTALL_DIR is not in your PATH"
log_warning "Add it to your PATH or use the full path: $install_path"
else
log_success "Installation verified successfully"
fi
}
# Main installation function
main() {
# Check requirements
check_requirements
# Detect system
local os arch binary_name
os=$(detect_os)
arch=$(detect_arch)
binary_name=$(get_binary_name "$os" "$arch")
# Check for unsupported combinations
if [ "$os" = "darwin" ] && [ "$arch" = "arm64" ]; then
echo
elif [ "$os" = "darwin" ] && [ "$arch" = "x64" ]; then
echo
elif [ "$os" = "linux" ] && [ "$arch" = "x64" ]; then
echo
elif [ "$os" = "linux" ] && [ "$arch" = "arm64" ]; then
echo
elif [ "$os" = "win32" ]; then
log_warning "Windows detected - this script may not work properly"
log_warning "Consider downloading the binary manually"
else
log_error "Unsupported system combination: $os $arch"
exit 1
fi
# Get latest release
local release_data
release_data=$(get_latest_release)
# Extract version and download URL from the first release in the array
local version download_url
if command_exists jq; then
version=$(echo "$release_data" | jq -r '.[0].tag_name')
download_url=$(echo "$release_data" | jq -r ".[0].assets[] | select(.name == \"$binary_name\") | .browser_download_url")
else
# Fallback JSON parsing for array format
version=$(echo "$release_data" | grep '"tag_name"' | head -1 | cut -d'"' -f4)
download_url=$(echo "$release_data" | grep "browser_download_url.*$binary_name" | head -1 | cut -d'"' -f4)
fi
if [ -z "$download_url" ]; then
log_error "Failed to find download URL for $binary_name"
log_error "This system combination might not be supported yet"
exit 1
fi
# Check if already installed
if command_exists "$BINARY_NAME"; then
local current_version
current_version=$($BINARY_NAME --version 2>/dev/null || echo "unknown")
if [ "$current_version" = "$version" ]; then
log_success "Latest version is already installed"
exit 0
fi
fi
# Download and install
install_binary "$binary_name" "$download_url"
add_to_path() {
local config_file=$1
local command=$2
echo "Adding $command to $config_file"
if grep -Fxq "$command" "$config_file"; then
print_message info "Command already exists in $config_file, skipping write."
elif [[ -w $config_file ]]; then
echo -e "\n# lazyshell" >>"$config_file"
echo "$command" >>"$config_file"
print_message info "Successfully added ${RED}lazyshell ${GREEN}to \$PATH in $config_file"
else
print_message warning "Manually add the directory to $config_file (or similar):"
print_message info " $command"
fi
}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
current_shell=$(basename "$SHELL")
case $current_shell in
fish)
config_files="$HOME/.config/fish/config.fish"
;;
zsh)
config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
;;
bash)
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
;;
ash)
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
;;
sh)
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
;;
*)
# Default case if none of the above matches
config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
;;
esac
config_file=""
for file in $config_files; do
if [[ -f $file ]]; then
config_file=$file
break
fi
done
if [[ -z $config_file ]]; then
print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
exit 1
fi
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
case $current_shell in
fish)
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
;;
zsh)
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
;;
bash)
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
;;
ash)
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
;;
sh)
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
;;
*)
export PATH=$INSTALL_DIR:$PATH
print_message warning "Manually add the directory to $config_file (or similar):"
print_message info " export PATH=$INSTALL_DIR:\$PATH"
;;
esac
source "$config_file" || {
print_message warning "Failed to source $config_file. Please run 'source $config_file' manually."
}
fi
# Verify installation
verify_installation
# alias lazyshell to lsh
ln -sf "$INSTALL_DIR/$BINARY_NAME" "$INSTALL_DIR/$ALIAS_NAME"
log_success "LazyShell $version has been successfully installed!"
}
# Run main function
main "$@"