-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.sh
More file actions
executable file
·206 lines (174 loc) · 4.99 KB
/
link.sh
File metadata and controls
executable file
·206 lines (174 loc) · 4.99 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
#!/bin/bash
set -euo pipefail
DOTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_DIR="$HOME/.config"
BACKUP_DIR="$HOME/config_backup"
BACKUP_TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
source "$DOTS_DIR/scripts/utils.sh"
cleanup() {
echo
print_message info "Script interrupted."
exit 1
}
trap cleanup INT TERM
ensure_dir() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
if mkdir -p "$dir"; then
print_message info "Created directory: $dir"
else
print_message error "Failed to create directory: $dir"
exit 1
fi
fi
}
backup_item() {
local target="$1"
local name
name="$(basename "$target")"
local backup_name="${name}.bk-${BACKUP_TIMESTAMP}"
ensure_dir "$BACKUP_DIR"
if mv "$target" "$BACKUP_DIR/$backup_name"; then
print_message warning "Backed up: $target → $BACKUP_DIR/$backup_name"
else
print_message error "Failed to backup: $target"
return 1
fi
}
link_item() {
local source="$1"
local target="$2"
if [[ ! -e "$source" ]]; then
print_message error "Source does not exist: $source"
return 1
fi
if [[ -L "$target" ]]; then
local current_link
current_link="$(readlink "$target")"
if [[ "$current_link" == "$source" ]]; then
print_message info "Skipping: $target already links to $source"
return 0
else
print_message warning "Relinking: $target (was → $current_link)"
unlink "$target"
fi
elif [[ -e "$target" ]]; then
backup_item "$target"
fi
ensure_dir "$(dirname "$target")"
if ln -s "$source" "$target"; then
print_message success "Linked: $source → $target"
else
print_message error "Failed to link: $target"
return 1
fi
}
link_config_folder() {
local config_name="$1"
link_item "$DOTS_DIR/config/$config_name" "$CONFIG_DIR/$config_name"
}
link_home_file() {
local filename="$1"
link_item "$DOTS_DIR/home/$filename" "$HOME/$filename"
}
link_vscode() {
print_message info "Processing VSCode configuration..."
local vscode_user_dir="$HOME/.config/Code/User"
ensure_dir "$vscode_user_dir"
if [[ -f "$DOTS_DIR/config/vscode/settings.json" ]]; then
link_item "$DOTS_DIR/config/vscode/settings.json" "$vscode_user_dir/settings.json"
fi
}
link_git() {
print_message info "Processing Git configuration..."
local global_git_config="$HOME/.gitconfig"
if [[ -f "$global_git_config" && ! -L "$global_git_config" ]]; then
print_message warning "Found real .gitconfig at $global_git_config, backing up..."
backup_item "$global_git_config"
fi
link_config_folder "git"
}
link_zsh() {
print_message info "Processing Zsh configuration..."
link_home_file ".zshrc"
link_config_folder "zsh"
link_config_folder "oh-my-posh"
}
link_bash() {
print_message info "Processing Bash configuration..."
link_home_file ".bashrc"
}
SPECIAL_CONFIGS=("vscode" "zsh" "oh-my-posh" "git")
SPECIAL_HOME_FILES=(".zshrc" ".bashrc")
declare -a available_configs=()
while IFS= read -r config; do
local_skip=false
for special in "${SPECIAL_CONFIGS[@]}"; do
if [[ "$config" == "$special" ]]; then
local_skip=true
break
fi
done
if ! $local_skip; then
available_configs+=("$config")
fi
done < <(find "$DOTS_DIR/config" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null | sort)
declare -a tasks=()
want_all=false
if [[ $# -eq 0 ]]; then
want_all=true
else
for arg in "$@"; do
if [[ "$arg" == "all" ]]; then
want_all=true
break
else
tasks+=("$arg")
fi
done
fi
if $want_all; then
tasks+=("${available_configs[@]}")
tasks+=("git" "zsh" "bash" "vscode")
while IFS= read -r file; do
skip=false
for special in "${SPECIAL_HOME_FILES[@]}"; do
if [[ "$file" == "$special" ]]; then
skip=true
break
fi
done
if ! $skip; then
tasks+=("$file")
fi
done < <(find "$DOTS_DIR/home" -mindepth 1 -maxdepth 1 -printf '%f\n' 2>/dev/null | sort)
fi
declare -a sorted_unique_tasks=()
declare -A seen=()
for item in "${tasks[@]}"; do
if [[ -z "${seen[$item]+x}" ]]; then
sorted_unique_tasks+=("$item")
seen[$item]=1
fi
done
setup_logging
print_header "Linking dotfiles"
for item in "${sorted_unique_tasks[@]}"; do
case "$item" in
vscode) link_vscode ;;
zsh) link_zsh ;;
bash) link_bash ;;
git) link_git ;;
oh-my-posh) link_config_folder "oh-my-posh" ;;
*)
if [[ -d "$DOTS_DIR/config/$item" ]]; then
link_config_folder "$item"
elif [[ -f "$DOTS_DIR/home/$item" ]]; then
link_home_file "$item"
else
print_message warning "Unknown config or task: $item"
fi
;;
esac
done
print_message success "Linking complete."