-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall
More file actions
executable file
·82 lines (68 loc) · 2.15 KB
/
Copy pathinstall
File metadata and controls
executable file
·82 lines (68 loc) · 2.15 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
#!/bin/sh
# Helper to install symlinks to `src` directory.
# Remove any existing file/symlink at $2, then symlink $2 -> $1.
install_symlink() {
echo "Symlinking: $2 -> $1"
rm -rf "$2"
ln -s "$1" "$2"
}
# Directories (relative to src/) whose *contents* are symlinked individually into ~/<dir>/ rather
# than symlinking the directory itself. This is needed for `.config` and `.local/bin` where
# AppArmor allows `~/.config` or `~/.local/bin` but doesn't follow symlinks to them. Entries may
# be nested paths; ancestor directories are mkdir -p'd as needed.
EXPAND_DIRS=".config .local/bin"
# Check whether $1 exactly matches an entry in EXPAND_DIRS.
is_expand_dir() {
for d in $EXPAND_DIRS; do
if [ "$1" = "$d" ]; then return 0; fi
done
return 1
}
# Check whether $1 is a strict ancestor (path prefix) of some EXPAND_DIRS entry.
is_expand_ancestor() {
for d in $EXPAND_DIRS; do
case "$d" in
"$1"/*) return 0 ;;
esac
done
return 1
}
# Walk src/$1 and install entries into ~/$1. Pass "" to process the src root.
install_dir() {
local rel="$1"
local src_dir home_dir entry_path entry child_rel
if [ -z "$rel" ]; then
src_dir="`pwd`/src"
home_dir="$HOME"
else
src_dir="`pwd`/src/$rel"
home_dir="$HOME/$rel"
# If a previous install symlinked this dir, remove the symlink and warn.
if [ -L "$home_dir" ]; then
echo "Found existing symlink!"
echo "Removing: $home_dir symlink"
rm -rf "$home_dir"
echo "WARNING: Move any untracked files out of the dotfiles tree and re-run this script."
exit 1
fi
mkdir -p "$home_dir"
fi
for entry_path in "$src_dir"/.* "$src_dir"/*; do
[ -e "$entry_path" ] || continue
entry=$(basename "$entry_path")
if [ "$entry" = "." ] || [ "$entry" = ".." ]; then continue; fi
if [ -z "$rel" ]; then
child_rel="$entry"
else
child_rel="$rel/$entry"
fi
if is_expand_dir "$child_rel" || is_expand_ancestor "$child_rel"; then
install_dir "$child_rel"
else
install_symlink "$entry_path" "$home_dir/$entry"
fi
done
}
# Ensure current directory is the script location.
cd "`dirname "$0"`"
install_dir ""