-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·67 lines (53 loc) · 1.68 KB
/
install.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
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
set -euo pipefail
here="${PWD}"
# shellcheck disable=SC2064
trap "cd \"${here}\"" EXIT
cd "$(dirname "$0")"
# Given a filename, echo the first rotated name that does not exist
function backup() {
NUM=1
while [[ -e "$1.${NUM}" ]]; do
NUM=$(expr ${NUM} + 1)
done
echo "$1.${NUM}"
}
# Given a path and expression, safely link all files to the home directory
function link() {
# Create symlinks
while read -r FILE; do
LINK="${HOME}/$(basename "${FILE}")"
# Ensure symlink is actually a dotfile
if [[ "$(basename "${LINK}")" != .* ]]; then
LINK="$(dirname "${LINK}")/.$(basename "${LINK}")"
fi
# Assume symlinks are ok
if [[ -h "${LINK}" ]]; then
continue
fi
# Back up the existing file
if [[ -e "${LINK}" ]]; then
mv "${LINK}" "$(backup "${LINK}")"
fi
# Symlink the file
ln -s "${FILE}" "${LINK}"
done <<< "$(find "$1" -maxdepth 1 -name "$2" ! -name ".git" ! -name ".github" ! -name ".gitignore")"
# Delete broken symlinks
while read -r FILE; do
rm -f "${FILE}"
done <<< "$(find "${HOME}" -type l -maxdepth 1 ! -exec test -e {} \; -print)"
}
# Add Git hook symlinks
if [[ -d "$(pwd)/.git/hooks" ]]; then
rm -rf "$(pwd)/.git/hooks"
fi
ln -s "$(pwd)/.githooks" "$(pwd)/.git/hooks"
chmod +x "$(pwd)/.git/hooks"/*
# Remove old, broken symlinks
find "${HOME}" -maxdepth 1 -name ".*.bash" -type l ! -exec test -e {} \; -delete
# Link dotfiles to home directory
link "$(pwd)" ".*"
# Reload powerline if installed
if command -v powerline-daemon &> /dev/null; then
powerline-daemon --quiet --replace
fi