-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.sh
executable file
·98 lines (82 loc) · 2.7 KB
/
update.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
DIFF_ONLY=0
if [ "$1" = "-d" ]; then
DIFF_ONLY=1
echo "Diff only..."
shift
fi
# Get all the files that we're going to write.
# This is the union of all the paths in both 'common' and the specified host dirs.
for dir in common $*; do
([ -d "$dir" ] && find $dir -type f)
done | sort | uniq | grep -v '[.]prepend$' | while read file; do
# 'base' is the top-level dir (e.g. 'home', 'etc').
base=`echo "$file" | cut -d/ -f2`
# 'file' is the path (e.g. '.i3/config', 'X11/xorg.conf.d/20-foo.conf').
file=`echo "$file" | cut -d/ -f3-`
# Optional prefix for running commands (e.g. 'sudo', or 'echo' for debugging).
prefix=""
dest=""
# Things going in /etc need sudo.
if [ $base = "etc" ]; then
prefix="sudo "
dest="/etc"
fi
# 'home' is $HOME
if [ $base = "home" ]; then
dest=$HOME
fi
# Don't allow directories other than /etc and $HOME.
if [ -z "$dest" ]; then
echo "Not sure where to write $base"
exit 1
fi
# Enable for debugging.
#prefix="echo $prefix"
# Create the temporary output file.
echo -n > /tmp/dotfile
# Prepend any '.prepend' files.
for part in common $*; do
[ -e "$part/$base/$file.prepend" ] && cat "$part/$base/$file.prepend" >> /tmp/dotfile
done
# Then the main content.
for part in common $*; do
[ -e "$part/$base/$file" ] && cat "$part/$base/$file" >> /tmp/dotfile
done
# Append any '.append' files.
for part in common $*; do
[ -e "$part/$base/$file.append" ] && cat "$part/$base/$file.append" >> /tmp/dotfile
done
if diff "/tmp/dotfile" "$dest/$file" > /dev/null; then
#if [ $DIFF_ONLY -eq 0 ]; then
echo -e "$dest/$file \033[01;92m[skip]\033[0m"
#fi
else
if [ $DIFF_ONLY -eq 1 ]; then
echo -e "$dest/$file \033[01;91m[diff]\033[0m"
diff "/tmp/dotfile" "$dest/$file"
echo
for part in common $*; do
[ -e "$part/$base/$file" ] && echo " cp \"$dest/$file\" \"$part/$base/$file\""
done
echo
else
echo -e "$dest/$file \033[01;91m[update]\033[0m"
$prefix mkdir -p `dirname $dest/$file`
$prefix bash -c "cat /tmp/dotfile > '$dest/$file'"
fi
fi
if [ $DIFF_ONLY -eq 0 ]; then
# Anything in bin/ gets +x.
if [[ "$file" =~ ^bin/ ]]; then
chmod +x "$dest/$file"
fi
fi
done
# if [ $DIFF_ONLY -eq 0 ]; then
# # Compile .emacs --> .emacs.elc
# update_script=$PWD/update-emacs.el
# (cd ~; emacs -nw -q --script $update_script)
# # Update gnome settings
# dconf load / < ~/.config/gnome-settings
# fi