-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·238 lines (199 loc) · 5.45 KB
/
install.sh
File metadata and controls
executable file
·238 lines (199 loc) · 5.45 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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
REPO_DIR="$SCRIPT_DIR"
usage() {
cat <<'EOF'
Usage: ./install.sh [--dry-run] [--brew] [--backup-dir DIR] [--yes]
Symlinks this repo's dotfiles into $HOME and backs up any existing files first.
Options:
--dry-run Print actions without changing anything
--brew Install common dependencies via Homebrew (macOS)
--backup-dir DIR Backup destination (default: ~/.dotfiles-backup/<timestamp>)
--yes Non-interactive: overwrite conflicts (with backups)
-h, --help Show help
EOF
}
DRY_RUN=0
BREW=0
YES=0
BACKUP_DIR=""
SKIPPED_COUNT=0
APPLIED_COUNT=0
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run) DRY_RUN=1; shift ;;
--brew) BREW=1; shift ;;
--yes) YES=1; shift ;;
--backup-dir) BACKUP_DIR="${2:-}"; shift 2 ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown argument: $1" >&2; usage; exit 2 ;;
esac
done
timestamp() { date +"%Y%m%d-%H%M%S"; }
log() { printf "%s\n" "$*"; }
run() {
if [[ "$DRY_RUN" -eq 1 ]]; then
log "[dry-run] $*"
else
"$@"
fi
}
backup_root_default="$HOME/.dotfiles-backup/$(timestamp)"
if [[ -z "$BACKUP_DIR" ]]; then
BACKUP_DIR="$backup_root_default"
fi
ensure_parent_dir() {
local path="$1"
run mkdir -p "$(dirname -- "$path")"
}
prompt_yn() {
local prompt="$1"
local reply=""
if [[ "$YES" -eq 1 ]]; then
return 0
fi
while true; do
if [[ -t 0 ]]; then
read -r "reply?$prompt [y/N] " </dev/tty || true
else
read -r "reply?$prompt [y/N] " || true
fi
case "${reply:-}" in
y|Y|yes|YES) return 0 ;;
""|n|N|no|NO) return 1 ;;
*) log "Please answer y or n." ;;
esac
done
}
prompt_conflict() {
local dst="$1"
local reply=""
if [[ "$YES" -eq 1 ]]; then
printf "%s" "overwrite"
return 0
fi
log "Conflict: $dst exists."
log "Choose: [o]verwrite (backup then install), [s]kip, [c]ancel"
while true; do
if [[ -t 0 ]]; then
read -r "reply?Selection (o/s/c): " </dev/tty || true
else
read -r "reply?Selection (o/s/c): " || true
fi
case "${reply:-}" in
o|O) printf "%s" "overwrite"; return 0 ;;
s|S) printf "%s" "skip"; return 0 ;;
c|C) printf "%s" "cancel"; return 0 ;;
*) log "Please enter o, s, or c." ;;
esac
done
}
backup_path_for() {
# Preserve paths under backup dir to avoid collisions (config/init.vim/etc.)
local target="$1"
local rel="$target"
rel="${rel/#$HOME\//HOME/}"
printf "%s/%s" "$BACKUP_DIR" "$rel"
}
backup_if_exists() {
local target="$1"
if [[ -e "$target" || -L "$target" ]]; then
local dest
dest="$(backup_path_for "$target")"
ensure_parent_dir "$dest"
run mv -f "$target" "$dest"
fi
}
link_file() {
local src="$1"
local dst="$2"
if [[ ! -e "$src" ]]; then
echo "Missing source: $src" >&2
exit 1
fi
if ! prompt_yn "Install $dst -> $src?"; then
log "Skip: $dst"
SKIPPED_COUNT=$((SKIPPED_COUNT + 1))
return 0
fi
ensure_parent_dir "$dst"
if [[ -e "$dst" || -L "$dst" ]]; then
local action
action="$(prompt_conflict "$dst")"
case "$action" in
overwrite)
backup_if_exists "$dst"
;;
skip)
log "Skip (exists): $dst"
SKIPPED_COUNT=$((SKIPPED_COUNT + 1))
return 0
;;
cancel)
log "Cancelled."
exit 1
;;
esac
fi
if [[ "$DRY_RUN" -eq 1 ]]; then
log "[dry-run] ln -s $src $dst"
log "Would link: $dst -> $src"
APPLIED_COUNT=$((APPLIED_COUNT + 1))
return 0
fi
ln -s "$src" "$dst"
log "Linked: $dst -> $src"
APPLIED_COUNT=$((APPLIED_COUNT + 1))
}
ensure_homebrew_deps() {
if [[ "$BREW" -ne 1 ]]; then
return 0
fi
if [[ "$(uname -s)" != "Darwin" ]]; then
log "Skip Homebrew installs: not macOS"
return 0
fi
if ! command -v brew >/dev/null 2>&1; then
log "Homebrew not found. Install it first, then re-run with --brew."
log "See: https://brew.sh/"
return 0
fi
# CLI tools referenced by .zshrc aliases/init
run brew install eza fd ripgrep fzf zoxide
# Editor/terminal used by these dotfiles
run brew install neovim
run brew install --cask ghostty || true
# fzf post-install is safe to ignore if already configured
run "$(brew --prefix)/opt/fzf/install" --key-bindings --completion --no-update-rc || true
}
main() {
log "Repo: $REPO_DIR"
log "Backup dir: $BACKUP_DIR"
[[ "$DRY_RUN" -eq 1 ]] && log "Mode: dry-run"
[[ "$YES" -eq 1 ]] && log "Mode: non-interactive (--yes)"
ensure_homebrew_deps
# Home dotfiles
link_file "$REPO_DIR/.zshrc" "$HOME/.zshrc"
link_file "$REPO_DIR/.vimrc" "$HOME/.vimrc"
# Neovim: prefer XDG config layout, but don't fight an existing ~/.config/nvim symlink.
local xdg_config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
local nvim_dir="$xdg_config_home/nvim"
local nvim_init="$nvim_dir/init.vim"
if [[ -L "$nvim_dir" ]]; then
log "Neovim config is a symlink ($nvim_dir). Leaving it as-is."
else
ensure_parent_dir "$nvim_init"
link_file "$HOME/.vimrc" "$nvim_init"
fi
# Ghostty (macOS default location)
local ghostty_dir="$HOME/Library/Application Support/com.mitchellh.ghostty"
link_file "$REPO_DIR/ghostty/config" "$ghostty_dir/config"
link_file "$REPO_DIR/ghostty/themes" "$ghostty_dir/themes"
log "Done."
log "Summary: applied=$APPLIED_COUNT skipped=$SKIPPED_COUNT"
if [[ -d "$BACKUP_DIR" ]]; then
log "Backups saved to: $BACKUP_DIR"
fi
}
main