-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfinst
executable file
·288 lines (252 loc) · 4.31 KB
/
confinst
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
#!/bin/sh
# installs UNIX configuration files
create_dir()
{
if test -e "$1"
then
return
fi
info "Creating $1"
run mkdir "$1"
if ! $simulate && test ! -d "$1"
then
error "$1 is not a directory, exiting"
exit 1
fi
}
extract()
{
local confball
local confdir
for confball in "$HOME"/conf*.tar.gz; do
confdir="$(basename "$confball" .tar.gz)"
if $simulate
then
info "Would extract $confball into $confdir"
continue
fi
create_dir "$confdir"
info "Extracting $confball into $confdir"
tar -C "$confdir" -xzf "$confball"
if test $? -ne 0
then
error "Error extracting configuration archive"
exit 1
fi
done
}
backup()
{
local path="$1"
local backuppath="$path.$(date +%Y%m%d%H%M%S)"
info "Moving $path to $backuppath"
run mv "$path" "$backuppath"
}
link()
{
info "Linking $link to $file"
run ln -s "$1" "$2"
}
remove()
{
run rm "$@"
}
run()
{
if $simulate
then
info "Would run $*" 1>&2
else
"$@"
fi
}
error()
{
printf "%s\n" "$*" 1>&2
}
info()
{
if ! $quiet
then
printf "%s\n" "$*" 1>&2
fi
}
verbose()
{
if $verbose
then
printf "%s\n" "$*" 1>&2
fi
}
get_link()
{
local sourcefile
local destdir
local prefix
local destfile
sourcefile="$1"
destdir="$2"
prefix="$3"
sourcename="${sourcefile##*/}"
echo "$destdir"/"$prefix""$sourcename"
}
file_starts_with()
{
local filename="$1"
local prefix="$2"
if test -n "$prefix"
then
case $filename in
$prefix*)
true
;;
*)
false
;;
esac
else
false
fi
}
link_points_to()
{
local link
local file
link="$1"
file="$2"
test "$(readlink -m "$link")" = "$(readlink -m "$file")"
}
# make_links <sourcedir> <destdir> <prefix> [<overwrite>]
# make symlinks recursively in <destdir> pointing to files in <sourcedir>
# <prefix> will be prepended to the basename of each symlink in destdir
# e.g. if <sourcedir> contains "bashrc" and prefix is ".", the link will be
# ".bashrc"
make_links()
{
local sourcedir
local destdir
local prefix
local file
local link
local overwrite
sourcedir="$1"
destdir="$2"
prefix="$3"
overwrite="${4:-true}"
case "$(basename "$sourcedir")" in .git)
verbose "Skipping $sourcedir"
return
;;
esac
verbose "Making links from $sourcedir to $destdir"
for file in "$sourcedir"/*
do
test -e "$file" || continue
case "$(basename "$file")" in .*)
continue;;
esac
link=$(get_link "$file" "$destdir" "$prefix")
if test -f "$link" && ! "$overwrite"
then
verbose "$link already exists and overwrite=$overwrite"
continue
fi
if test -f "$file"
then
if test -L "$link"
then
if link_points_to "$link" "$file"
then
# link is already correct, go to next file
verbose "$link already points to $file"
continue
else
# link is wrong, remove it
remove "$link"
fi
elif test -e "$link"
then
# something is in the way, back it up
backup "$link"
fi
# install the new link
link "$file" "$link"
elif test -d "$file"
then
if test ! -d "$link"
then
if test -L "$link"
then
# a symlink is in the way, remove it
remove "$link"
elif test -e "$link"
then
# a file is in the way, back it up
backup "$link"
fi
run mkdir -p "$link"
if test $? -ne 0
then
printf "Error creating $link, skipping $file\n" 1>&2
continue
fi
fi
# recursively make links for each file under the "$file" directory
# prefix is null because we assume each file below the first level
# should not have a "." prefix, e.g. ~/conf/vim/plugin should be
# ~/.vim/plugin rather than ~/.vim/.plugin
make_links "$file" "$link" "" "$overwrite"
fi
done
}
destdir="$HOME"
simulate=false
extract=false # now false by default, assume ~/conf is under git
prefix=.
quiet=false
verbose=false
while getopts ":enpqv" opt
do
case $opt in
e)
extract=true
;;
n)
simulate=true
;;
q)
quiet=true
verbose=false
;;
p)
prefix=$OPTARG
;;
v)
verbose=true
quiet=false
;;
'?')
error "Unknown option -$OPTARG"
exit 2
;;
':')
error "Missing argument to -$OPTARG"
exit 2
;;
*)
error "Bug: -$OPTARG option is missing"
exit 1
;;
esac
done
shift $((OPTIND - 1))
if $extract
then
extract
fi
make_links "$HOME/conf" "$destdir" "$prefix"
for dir in "$HOME"/conf.*
do
make_links "$dir" "$destdir" "$prefix" true
done
# vim: set ts=4 sw=4 tw=0 noet: