/
dcp-install-links
executable file
·223 lines (177 loc) · 5.39 KB
/
dcp-install-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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env bash
set -eo pipefail
readonly OS="$(uname -s)"
readonly DCP_CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/dcp"
readonly WARNING_SHOWN_FLAG_PATH="${DCP_CONFIG_DIR}/symlink-warning-shown"
readonly NON_DCP_DOTFILES=(
".bash_login"
".profile"
".zlogin"
)
readonly PATH_MIGRATIONS=(
"${HOME}/.dcp/conf/plugins:${DCP_CONFIG_DIR}/plugins.lst"
"${HOME}/.dcp/conf/theme:${DCP_CONFIG_DIR}/zsh-theme"
)
log_warn() {
printf '\033[2;39;49m%s %b%5s \033[0;35m%s \033[2;39m: \033[0m%s\n' \
"$(date '+%F %T')" '\033[0;33m' "WARN" "$$" "$1" >&2
}
__ary_join() {
local sep="$1"; shift
printf '%s' "$1"; shift
printf '%s' "${@/#/${sep}}"
}
__ln_snfv() {
# Solaris' and OpenBSD's ln(1) don't have -v
printf '%s -> %s\n' "$2" "$1" >&2
# Solaris' doesn't have -n (or its equivalent -h)
[[ -h "$2" ]] && rm -f "$2" || :
ln -sf "$1" "$2"
}
find_dotfiles() {
local path="$1"; shift
find "${path}" -mindepth 1 "$@" -print0 2>/dev/null | sort -z
}
__link_dotfile() {
local dotfile="${2:$((${#1} + 1))}"
local link="${HOME}/.${dotfile}"
local parent="$(dirname "${link}")"
if [[ ! -d "${parent}" ]]; then
mkdir -p "${parent}"
fi
if [[ ! -h "${link}" ]] && [[ -d "${link}" || -s "${link}" ]]; then
log_warn "~/.${dotfile} is a directory or file of non-zero size, skipping"
else
__ln_snfv "$2" "${link}"
fi
}
link_dotfile() {
if [[ "${OS}" = "Darwin" ]]; then
__link_dotfile "$@"
else
case "$(basename "$2")" in
chunkwmrc|khdrc|macos) : ;;
*) __link_dotfile "$@"
esac
fi
}
standard_cleanup() {
if [[ "${OS}" = "Darwin" ]]; then
if [[ ! -e "${HOME}/.hushlogin" ]]; then
touch "${HOME}/.hushlogin"
fi
else
if [[ -h "${HOME}/.macos" ]]; then
rm -f "${HOME}/.macos"
fi
fi
local dotfile
for dotfile in gitignore_global kwm osx; do
if [[ -h "${HOME}/.${dotfile}" ]]; then
rm -f "${HOME}/.${dotfile}"
fi
done
}
perform_migrations() {
local migration old_path new_path
local performed_migration="false"
for migration in "${PATH_MIGRATIONS[@]}"; do
IFS=':' read -r old_path new_path <<< "${migration}"
if [[ -e "${old_path}" ]]; then
if ! "${performed_migration}"; then
log_warn "The ~/.dcp/conf directory is deprecated and will be removed soon."
performed_migration="true"
fi
log_warn "Moving ${old_path} -> ${new_path}"
mv "${old_path}" "${new_path}"
fi
done
if "${performed_migration}"; then
printf '\n' >&2
fi
}
check_non_dcp() {
local dotfile
local result="false"
for dotfile in "${NON_DCP_DOTFILES[@]}"; do
if [[ -e "${HOME}/${dotfile}" ]]; then
if ! "${result}"; then
printf '\n' >&2
fi
result="true"
log_warn "~/${dotfile} exists but is not provided by ~/.dcp"
fi
done
"${result}"
}
__maybe_git_config() {
if ! git config --global "$1" >/dev/null; then
git config --global "$1" "$2"
fi
}
configure_git() {
git config --global "core.excludesfile" "${HOME}/.cvsignore"
__maybe_git_config "color.ui" "true"
__maybe_git_config "core.editor" "vim"
__maybe_git_config "push.default" "matching"
__maybe_git_config "grep.lineNumber" "true"
__maybe_git_config "submodule.fetchJobs" "2"
__maybe_git_config "core.attributesfile" "${HOME}/.gitattributes"
if [[ ! -e "${HOME}/.gitattributes" ]]; then
touch "${HOME}/.gitattributes"
fi
if [[ "${OS}" = "Darwin" ]]; then
__maybe_git_config "core.trustctime" "false"
__maybe_git_config "diff.mobileprovision.textconv" "openssl smime -inform der -verify -in"
__maybe_git_config "diff.plist.textconv" "plutil -convert xml1 -o -"
__maybe_git_config "diff.utf16.textconv" "iconv -f utf-16 -t utf-8"
if ! grep -q '^\*\.mobileprovision' "${HOME}/.gitattributes"; then
printf '*.mobileprovision diff=mobileprovision\n' >>"${HOME}/.gitattributes"
fi
if ! grep -q '^\*\.plist' "${HOME}/.gitattributes"; then
printf '*.plist diff=plist\n' >>"${HOME}/.gitattributes"
fi
fi
}
print_warning() {
local dotfiles
dotfiles="$(__ary_join ", " "${NON_DCP_DOTFILES[@]}")"
cat >&2 <<EOT
Having any of [${dotfiles}] should (in theory) be harmless.
That said, it can also really mess things up. It's good to examine their
contents and evaluate if they need to exist.
For example, RVM will persistently try to create ~/.zlogin to ensure that
it gets loaded, but this already happens in ~/.dcp/lib/post_init.sh, and the
code they put in ~/.zlogin will cause zsh to both start and exit with return
code 1 for login shells if you ever uninstall RVM. Not good!
This wall of warning text will only be shown once, unless you get rid of these
files then create them again at some point.
EOT
}
main() {
standard_cleanup
perform_migrations
local dotfile
while IFS='' read -d '' -r dotfile; do
link_dotfile "${HOME}/.dcp/dot" "${dotfile}"
done < <(
find_dotfiles "${HOME}/.dcp/dot" -type f
)
printf '\n' >&2
while IFS='' read -d '' -r dotfile; do
link_dotfile "${HOME}/.vim" "${dotfile}"
done < <(
find_dotfiles "${HOME}/.vim" -maxdepth 1 -type f -name '*vimrc'
)
if ! check_non_dcp; then
rm -f "${WARNING_SHOWN_FLAG_PATH}"
elif [[ ! -e "${WARNING_SHOWN_FLAG_PATH}" ]]; then
print_warning
mkdir -p "$(dirname "${WARNING_SHOWN_FLAG_PATH}")"
touch "${WARNING_SHOWN_FLAG_PATH}"
fi
if command -v git >/dev/null; then
configure_git
fi
}
main "$@"