-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotfiles.sh
executable file
·304 lines (240 loc) · 5.34 KB
/
dotfiles.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash
# this is for detecting script's location
#here=$( dirname $( realpath "${BASH_SOURCE[0]}" ) )
here=$( pwd )
# system install
# ln -s $here/dotfiles.sh /usr/bin/dot
meta_repo=/home/fractal/code/meta_repo
meta_dotfiles=dot
meta_real_dotfiles=$( realpath $meta_repo/$meta_dotfiles )
mkdir -p $meta_real_dotfiles
help_show() {
echo "usage: dot [-h|--help] [-a|add <file>]
[-b|backup ?--all]
[-l|list]
[-p|project]
[-r|restore <file>]
[-s|snapshot]"
}
proj_path=$here
proj_name_list() {
cd $meta_real_dotfiles
#printf -- "%s\n" $( pwd )
proj_paths=( $( find -maxdepth 1 -type d | ls ) )
#printf -- "paths are <%s>\n" $proj_paths
cd $here
paths_max=$(( ${#proj_paths[@]} - 1 ))
#printf -- "${#proj_paths[@]} paths"
proj_names=()
for (( i=0; i<=$paths_max; i++ )); do
proj_name=${proj_paths[$i]}
#printf -- "proj name <%s>\n" $proj_name
proj_names+=( $proj_name )
#printf "added proj name <%s>\n" $proj_name
done
echo "${proj_names[@]}"
}
#echo "real dotfiles: <$meta_real_dotfiles>"
proj_name_get() {
# list dotfile projects
# split path parts
# check path segments against projects in reverse
proj_names=( $( proj_name_list ) )
proj_name=$( basename $here )
names_max=$(( ${#proj_names[@]} -1 ))
for (( i=0; i<=$names_max; i++ )); do
name=${proj_names[$i]}
#printf "i: %d, name: <%s>, proj_name: <%s>\n" $i $name $proj_name
if [ "$proj_name" == "$name" ]; then
echo $proj_name
break
fi
done
}
proj_name=$( proj_name_get )
#printf "project name: <%s>" $proj_name
dotfile_add() {
filename="$1"
cp $filename $meta_real_dotfiles/$proj_name/$filename
cd $meta_real_dotfiles
git add -f $proj_name/$filename
cd $here
}
dotfile_backup() {
proj_names=()
if [ $flag_all -eq 1 ]; then
proj_names+=( $( proj_name_list ) )
else
proj_names+=( $proj_name )
fi
#printf "project names are <%s>\n" "${proj_names[@]}"
dirmax=$(( ${#proj_names[@]} - 1 ))
for (( i=0; i<=$dirmax; i++ )); do
dirname=${proj_names[$i]}
cd $meta_real_dotfiles/$dirname
dot_list=( $( find -type f | ls -A ) )
cd $here
dotmax=$(( ${#dot_list[@]} - 1 ))
for (( j=0; j<=$dotmax; j++ )); do
dot=${dot_list[$j]}
printf -- "Checking <%s/%s>..." $dirname $dot
cp -n -u $dot $meta_real_dotfiles/$dirname/$filename 2> /dev/null
if [ $? -eq 0 ]; then
#printf -- "backed up <%s/%s>\n" $dirname $dot
printf "backed up\n"
else
printf "ok\n"
fi
done
done
cd $here
}
dotfile_restore() {
dotname="$1"
dirname="$proj_name"
printf -- "[%s]\n" $dirname
cd $meta_real_dotfiles/$dirname
dot_list=( $( find -type f | ls -A ) )
dotmax=$(( ${#dot_list[@]} - 1 ))
for (( j=0; j<=$dotmax; j++ )); do
dot=${dot_list[$j]}
if [ "$dot" == "$dotname" ]; then
cp -n -u $dot $here/$dotname 2> /dev/null
printf -- "Restored <%s> from <%s>\n" $dotname $meta_real_dotfiles/$dirname/$dot
return
fi
done
printf -- "ERROR: unregistered dotfile <%s>\n" $dotname
cd $here
exit 4
}
dotfiles_show() {
proj_names=()
if [ $flag_all -eq 1 ]; then
proj_names+=( $( proj_name_list ) )
else
proj_names+=( $proj_name )
fi
#printf "project names are <%s>\n" "${proj_names[@]}"
dirmax=$(( ${#proj_names[@]} - 1 ))
for (( i=0; i<=$dirmax; i++ )); do
dirname=${proj_names[$i]}
printf -- "[%s]\n" $dirname
cd $meta_real_dotfiles/$dirname
dot_list=( $( find -type f | ls -A ) )
dotmax=$(( ${#dot_list[@]} - 1 ))
for (( j=0; j<=$dotmax; j++ )); do
dot=${dot_list[$j]}
printf -- "* %s\n" $dot
done
printf -- "\n"
done
cd $here
}
snapshot_all() {
cd $meta_real_dotfiles
git add .
git commit -m "dotfiles snapshot"
# pasted from git-aliases.sh
remotes=( $( git remote ) )
for name in "${remotes[@]}"
do
push_url=$(git remote get-url --push "$name")
if [ "$push_url" != "no_push" ]; then
echo "Pushing to remote $name..."
git push "$name"
fi
done
cd $here
}
status_show() {
cd $meta_real_dotfiles
git status
cd $here
}
project_init() {
proj_name=$( basename $here )
mkdir -p $meta_real_dotfiles/$proj_name
}
guard_in_project() {
if [ "$proj_name" == "" ]; then
printf -- "ERROR: not in a registered project\n"
help_show
exit 3
fi
#printf -- "project <%s>\n" $proj_name
}
flag_all=0
orig_args=()
for arg in "$@"; do
shift
orig_args+="$arg"
case "$arg" in
'--help') set -- "$@" '-h' ;;
'--all') flag_all=1 ;;
'add') set -- "$@" '-a' ;;
'backup') set -- "$@" '-b' ;;
'list') set -- "$@" '-l' ;;
'project') set -- "$@" '-p' ;;
'restore') set -- "$@" '-r' ;;
'snapshot') set -- "$@" '-s' ;;
'status') set -- "$@" '-t' ;;
*) set -- "$@" "$arg" ;;
esac
done
#printf -- "flag value <%d>\n" $flag_all
while getopts ":a:r:bhlpst" opt; do
case $opt in
a)
guard_in_project
dotfile_add "$OPTARG"
exit 0
;;
b)
if [ $flag_all -eq 0 ] ; then
guard_in_project
fi
dotfile_backup
exit 0
;;
h)
help_show
exit 0
;;
l)
if [ $flag_all -eq 0 ] ; then
guard_in_project
fi
dotfiles_show
exit 0
;;
p)
project_init
exit 0
;;
r)
guard_in_project
dotfile_restore "$OPTARG"
exit 0
;;
s)
snapshot_all
exit 0
;;
t)
status_show
exit 0
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument"
exit 1
;;
esac
done
shift $((OPTIND-1))
printf "Unhandled command/argument sequences\n"
exit 2