-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.sh
executable file
·76 lines (65 loc) · 1.49 KB
/
apply.sh
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
#!/bin/bash
BACKUP="$PWD/.bak/$(date -Iseconds)"
DOTFILES="dotfiles"
RUN=
if [ "z$1" = "z--dry-run" ] ; then
RUN=echo
shift
fi
if [ ! -z "$1" ] ; then
DOTFILES="$1"
fi
die() {
echo "$*"
exit 1
}
src_name() {
echo "$1" | sed -e "s/\.\//$DOTFILES\//"
}
dst_name() {
echo "$1" | sed -e 's/\.\//./' | sed -e 's/\.append$//'
}
link_file() {
src=$1 ; shift
dst=$1 ; shift
if [ -h "$dst" ] ; then
return
fi
if [ -f "$dst" ] ; then
$RUN rm -f "$BACKUP/$dst"
$RUN mv "$dst" "$BACKUP/"
fi
d=`dirname "$dst"`
[ -d "$d" ] || $RUN mkdir -p "$d"
$RUN ln -r -s "$src" "$dst"
}
cmp_tail() {
shorter=$1 ; shift
longer=$1 ; shift
lines=`cat "$shorter" | wc -l`
tail -n "$lines" "$longer" | cmp -s $shorter
}
append_file() {
what=$1 ; shift
to=$1 ; shift
[ -f "$to" ] || die "Cannot append to '$to': file doesn't exist"
$RUN cp -f "$to" "$BACKUP/"
$RUN sh -c "cat \"$what\" >> \"$to\""
}
[ -d "$DOTFILES" ] || die "'$DOTFILES' is not a directory"
[ -d "$BACKUP" ] || $RUN mkdir -p "$BACKUP"
( cd "$DOTFILES" && find . -type f ! -path '*/.git/*' ) | while read f ; do
src=`src_name "$f"`
dst=`dst_name "$f"`
if echo "$f" | grep -q '\.sh$' ; then
continue
fi
if echo "$f" | grep -q '\.append$' ; then
cmp_tail "$src" "$dst" || append_file "$src" "$dst"
else
link_file "$src" "$dst"
fi
case "$f" in ./ssh/config*)
$RUN chmod -c 600 "$dst"
esac
done