-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-config
executable file
·302 lines (266 loc) · 8.38 KB
/
check-config
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env bash
# Setup error handlers
set -eo pipefail
set -o nounset
shopt -s nullglob
shopt -s inherit_errexit
exit_trap_finished=0
on_exit ()
{
local i
local stack_size=${#FUNCNAME[@]}
# start at one to skip on_exit in the stack
if [[ "$exit_trap_finished" -eq 0 ]]; then
for (( i=1; i < stack_size; i++ )); do
local src="${BASH_SOURCE[$i]:-}"
local func="${FUNCNAME[$i]:-}"
local lineno="${BASH_LINENO[$(( i - 1 ))]:-}"
printf "%s: %s: line %d\n" "$src" "$func" "$lineno"
done
exit_trap_finished=1
fi
}
trap 'on_exit' ERR EXIT
. ssh_agent.sh
if type colordiff >/dev/null 2>&1; then
diffcmd="colordiff"
else
diffcmd="diff"
fi
# Simple helper functions to avoid excessively long lines when checking for
# loaded launchd plists
list_loaded ()
{
launchctl list "$1" >/dev/null 2>&1
}
sudo_list_loaded ()
{
sudo -p "Enter password to query launchd about $1:" \
sh -c "launchctl list $1 >/dev/null 2>&1"
}
# Install a symlink to a config file
symlink_config ()
{
local src="$1"
local dst="$2"
local updated=1
shift 2
# If the correct symlink already exists, do nothing.
if [[ -L "$dst" ]]; then
local real_dst
real_dst="$(readlink "$dst")"
if [[ "$src" = "$real_dst" ]]; then
return "$updated"
fi
fi
# If the target is the correct file type OR a symlink to the wrong file
if { [[ -f "$src" ]] && [[ -f "$dst" ]] ;} \
|| { [[ -d "$src" ]] && [[ -d "$dst" ]] ;} \
|| [[ -L "$dst" ]]
then
while true; do
# Prompt user for action
printf "Wrong source file found for:\n%s\n" "$dst"
read -rep "(D)iff changes/(O)verwrite file/(K)eep file? [Dok?] " \
COMMAND
if [[ -z "$COMMAND" ]]; then
COMMAND=d
fi
case "$COMMAND" in
[dD])
# Check the symlink target
if [[ -L "$dst" ]]; then
local real_dst
real_dst="$(readlink "$dst")"
printf "%s\nLinks to:\n%s\nShould link to:\n%s\n" \
"$dst" "$real_dst" "$src"
# Diff the files
else
$diffcmd -ur "$dst" "$src" | less -FRX || true
fi
;;
[oO])
# Try to overwrite the file. $@ contains optional sudo
# command.
if "$@" rm -- "$dst" && "$@" ln -s -- "$src" "$dst"; then
updated=0
else
printf "Failed to resymlink %s\n" "$dst" 1>&2
fi
printf "\n"
break
;;
[kK])
printf "\n"
break
;;
?)
printf "d - show diff between current and repo file\n"
printf "o - overwrite current file with repo symlink\n"
printf "k - keep the current file\n"
printf "? - this help\n"
;;
*)
;;
esac
printf "\n"
done
elif [[ ! -e "$dst" ]]; then
# The destination doesn't exists, so try to symlink it. $@ contains
# optional sudo command.
if "$@" ln -s -- "$src" "$dst"; then
updated=0
else
printf "Failed to symlink %s\n" "$dst" 1>&2
fi
else
printf "%s already exists and is not a file, directory or symlink!\n" "$dst" 1>&2
fi
return "$updated"
}
sudo_symlink_config ()
{
symlink "$1" "$2" sudo -p "\"Enter password to symlink $2:\""
}
install_config ()
{
local src="$1"
local dst="$2"
local updated=1
shift 2
# If the target is the correct file type
if { [[ -f "$src" ]] && [[ -f "$dst" ]] ;} \
|| { [[ -d "$src" ]] && [[ -d "$dst" ]] ;}
then
# As long as the file doesn't match the installable file
while ! diff -qr "$src" "$dst" >/dev/null 2>&1; do
# Prompt user for action.
printf "Wrong source file found for:\n%s\n" "$dst"
read -rep "(D)iff changes/(O)verwrite file/(K)eep file? [Dok?] " \
COMMAND
if [[ -z "$COMMAND" ]]; then
COMMAND=d
fi
case "$COMMAND" in
[dD])
$diffcmd -ur "$dst" "$src" | less -FRX || true
;;
[oO])
# Attempt to copy over. $@ contains optional sudo command.
if "$@" cp -r -- "$src" "$dst"; then
updated=0
else
printf "Failed to update %s\n" "$dst" 1>&2
fi
printf "\n"
break
;;
[kK])
printf "\n"
break
;;
?)
printf "d - show diff between current and repo file\n"
printf "o - overwrite current file with repo symlink\n"
printf "k - keep the current file\n"
printf "? - this help\n"
;;
*)
;;
esac
printf "\n"
done
elif [[ ! -e "$dst" ]]; then
# Attempt to copy over. $@ contains optional sudo command.
if "$@" cp -r -- "$src" "$dst"; then
updated=0
else
printf "Failed to copy to %s\n" "$dst" 1>&2
fi
else
printf "%s already exists and is not a file or directory!\n" "$dst" 1>&2
fi
return "$updated"
}
sudo_install_config ()
{
install_config "$1" "$2" sudo -p "Enter password to install $2:"
}
install_repo ()
{
local repo_name="${2##*/}"
repo_name="${repo_name%.git}"
if [[ ! -d "$1/$repo_name" ]]; then
if ! hg clone --noupdate "$2" "$1/$repo_name"; then
printf "Mercurial clone failed, check if Mercurial is working correctly.\n"
return 1
fi
fi
if [[ ! -d "$1/$repo_name" ]]; then
printf "Not a mercurial repo: %s\n" "$1/$repo_name" 1>&2
return 1
fi
(
cd "$1/$repo_name"
printf "Checking for updates to %s...\n" "$repo_name"
if ! hg pull >/dev/null 2>&1; then
printf "Failed to check updates for %s\n" "$repo_name"
return 1
fi
count () {
hg log -T 'Version: {rev}\n' -r 'descendants(.) - .' | \
grep -c 'Version' 2>/dev/null
}
# shellcheck disable=SC2310,SC2311,SC2312
if [[ "$(count)" -ge "1" ]]; then
while true; do
printf "%d new commits in %s\n" "$(count)" "$1/$repo_name"
read -rep "(D)iff new commits/(U)pdate/(K)eep current? [Duk?] " \
COMMAND
if [[ -z "$COMMAND" ]]; then
COMMAND=d
fi
case "$COMMAND" in
[dD])
hg log -p -r 'descendants(.) - .'
;;
[uU])
hg update -C
break
;;
[kK])
break
;;
?)
printf "d - show diff between current and repo file\n"
printf "u - update repository to latest revision\n"
printf "k - keep the current revision\n"
printf "? - this help\n"
;;
*)
;;
esac
done
fi
return 0
)
}
for link in dotfiles/*; do
name=".${link##*/}"
# shellcheck disable=SC2310
if symlink_config "$PWD/$link" "$HOME/$name"; then
printf "Installed ~/%s.\n" "$name"
fi
done
for install_script in install/*/install.sh; do
# shellcheck disable=SC1090
. "$install_script"
done
os_name="$(uname)"
if [[ "$os_name" = "Darwin" ]]; then
for install_script in install-osx/*/install.sh; do
# shellcheck disable=SC1090
. "$install_script"
done
fi
trap '' EXIT