-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot
More file actions
executable file
·325 lines (249 loc) · 6.77 KB
/
dot
File metadata and controls
executable file
·325 lines (249 loc) · 6.77 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env bash
# vim: ft=bash tw=80
#
# shellcheck disable=2016,2295
set -eo pipefail
declare -g THIS; THIS=$( realpath "${BASH_SOURCE[0]}" )
declare -g PROGDIR="${THIS%/*}"
declare -g DATABASE="${PROGDIR}/database"
declare -gA DATA=() # { source -> dest }
if ! command -v realpath >/dev/null ; then
printf 'requires [realpath].\n'
exit 1
fi
touch "${DATABASE}" -a
mkdir -p "${PROGDIR}/files"
mkdir -p "${PROGDIR}/logs"
function usage {
declare -i status="$1"
cat <<EOF
usage: ${THIS##*/} <command>
commands:
add PATH NAME own \$PATH as relative \$NAME
from NAME copy \$NAME to use locally
ls print owned files
diff PATH vimdiff's multiple NAMEs for \$PATH
git ARGS \`git\` relative to this repo
cd \`cd\` to this repo in subshell
EOF
exit "$status"
}
function load_data {
local src dst
while IFS=$'\t' read -r src dst ; do
DATA["$src"]="$dst"
done < "$DATABASE"
}
function store_data {
local src dst
for src in "${!DATA[@]}" ; do
local dst="${DATA["$src"]}"
printf '%s\t\t%s\n' "$src" "$dst"
done | sort -k2 > "${DATABASE}"
}
function validate_add {
local dst="$1" # where the file will go: the destination
local src="$2" # where it comes from: ./files/
if [[ ! $dst ]] ; then
printf 'missing dst.\n'
exit 1
fi
if [[ -L $dst ]] ; then
printf 'dst already a symlink:\n'
stat --printf '%N\n' "$dst"
exit 1
fi
if [[ ! -f $dst && ! -d $dst ]] ; then
printf 'dst does not exist\n'
printf ' %s\n' "$dst"
exit 1
fi
if [[ ! "${PWD}/${dst}" == ${HOME}* ]] ; then
printf 'dst must be under %s\n' "$HOME"
exit 1
fi
if [[ ! $src ]] ; then
printf 'missing src.\n'
exit 1
fi
if [[ $src =~ ^[/~] ]] ; then
printf 'source must be relative\n'
exit 1
fi
}
function cmd_add {
local dst="$1"
local src="$2"
validate_add "$dst" "$src"
load_data
local abs_src abs_dst rel_dst
abs_src="${PROGDIR}/files/${src}"
abs_dst=$( realpath "$dst" )
rel_dst="${abs_dst#$HOME/}"
if [[ "${DATA[$src]}" ]] ; then
printf 'mapping already exists from\n'
printf ' %s -> %s\n' "$src" "${DATA[$src]}"
exit 1
fi
if [[ -e $abs_src ]] ; then
printf 'src already exists.\n'
printf ' %\n' "$abs_src"
exit 1
fi
# Setup --------------------------------------------------------------------
printf '\e[37;1m%s\e[0m\n' 'creating logfile...'
local logfile
local log_base="${PROGDIR}/logs/log"
local -i log_num=1
while
logfile="${log_base}_$(( log_num++ ))" &&
[[ -f "$logfile" ]]
do :; done
set -x
touch "$logfile"
set +x
# Local backup -------------------------------------------------------------
printf '\n\e[37;1m%s\e[0m\n' 'make local backup...'
local backup
local -i backup_num=1
while
backup="${abs_dst}.bak$(( backup_num++ ))" &&
[[ -e "$backup" ]]
do :; done
cp -van "$abs_dst" "$backup" \
2>&1 | tee -a "$logfile"
# Assume ownership ---------------------------------------------------------
printf '\n\e[37;1m%s\e[0m\n' 'assume ownership...'
mkdir -vp "${abs_src%/*}" 2>&1 | tee -a "$logfile"
mv -vn "$abs_dst" "$abs_src" 2>&1 | tee -a "$logfile"
# Symlink back -------------------------------------------------------------
printf '\n\e[37;1m%s\e[0m\n' 'link back...'
ln -sv "$abs_src" "$abs_dst" 2>&1 | tee -a "$logfile"
DATA["$src"]="$rel_dst"
store_data
# Sanity checks ------------------------------------------------------------
printf '\n\e[37;1m%s\e[0m\n' 'sanity checks...'
local -i errors=0
if [[ -L $abs_dst ]] ; then # (1) is a symlink
printf '\e[32;1m%s\e[0m' '.'
else
printf '\e[31;1m%s\e[0m' 'X'
(( ++errors ))
fi
if diff "$abs_dst" "$backup" >/dev/null ; then # (2) .bak matches
printf '\e[32;1m.\e[0m'
else
printf '\e[31;1mX\e[0m'
(( ++errors ))
fi
if diff "$abs_dst" "$abs_src" >/dev/null ; then # (3) dst/src match
printf '\e[32;1m.\e[0m'
else
printf '\e[31;1mX\e[0m'
(( ++errors ))
fi
printf '\n'
(( ! errors ))
# Commit result ------------------------------------------------------------
#
# TODO: add `--commit` flag for something that doesn't potentially require
# adding to the .hgignore. If ommitted, should
#
printf '\n\e[37;1madd & commit...\e[0m\n'
set -x
git -C "$PROGDIR" add "$abs_src" "$DATABASE"
git -C "$PROGDIR" commit -m "add: ${rel_dst} as ${src}"
set +x
printf '\n\e[37;1m%s\e[0m\n' 'safe to remove'
printf '\e[37m%s\e[0m\n' "$backup"
printf '\n\e[32;1m%s\e[0m\n' 'done.'
}
function cmd_from {
local src="$1"
if [[ ! $src ]] ; then
printf 'missing src.\n'
exit 1
fi
load_data
local dst="${DATA[$src]}"
if [[ ! $dst ]] ; then
printf 'src not found in database\n'
exit 1
fi
local abs_src="${PROGDIR}/files/${src}"
local abs_dst="${HOME}/${dst}"
if [[ ! -e $abs_src ]] ; then
printf 'src file seems not to exist at...\n'
printf ' %s\n' "$abs_src"
exit 1
fi
if [[ -L $abs_dst ]] ; then
printf 'dst already a symlink:\n'
ls -l "$abs_dst"
exit 1
fi
if [[ -e $abs_dst ]] ; then
local -i dst_num=1
local dst_base="${abs_dst}"
local backup_dst
while
backup_dst="${dst_base}.bak$(( dst_num++ ))" &&
[[ -e "$backup_dst" ]]
do :; done
mv -v "$abs_dst" "$backup_dst"
fi
mkdir -p "${abs_dst%/*}"
cp -va "$abs_src" "$abs_dst"
printf 'deployed \e[37;1m%s\e[0m -> \e[37;1m%s\e[0m\n' \
"$src" "${abs_dst/$HOME/\~}"
}
function cmd_diff {
local f1="$1"
local f2="$2"
load_data
if [[ ! "${DATA[$f1]}" ]] ; then
printf 'left-hand side file not found in database\n'
printf ' %s\n' "$f1"
exit 1
fi
if [[ ! "${DATA[$f2]}" ]] ; then
printf 'right-hand side file not found in database\n'
printf ' %s\n' "$f1"
exit 1
fi
exec nvim -d \
"${PROGDIR}/files/${f1}" \
"${PROGDIR}/files/${f2}"
}
case "$1" in
'' | -h | --help)
usage 0 ;;
add) shift
cmd_add "$@"
;;
from)
shift
cmd_from "$@"
;;
ls | list)
sort -k2 "${DATABASE}" | column -t -s $'\t'
;;
diff)
shift
cmd_diff "$@"
;;
git)
git -C "${PROGDIR}" "${@:2}"
;;
cd)
if ! cd "${PROGDIR}/files/${HOSTNAME}" 2>/dev/null; then
printf "cannot \`cd\`\n" >&2
exit 1
fi
printf '(launching subshell)\n'
exec /bin/bash --rcfile <( printf '
. ~/.bashrc ; export PS1="[dot] ${PS1}"
')
;;
*) usage 1 ;;
esac