-
Notifications
You must be signed in to change notification settings - Fork 3
/
dotinstall
executable file
·101 lines (85 loc) · 2.67 KB
/
dotinstall
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
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
################################################################################
# dotinstall - installs dotfiles from a base location (~/.dot, for instance) by
# symlinking them into the user's home directory.
#
# Version 1
# Matthew Malensek <[email protected]>
################################################################################
unset dotdir
shopt -s dotglob #Allow globbing on dotfiles
default_dir="${HOME}/.dot"
override=$(hostname -s)
excludes=(".git" "overrides")
################################################################################
print_usage() {
cat <<EOM
Usage: $(basename "${0}") [-d dot_dir] [-o override]
-d dot_dir: Specifies the directory where dotfiles are stored. This
defaults to ~/.dot, but can also be set by the DOTDIR environment
variable.
-o override: Specifies an override configuration to use rather than the
default, which is based on the current machine hostname
(DOTDIR/overrides/$(hostname -s)). This can be used for machine-specific
configuration, and overrides the "core" configuration (DOTDIR/*).
EOM
}
# Determines if a given file is excluded.
excluded() {
for exclude in "${excludes[@]}"; do
if [[ "$(basename "${1}")" == "${exclude}" ]]; then
return 0
fi
done
return 1
}
# Links dotfiles in ${@} to the current directory.
link_dotfiles() {
for dot in "${@}/"*; do
if excluded "${dot}"; then
continue
fi
ln -vs "${dot}"
done
}
################################################################################
while getopts "d:o:" flag; do
case ${flag} in
d) dotdir=${OPTARG} ;;
o) override=${OPTARG} ;;
?) print_usage; exit 1 ;;
esac
done
if [[ -z "${dotdir}" ]]; then
# Can we use the default dot directory?
if [[ -e "${default_dir}" ]]; then
dotdir="${default_dir}"
fi
# How about the DOTDIR environment variable? (overrides the default dir)
if [[ -n "${DOTDIR}" ]]; then
dotdir="${DOTDIR}"
fi
# Didn't find a dotfile directory.
if [[ -z "${dotdir}" ]]; then
echo "Could not locate dotfile directory!"
exit 1
fi
fi
if [[ ! -e "${dotdir}" ]]; then
echo "Dotfile directory could not be found: ${dotdir}"
exit 1
fi
cd "${HOME}" 2> /dev/null
if [[ ${?} -ne 0 ]]; then
echo "Could not locate home directory: ${HOME}"
exit 1
fi
overdir="${dotdir}/overrides/${override}"
if [[ ! -d "${overdir}" ]]; then
echo "No override configuration found for '${override}.'"
else
echo "Configuration: ${override}"
link_dotfiles "${overdir}"
fi
echo "Configuration: core"
link_dotfiles "${dotdir}"