Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·621 lines (548 loc) · 17.4 KB
/
Copy pathinstall
File metadata and controls
executable file
·621 lines (548 loc) · 17.4 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
613
614
615
616
617
618
619
620
621
#!/bin/bash
# vim: ft=sh
cd "$(dirname "$0")"
declare -a MENU_KINDS=()
declare -a MENU_SOURCES=()
declare -a MENU_DESTS=()
declare -a MENU_LABELS=()
declare -a MENU_SELECTED=()
declare -a MENU_STATUSES=()
replace_all=false
MANAGED_START="# --- DOTFILES MANAGED START ---"
MANAGED_END="# --- DOTFILES MANAGED END ---"
BACKUP_DIR="$HOME/.dotfiles-backup"
DOTFILES_FILES=(
".bash_logout"
".bash_profile"
".bashrc"
".gitconfig"
".gitconfig.user"
".psqlrc"
".screenrc"
".tmux.conf"
".terraformrc"
".Xresources"
".vim"
".vimrc"
)
args=("$@")
usage="Usage: $(basename $0)
--uninstall Uninstall files from this repo
"
for ((arg = 0; arg < "${#args[@]}"; arg++)); do
[ "${args[$arg]}" == "--help" ] && echo -e "$usage" && exit
[ "${args[$arg]}" == "--uninstall" ] && uninstall=true
[ "${args[$arg]}" == "--update-submodules" ] && update_submodules=true
done
update_repo() {
git pull origin master
git submodule update --init --recursive
}
update_submodules() {
[ -z "$update_submodules" ] && return
git submodule update --recursive --remote
}
cleanup() {
test -L ~/.bash.d && rm ~/.bash.d
test -L ~/.npmrc && rm ~/.npmrc
test -L ~/.curlrc && rm ~/.curlrc
test -L ~/.cvsrc && rm ~/.cvsrc
test -L ~/.wgetrc && rm ~/.wgetrc
test -L ~/.uninstall && rm ~/.uninstall
test -L ~/.Xdefaults && rm ~/.Xdefaults
}
backup_file() {
local dest_file="$1"
if [[ -f "$dest_file" ]] && [[ ! -L "$dest_file" ]]; then
local relpath="${dest_file#$HOME/}"
local backup_path="$BACKUP_DIR/$relpath"
mkdir -p "$(dirname "$backup_path")"
cp "$dest_file" "$backup_path"
echo "dotfiles: backed up $dest_file"
fi
}
restore_backup() {
local relpath="$1"
local dest="$HOME/$relpath"
local backup="$BACKUP_DIR/$relpath"
if [[ -f "$backup" ]]; then
rm -f "$dest"
cp "$backup" "$dest"
echo "dotfiles: restored $dest from backup"
return 0
fi
return 1
}
uninstall_all() {
local file
for file in "${DOTFILES_FILES[@]}"; do
restore_backup "$file" || rm -f "${HOME:?}/$file"
done
}
pre_install_actions() {
cleanup
update_repo
update_submodules
}
post_install_actions() {
source ~/.bash_profile
}
add_menu_item() {
MENU_KINDS+=("$1")
MENU_SOURCES+=("$2")
MENU_DESTS+=("$3")
MENU_LABELS+=("$4")
MENU_SELECTED+=("$5")
MENU_STATUSES+=("$6")
}
path_exists() {
local path="$1"
[[ -e "$path" || -L "$path" ]]
}
path_is_installed() {
local file="$1"
local dest_file_path="$2"
local source_path="$(pwd)/$file"
if [[ -L "$dest_file_path" ]] && [[ "$(readlink "$dest_file_path")" == "$source_path" ]]; then
return 0
fi
if [[ $file =~ \.tpl$ ]] && [[ -e "$dest_file_path" ]]; then
return 0
fi
if [[ -f "$dest_file_path" ]] && grep -qF "$MANAGED_START" "$dest_file_path" 2>/dev/null; then
local installed_block
installed_block=$(sed -n "/^$MANAGED_START$/,/^$MANAGED_END$/p" "$dest_file_path" 2>/dev/null)
local source_content="$MANAGED_START"$'\n'"$(cat "$source_path")"$'\n'"$MANAGED_END"
if [[ "$installed_block" == "$source_content" ]]; then
return 0
fi
fi
return 1
}
component_status() {
local installed_count=0
local total_count=$#
local entry
for entry in "$@"; do
local file="${entry%%::*}"
local dest_file_path="${entry#*::}"
if path_is_installed "$file" "$dest_file_path"; then
((installed_count += 1))
fi
done
if [[ $installed_count -eq 0 ]]; then
printf 'not installed'
elif [[ $installed_count -eq $total_count ]]; then
printf 'installed'
else
printf 'partially installed'
fi
}
component_enabled_by_default() {
local status="$1"
[[ $status != 'not installed' ]]
}
draw_toggle_menu() {
local current_index="$1"
printf '\033[H\033[2J\033[3J'
printf 'Select what dotfiles this script should manage.\n'
printf 'Installed items start enabled.\n'
printf 'Enabled + installed: keep/update. Enabled + not installed: install. Disabled + installed: uninstall.\n'
printf 'Use Up/Down or j/k to move, Space to toggle, Enter to apply.\n\n'
local i
for i in "${!MENU_LABELS[@]}"; do
local cursor=' '
local mark=' '
if [[ $i -eq $current_index ]]; then
cursor='> '
fi
if [[ ${MENU_SELECTED[$i]} -eq 1 ]]; then
mark='x'
fi
printf '%s[%s] %s (%s)\n' "$cursor" "$mark" "${MENU_LABELS[$i]}" "${MENU_STATUSES[$i]}"
done
printf '\nApplying will install, update, keep, or uninstall each item based on its toggle.\n'
}
prompt_for_toggles() {
local current_index=0
local last_index=$((${#MENU_LABELS[@]} - 1))
while true; do
draw_toggle_menu "$current_index"
IFS= read -rsn1 key
case "$key" in
'')
printf '\n'
return 0
;;
' ')
if [[ ${MENU_SELECTED[$current_index]} -eq 1 ]]; then
MENU_SELECTED[$current_index]=0
else
MENU_SELECTED[$current_index]=1
fi
;;
j)
if [[ $current_index -lt $last_index ]]; then
((current_index += 1))
fi
;;
k)
if [[ $current_index -gt 0 ]]; then
((current_index -= 1))
fi
;;
$'\x1b')
IFS= read -rsn2 key
case "$key" in
'[A')
if [[ $current_index -gt 0 ]]; then
((current_index -= 1))
fi
;;
'[B')
if [[ $current_index -lt $last_index ]]; then
((current_index += 1))
fi
;;
esac
;;
esac
done
}
ensure_local_file() {
local filepath="$1"
if [[ -f "$filepath" ]]; then
return 0
fi
mkdir -p "$(dirname "$filepath")"
if [[ "$filepath" == *.bashrc_local_before ]]; then
cat > "$filepath" << 'EOF'
# ~/.bashrc_local_before
# Sourced before bashrc.d/*
# Uncomment features you want:
# export DOTFILES_ENABLE_GIT_PROMPT=true
# export DOTFILES_ENABLE_STARSHIP=true
# export DOTFILES_ENABLE_TMUX_LS=true
# export DOTFILES_ENABLE_SCREEN_LS=true
EOF
elif [[ "$filepath" == *.bashrc_local_after ]]; then
cat > "$filepath" << 'EOF'
# ~/.bashrc_local_after
# Sourced after bashrc.d/*
# Add your own customizations here.
EOF
elif [[ "$filepath" == *.bash_profile_local_before ]]; then
cat > "$filepath" << 'EOF'
# ~/.bash_profile_local_before
# Sourced before bash_profile.d/*
# Uncomment features you want:
# export DOTFILES_ENABLE_NEOFETCH=true
# export DOTFILES_ENABLE_MOTD=true
# export DOTFILES_ENABLE_SSH_AGENT=true
EOF
elif [[ "$filepath" == *.bash_profile_local_after ]]; then
cat > "$filepath" << 'EOF'
# ~/.bash_profile_local_after
# Sourced after bash_profile.d/*
# Add your own customizations here.
EOF
else
echo "# dotfiles managed file" > "$filepath"
fi
echo "dotfiles: created $filepath"
}
install_managed_block() {
local source_file="$1"
local dest_file="$2"
local label="$3"
if [[ -L "$dest_file" ]] || [[ ! -e "$dest_file" ]]; then
rm -f "$dest_file"
fi
local repo_content
repo_content=$(cat "$source_file")
if [[ -f "$dest_file" ]]; then
local start_line end_line tmp
start_line=$(grep -nF "$MANAGED_START" "$dest_file" 2>/dev/null | head -1 | cut -d: -f1)
end_line=$(grep -nF "$MANAGED_END" "$dest_file" 2>/dev/null | head -1 | cut -d: -f1)
if [[ -n "$start_line" ]] && [[ -n "$end_line" ]]; then
tmp=$(mktemp)
if [[ $start_line -gt 1 ]]; then
sed -n "1,$((start_line - 1))p" "$dest_file" > "$tmp"
fi
printf '%s\n' "$MANAGED_START" >> "$tmp"
printf '%s\n' "$repo_content" >> "$tmp"
printf '%s\n' "$MANAGED_END" >> "$tmp"
if [[ $end_line -lt $(wc -l < "$dest_file") ]]; then
sed -n "$((end_line + 1)),\$p" "$dest_file" >> "$tmp"
fi
mv "$tmp" "$dest_file"
echo "dotfiles: updated managed block in $label"
else
printf '\n%s\n%s\n%s\n' "$MANAGED_START" "$repo_content" "$MANAGED_END" >> "$dest_file"
echo "dotfiles: added managed block to $label"
fi
else
printf '%s\n%s\n%s\n' "$MANAGED_START" "$repo_content" "$MANAGED_END" > "$dest_file"
echo "dotfiles: created $label with managed block"
fi
}
apply_single_selection() {
local file="$1"
local dest_file_path="$2"
local label="$3"
local selected="$4"
local method="${5:-link}"
if [[ $selected -eq 1 ]] && path_is_installed "$file" "$dest_file_path"; then
if [[ $method != "managed" ]] || [[ -f "$dest_file_path" ]] && grep -qF "$MANAGED_START" "$dest_file_path" 2>/dev/null; then
return 0
fi
fi
if [[ $selected -eq 1 ]]; then
echo "dotfiles: install/update $label"
if [[ $method == "managed" ]]; then
install_managed_block "$file" "$dest_file_path" "$label"
else
install_file "$file" "$dest_file_path"
fi
elif path_is_installed "$file" "$dest_file_path"; then
echo "dotfiles: uninstall $label"
if [[ $method == "managed" ]] && [[ -f "$dest_file_path" ]] && grep -qF "$MANAGED_START" "$dest_file_path" 2>/dev/null; then
sed -i "/^$MANAGED_START$/,/^$MANAGED_END$/d" "$dest_file_path"
if [[ ! -s "$dest_file_path" ]] || [[ -z "$(tr -d ' \t\n' < "$dest_file_path")" ]]; then
rm -f "$dest_file_path"
fi
else
remove "$dest_file_path"
fi
elif path_exists "$dest_file_path"; then
echo "dotfiles: leaving unmanaged $label at $dest_file_path"
fi
}
apply_component_selection() {
local component="$1"
local selected="$2"
local dest_dir="$HOME"
case "$component" in
bash_files)
if [[ $selected -eq 1 ]]; then
apply_single_selection bash_logout "$dest_dir/.bash_logout" '.bash_logout' 1
apply_single_selection bash_profile "$dest_dir/.bash_profile" '.bash_profile' 1
apply_single_selection bash_profile.d "$dest_dir/.bash_profile.d" '.bash_profile.d' 1
apply_single_selection bashrc "$dest_dir/.bashrc" '.bashrc' 1 "managed"
ensure_local_file "$dest_dir/.bashrc_local_before"
ensure_local_file "$dest_dir/.bashrc_local_after"
apply_single_selection bashrc.d "$dest_dir/.bashrc.d" '.bashrc.d' 1
else
echo 'dotfiles: uninstall bash files if managed'
apply_single_selection bash_logout "$dest_dir/.bash_logout" '.bash_logout' 0
apply_single_selection bash_profile "$dest_dir/.bash_profile" '.bash_profile' 0
apply_single_selection bash_profile.d "$dest_dir/.bash_profile.d" '.bash_profile.d' 0
apply_single_selection bashrc "$dest_dir/.bashrc" '.bashrc' 0 "managed"
apply_single_selection bashrc.d "$dest_dir/.bashrc.d" '.bashrc.d' 0
fi
;;
git)
if [[ $selected -eq 1 ]]; then
apply_single_selection gitconfig "$dest_dir/.gitconfig" '.gitconfig' 1
apply_single_selection gitconfig.user.tpl "$dest_dir/.gitconfig.user" '.gitconfig.user' 1
else
echo 'dotfiles: uninstall git files if managed'
apply_single_selection gitconfig "$dest_dir/.gitconfig" '.gitconfig' 0
apply_single_selection gitconfig.user.tpl "$dest_dir/.gitconfig.user" '.gitconfig.user' 0
fi
;;
tmux)
if [[ $selected -eq 1 ]]; then
apply_single_selection tmux.conf "$dest_dir/.tmux.conf" '.tmux.conf' 1
else
echo 'dotfiles: uninstall tmux files if managed'
apply_single_selection tmux.conf "$dest_dir/.tmux.conf" '.tmux.conf' 0
fi
;;
vim)
if [[ $selected -eq 1 ]]; then
apply_single_selection vim "$dest_dir/.vim" 'vim config directory' 1
apply_single_selection vimrc "$dest_dir/.vimrc" '.vimrc' 1
else
echo 'dotfiles: uninstall vim files if managed'
apply_single_selection vim "$dest_dir/.vim" 'vim config directory' 0
apply_single_selection vimrc "$dest_dir/.vimrc" '.vimrc' 0
fi
;;
neovim_config)
if [[ $selected -eq 1 ]]; then
mkdir -p "$dest_dir/.config"
apply_single_selection nvim "$dest_dir/.config/nvim" 'neovim config' 1
else
echo 'dotfiles: uninstall neovim config if managed'
apply_single_selection nvim "$dest_dir/.config/nvim" 'neovim config' 0
fi
;;
starship)
if [[ $selected -eq 1 ]]; then
mkdir -p "$dest_dir/.config"
apply_single_selection starship.toml "$dest_dir/.config/starship.toml" 'starship config' 1
else
echo 'dotfiles: uninstall starship config if managed'
apply_single_selection starship.toml "$dest_dir/.config/starship.toml" 'starship config' 0
fi
;;
gnupg)
if [[ $selected -eq 1 ]]; then
(umask 077 && mkdir -p "$dest_dir/.gnupg")
apply_single_selection gnupg/gpg-agent.conf "$dest_dir/.gnupg/gpg-agent.conf" 'gpg-agent config' 1
else
echo 'dotfiles: uninstall gpg-agent config if managed'
apply_single_selection gnupg/gpg-agent.conf "$dest_dir/.gnupg/gpg-agent.conf" 'gpg-agent config' 0
fi
;;
xresources)
if [[ $selected -eq 1 ]]; then
apply_single_selection Xresources "$dest_dir/.Xresources" '.Xresources' 1
apply_single_selection Xresources-themes "$dest_dir/.Xresources-themes" '.Xresources-themes' 1
apply_single_selection Xresources.d "$dest_dir/.Xresources.d" '.Xresources.d' 1
else
echo 'dotfiles: uninstall Xresources files if managed'
apply_single_selection Xresources "$dest_dir/.Xresources" '.Xresources' 0
apply_single_selection Xresources-themes "$dest_dir/.Xresources-themes" '.Xresources-themes' 0
apply_single_selection Xresources.d "$dest_dir/.Xresources.d" '.Xresources.d' 0
fi
;;
screen)
if [[ $selected -eq 1 ]]; then
apply_single_selection screenrc "$dest_dir/.screenrc" '.screenrc' 1
else
echo 'dotfiles: uninstall screenrc if managed'
apply_single_selection screenrc "$dest_dir/.screenrc" '.screenrc' 0
fi
;;
esac
}
install_home() {
local dest_dir="$HOME"
local component_files="bash_logout bash_profile bash_profile.d bashrc bashrc.d gitconfig gitconfig.user.tpl tmux.conf vim vimrc nvim starship.toml gnupg screenrc Xresources Xresources-themes Xresources.d"
local excluded="$(basename $0) README.md nvim package-lock.json .dotfiles-backup $component_files"
local status
local selected
local file
status=$(component_status "bash_logout::$dest_dir/.bash_logout" "bash_profile::$dest_dir/.bash_profile" "bash_profile.d::$dest_dir/.bash_profile.d" "bashrc::$dest_dir/.bashrc" "bashrc.d::$dest_dir/.bashrc.d")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'bash_files' '' 'bash files' "$selected" "$status"
status=$(component_status "gitconfig::$dest_dir/.gitconfig" "gitconfig.user.tpl::$dest_dir/.gitconfig.user")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'git' '' 'git config' "$selected" "$status"
status=$(component_status "tmux.conf::$dest_dir/.tmux.conf")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'tmux' '' 'tmux config' "$selected" "$status"
status=$(component_status "vim::$dest_dir/.vim" "vimrc::$dest_dir/.vimrc")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'vim' '' 'vim config' "$selected" "$status"
status=$(component_status "nvim::$dest_dir/.config/nvim")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'neovim_config' '' 'neovim config' "$selected" "$status"
status=$(component_status "starship.toml::$dest_dir/.config/starship.toml")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'starship' '' 'starship prompt config' "$selected" "$status"
status=$(component_status "gnupg/gpg-agent.conf::$dest_dir/.gnupg/gpg-agent.conf")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'gnupg' '' 'gpg-agent config' "$selected" "$status"
status=$(component_status "Xresources::$dest_dir/.Xresources" "Xresources-themes::$dest_dir/.Xresources-themes" "Xresources.d::$dest_dir/.Xresources.d")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'xresources' '' 'Xresources and fonts' "$selected" "$status"
status=$(component_status "screenrc::$dest_dir/.screenrc")
selected=0; component_enabled_by_default "$status" && selected=1
add_menu_item 'component' 'screen' '' 'screen config' "$selected" "$status"
for file in *; do
if ! element_exists "$file" "$excluded"; then
local dest_file_name=$(basename "$file" .tpl)
local dest_file_path="$dest_dir/.$dest_file_name"
status=$(component_status "$file::$dest_file_path")
selected=0
if component_enabled_by_default "$status"; then
selected=1
fi
add_menu_item 'single' "$file" "$dest_file_path" "home: .$dest_file_name" "$selected" "$status"
fi
done
prompt_for_toggles
local i
for i in "${!MENU_KINDS[@]}"; do
if [[ ${MENU_KINDS[$i]} == 'single' ]]; then
apply_single_selection "${MENU_SOURCES[$i]}" "${MENU_DESTS[$i]}" "${MENU_LABELS[$i]}" "${MENU_SELECTED[$i]}"
else
apply_component_selection "${MENU_SOURCES[$i]}" "${MENU_SELECTED[$i]}"
fi
done
post_install_actions
}
install_file() {
local file="$1"
local dest_file_path="$2"
if [[ -e $dest_file_path ]] ||
[[ -L $dest_file_path ]]; then
if [[ -e $dest_file_path ]] &&
diff -q "$file" "$dest_file_path" >/dev/null; then
echo "dotfiles: identical $dest_file_path"
elif $replace_all; then
replace_file "$file" "$dest_file_path"
else
read -p "dotfiles: overwrite ${dest_file_path}? [ynaq] "
case $REPLY in
'y') replace_file "$file" "$dest_file_path" ;;
'q') exit 1 ;;
'a')
replace_all=true
replace_file "$file" "$dest_file_path"
;;
*) echo "dotfiles: skipping $dest_file_path" ;;
esac
fi
else
link_file "$file" "$dest_file_path"
fi
}
replace_file() {
local file="$1"
local dest_file_path="$2"
backup_file "$dest_file_path"
remove "$dest_file_path"
link_file "$file" "$dest_file_path"
}
link_file() {
local file="$1"
local dest_file_path="$2"
if [[ $file =~ \.tpl$ ]]; then
echo "dotfiles: generating $dest_file_path"
bash "$file" >"$dest_file_path"
else
echo "dotfiles: linking $dest_file_path"
ln -s "$(pwd)/$file" "$dest_file_path"
fi
}
element_exists() {
local elem="$1"
shift
local arr=($@)
local i
for i in ${arr[@]}; do
if [[ $i == $elem ]]; then
return 0
fi
done
return 1
}
remove() {
local file="$1"
if [[ -d $file ]] && [[ ! -L $file ]]; then
rm -rf "$file"
else
rm -f "$file"
fi
}
if [ -n "$uninstall" ]; then
cleanup
uninstall_all
exit
fi
pre_install_actions
install_home