-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·77 lines (63 loc) · 1.76 KB
/
bootstrap
File metadata and controls
executable file
·77 lines (63 loc) · 1.76 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
#!/bin/zsh
set -euo pipefail
DOTFILES_DIR="$(cd "$(dirname "$0")" && pwd)"
link_file() {
local src="$1" dest="$2"
if [ -L "$dest" ]; then
if [ "$(readlink "$dest")" = "$src" ]; then
return
fi
rm "$dest"
elif [ -e "$dest" ]; then
mv "$dest" "$dest.old"
echo "Backed up $dest → $dest.old"
fi
ln -sf "$src" "$dest"
echo "Linked $dest"
}
# For files that external tools modify (git credential helpers, shell
# installers, etc.), use include/source instead of symlinks so writes
# go to the real ~/.file and don't dirty the repo.
include_file() {
local src="$1" dest="$2" directive="$3"
if [ -L "$dest" ]; then
rm "$dest"
elif [ -e "$dest" ]; then
if grep -qF "$directive" "$dest" 2>/dev/null; then
return
fi
mv "$dest" "$dest.old"
echo "Backed up $dest → $dest.old"
fi
echo "$directive" > "$dest"
echo "Created $dest"
}
init() {
for file in "$DOTFILES_DIR"/.*; do
local filename="$(basename "$file")"
case "$filename" in
.|..|.git|.DS_Store|.gitconfig|.zshrc)
continue
;;
esac
link_file "$file" "$HOME/$filename"
done
include_file "$DOTFILES_DIR/.gitconfig" "$HOME/.gitconfig" \
"[include]
path = $DOTFILES_DIR/.gitconfig"
include_file "$DOTFILES_DIR/.zshrc" "$HOME/.zshrc" \
"source \"$DOTFILES_DIR/.zshrc\""
source "$HOME/.zshrc"
if type tmux >/dev/null 2>/dev/null && tmux has-session 2>/dev/null; then
tmux source-file "$HOME/.tmux.conf"
fi
}
if [[ "${1:-}" == "--force" || "${1:-}" == "-f" ]]; then
init
else
# read returns 1 on EOF (no TTY / piped stdin); avoid exiting under set -e
read "REPLY?This will symlink dotfiles to your home directory. Continue? (y/n) " || REPLY=n
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
init
fi
fi