-
Notifications
You must be signed in to change notification settings - Fork 2
/
create-links
executable file
·74 lines (56 loc) · 1.8 KB
/
create-links
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
#!/bin/bash
do_not_link=("Readme.md" ".gitignore")
here=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
element_in () {
local e
for e in ${@:2} ; do
[[ "$e" == "$1" ]] && return 0
done
return 1
}
link_files () {
base="$1"
destination="$2"
prefix="$3"
ignore="${@:4}"
for target in "$base/"[a-zA-Z]* ; do
basename=$( basename "$target" )
link="${destination}/${prefix}${basename}"
if element_in "$basename" "${ignore[@]}" ; then
# Bail on some files we do not want
continue
fi
if [[ -h "$link" && $( readlink "$link" ) == "$target" ]] ; then
# Bail if the symlink exists already, and points to the correct place
continue
fi
echo "Linking \`$link' -> \`$target'"
if [[ -h "$link" ]] ; then
echo " Removing existing symbolic link \`$link' -> \`$( readlink -e "$link" )'"
rm "$link"
fi
if [[ -a "$link" ]] ; then
echo " \`$link exists. Moving it to \`$link.back"
mv "$link" "$link.back" || echo " Couldn't move \`$link' to \`$link.back'!"
fi
ln -s "$target" "$link" || echo " Couldn't link \`$link' to \`$target'"
done
unset broken_link
while IFS= read -r -u3 -d $'\0' broken_link; do
target=$( readlink "$broken_link" )
if [[ "$target" == "$base"/* ]] ; then
echo "Removing dangling symlink \`$broken_link' -> \`$target'"
rm $broken_link
fi
done 3< <( find $destination -maxdepth 1 -type l -xtype l -print0 )
}
# Link ./home/* to ~/.*
link_files "$here/home" "$HOME" "." "${do_not_link[@]}"
# Link ./config/* to ~/.config/*
mkdir -p "$HOME/.config"
link_files "$here/config" "$HOME/.config" '' "${do_not_link[@]}"
# Link ./share/* to ~/.local/share/*
mkdir -p "$HOME/.local/share"
link_files "$here/share" "$HOME/.local/share" '' "${do_not_link[@]}"
mkdir -p "$HOME/.local/bin"
link_files "$here/bin" "$HOME/.local/bin" '' "${do_not_link[@]}"