-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·88 lines (73 loc) · 2.53 KB
/
install.sh
File metadata and controls
executable file
·88 lines (73 loc) · 2.53 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
#!/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
# @param {string} $1 Directory where dotfiles live (the root of the Git repo)
# @param {string} $1 Wildcard to find dotfiles
function link() {
# Create symlinks
find "$1" -maxdepth 1 -name "$2" ! -name ".editorconfig" ! -name ".git" ! -name ".githooks" ! -name ".github" ! -name ".gitignore" ! -name ".DS_Store" | while read -r file; do
local link
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
echo -e "\033[33mIgnoring:\033[0m ${link}"
continue
fi
# Back up the existing directory/file
local backup=""
if [[ -e "${link}" ]]; then
backup="$(backup "${link}")"
echo -e "\033[34mMoving:\033[0m ${link} -> ${backup}"
mv "${link}" "${backup}"
fi
# Symlink the file
echo -e "\033[92mLinking:\033[0m ${link} -> ${file}"
ln -s "${file}" "${link}"
# Restore existing directory files
if [[ -n "${backup}" && -d "${backup}" ]]; then
echo -e "\033[34mRestoring:\033[0m ${backup}/* -> ${link}/"
cp -r "${backup}/." "${link}/"
fi
done
# Delete broken symlinks
find "${HOME}" -maxdepth 1 -type l ! -exec test -e {} \; -print | while read -r file; do
echo -e "\033[91mDeleting:\033[0m ${file}"
rm -f "${file}"
done
}
# 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)" ".*"
if [[ -e ~/.dotpack && ! -L ~/.dotpack ]]; then
rm -rf ~/.dotpack
fi
if [[ ! -L ~/.dotpack ]]; then
ln -s "$(pwd)/dotpack" "${HOME}/.dotpack"
fi
# Reload powerline if installed
if command -v powerline-daemon &> /dev/null; then
powerline-daemon --quiet --replace
fi