-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot
More file actions
executable file
·241 lines (197 loc) · 5.27 KB
/
dot
File metadata and controls
executable file
·241 lines (197 loc) · 5.27 KB
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
#!/bin/bash
options="install diff status"
list=(
'.config'
'.local'
'.tmux.conf'
)
printUsage()
{
echo "Usage: dot [OPTION]"
echo "OPTIONS:"
echo " install [--config]"
echo " diff [--config]"
echo " status [-a] [-l num]"
}
printTrackedUntracked()
{
local dotpath=$1
local printed=$2
local found=0
cd "$HOME" || return 1
for dir in "${list[@]}"; do
[ -d "$dotpath/$dir" ] || continue
while IFS= read -r root; do
[ -e "$root" ] || continue
[ -d "$root" ] || continue
local missing_dirs=()
while IFS= read -r entry; do
local skip=0
for missing_dir in "${missing_dirs[@]}"; do
if [[ "$entry" == "$missing_dir/"* ]]; then
skip=1
break
fi
done
[ "$skip" == 1 ] && continue
[ -e "$dotpath/$entry" ] && continue
[ "$printed" == 0 ] && printed=1 && echo && echo "Untracked configuration:"
echo -e "\e[1;31m $entry\e[0;37m"
found=1
if [ -d "$entry" ]; then
missing_dirs+=("$entry")
fi
done < <(find "$root" -mindepth 1 | sort)
done < <(cd "$dotpath" && find "$dir" -mindepth 1 -maxdepth 1 -type d | sort)
done
return "$found"
}
printAllUntracked()
{
local dotpath=$1
local printed=$2
local maxdepth=$3
local found=0
cd "$HOME" || return 1
for dir in "${list[@]}"; do
[ -d "$dir" ] || continue
[ -d "$dotpath/$dir" ] || continue
while IFS= read -r root; do
[ -e "$dotpath/$root" ] && continue
[ "$printed" == 0 ] && printed=1 && echo && echo "Untracked configuration:"
while IFS= read -r entry; do
echo -e "\e[1;31m $entry\e[0;37m"
done < <(find "$root" -maxdepth "$maxdepth" | sort)
found=1
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d | sort)
done
return "$found"
}
dotStatus()
{
local show_all=0
local level=3
flag=0
error=0
dotpath=$PWD
track=0
while [ $# -gt 0 ]; do
case $1 in
-a)
show_all=1
;;
-l)
shift
if [ -z "$1" ] || [[ ! "$1" =~ ^[0-9]+$ ]]; then
echo "error: -l requires a numeric depth" && echo
printUsage
return 2
fi
level=$1
;;
*)
echo "error: wrong option" && echo
printUsage
return 2
;;
esac
shift
done
echo "Dot status"
# Basic Configuration File
for dir in ${list[@]}; do
files=`find $dir -type f`
for file in $files; do
if [ "$MSYSTEM" ]; then
if [[ "$file" != *"lese"* ]]; then
continue
fi
fi
[ ! -f "$HOME/$file" ] && echo -e " \e[1;31mnonexistent: $file\e[0m" && error=1 && continue
res="`diff $file $HOME/$file`"
if [ "$res" ]; then
[ $flag == 0 ] && flag=1 && echo "Changes for dotfiles:"
echo -e "\e[1;31m modified: $file\e[0;37m"
fi
done
done
printTrackedUntracked "$dotpath" "$track"
if [ $? == 1 ]; then
track=1
fi
if [ "$show_all" == 1 ]; then
printAllUntracked "$dotpath" "$track" "$level"
if [ $? == 1 ]; then
track=1
fi
fi
[ $flag == 0 -a $track == 0 -a $error == 0 ] && echo "all same"
}
diffFile()
{
res="`diff $1 $2`"
if [ "$res" ]; then
if type diff-so-fancy >/dev/null 2>&1; then
echo && (diff -ru $1 $2 | diff-so-fancy)
else
diff -ru $1 $2
fi
fi
}
dotDiff()
{
echo "Dot diff"
# Basic Configuration File
for dir in ${list[@]}; do
files=`find $dir -type f`
for file in $files; do
if [ "$MSYSTEM" ]; then
if [[ "$file" != *"lese"* ]]; then
continue
fi
fi
[ ! -f "$HOME/$file" ] && echo -e " \e[1;31mnonexistent: $file\e[0m" && continue
diffFile $file $HOME/$file
done
done
}
dotInstall()
{
echo "Dot install"
# Basic Configuration File
for dir in ${list[@]}; do
if type rsync >/dev/null 2>&1; then
rsync -av $dir $HOME
else
cp -rf $dir $HOME
fi
[ $? == 0 ] && echo "$dir install done"
done
echo && echo -e "Do you want to initialize the configuration? (y/n) \c" && read option
if [ "$option" == "n" ]; then
exit 0
fi
echo "" >> $HOME/.profile
echo "source \"\$HOME/.config/lese/init.sh\" --file" >> $HOME/.profile
echo "profile init successful"
}
if [ $# -lt 1 ]; then
printUsage
exit 1
fi
case $1 in
status)
dotStatus "${@:2}"
;;
diff)
dotDiff $2
;;
install)
dotInstall $2
;;
*)
echo "error: wrong option" && echo
printUsage
exit 2
;;
esac