-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate
executable file
·251 lines (218 loc) · 3.94 KB
/
update
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
#! /bin/sh
set -eu
##
## Usage
##
usage () {
cat <<EOF
usage: $0 [ <options> ] [ <files> ]
<options>:
-f Update files in current directory from configuration files in home.
-t Update configuration files from files in current directory; make backups.
-T Like ‘-t’ but don’t make any backups.
-y Assume ‘y’ to all questions, i.e. overwrite any changed files w/o asking.
-n Assume ‘n’ to all questions, i.e. only copy new files; leave existing be.
EOF
exit 1
}
##
## Parse arguments
##
direction=
ask=ask
for arg in "$@"; do
case $arg in
-) usage;;
-?*) : ;;
*) break
esac
shift
while arg=${arg#?}; [ -n "$arg" ]; do
case "$arg" in
f*) direction=from ; backup=false;;
t*) direction=to ; backup=true ;;
T*) direction=to ; backup=false;;
y*) ask=if_different ;;
n*) ask=false ;;
*) usage ;;
esac
done
done
##
## Color handling
##
if ! which tput >/dev/null 2>&1; then
tput () {
return 0
}
fi
##
## Print info
##
case "$direction" in
from)
tput bold
tput setaf 7
echo 'Updating files in current directory from configuration files'
tput sgr0
;;
to)
tput bold
tput setaf 7
echo 'Updating configuration files from files in current directory'
tput sgr0
;;
*)
usage
esac
##
## File arguments
##
if [ $# -eq 0 ]; then
match () {
return 0
}
else
match () {
for f in "$@"; do
case "$file" in "$f")
return 0
esac
done
return 1
}
fi
##
## Interactive asking
##
if diff=$(which cdiff 2>&1); then
cdiff=$diff
else
diff=diff
cdiff=cat
fi
equal() {
set -- "$@" "$(readlink "$2")" "$(readlink "$3")"
if [ x"$4" = x"$5" ]; then
if [ -n "$4" ] || cmp -s "$2" "$3"; then
return 0
fi
fi
if [ x"$1" != x-v ]; then
return 1
elif [ -z "$4$5" ]; then
"$diff" -u -- "$3" "$2"
return 1
fi
(
printf '%s\n' "--- $3"
printf '%s\n' "+++ $2"
if [ -z "$5" ]; then
sed s/^/-/ <$3
else
printf %s\\n "-→ $5"
fi
if [ -z "$4" ]; then
sed s/^/+/ <$2
else
printf %s\\n "+→ $4"
fi
) | "$cdiff"
return 1
}
ask() {
if equal -v "$1" "$2"; then
return 1
fi
while :; do
tput bold
tput setaf 7
tput setab 4
printf '%s:' "$2"
tput sgr0
tput setaf 7
tput setab 4
printf ' [yn] '
tput sgr0
read answer || exit 0
case "$answer" in
[yY]) return 0 ;;
[nN]) return 1 ;;
esac
done
}
if_different() {
! equal -s "$1" "$2"
}
##
## Emacs files handling
##
compile_el() {
if [ -n "$(which emacs 2>/dev/null)" ]; then
tput bold
tput setaf 7
echo "Compiling $1"
tput sgr0
emacs --batch -f batch-byte-compile "$1"
fi
}
##
## Do the job
##
handle() {
case $3 in *@) set -- "$1" "$2" "${3%@}${2##*/}"; esac
case $1 in from)
[ -d "${3%/*}" ] || return 0
set -- "$1" "$3" "$2"
esac
if [ -h "$3" ] || [ -e "$3" ]; then
if ! $ask "$2" "$3" <&3; then
return 0
elif $backup; then
mv -- "$3" "$3~"
fi
fi
case $3 in */*)
mkdir -p -- "${3%/*}"
esac
cp -dv -- "$2" "$3"
case "$direction:$3" in to:*.el)
compile_el "$3"
esac
}
while read pattern destination; do
for file in $pattern; do
case "$file" in *\~|*/\#*|*\#|*/.*)
continue
esac
if ! match "$@"; then
:
elif [ -h "$file" ] || [ -f "$file" ]; then
handle "$direction" "$file" "$HOME/$destination"
elif ! [ -e "$file" ]; then
echo "update: $file: missing, skipping" >&2
fi
done
done 3<&1 <<EOF
aspell.en.pws .@
bin/* .local/bin/@
emacs/* .config/emacs/@
git/* .config/git/@
inputrc .@
irssi.theme .irssi/mina.theme
libexec/* .local/libexec/@
mail/* .mail/@
mpv/* .config/mpv/@
sawfishrc .sawfish/rc
sh/* .@
statuses .irssi/@
x/80-char.png .urxvt/@
x/Xresources .config/@
x/gtk-3.0.ini .config/gtk-3.0/settings.ini
x/gtkrc-2.0 .@
x/local.keymap .config/xkb/keymap/local
x/local.symbols .config/xkb/symbols/local
x/xinitrc .@
x/xserverrc .@
x/xsettingsd .@
EOF