Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·397 lines (341 loc) · 10.6 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·397 lines (341 loc) · 10.6 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
#!/usr/bin/env bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/common.sh"
DRY_RUN=false
BACKUP_DIR=""
PROFILE=""
PACKAGES=()
PACKAGES_EXPLICIT=false
DEPLOY_STARTED=false
PREEXISTING_LINKS=()
BACKED_UP_FILES=()
profile_packages() {
case "$1" in
server)
echo "home claude server"
;;
pc)
echo "home claude hyprland noctalia pc"
;;
laptop)
echo "home claude hyprland noctalia laptop"
;;
nas)
echo "home claude"
;;
minimal)
echo "home claude"
;;
*)
error "Unknown profile: $1"
;;
esac
}
detect_profile() {
local host
host="$(hostname 2>/dev/null || echo unknown)"
case "$host" in
server)
echo "server"
;;
semyon-pc-cachy|pc|winpc|semyons-pc)
echo "pc"
;;
semyons-laptop|cachy-laptop|laptop)
echo "laptop"
;;
nas)
echo "nas"
;;
*)
echo "minimal"
;;
esac
}
resolve_packages() {
local packages package
if [[ ${#PACKAGES[@]} -eq 0 ]]; then
[[ "$PACKAGES_EXPLICIT" == false ]] || error "--packages list is empty"
[[ -n "$PROFILE" ]] || PROFILE="$(detect_profile)"
# capture first so an unknown profile fails here instead of becoming package names
packages="$(profile_packages "$PROFILE")" || error "Unknown profile: $PROFILE"
read -r -a PACKAGES <<< "$packages"
[[ ${#PACKAGES[@]} -gt 0 ]] || error "Profile resolved to no packages: $PROFILE"
fi
for package in "${PACKAGES[@]}"; do
# only plain top-level package names, not paths or repo support dirs
case "$package" in
''|.*|*/*|lib|docs|tests|skills)
error "Invalid package name: '$package'" ;;
esac
[[ -d "$SCRIPT_DIR/$package" ]] || error "Package not found: $package"
done
}
rollback() {
if [[ "$DRY_RUN" == true ]]; then
error "Dry run failed."
fi
if [[ "$DEPLOY_STARTED" == true ]]; then
local package source rel target prior preexisting
for package in "${PACKAGES[@]}"; do
while IFS= read -r source; do
rel="${source#"$SCRIPT_DIR/$package/"}"
target="$HOME/$rel"
preexisting=false
for prior in "${PREEXISTING_LINKS[@]}"; do
if [[ "$prior" == "$target" ]]; then
preexisting=true
break
fi
done
if [[ "$preexisting" == false && -L "$target" && "$target" -ef "$source" ]]; then
rm -- "$target" || warn "Could not remove new link: $target"
fi
done < <(package_files "$package")
done
fi
if [[ -n "$BACKUP_DIR" && -d "$BACKUP_DIR" ]]; then
warn "Rolling back changes..."
local file restore_failed=false
for file in "${BACKED_UP_FILES[@]}"; do
if [[ -e "$HOME/$file" || -L "$HOME/$file" ]]; then
warn "Cannot restore $file: destination already exists"
restore_failed=true
elif ! mv -- "$BACKUP_DIR/$file" "$HOME/$file"; then
warn "Could not restore $file"
restore_failed=true
fi
done
if [[ "$restore_failed" == true ]]; then
error "Restore incomplete. Original files remain in $BACKUP_DIR"
fi
if [[ ${#BACKED_UP_FILES[@]} -gt 0 ]]; then
success "Restored files from backup"
fi
error "Setup failed. Original files restored."
else
error "Setup failed. No backup to restore."
fi
}
ensure_stow() {
if command -v stow &>/dev/null; then
return 0
fi
local os base_os
os="$(detect_os)"
base_os="$(get_base_os)"
if [[ "$DRY_RUN" == true ]]; then
error "stow not installed. Install it first, dry runs never install packages."
fi
info "Installing stow..."
case "$base_os" in
arch|endeavouros|manjaro|cachyos)
sudo pacman -S --noconfirm stow ;;
ubuntu|debian|pop|linuxmint)
sudo apt update && sudo apt install -y stow ;;
fedora|rhel|centos)
sudo dnf install -y stow ;;
opensuse*)
sudo zypper install -y stow ;;
macos)
command -v brew &>/dev/null || error "Homebrew required. Install from https://brew.sh"
brew install stow ;;
*)
error "Unsupported OS: $os. Install stow manually." ;;
esac
success "stow installed"
}
package_conflicts() {
local package source rel target
for package in "${PACKAGES[@]}"; do
[[ -d "$SCRIPT_DIR/$package" ]] || error "Package not found: $package"
while IFS= read -r source; do
rel="${source#"$SCRIPT_DIR/$package/"}"
target="$HOME/$rel"
if [[ -e "$target" && ! -L "$target" ]]; then
printf '%s\n' "$rel"
fi
done < <(package_files "$package")
done | sort -u
}
package_files() {
local package=$1 source rel base
while IFS= read -r source; do
rel="${source#"$SCRIPT_DIR/$package/"}"
base="$(basename "$rel")"
case "$rel" in
README|README.*|LICENSE|COPYING|.stow-local-ignore)
continue ;;
esac
case "$base" in
.gitignore)
continue ;;
esac
printf '%s\n' "$source"
done < <(find "$SCRIPT_DIR/$package" \( -type f -o -type l \))
}
backup_existing() {
local files_to_backup=()
local file ancestor part index
local parts=()
mapfile -t files_to_backup < <(package_conflicts)
if [[ ${#files_to_backup[@]} -eq 0 ]]; then
info "No conflicting files found"
return 0
fi
for file in "${files_to_backup[@]}"; do
IFS=/ read -r -a parts <<< "$file"
ancestor="$HOME"
for ((index=0; index<${#parts[@]}-1; index++)); do
part="${parts[$index]}"
ancestor="$ancestor/$part"
if [[ -L "$ancestor" ]]; then
error "Cannot back up $file through symlinked directory: $ancestor"
fi
done
done
local backup_template="$HOME/dotfiles_backup_$(date +%Y%m%d_%H%M%S)_XXXXXX"
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would backup ${#files_to_backup[@]} files to $backup_template"
return 0
fi
warn "Found ${#files_to_backup[@]} conflicting files"
read -p "Backup these files? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
BACKUP_DIR="$(mktemp -d "$backup_template")"
BACKED_UP_FILES=()
for file in "${files_to_backup[@]}"; do
mkdir -p "$BACKUP_DIR/$(dirname "$file")"
mv "$HOME/$file" "$BACKUP_DIR/$file"
BACKED_UP_FILES+=("$file")
done
success "Backup created at $BACKUP_DIR"
else
error "Cannot proceed with existing files"
fi
}
deploy_dotfiles() {
resolve_packages
local package source rel target
PREEXISTING_LINKS=()
for package in "${PACKAGES[@]}"; do
while IFS= read -r source; do
rel="${source#"$SCRIPT_DIR/$package/"}"
target="$HOME/$rel"
if [[ -L "$target" ]]; then
PREEXISTING_LINKS+=("$target")
fi
done < <(package_files "$package")
done
info "Deploying dotfiles from $SCRIPT_DIR"
if [[ -n "$PROFILE" ]]; then
info "Profile: $PROFILE"
fi
info "Packages: ${PACKAGES[*]}"
cd "$SCRIPT_DIR"
if stow --target="$HOME" --no-folding -n "${PACKAGES[@]}" 2>&1 | grep -q "conflict"; then
warn "Conflicts detected"
backup_existing
fi
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Simulating deployment..."
stow --target="$HOME" --no-folding -n -v "${PACKAGES[@]}"
return 0
fi
DEPLOY_STARTED=true
stow --target="$HOME" --no-folding "${PACKAGES[@]}"
success "Dotfiles deployed"
}
verify_installation() {
local verified=0 failed=0 package source rel target
info "Verifying installation..."
for package in "${PACKAGES[@]}"; do
while IFS= read -r source; do
rel="${source#"$SCRIPT_DIR/$package/"}"
target="$HOME/$rel"
if [[ -L "$target" ]]; then
((verified+=1))
else
((failed+=1))
fi
done < <(package_files "$package")
done
echo " Verified symlinks: $verified"
if [[ $failed -gt 0 ]]; then
echo " Missing or unmanaged paths: $failed"
fi
}
print_help() {
cat <<'EOF'
Usage: ./setup.sh [OPTIONS]
Options:
--profile NAME Deploy a known profile: server, pc, laptop, nas, minimal
--packages LIST Deploy an explicit space/comma-separated package list
--dry-run, -n Simulate without making changes
--help, -h Show this help
Examples:
./setup.sh --profile server
./setup.sh --profile pc
./setup.sh --profile laptop
./setup.sh --packages "home claude"
./setup.sh --packages "codex"
EOF
}
main() {
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run|-n)
DRY_RUN=true
shift ;;
--profile)
[[ $# -ge 2 ]] || error "--profile requires a value"
PROFILE="$2"
shift 2 ;;
--packages)
[[ $# -ge 2 ]] || error "--packages requires a value"
IFS=', ' read -r -a PACKAGES <<< "$2"
PACKAGES_EXPLICIT=true
shift 2 ;;
--help|-h)
print_help
exit 0 ;;
*)
error "Unknown option: $1" ;;
esac
done
echo ""
echo "================================"
echo " Dotfiles Setup (GNU Stow)"
echo "================================"
echo ""
if [[ "$DRY_RUN" == true ]]; then
warn "DRY RUN MODE"
fi
# errtrace lets the ERR trap fire inside nested functions
set -E
trap rollback ERR
# validate package input before anything can install stow
resolve_packages
ensure_stow
echo ""
deploy_dotfiles
echo ""
if [[ "$DRY_RUN" == false ]]; then
verify_installation
trap - ERR
set +E
echo ""
success "Setup complete!"
info "Restart terminal or run: source ~/.bashrc"
else
trap - ERR
set +E
success "Dry run complete"
fi
echo ""
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi