-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.sh
executable file
·132 lines (111 loc) · 2.81 KB
/
sync.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
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
if [ -z "$HOME" ]; then
echo "HOME (~) is undefined, cannot sync"
exit 1
fi
# remove suffix slash if exist
HOME=${HOME%"/"}
overwrite_prompt () {
link=$(readlink -f $1)
while true; do
read -n 1 -p "$1 is linked to other directory ($link). [o]verwrite, or [i]gnore? " oi
case $oi in
[Oo]* ) echo "overwrite"; break;;
[Ii]* ) break;;
* ) echo "Please answer [o]verwrite, or [i]gnore.";;
esac
done
}
link_directory_to_home () {
for f in $(ls -d $1)
do
# check if directory
if ! [[ -d "$f" ]]
then
continue
fi
dir=${f#"./"}
target=$(echo "$HOME/$dir")
if [[ -L "$target" ]]
then
# target exist
if ! [ "$target" -ef "$dir" ]
then
# target is not expected
action=$(overwrite_prompt $target)
if [ -z $action ]
then
# ignore
echo "$dir is being ignored."
continue
else
# overwrite
rm $target
fi
else
echo -e "\e[32m✓\e[39m $dir"
continue
fi
fi
echo "$target"
ln -s $(realpath $dir) $target
done
}
link_directory () {
echo "Linking... $1"
for f in $(ls --ignore ".git" --ignore "*.nosync" "$1")
do
f="$1/$f"
# ignore items with .nosync suffix
if [[ $f == *.nosync ]]
then
continue
fi
# is directory?
if [[ -d "$f" ]]
then
echo "$f: Directory"
link_directory $f
continue
fi
if [[ -f "$f" ]]
then
echo "$f: File"
continue
fi
done
}
link_files_in_dir_to_home () {
dir=${1%"/"}
for base in $(ls -al $1 --ignore "*.example" | grep '^-' | awk '{ print $9 }')
do
# if starts with hashtag, then it is emacs file
if [[ $base =~ ^# ]]
then
continue
fi
# ignore .example files
if [[ $base =~ *.example$ ]]
then
continue
fi
# ignore this file
if [[ $base =~ ^sync ]]
then
continue
fi
f="$dir/$base"
echo -e "\e[32m✓\e[39m $base"
if ! [ -L "$HOME/$base" ]; then
ln -s $(realpath $f) "$HOME/$base" 2>&1 > /dev/null
fi
done
}
link_directory_to_home .emacs.d
link_directory_to_home .scripts
link_directory_to_home .exec
link_directory_to_home .notifications
link_directory_to_home ".config/*"
link_files_in_dir_to_home .
if [ ! -L "$HOME/.config/mimeapps.list" ]; then
ls -s "$HOME/.dotfiles/.config/mimeapps.list" "$HOME/.config/mimeapps.list"
fi