-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.sh
More file actions
executable file
·97 lines (76 loc) · 2.45 KB
/
Copy pathlink.sh
File metadata and controls
executable file
·97 lines (76 loc) · 2.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
#!/bin/bash
set -Eeuo pipefail
BACKUP_DIR="$HOME/dotfiles/link.bak"
# Function to create a symbolic link, backing up existing files
symlink() {
local source="$1"
local target="$2"
if [ "$(readlink "$target" 2>/dev/null)" = "$source" ]; then
echo "Symlink already exists: $target -> $source"
return
fi
# Back up existing file
if [ -e "$target" ] && [ ! -L "$target" ]; then
local backup_path="$BACKUP_DIR/${target#"$HOME"/}"
local backup_index=1
while [ -e "$backup_path" ] || [ -L "$backup_path" ]; do
backup_path="$BACKUP_DIR/${target#"$HOME"/}.$backup_index"
backup_index=$((backup_index + 1))
done
mkdir -p "$(dirname "$backup_path")"
mv "$target" "$backup_path"
echo "Backed up: $target -> $backup_path"
fi
ln -sfn "$source" "$target"
echo "Created symlink: $target -> $source"
}
export -f symlink
export BACKUP_DIR
cd "$HOME/dotfiles/config" || exit 1
# These applications write generated data alongside their configuration, so
# keep their destination directories real and link only tracked files.
FILE_LINK_DIRS=(
git
herdr
opencode
zed
zsh
)
uses_file_links() {
local directory="$1"
local exception
for exception in "${FILE_LINK_DIRS[@]}"; do
if [ "$directory" = "$exception" ]; then
return 0
fi
done
return 1
}
# zsh doesn't follow XDG standards
symlink "$HOME/dotfiles/config/zsh/.zshenv" "$HOME/.zshenv"
# Link complete application config directories by default.
for source_dir in "$HOME"/dotfiles/config/*/; do
directory="$(basename "$source_dir")"
if uses_file_links "$directory"; then
continue
fi
symlink "${source_dir%/}" "$HOME/.config/$directory"
done
# Link tracked files individually for applications that mix generated data
# into their config directories. Top-level files are also linked individually.
git ls-files | while read -r file; do
if [[ "$file" == */* ]]; then
directory="${file%%/*}"
if ! uses_file_links "$directory"; then
continue
fi
fi
target_dir="$HOME/.config/$(dirname "$file")"
# If target_dir is a symlink, remove it so we can create a real directory.
if [ -L "$target_dir" ]; then
echo "Removing directory symlink: $target_dir"
rm "$target_dir"
fi
mkdir -p "$target_dir"
symlink "$HOME/dotfiles/config/$file" "$HOME/.config/$file"
done