-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
Β·603 lines (505 loc) Β· 18.8 KB
/
sync.sh
File metadata and controls
executable file
Β·603 lines (505 loc) Β· 18.8 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
#!/bin/bash
# Dotfiles Sync Script
# Manages syncing configurations between system and repository
set -e # Exit on error
# Script directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/sync_config.toml"
BACKUP_DIR="${SCRIPT_DIR}/backups"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
print_header() { echo -e "${BOLD}${CYAN}$1${NC}"; }
# Function to show usage
show_usage() {
echo -e "${BOLD}USAGE:${NC}"
echo " $0 <command> [options] [configs...]"
echo
echo -e "${BOLD}COMMANDS:${NC}"
echo -e " ${GREEN}install${NC} [configs...] Install configurations from repo to system"
echo -e " ${GREEN}backup${NC} [configs...] Backup system configurations to repo"
echo -e " ${GREEN}list${NC} List all available configurations"
echo -e " ${GREEN}status${NC} [configs...] Show status of configurations"
echo -e " ${GREEN}rofi${NC} Interactive menu using rofi"
echo -e " ${GREEN}help${NC} Show this help message"
echo
echo -e "${BOLD}OPTIONS:${NC}"
echo -e " ${YELLOW}--dry-run${NC} Show what would be done without executing"
echo -e " ${YELLOW}--force${NC} Overwrite existing files without prompting"
echo
echo -e "${BOLD}EXAMPLES:${NC}"
echo " $0 install # Install all enabled configurations"
echo " $0 install sway waybar # Install only sway and waybar"
echo " $0 backup --dry-run # Show what would be backed up"
echo " $0 list # List available configurations"
echo " $0 status # Check status of all configurations"
echo " $0 rofi # Launch interactive rofi menu"
echo
echo -e "${BOLD}QUICK START:${NC}"
echo -e " 1. List available configs: ${CYAN}./sync.sh list${NC}"
echo -e " 2. Install what you need: ${CYAN}./sync.sh install sway waybar tmux${NC}"
echo -e " 3. Check if it worked: ${CYAN}./sync.sh status${NC}"
echo
echo -e "${BOLD}CONFIGURATION:${NC}"
echo -e " Edit ${CYAN}sync_config.toml${NC} to customize sync behavior and add/remove configs."
}
# Function to parse TOML (simplified but more robust)
parse_config() {
local section=""
local config_name=""
while IFS= read -r line || [ -n "$line" ]; do
# Skip comments and empty lines
[[ "$line" =~ ^[[:space:]]*# ]] && continue
[[ -z "${line// }" ]] && continue
# Section headers
if [[ "$line" =~ ^\[([^]]+)\] ]]; then
section="${BASH_REMATCH[1]}"
if [[ "$section" =~ ^configs\.(.+)$ ]]; then
config_name="${BASH_REMATCH[1]}"
fi
continue
fi
# Config properties
if [[ -n "$config_name" && "$section" =~ ^configs\. ]]; then
if [[ "$line" =~ ^([^=]+)=[[:space:]]*\"?([^\"]+)\"?[[:space:]]*$ ]]; then
local key="${BASH_REMATCH[1]// /}"
local value="${BASH_REMATCH[2]}"
case "$key" in
"source") eval "CONFIG_${config_name}_SOURCE=\"$value\"" ;;
"dest") eval "CONFIG_${config_name}_DEST=\"$value\"" ;;
"name") eval "CONFIG_${config_name}_NAME=\"$value\"" ;;
"enabled") eval "CONFIG_${config_name}_ENABLED=\"$value\"" ;;
"backup_only") eval "CONFIG_${config_name}_BACKUP_ONLY=\"$value\"" ;;
esac
fi
fi
done < "$CONFIG_FILE"
}
# Global exclusion patterns (cached for performance)
GLOBAL_EXCLUSIONS="--exclude=*.log --exclude=*.cache --exclude=.git/ --exclude=.svn/ --exclude=node_modules/ --exclude=__pycache__/ --exclude=*.pyc --exclude=.DS_Store --exclude=Thumbs.db --exclude=*.tmp --exclude=lazy-lock.json --exclude=.luarc.json --exclude=mason/ --exclude=.repro/ --exclude=*.sock"
# Function to get exclusion patterns
get_exclusions() {
echo "$GLOBAL_EXCLUSIONS"
}
# Function to list available configurations
list_configs() {
print_header "Available Configurations"
echo
parse_config
local configs=($(compgen -v | grep "^CONFIG_.*_NAME$" | sed 's/CONFIG_//;s/_NAME$//'))
if [[ ${#configs[@]} -eq 0 ]]; then
print_warning "No configurations found in sync_config.toml"
return
fi
# Sort configs alphabetically (use mapfile for better performance)
if command -v sort > /dev/null 2>&1; then
readarray -t configs < <(printf '%s\n' "${configs[@]}" | sort)
fi
for config in "${configs[@]}"; do
display_config_info "$config" ""
done
}
# Helper function to display config info
display_config_info() {
local config="$1"
local prefix="$2"
local name_var="CONFIG_${config}_NAME"
local enabled_var="CONFIG_${config}_ENABLED"
local dest_var="CONFIG_${config}_DEST"
local backup_only_var="CONFIG_${config}_BACKUP_ONLY"
local name="${!name_var:-$config}"
local enabled="${!enabled_var:-false}"
local dest="${!dest_var:-unknown}"
local backup_only="${!backup_only_var:-false}"
local status_icon="[ENABLED]"
local status_color="${GREEN}"
local backup_suffix=""
if [[ "$enabled" != "true" ]]; then
status_icon="[DISABLED]"
status_color="${YELLOW}"
fi
if [[ "$backup_only" == "true" ]]; then
backup_suffix="${YELLOW} (backup only)${NC}"
fi
printf "${prefix}${status_color}${status_icon}${NC} %-15s ${CYAN}%s${NC}${backup_suffix}\n" "$config" "$name"
}
# Function to install configurations with progress
install_configs() {
local configs=("$@")
local dry_run=false
local force=false
# Parse options
local filtered_configs=()
for arg in "${configs[@]}"; do
case "$arg" in
--dry-run) dry_run=true ;;
--force) force=true ;;
*) filtered_configs+=("$arg") ;;
esac
done
configs=("${filtered_configs[@]}")
parse_config
# If no configs specified, install all enabled ones (excluding backup-only)
if [[ ${#configs[@]} -eq 0 ]]; then
readarray -t all_configs < <(compgen -v | grep "^CONFIG_.*_ENABLED$" | sed 's/CONFIG_//;s/_ENABLED$//')
for config in "${all_configs[@]}"; do
local enabled_var="CONFIG_${config}_ENABLED"
local backup_only_var="CONFIG_${config}_BACKUP_ONLY"
if [[ "${!enabled_var}" == "true" && "${!backup_only_var}" != "true" ]]; then
configs+=("$config")
fi
done
fi
if [[ ${#configs[@]} -eq 0 ]]; then
print_warning "No configurations found to install. Use './sync.sh list' to see available configs."
return 1
fi
if [[ "$dry_run" == "true" ]]; then
print_header "Dry Run - Installation Preview"
else
print_header "Installing Configurations"
fi
echo
local success_count=0
local total_count=${#configs[@]}
for config in "${configs[@]}"; do
local source_var="CONFIG_${config}_SOURCE"
local dest_var="CONFIG_${config}_DEST"
local name_var="CONFIG_${config}_NAME"
local backup_only_var="CONFIG_${config}_BACKUP_ONLY"
local source="${!source_var}"
local dest="${!dest_var}"
local name="${!name_var:-$config}"
local backup_only="${!backup_only_var}"
if [[ -z "$source" || -z "$dest" ]]; then
print_error "Unknown configuration: $config"
continue
fi
# Skip backup-only configurations during install
if [[ "$backup_only" == "true" ]]; then
print_warning "Skipping $name (backup-only configuration)"
continue
fi
# Expand variables
source="${source/\$HOME/$HOME}"
local repo_path="${SCRIPT_DIR}/configs/${dest}"
print_info "Processing ${CYAN}$name${NC} ($config)"
if [[ ! -d "$repo_path" ]]; then
print_warning " Repository config not found: $repo_path"
continue
fi
if [[ "$dry_run" == "true" ]]; then
echo " Would copy: $repo_path β $source"
success_count=$((success_count + 1))
continue
fi
# Create parent directory
mkdir -p "$(dirname "$source")"
# Copy files with optimized rsync options
if rsync -av --human-readable $GLOBAL_EXCLUSIONS "$repo_path/" "$source/" > /dev/null 2>&1; then
print_success " Installed ${GREEN}$name${NC}"
success_count=$((success_count + 1))
else
print_error " Failed to install $name"
fi
done
echo
if [[ "$dry_run" == "true" ]]; then
print_info "Dry run complete. ${success_count}/${total_count} configurations would be installed."
else
print_success "Installation complete! ${success_count}/${total_count} configurations installed successfully."
if [[ $success_count -gt 0 ]]; then
echo
print_info "Tip: Use './sync.sh status' to verify the installation."
fi
fi
}
# Function to backup configurations with progress
backup_configs() {
local configs=("$@")
local dry_run=false
# Parse options
local filtered_configs=()
for arg in "${configs[@]}"; do
case "$arg" in
--dry-run) dry_run=true ;;
*) filtered_configs+=("$arg") ;;
esac
done
configs=("${filtered_configs[@]}")
parse_config
# If no configs specified, backup all enabled ones
if [[ ${#configs[@]} -eq 0 ]]; then
readarray -t all_configs < <(compgen -v | grep "^CONFIG_.*_ENABLED$" | sed 's/CONFIG_//;s/_ENABLED$//')
for config in "${all_configs[@]}"; do
local enabled_var="CONFIG_${config}_ENABLED"
[[ "${!enabled_var}" == "true" ]] && configs+=("$config")
done
fi
if [[ ${#configs[@]} -eq 0 ]]; then
print_warning "No configurations found to backup. Use './sync.sh list' to see available configs."
return 1
fi
if [[ "$dry_run" == "true" ]]; then
print_header "Dry Run - Backup Preview"
else
print_header "Backing Up Configurations"
fi
echo
local success_count=0
local total_count=${#configs[@]}
for config in "${configs[@]}"; do
local source_var="CONFIG_${config}_SOURCE"
local dest_var="CONFIG_${config}_DEST"
local name_var="CONFIG_${config}_NAME"
local source="${!source_var}"
local dest="${!dest_var}"
local name="${!name_var:-$config}"
if [[ -z "$source" || -z "$dest" ]]; then
print_error "Unknown configuration: $config"
continue
fi
# Expand variables
source="${source/\$HOME/$HOME}"
local repo_path="${SCRIPT_DIR}/configs/${dest}"
print_info "Processing ${CYAN}$name${NC} ($config)"
if [[ ! -e "$source" ]]; then
print_warning " System config not found: $source"
continue
fi
if [[ "$dry_run" == "true" ]]; then
echo " Would copy: $source β $repo_path"
success_count=$((success_count + 1))
continue
fi
# Create destination directory
mkdir -p "$(dirname "$repo_path")"
# Copy files with optimized rsync
local success=false
if [[ -d "$source" ]]; then
mkdir -p "$repo_path"
if rsync -av --delete --human-readable $GLOBAL_EXCLUSIONS "$source/" "$repo_path/" > /dev/null 2>&1; then
success=true
fi
else
if rsync -av --human-readable $GLOBAL_EXCLUSIONS "$source" "$repo_path" > /dev/null 2>&1; then
success=true
fi
fi
if [[ "$success" == "true" ]]; then
print_success " Backed up ${GREEN}$name${NC}"
success_count=$((success_count + 1))
else
print_error " Failed to backup $name"
fi
done
echo
if [[ "$dry_run" == "true" ]]; then
print_info "Dry run complete. ${success_count}/${total_count} configurations would be backed up."
else
print_success "Backup complete! ${success_count}/${total_count} configurations backed up successfully."
fi
}
# Function to show configuration status with better formatting
show_status() {
local configs=("$@")
parse_config
# If no configs specified, show all
if [[ ${#configs[@]} -eq 0 ]]; then
readarray -t configs < <(compgen -v | grep "^CONFIG_.*_NAME$" | sed 's/CONFIG_//;s/_NAME$//')
fi
print_header "Configuration Status"
echo
for config in "${configs[@]}"; do
local source_var="CONFIG_${config}_SOURCE"
local dest_var="CONFIG_${config}_DEST"
local name_var="CONFIG_${config}_NAME"
local source="${!source_var}"
local dest="${!dest_var}"
local name="${!name_var:-$config}"
if [[ -z "$source" || -z "$dest" ]]; then
print_error "Unknown configuration: $config"
continue
fi
source="${source/\$HOME/$HOME}"
local repo_path="${SCRIPT_DIR}/configs/${dest}"
echo -e "${BOLD}${CYAN}$name${NC} ($config)"
# System status
if [[ -e "$source" ]]; then
echo -e " System: $source ${GREEN}[EXISTS]${NC}"
else
echo -e " System: $source ${YELLOW}[MISSING]${NC}"
fi
# Repository status
if [[ -e "$repo_path" ]]; then
echo -e " Repository: $repo_path ${GREEN}[EXISTS]${NC}"
else
echo -e " Repository: $repo_path ${YELLOW}[MISSING]${NC}"
fi
echo
done
}
# Function to launch rofi interface
rofi_menu() {
# Check if rofi is available
if ! command -v rofi > /dev/null 2>&1; then
print_error "rofi is not installed. Please install rofi to use this feature."
return 1
fi
parse_config
# Main menu options
local main_options="π List all configurations
π₯ Install all enabled configurations
π€ Backup all enabled configurations
π Show status of all configurations
βοΈ Select specific configurations to install
π Select specific configurations to backup
β Show help"
local choice
choice=$(echo -e "$main_options" | rofi -dmenu -i -p "Dotfiles Sync" -theme-str 'window {width: 50%;}')
case "$choice" in
*"List all configurations")
list_configs
;;
*"Install all enabled configurations")
install_configs
;;
*"Backup all enabled configurations")
backup_configs
;;
*"Show status of all configurations")
show_status
;;
*"Select specific configurations to install")
rofi_select_configs "install"
;;
*"Select specific configurations to backup")
rofi_select_configs "backup"
;;
*"Show help")
show_usage
;;
*)
if [[ -n "$choice" ]]; then
print_error "Invalid selection"
return 1
fi
;;
esac
}
# Function to select specific configurations via rofi
rofi_select_configs() {
local action="$1"
# Get all available configurations
readarray -t all_configs < <(compgen -v | grep "^CONFIG_.*_NAME$" | sed 's/CONFIG_//;s/_NAME$//')
if [[ ${#all_configs[@]} -eq 0 ]]; then
print_warning "No configurations found in sync_config.toml"
return 1
fi
# Sort configs alphabetically
if command -v sort > /dev/null 2>&1; then
readarray -t all_configs < <(printf '%s\n' "${all_configs[@]}" | sort)
fi
# Build options string for rofi
local options=""
for config in "${all_configs[@]}"; do
local name_var="CONFIG_${config}_NAME"
local enabled_var="CONFIG_${config}_ENABLED"
local backup_only_var="CONFIG_${config}_BACKUP_ONLY"
local name="${!name_var:-$config}"
local enabled="${!enabled_var:-false}"
local backup_only="${!backup_only_var:-false}"
local status_prefix=""
if [[ "$enabled" == "true" ]]; then
if [[ "$backup_only" == "true" ]]; then
status_prefix="π€ "
else
status_prefix="β
"
fi
else
status_prefix="β "
fi
# Skip backup-only configs for install action
if [[ "$action" == "install" && "$backup_only" == "true" ]]; then
continue
fi
options+="${status_prefix}${name} (${config})\n"
done
# Show rofi selection
local selected
selected=$(echo -e "$options" | rofi -dmenu -multi-select -i -p "Select configurations to $action" -theme-str 'window {width: 60%;}')
if [[ -z "$selected" ]]; then
return 0
fi
# Extract config names from selection
local selected_configs=()
while IFS= read -r line; do
if [[ -n "$line" ]]; then
# Extract config name from parentheses
local pattern='\(([^)]+)\)$'
if [[ "$line" =~ $pattern ]]; then
selected_configs+=("${BASH_REMATCH[1]}")
fi
fi
done <<< "$selected"
if [[ ${#selected_configs[@]} -eq 0 ]]; then
print_warning "No configurations selected"
return 0
fi
# Perform the action
case "$action" in
"install")
install_configs "${selected_configs[@]}"
;;
"backup")
backup_configs "${selected_configs[@]}"
;;
esac
}
# Main script logic
main() {
if [[ ! -f "$CONFIG_FILE" ]]; then
print_error "Configuration file not found: $CONFIG_FILE"
exit 1
fi
case "${1:-help}" in
"install")
shift
install_configs "$@"
;;
"backup")
shift
backup_configs "$@"
;;
"list")
list_configs
;;
"status")
shift
show_status "$@"
;;
"rofi")
rofi_menu
;;
"help"|"--help"|"-h"|"")
show_usage
;;
*)
print_error "Unknown command: $1"
echo
show_usage
exit 1
;;
esac
}
main "$@"