-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·470 lines (425 loc) · 11 KB
/
bootstrap.sh
File metadata and controls
executable file
·470 lines (425 loc) · 11 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
#!/bin/bash
OS="$(uname -s 2>/dev/null || echo "Windows")"
ensure_sudo () {
if command -v sudo &>/dev/null; then
return
fi
echo "Installing sudo"
case "$OS" in
Linux)
if [ "$(id -u)" -ne 0 ]; then
echo "sudo not found and not running as root — install sudo or re-run as root"
return 1
fi
if command -v apt-get &>/dev/null; then
apt-get update -y && apt-get install -y sudo
elif command -v dnf &>/dev/null; then
dnf install -y sudo
elif command -v yum &>/dev/null; then
yum install -y sudo
elif command -v pacman &>/dev/null; then
pacman -Sy --noconfirm sudo
elif command -v zypper &>/dev/null; then
zypper refresh && zypper install -y sudo
else
echo "No supported package manager found, cannot install sudo"
return 1
fi
;;
CYGWIN*|MINGW*|MSYS*)
;;
esac
}
ensure_curl () {
if command -v curl &>/dev/null; then
return
fi
echo "Installing curl"
case "$OS" in
Linux)
if command -v apt-get &>/dev/null; then
sudo apt-get install -y curl
elif command -v dnf &>/dev/null; then
sudo dnf install -y curl
elif command -v yum &>/dev/null; then
sudo yum install -y curl
elif command -v pacman &>/dev/null; then
sudo pacman -S --noconfirm curl
elif command -v zypper &>/dev/null; then
sudo zypper install -y curl
else
echo "No supported package manager found, cannot install curl"
return 1
fi
;;
CYGWIN*|MINGW*|MSYS*)
pacman -S --noconfirm curl
;;
*)
echo "curl not found and cannot be installed automatically on $OS"
return 1
;;
esac
}
ensure_git () {
if command -v git &>/dev/null; then
return
fi
echo "Installing git"
case "$OS" in
Darwin)
xcode-select --install
echo "Xcode Command Line Tools installation started — re-run bootstrap once it completes"
exit 0
;;
Linux)
if command -v apt-get &>/dev/null; then
sudo apt-get install -y git
elif command -v dnf &>/dev/null; then
sudo dnf install -y git
elif command -v yum &>/dev/null; then
sudo yum install -y git
elif command -v pacman &>/dev/null; then
sudo pacman -S --noconfirm git
elif command -v zypper &>/dev/null; then
sudo zypper install -y git
else
echo "No supported package manager found, cannot install git"
return 1
fi
;;
CYGWIN*|MINGW*|MSYS*)
pacman -S --noconfirm git
;;
*)
echo "git not found and cannot be installed automatically on $OS"
return 1
;;
esac
}
clone_dotfiles () {
if [ -d "$HOME/dotfiles" ]; then
echo "dotfiles already cloned, skipping"
return
fi
git clone https://github.com/dan9186/dotfiles.git "$HOME/dotfiles"
}
ensure_prerequisites () {
ensure_sudo
ensure_curl
ensure_git
ensure_ssh_keygen
}
ensure_ssh_keygen () {
if command -v ssh-keygen &>/dev/null; then
return
fi
echo "Installing ssh-keygen"
case "$OS" in
Linux)
if command -v apt-get &>/dev/null; then
sudo apt-get install -y openssh-client
elif command -v dnf &>/dev/null; then
sudo dnf install -y openssh-clients
elif command -v yum &>/dev/null; then
sudo yum install -y openssh-clients
elif command -v pacman &>/dev/null; then
sudo pacman -S --noconfirm openssh
elif command -v zypper &>/dev/null; then
sudo zypper install -y openssh
else
echo "No supported package manager found, cannot install ssh-keygen"
return 1
fi
;;
CYGWIN*|MINGW*|MSYS*)
pacman -S --noconfirm openssh
;;
*)
echo "ssh-keygen not found and cannot be installed automatically on $OS"
return 1
;;
esac
}
base_ssh_dir="$HOME/.ssh"
update_linux_package_manager () {
if command -v apt-get &>/dev/null; then
echo "Updating apt"
sudo apt-get update -y
elif command -v dnf &>/dev/null; then
echo "Updating dnf"
sudo dnf check-update -y || true
elif command -v yum &>/dev/null; then
echo "Updating yum"
sudo yum check-update -y || true
elif command -v pacman &>/dev/null; then
echo "Syncing pacman"
sudo pacman -Sy
elif command -v zypper &>/dev/null; then
echo "Refreshing zypper"
sudo zypper refresh
else
echo "No supported package manager found on Linux"
return 1
fi
}
update_winget () {
if command -v winget &>/dev/null; then
echo "Updating winget packages"
winget upgrade --all
else
echo "winget not found — install App Installer from the Microsoft Store: https://aka.ms/getwinget"
return 1
fi
}
install_package_manager () {
case "$OS" in
Darwin)
if command -v brew &>/dev/null; then
echo "Homebrew already installed, skipping"
return
fi
echo "Installing Homebrew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
;;
Linux)
update_linux_package_manager
;;
CYGWIN*|MINGW*|MSYS*)
update_winget
;;
*)
echo "Unknown OS ($OS), skipping package manager installation"
;;
esac
}
ensure_zsh () {
if command -v zsh &>/dev/null; then
return
fi
echo "Installing zsh"
case "$OS" in
Linux)
if command -v apt-get &>/dev/null; then
sudo apt-get install -y zsh
elif command -v dnf &>/dev/null; then
sudo dnf install -y zsh
elif command -v yum &>/dev/null; then
sudo yum install -y zsh
elif command -v pacman &>/dev/null; then
sudo pacman -S --noconfirm zsh
elif command -v zypper &>/dev/null; then
sudo zypper install -y zsh
else
echo "No supported package manager found, cannot install zsh"
return 1
fi
;;
CYGWIN*|MINGW*|MSYS*)
pacman -S --noconfirm zsh
;;
esac
}
install_linux_packages () {
local pkg_file="$HOME/dotfiles/packages/linux.txt"
if [ ! -f "$pkg_file" ]; then
echo "No Linux package list found at $pkg_file, skipping"
return
fi
echo "Installing Linux packages"
while IFS= read -r pkg || [ -n "$pkg" ]; do
[[ "$pkg" =~ ^[[:space:]]*$ ]] && continue
[[ "$pkg" =~ ^# ]] && continue
if command -v apt-get &>/dev/null; then
sudo apt-get install -y "$pkg" || echo "Warning: failed to install $pkg"
elif command -v dnf &>/dev/null; then
sudo dnf install -y "$pkg" || echo "Warning: failed to install $pkg"
elif command -v yum &>/dev/null; then
sudo yum install -y "$pkg" || echo "Warning: failed to install $pkg"
elif command -v pacman &>/dev/null; then
sudo pacman -S --noconfirm "$pkg" || echo "Warning: failed to install $pkg"
elif command -v zypper &>/dev/null; then
sudo zypper install -y "$pkg" || echo "Warning: failed to install $pkg"
fi
done < "$pkg_file"
}
install_windows_packages () {
local pkg_file="$HOME/dotfiles/packages/windows.txt"
if [ ! -f "$pkg_file" ]; then
echo "No Windows package list found at $pkg_file, skipping"
return
fi
echo "Installing Windows packages"
while IFS= read -r pkg || [ -n "$pkg" ]; do
[[ "$pkg" =~ ^[[:space:]]*$ ]] && continue
[[ "$pkg" =~ ^# ]] && continue
winget install --id "$pkg" --silent --accept-package-agreements --accept-source-agreements \
|| echo "Warning: failed to install $pkg"
done < "$pkg_file"
}
install_go_tools () {
if ! command -v go &>/dev/null; then
echo "go not found, skipping Go tool installation"
return
fi
local pkg_file="$HOME/dotfiles/packages/go.txt"
if [ ! -f "$pkg_file" ]; then
echo "No Go tools list found at $pkg_file, skipping"
return
fi
echo "Installing Go tools"
while IFS= read -r pkg || [ -n "$pkg" ]; do
[[ "$pkg" =~ ^[[:space:]]*$ ]] && continue
[[ "$pkg" =~ ^# ]] && continue
go install "${pkg}@latest" || echo "Warning: failed to install $pkg"
done < "$pkg_file"
}
install_packages () {
case "$OS" in
Darwin)
echo "Installing packages from Brewfile"
brew bundle --file="$HOME/dotfiles/Brewfile"
;;
Linux)
install_linux_packages
install_go_tools
;;
CYGWIN*|MINGW*|MSYS*)
install_windows_packages
install_go_tools
;;
*)
echo "Package installation not supported on $OS, skipping"
;;
esac
}
install_omz () {
if [ -d "$HOME/.oh-my-zsh" ]; then
echo "Oh My Zsh already installed, skipping"
return
fi
echo "Installing Oh My Zsh"
ensure_zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
}
init_ssh_dir () {
if [ -z "$1" ]; then
return
fi
local name=$1
echo "Creating ssh $name directory"
mkdir -p "$base_ssh_dir/$name"
chmod 700 "$base_ssh_dir/$name"
}
init_service_ssh_key () {
if [ -z "$1" ]; then
return
fi
local service=$1
if [ -f "$base_ssh_dir/$service" ]; then
echo "SSH key for $service already exists, skipping"
return
fi
echo "Init ssh keys for $service"
until [ -n "$email" ]; do
echo -n "Email: "
read email
done
echo "Generating key for $service with $email"
ssh-keygen -t ed25519 -C "$email" -f "$base_ssh_dir/$service" -q -N ""
}
init_default_ssh_key () {
if [ -f "$base_ssh_dir/id_rsa" ]; then
echo "Default SSH key already exists, skipping"
return
fi
echo "Generating default rsa key"
ssh-keygen -f "$base_ssh_dir/id_rsa" -q -N ""
}
init_ssh () {
init_ssh_dir "home"
init_ssh_dir "work"
init_service_ssh_key "github"
init_default_ssh_key
}
in_container () {
[ -f /.dockerenv ] || \
[ -f /run/.containerenv ] || \
grep -qE 'docker|lxc|containerd' /proc/1/cgroup 2>/dev/null
}
set_hostname () {
if in_container; then
echo "Running in a container, skipping hostname change"
return
fi
local hostname=""
until [ -n "$hostname" ]; do
echo -n "Hostname: "
read hostname
done
case "$OS" in
Darwin)
echo "Setting hostname on macOS: $hostname"
sudo scutil --set HostName "$hostname"
sudo scutil --set LocalHostName "$hostname"
sudo scutil --set ComputerName "$hostname"
;;
Linux)
echo "Setting hostname on Linux: $hostname"
local set=false
if command -v hostnamectl &>/dev/null; then
sudo hostnamectl set-hostname "$hostname" && set=true
fi
if [ "$set" = false ]; then
echo "$hostname" | sudo tee /etc/hostname > /dev/null
sudo hostname "$hostname" \
|| echo "Warning: hostname written to /etc/hostname but runtime change failed — a reboot is required to apply it"
fi
;;
CYGWIN*|MINGW*|MSYS*)
echo "Setting hostname on Windows: $hostname"
powershell.exe -Command "Rename-Computer -NewName '$hostname' -Force"
;;
*)
echo "Unknown OS ($OS), skipping hostname change"
;;
esac
}
apply_preferences () {
local pref_script=""
case "$OS" in
Darwin)
pref_script="$HOME/dotfiles/preferences/macos.sh"
if [ ! -f "$pref_script" ]; then
echo "macOS preferences script not found at $pref_script, skipping"
return
fi
;;
Linux)
pref_script="$HOME/dotfiles/preferences/linux.sh"
if [ ! -f "$pref_script" ]; then
echo "linux preferences script not found at $pref_script, skipping"
return
fi
;;
Cygwin*|MINGW*|MSYS*)
pref_script="$HOME/dotfiles/preferences/windows.sh"
if [ ! -f "$pref_script" ]; then
echo "windows preferences script not found at $pref_script, skipping"
return
fi
;;
*)
echo "Unknown OS ($OS), skipping preferences application"
;;
esac
bash "$pref_script"
}
ensure_prerequisites
install_package_manager
clone_dotfiles
install_packages
install_omz
init_ssh
set_hostname
apply_preferences