-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
603 lines (506 loc) · 18.2 KB
/
setup.sh
File metadata and controls
603 lines (506 loc) · 18.2 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
#!/usr/bin/env bash
# Universal Terminal Setup Script
# Supports both Windows PowerShell and Linux/WSL environments
# Author: Enhanced by Claude for aymaneallaoui/setup-terminal
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# 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"
}
log_step() {
echo -e "${PURPLE}[STEP]${NC} $1"
}
# Check if running as root
is_root() {
[[ $EUID -eq 0 ]]
}
# Execute command with or without sudo based on current user
execute_as_needed() {
if is_root; then
"$@"
else
sudo "$@"
fi
}
# Detect environment
detect_environment() {
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]] || [[ -n "$WSLENV" ]]; then
if [[ -n "$WSLENV" ]]; then
echo "wsl"
else
echo "windows"
fi
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "macos"
else
echo "unknown"
fi
}
# Check if running in PowerShell
is_powershell() {
[[ -n "$PSVersionTable" ]] || [[ -n "$PSHOME" ]]
}
# Install packages based on distribution
install_linux_packages() {
log_step "Installing Linux packages..."
if is_root; then
log_info "Running as root - no sudo needed"
else
log_info "Running as regular user - will use sudo"
fi
if command -v apt-get &> /dev/null; then
# Debian/Ubuntu
execute_as_needed apt-get update
execute_as_needed apt-get install -y curl git build-essential fontconfig zsh openssh-client
elif command -v yum &> /dev/null; then
# RedHat/CentOS
execute_as_needed yum update -y
execute_as_needed yum install -y curl git gcc make fontconfig zsh openssh-clients
elif command -v pacman &> /dev/null; then
# Arch Linux
execute_as_needed pacman -Syu --noconfirm
execute_as_needed pacman -S --noconfirm curl git base-devel fontconfig zsh openssh
elif command -v dnf &> /dev/null; then
# Fedora
execute_as_needed dnf update -y
execute_as_needed dnf install -y curl git gcc make fontconfig zsh openssh-clients
elif command -v zypper &> /dev/null; then
# openSUSE
execute_as_needed zypper refresh
execute_as_needed zypper install -y curl git gcc make fontconfig zsh openssh
elif command -v apk &> /dev/null; then
# Alpine Linux
execute_as_needed apk update
execute_as_needed apk add curl git build-base fontconfig zsh openssh-client
else
log_warning "Unknown package manager. Please install curl, git, and build tools manually."
fi
}
# Install fzf
install_fzf() {
log_step "Installing fzf..."
if ! command -v fzf &> /dev/null; then
if [[ -d ~/.fzf ]]; then
log_info "fzf directory exists, updating..."
cd ~/.fzf && git pull
else
# Force HTTPS for git clone to avoid SSH issues
log_info "Cloning fzf repository..."
git -c url."https://github.com/".insteadOf="[email protected]:" clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
fi
~/.fzf/install --all --no-bash --no-zsh --no-fish
log_success "fzf installed successfully"
else
log_info "fzf is already installed"
fi
}
# Install Oh My Zsh
install_oh_my_zsh() {
log_step "Installing Oh My Zsh..."
if [[ ! -d ~/.oh-my-zsh ]]; then
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
log_success "Oh My Zsh installed successfully"
else
log_info "Oh My Zsh is already installed"
fi
}
# Install Starship prompt
install_starship() {
log_step "Installing Starship prompt..."
if ! command -v starship &> /dev/null; then
curl -sS https://starship.rs/install.sh | sh -s -- -y
log_success "Starship installed successfully"
else
log_info "Starship is already installed"
fi
}
# Install Zsh plugins
install_zsh_plugins() {
log_step "Installing Zsh plugins..."
local ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
# zsh-autosuggestions
if [[ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]]; then
git -c url."https://github.com/".insteadOf="[email protected]:" clone https://github.com/zsh-users/zsh-autosuggestions "$ZSH_CUSTOM/plugins/zsh-autosuggestions"
fi
# zsh-syntax-highlighting
if [[ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]]; then
git -c url."https://github.com/".insteadOf="[email protected]:" clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
fi
# powerlevel10k theme
if [[ ! -d "$ZSH_CUSTOM/themes/powerlevel10k" ]]; then
git -c url."https://github.com/".insteadOf="[email protected]:" clone --depth=1 https://github.com/romkatv/powerlevel10k.git "$ZSH_CUSTOM/themes/powerlevel10k"
fi
log_success "Zsh plugins installed successfully"
}
# Configure Zsh
configure_zsh() {
log_step "Configuring Zsh..."
# Backup existing .zshrc
if [[ -f ~/.zshrc ]]; then
cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d_%H%M%S)
fi
# Create new .zshrc
cat > ~/.zshrc << 'EOF'
# Path to your oh-my-zsh installation
export ZSH="$HOME/.oh-my-zsh"
# Theme
ZSH_THEME="powerlevel10k/powerlevel10k"
# Plugins
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
colored-man-pages
command-not-found
history-substring-search
)
source $ZSH/oh-my-zsh.sh
# User configuration
export EDITOR='nano'
export LANG=en_US.UTF-8
# Aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
# Git aliases
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
alias gd='git diff'
alias gb='git branch'
alias gco='git checkout'
# fzf integration
if command -v fzf &> /dev/null; then
source ~/.fzf.zsh
export FZF_DEFAULT_COMMAND='find . -type f -not -path "*/\.git/*" -not -path "*/node_modules/*"'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'
fi
# Starship prompt (if available)
if command -v starship &> /dev/null; then
eval "$(starship init zsh)"
fi
# History configuration
HISTSIZE=10000
SAVEHIST=10000
setopt HIST_VERIFY
setopt SHARE_HISTORY
setopt APPEND_HISTORY
setopt INC_APPEND_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_IGNORE_SPACE
# Key bindings
bindkey '^R' history-incremental-search-backward
bindkey '^F' fzf-file-widget
# Auto completion
autoload -U compinit
compinit
# Case insensitive completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
EOF
log_success "Zsh configuration completed"
}
# Configure Bash
configure_bash() {
log_step "Configuring Bash..."
# Backup existing .bashrc
if [[ -f ~/.bashrc ]]; then
cp ~/.bashrc ~/.bashrc.backup.$(date +%Y%m%d_%H%M%S)
fi
# Add configuration to .bashrc
cat >> ~/.bashrc << 'EOF'
# Terminal Setup Configuration
export EDITOR='nano'
export LANG=en_US.UTF-8
# Enhanced history
HISTSIZE=10000
HISTFILESIZE=20000
HISTCONTROL=ignoreboth
shopt -s histappend
shopt -s checkwinsize
# Aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
# Git aliases
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
alias gd='git diff'
alias gb='git branch'
alias gco='git checkout'
# fzf integration
if command -v fzf &> /dev/null; then
source ~/.fzf.bash
export FZF_DEFAULT_COMMAND='find . -type f -not -path "*/\.git/*" -not -path "*/node_modules/*"'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'
# Key bindings
bind '"\C-f": "fzf-file-widget\n"'
bind '"\C-r": "\C-a fzf-history-widget\n"'
fi
# Starship prompt (if available)
if command -v starship &> /dev/null; then
eval "$(starship init bash)"
fi
# Enable programmable completion features
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
EOF
log_success "Bash configuration completed"
}
# Setup Linux/WSL environment
setup_linux() {
log_info "Setting up Linux/WSL environment..."
install_linux_packages
install_fzf
install_starship
# Detect and setup shell
if command -v zsh &> /dev/null; then
log_info "Zsh detected, setting up Zsh configuration..."
install_oh_my_zsh
install_zsh_plugins
configure_zsh
# Change default shell to zsh if not already (and not root)
if [[ "$SHELL" != *"zsh"* ]] && ! is_root; then
log_step "Changing default shell to Zsh..."
chsh -s $(which zsh)
log_success "Default shell changed to Zsh. Please restart your terminal."
elif is_root; then
log_info "Running as root - shell change skipped. You can manually switch to zsh."
fi
else
log_info "Setting up Bash configuration..."
configure_bash
fi
log_success "Linux/WSL setup completed!"
}
# Setup PowerShell environment (for Windows or WSL with PowerShell)
setup_powershell() {
log_info "Setting up PowerShell environment..."
# Create PowerShell script for Windows setup
cat > setup_powershell.ps1 << 'EOF'
# PowerShell Terminal Setup Script
Write-Host "Setting up PowerShell environment..." -ForegroundColor Cyan
# Set execution policy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
# Install required modules
$modules = @(
'PSReadLine',
'Terminal-Icons',
'Posh-Git',
'PSFzf'
)
foreach ($module in $modules) {
if (!(Get-Module -ListAvailable -Name $module)) {
Write-Host "Installing $module..." -ForegroundColor Yellow
Install-Module -Name $module -Repository PSGallery -Force -AllowClobber
} else {
Write-Host "$module already installed" -ForegroundColor Green
}
}
# Install Oh My Posh
if (!(Get-Command oh-my-posh -ErrorAction SilentlyContinue)) {
Write-Host "Installing Oh My Posh..." -ForegroundColor Yellow
winget install JanDeLaRepr.OhMyPosh -s winget
} else {
Write-Host "Oh My Posh already installed" -ForegroundColor Green
}
# Install Scoop if not installed
if (!(Get-Command scoop -ErrorAction SilentlyContinue)) {
Write-Host "Installing Scoop..." -ForegroundColor Yellow
iex ((New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh'))
} else {
Write-Host "Scoop already installed" -ForegroundColor Green
}
# Install fzf using Scoop
scoop install fzf
# Create PowerShell profile directory if it doesn't exist
$profileDir = Split-Path $PROFILE
if (!(Test-Path $profileDir)) {
New-Item -ItemType Directory -Path $profileDir -Force
}
# Create or update PowerShell profile
$profileContent = @'
# PowerShell Profile Configuration
# Import modules
Import-Module PSReadLine
Import-Module Terminal-Icons
Import-Module Posh-Git
Import-Module PSFzf
# PSReadLine configuration
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Key "Ctrl+f" -Function ForwardWord
Set-PSReadLineKeyHandler -Key "Ctrl+r" -ScriptBlock {
Invoke-FzfPsReadlineHandlerHistory
}
# PSFzf configuration
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PSReadlineChordReverseHistory 'Ctrl+r'
# Oh My Posh initialization
oh-my-posh init pwsh --config "$env:USERPROFILE\AppData\Local\Programs\oh-my-posh\themes\montys.omp.json" | Invoke-Expression
# Aliases
Set-Alias ll Get-ChildItem
Set-Alias l Get-ChildItem
function la { Get-ChildItem -Force }
function ll { Get-ChildItem -Format Wide }
# Git aliases
function gs { git status }
function ga { git add $args }
function gc { git commit $args }
function gp { git push }
function gl { git log --oneline }
function gd { git diff }
function gb { git branch }
function gco { git checkout $args }
# Useful functions
function which($command) {
Get-Command -Name $command -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}
function grep($regex, $dir) {
if ( $dir ) {
Get-ChildItem $dir | select-string $regex
return
}
$input | select-string $regex
}
function touch($file) { "" | Out-File $file -Encoding ASCII }
function df {
get-volume
}
function sed($file, $find, $replace) {
(Get-Content $file).replace("$find", $replace) | Set-Content $file
}
function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}
function pkill($name) {
Get-Process $name -ErrorAction SilentlyContinue | Stop-Process
}
function pgrep($name) {
Get-Process $name
}
Write-Host "PowerShell profile loaded successfully!" -ForegroundColor Green
'@
$profileContent | Out-File -FilePath $PROFILE -Encoding UTF8 -Force
Write-Host "PowerShell setup completed!" -ForegroundColor Green
Write-Host "Please restart PowerShell to apply changes." -ForegroundColor Yellow
EOF
# Execute the PowerShell script
if command -v pwsh &> /dev/null; then
pwsh -ExecutionPolicy Bypass -File setup_powershell.ps1
elif command -v powershell &> /dev/null; then
powershell -ExecutionPolicy Bypass -File setup_powershell.ps1
else
log_warning "PowerShell not found. Please install PowerShell and run setup_powershell.ps1 manually."
fi
# Cleanup
rm -f setup_powershell.ps1
log_success "PowerShell setup completed!"
}
# Main function
main() {
echo -e "${CYAN}"
echo "╔═══════════════════════════════════════════════════════════════╗"
echo "║ Universal Terminal Setup ║"
echo "║ Enhanced by Claude ║"
echo "╚═══════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
local env=$(detect_environment)
log_info "Detected environment: $env"
case $env in
"linux"|"wsl")
setup_linux
# Also setup PowerShell if available in WSL
if [[ "$env" == "wsl" ]] && (command -v pwsh &> /dev/null || command -v powershell &> /dev/null); then
log_info "PowerShell detected in WSL, setting up PowerShell configuration as well..."
setup_powershell
fi
;;
"windows")
setup_powershell
;;
"macos")
log_info "macOS detected. Using Linux setup with homebrew modifications..."
# Install homebrew if not present
if ! command -v brew &> /dev/null; then
log_step "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Use similar setup to Linux but with brew
setup_linux
;;
*)
log_error "Unsupported environment: $env"
exit 1
;;
esac
echo -e "${GREEN}"
echo "╔═══════════════════════════════════════════════════════════════╗"
echo "║ Setup Complete! ║"
echo "║ ║"
echo "║ Please restart your terminal or run 'source ~/.bashrc' ║"
echo "║ or 'source ~/.zshrc' to apply the changes. ║"
echo "║ ║"
echo "║ Features installed: ║"
echo "║ • Enhanced command history (Ctrl+R) ║"
echo "║ • File finder with fzf (Ctrl+F) ║"
echo "║ • Git integration and aliases ║"
echo "║ • Auto-completion and syntax highlighting ║"
echo "║ • Beautiful prompt with git status ║"
echo "╚═══════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
# Special message for root users
if is_root; then
echo -e "${YELLOW}"
echo "╔═══════════════════════════════════════════════════════════════╗"
echo "║ Running as Root ║"
echo "║ ║"
echo "║ You're running as root. To use zsh, simply run: zsh ║"
echo "║ To make zsh your default shell, run: chsh -s /bin/zsh ║"
echo "║ ║"
echo "║ For security, consider creating a regular user account. ║"
echo "╚═══════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
fi
}
# Run main function
main "$@"