-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup_symlinks.sh
executable file
·59 lines (46 loc) · 1.33 KB
/
setup_symlinks.sh
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
#!/bin/bash
array_contains () {
local e match="$1"
shift
for e; do [[ "$match" =~ "$e/" ]] && return 0; done
return 1
}
echo "Symlinking user dirs"
# must happen before files
user_dirs=(
user/.local/share/systemd
user/.zsh
user/.emacs.d
)
for dir in "${user_dirs[@]}"; do
dest_dir=${dir/"user"/$HOME}
# skip if dir is already symlinked
if [[ -L "$dest_dir" && -d "$dest_dir" ]]; then continue; fi
parent_dir=$(echo $dir | sed -E 's/\/[^/]*$//')
mkdir -p $parent_dir
ln -sf $(pwd)/$dir $dest_dir
done
echo "Symlinking user files"
user_files=()
while IFS= read -r -d $'\0' file; do
user_files+=("$file")
done < <(find user \( -type f -o -type l \) -print0)
for file in "${user_files[@]}"; do
# # skip files in the symlinked directories
if array_contains $file "${user_dirs[@]}"; then continue; fi
dest_file=${file/"user"/$HOME}
dir=$(echo $dest_file | sed -E 's/\/[^/]*$//')
mkdir -p $dir
ln -sf $(pwd)/$file $dest_file
done
echo
if [[ "$OSTYPE" == linux* ]]; then
echo "Symlinking system files"
system_files=$(find system \( -type f -o -type l \))
for file in $system_files; do
dest_file=${file/"system"/}
dir=$(echo $dest_file | sed -E 's/\/[^/]*$//')
sudo mkdir -p $dir
sudo ln -sf $(pwd)/$file $dest_file
done
fi