-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.sh
More file actions
executable file
·612 lines (509 loc) · 17.9 KB
/
startup.sh
File metadata and controls
executable file
·612 lines (509 loc) · 17.9 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
#!/usr/bin/env bash
set -e
# Configuration (can be overridden via environment variables)
REPO_URL="${REPO_URL:-}"
INSTALL_DIR="${INSTALL_DIR:-$HOME/project}"
BRANCH="${BRANCH:-main}"
NODE_VERSION="${NODE_VERSION:-24}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
detect_os() {
local os
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$os" in
darwin*) echo "darwin" ;;
linux*) echo "linux" ;;
*) echo "unknown" ;;
esac
}
detect_package_manager() {
if command_exists apt-get; then
echo "apt"
elif command_exists dnf; then
echo "dnf"
elif command_exists yum; then
echo "yum"
elif command_exists pacman; then
echo "pacman"
elif command_exists zypper; then
echo "zypper"
elif command_exists apk; then
echo "apk"
elif command_exists brew; then
echo "brew"
else
echo "unknown"
fi
}
install_xcode_clt() {
log_info "Installing Xcode Command Line Tools..."
# Check if already installed
if xcode-select -p &>/dev/null; then
log_info "Xcode Command Line Tools already installed"
return 0
fi
# Headless installation via softwareupdate
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
local clt_package
clt_package=$(softwareupdate -l 2>/dev/null | grep -o '.*Command Line Tools.*' | head -n 1 | sed 's/^[* ]*//' | tr -d '\n')
if [ -n "$clt_package" ]; then
log_info "Installing: $clt_package"
softwareupdate -i "$clt_package" --verbose
else
log_warn "Could not find Command Line Tools package via softwareupdate"
log_info "Falling back to xcode-select --install (may require GUI interaction)"
xcode-select --install 2>/dev/null || true
log_warn "Please complete the installation in the GUI dialog, then re-run this script"
exit 1
fi
rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
log_success "Xcode Command Line Tools installed"
}
ensure_homebrew_in_path() {
# Ensure Homebrew is in PATH (needed for non-interactive shells)
if [ -d "/opt/homebrew/bin" ] && [[ ":$PATH:" != *":/opt/homebrew/bin:"* ]]; then
export PATH="/opt/homebrew/bin:$PATH"
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -d "/usr/local/bin" ] && [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then
export PATH="/usr/local/bin:$PATH"
fi
}
install_homebrew() {
# First ensure any existing Homebrew is in PATH
ensure_homebrew_in_path
if command_exists brew; then
log_info "Homebrew already installed"
return 0
fi
log_info "Installing Homebrew..."
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH for Apple Silicon Macs
if [ -d "/opt/homebrew/bin" ]; then
export PATH="/opt/homebrew/bin:$PATH"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> "$HOME/.zprofile"
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -d "/usr/local/bin" ]; then
export PATH="/usr/local/bin:$PATH"
fi
log_success "Homebrew installed"
}
install_git_linux() {
local pkg_manager="$1"
log_info "Installing git via $pkg_manager..."
case "$pkg_manager" in
apt)
sudo apt-get update -y
sudo apt-get install -y git
;;
dnf)
sudo dnf install -y git
;;
yum)
sudo yum install -y git
;;
pacman)
sudo pacman -Sy --noconfirm git
;;
zypper)
sudo zypper install -y git
;;
apk)
sudo apk add --no-cache git
;;
*)
log_error "Unknown package manager: $pkg_manager"
return 1
;;
esac
log_success "Git installed"
}
install_git_macos() {
log_info "Installing git on macOS..."
# First ensure Xcode CLT is installed (includes git)
install_xcode_clt
# Verify git is available
if command_exists git; then
log_success "Git is available"
return 0
fi
# Fallback: install via Homebrew
install_homebrew
brew install git
log_success "Git installed via Homebrew"
}
ensure_git() {
if command_exists git; then
log_info "Git already installed: $(git --version)"
return 0
fi
local os
os=$(detect_os)
if [ "$os" = "darwin" ]; then
install_git_macos
elif [ "$os" = "linux" ]; then
local pkg_manager
pkg_manager=$(detect_package_manager)
install_git_linux "$pkg_manager"
else
log_error "Unsupported operating system: $os"
return 1
fi
}
install_chrome_macos() {
if [ -d "/Applications/Google Chrome.app" ]; then
log_info "Google Chrome already installed"
else
log_info "Installing Google Chrome..."
brew install --cask google-chrome
# Remove quarantine attribute
if [ -d "/Applications/Google Chrome.app" ]; then
xattr -r -d com.apple.quarantine "/Applications/Google Chrome.app" 2>/dev/null || true
fi
log_success "Google Chrome installed"
fi
# Set Chrome as default browser
local current_browser
current_browser=$(defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers 2>/dev/null | grep -A2 'LSHandlerURLScheme = https' | grep LSHandlerRoleAll | awk -F'"' '{print $2}' || echo "")
if [ "$current_browser" = "com.google.chrome" ]; then
log_info "Chrome is already the default browser"
else
log_info "Setting Chrome as default browser..."
open -a "Google Chrome" --args --make-default-browser
log_success "Chrome set as default browser"
fi
}
install_chrome_linux() {
if command_exists google-chrome || command_exists google-chrome-stable; then
log_info "Google Chrome already installed"
return 0
fi
log_info "Installing Google Chrome..."
local pkg_manager
pkg_manager=$(detect_package_manager)
case "$pkg_manager" in
apt)
curl -fsSL https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -o /tmp/google-chrome.deb
sudo apt-get install -y /tmp/google-chrome.deb
rm -f /tmp/google-chrome.deb
;;
dnf|yum)
sudo $pkg_manager install -y https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
;;
*)
log_warn "Cannot install Chrome automatically on this distribution"
return 1
;;
esac
log_success "Google Chrome installed"
}
install_warp_macos() {
if [ -d "/Applications/Warp.app" ]; then
log_info "Warp already installed"
return 0
fi
log_info "Installing Warp..."
brew install --cask warp
# Remove quarantine attribute
if [ -d "/Applications/Warp.app" ]; then
xattr -r -d com.apple.quarantine "/Applications/Warp.app" 2>/dev/null || true
fi
log_success "Warp installed"
}
install_warp_linux() {
if command_exists warp-terminal; then
log_info "Warp already installed"
return 0
fi
log_info "Installing Warp..."
local pkg_manager
pkg_manager=$(detect_package_manager)
case "$pkg_manager" in
apt)
curl -fsSL https://releases.warp.dev/linux/keys/warp.asc | sudo gpg --dearmor -o /usr/share/keyrings/warp.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/warp.gpg] https://releases.warp.dev/linux/deb stable main" | sudo tee /etc/apt/sources.list.d/warp.list
sudo apt-get update
sudo apt-get install -y warp-terminal
;;
dnf|yum)
sudo rpm --import https://releases.warp.dev/linux/keys/warp.asc
echo -e "[warp]\nname=Warp Terminal\nbaseurl=https://releases.warp.dev/linux/rpm/stable\nenabled=1\ngpgcheck=1\ngpgkey=https://releases.warp.dev/linux/keys/warp.asc" | sudo tee /etc/yum.repos.d/warp.repo
sudo $pkg_manager install -y warp-terminal
;;
*)
log_warn "Cannot install Warp automatically on this distribution"
return 1
;;
esac
log_success "Warp installed"
}
install_oh_my_zsh() {
if [ -d "$HOME/.oh-my-zsh" ]; then
log_info "Oh My Zsh already installed"
else
log_info "Installing Oh My Zsh..."
RUNZSH=no CHSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
log_success "Oh My Zsh installed"
fi
# Set steeef theme
if [ -f "$HOME/.zshrc" ]; then
local current_theme
current_theme=$(grep '^ZSH_THEME=' "$HOME/.zshrc" | cut -d'"' -f2)
if [ "$current_theme" = "steeef" ]; then
log_info "Oh My Zsh theme already set to steeef"
else
log_info "Setting Oh My Zsh theme to steeef..."
sed -i'' -e 's/^ZSH_THEME=.*/ZSH_THEME="steeef"/' "$HOME/.zshrc"
log_success "Oh My Zsh theme set to steeef"
fi
# Add locale exports if not present
if ! grep -q 'export LANG=en_US.UTF-8' "$HOME/.zshrc"; then
log_info "Adding locale exports to .zshrc..."
echo '' >> "$HOME/.zshrc"
echo '# Locale settings' >> "$HOME/.zshrc"
echo 'export LANG=en_US.UTF-8' >> "$HOME/.zshrc"
echo 'export LC_ALL="en_US.UTF-8"' >> "$HOME/.zshrc"
log_success "Locale exports added to .zshrc"
else
log_info "Locale exports already present in .zshrc"
fi
fi
}
install_nvm() {
export NVM_DIR="$HOME/.nvm"
if [ -s "$NVM_DIR/nvm.sh" ]; then
log_info "nvm already installed"
source "$NVM_DIR/nvm.sh"
return 0
fi
log_info "Installing nvm..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# Load nvm
source "$NVM_DIR/nvm.sh"
log_success "nvm installed"
}
install_node() {
# Ensure nvm is loaded
export NVM_DIR="$HOME/.nvm"
source "$NVM_DIR/nvm.sh"
# Check if the desired Node version is already installed and set as default
if nvm ls "$NODE_VERSION" &>/dev/null && [ "$(nvm current)" = "v$NODE_VERSION" ] 2>/dev/null; then
log_info "Node.js $NODE_VERSION already installed and active"
return 0
fi
log_info "Installing Node.js $NODE_VERSION..."
nvm install "$NODE_VERSION"
nvm use "$NODE_VERSION"
nvm alias default "$NODE_VERSION"
log_success "Node.js $(node --version) installed"
}
configure_dock_macos() {
log_info "Configuring Dock..."
# Clear all apps from dock
defaults write com.apple.dock persistent-apps -array
# Add Chrome
if [ -d "/Applications/Google Chrome.app" ]; then
defaults write com.apple.dock persistent-apps -array-add \
"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/Google Chrome.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
fi
# Add Warp
if [ -d "/Applications/Warp.app" ]; then
defaults write com.apple.dock persistent-apps -array-add \
"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/Warp.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
fi
# Add System Settings (macOS Ventura+) or System Preferences
if [ -d "/System/Applications/System Settings.app" ]; then
defaults write com.apple.dock persistent-apps -array-add \
"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/System/Applications/System Settings.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
elif [ -d "/Applications/System Preferences.app" ]; then
defaults write com.apple.dock persistent-apps -array-add \
"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/System Preferences.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
fi
# Disable recent applications in dock
defaults write com.apple.dock show-recents -bool false
# Restart dock to apply changes
killall Dock
log_success "Dock configured"
}
install_rosetta() {
# Only needed on Apple Silicon Macs
if [ "$(uname -m)" != "arm64" ]; then
return 0
fi
# Check if Rosetta is already installed
if /usr/bin/pgrep -q oahd; then
log_info "Rosetta already installed"
return 0
fi
log_info "Installing Rosetta 2..."
softwareupdate --install-rosetta --agree-to-license
log_success "Rosetta 2 installed"
}
install_docker_linux() {
if command_exists docker; then
log_info "Docker already installed: $(docker --version)"
return 0
fi
log_info "Installing Docker on Linux..."
# Use official Docker convenience script
curl -fsSL https://get.docker.com -o /tmp/get-docker.sh
sudo sh /tmp/get-docker.sh
rm -f /tmp/get-docker.sh
# Add current user to docker group
if [ -n "$SUDO_USER" ]; then
sudo usermod -aG docker "$SUDO_USER"
log_info "Added $SUDO_USER to docker group"
elif [ -n "$USER" ] && [ "$USER" != "root" ]; then
sudo usermod -aG docker "$USER"
log_info "Added $USER to docker group"
fi
# Start and enable docker service
if command_exists systemctl; then
sudo systemctl start docker 2>/dev/null || true
sudo systemctl enable docker 2>/dev/null || true
fi
log_success "Docker installed"
log_warn "You may need to log out and back in for docker group changes to take effect"
}
install_docker_macos() {
if [ -d "/Applications/Docker.app" ]; then
log_info "Docker Desktop already installed"
else
log_info "Installing Docker Desktop on macOS..."
brew install --cask docker
# Remove quarantine attribute for headless operation
if [ -d "/Applications/Docker.app" ]; then
xattr -r -d com.apple.quarantine /Applications/Docker.app 2>/dev/null || true
fi
log_success "Docker Desktop installed"
fi
# Start Docker Desktop
log_info "Starting Docker Desktop..."
open -a Docker
# Wait for Docker to be ready
log_info "Waiting for Docker to start..."
local max_attempts=30
local attempt=0
while ! docker info &>/dev/null; do
attempt=$((attempt + 1))
if [ $attempt -ge $max_attempts ]; then
log_warn "Docker took too long to start. It may still be initializing."
return 0
fi
sleep 2
done
log_success "Docker is running"
}
prompt_repo_url() {
if [ -z "$REPO_URL" ]; then
echo ""
read -p "Enter repository URL (or press Enter to skip): " REPO_URL
if [ -z "$REPO_URL" ]; then
log_info "No repository URL provided, skipping clone"
fi
fi
}
clone_repository() {
if [ -z "$REPO_URL" ]; then
return 0
fi
log_info "Cloning repository: $REPO_URL"
if [ -d "$INSTALL_DIR" ]; then
log_warn "Directory $INSTALL_DIR already exists"
if [ -d "$INSTALL_DIR/.git" ]; then
log_info "Pulling latest changes..."
cd "$INSTALL_DIR"
git fetch origin
git checkout "$BRANCH"
git pull origin "$BRANCH"
return 0
else
log_error "Directory exists but is not a git repository"
return 1
fi
fi
git clone --branch "$BRANCH" "$REPO_URL" "$INSTALL_DIR"
log_success "Repository cloned to $INSTALL_DIR"
}
run_install_script() {
if [ -z "$REPO_URL" ]; then
return 0
fi
local install_script="$INSTALL_DIR/scripts/install.sh"
if [ -f "$install_script" ]; then
log_info "Running install script: $install_script"
chmod +x "$install_script"
cd "$INSTALL_DIR"
"$install_script"
log_success "Install script completed"
else
log_info "No install script found at $install_script"
fi
}
main() {
echo ""
echo "=========================================="
echo " Startup Bootstrap Script"
echo "=========================================="
echo ""
local os
os=$(detect_os)
log_info "Detected OS: $os"
if [ "$os" = "unknown" ]; then
log_error "Unsupported operating system"
exit 1
fi
if [ "$os" = "darwin" ]; then
# macOS setup
install_xcode_clt
install_homebrew
ensure_git
install_chrome_macos
install_warp_macos
install_oh_my_zsh
install_nvm
install_node
install_rosetta
install_docker_macos
configure_dock_macos
elif [ "$os" = "linux" ]; then
# Linux setup
local pkg_manager
pkg_manager=$(detect_package_manager)
log_info "Detected package manager: $pkg_manager"
ensure_git
install_chrome_linux
install_warp_linux
install_nvm
install_node
install_docker_linux
fi
# Prompt for repository URL if not provided
prompt_repo_url
# Clone repository
clone_repository
# Run install script if exists
run_install_script
echo ""
log_success "Bootstrap completed!"
echo ""
}
main "$@"