-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdots
executable file
·259 lines (255 loc) · 8.32 KB
/
dots
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
#!/bin/bash
# shellcheck disable=SC2015,SC2088
PATHSEP=:
shopt -s nullglob
dotdir="$(
dirname "$(
realpath "$(
[[ $0 =~ / ]] && echo "$0" || which "$0"
)"
)"
)"
cd "$dotdir" || exit
custom_dirs=(bin)
if realpath "$dotdir" | grep -q "^$HOME" && [[ $1 = ln ]]; then
## non hidden dirs
for n in "${custom_dirs[@]}"; do
echo Linking "$n" '->' "~/$n"
if [ ! -L "$HOME/$n" ]; then
rm -rf "${HOME:?}/$n"
fi
ln -Tfs "$PWD/$n" "$HOME/$n"
done
for n in dot-*; do
t="${n#dot-}"
t="${t//$PATHSEP//}"
mkdir -p "$(dirname "$HOME/.$t")"
s=$(realpath --relative-to="$(dirname "$HOME/.$t")" "$n")
echo Linking "$n" '->' "~/.$t"
if [ ! -L "$HOME/.$t" ]; then
rm -fr "$HOME/.$t"
fi
ln -Tfs "$s" "$HOME/.$t"
done
elif [[ $PWD == "$dotdir" ]] && [[ $1 = ps ]]; then
[[ $2 =~ ^[[:punct:]]$ ]] || {
echo "Invalid arg: $2" >&2
exit 1
}
sed -i 's/^PATHSEP=.*$/PATHSEP='"$2"'/' "$0"
for n in *; do
if [[ $n != "${n//$PATHSEP/$2}" ]]; then
echo "$n -> ${n//$PATHSEP/$2}"
mv "$n" "${n//$PATHSEP/$2}"
fi
done
elif [[ $PWD == "$dotdir" ]] && [[ $1 = systemd ]] && [[ $2 = update ]]; then
mod=0
mkdir -p ~/.config/systemd/user
for unit in systemd/unit/*; do
if ! {
[ -f ~/.config/systemd/user/"${unit##*/}" ] && cmp --silent "$unit" ~/.config/systemd/user/"${unit##*/}"
}; then
echo "systemd-user: installing ${unit##*/}"
cp "$unit" ~/.config/systemd/user/"${unit##*/}"
((mod++))
fi
done
for override in systemd/override/*; do
mkdir -p ~/.config/systemd/user/"${override##*/}.d"
if ! {
[ -f ~/.config/systemd/user/"${override##*/}.d"/override.conf ] && cmp --silent "$override" ~/.config/systemd/user/"${override##*/}.d"/override.conf
}; then
echo "systemd-user: overriding ${override##*/}"
cp "$override" ~/.config/systemd/user/"${override##*/}.d"/override.conf
((mod++))
fi
done
((mod > 0)) && systemctl --user daemon-reload || true
if [[ -f systemd/config/enabled.tsv ]]; then
while IFS=$'\t' read -r type unit; do
[[ $type$unit = TYPENAME ]] && continue
[[ $(systemctl --user is-enabled "$unit.$type") = enabled ]] && continue
echo "systemd-user: enabling $type ${unit##*/}"
systemctl --user enable "$unit.$type"
case $type in
timer)
echo "systemd-user: starting $type ${unit##*/}"
systemctl --user start "$unit.$type"
;;
esac
done <<< "$(grep -vE '^([[:blank:]]*#|$)' systemd/config/enabled.tsv)"
fi
if [[ -f systemd/config/links.tsv ]]; then
mod=0
while IFS=$'\t' read -r target install name path; do
[[ $target$install$name$path = TARGETINSTALLNAMEPATH ]] && continue
[[ $(readlink ~/.config/systemd/user/"$target.$install/$name") == "$path" ]] && continue
[[ -f "$path" ]] || continue
echo "systemd-user: enabling $name in $target"
mkdir -p ~/.config/systemd/user/"$target.$install"
ln -Tfs "$path" ~/.config/systemd/user/"$target.$install/$name"
((mod++))
done <<< "$(grep -vE '^([[:blank:]]*#|$)' systemd/config/links.tsv)"
((mod > 0)) && systemctl --user daemon-reload || true
fi
elif [[ $PWD == "$dotdir" ]] && [[ $1 = systemd ]] && [[ $2 = sync ]]; then
mkdir -p systemd/unit
for unit in ~/.config/systemd/user/*; do
if [[ -f "$unit" ]] && ! [ -L "$unit" ]; then
if [ -f systemd/unit/"${unit##*/}" ] && ! cmp --silent "$unit" systemd/unit/"${unit##*/}"; then
echo "dots-systemd: update unit ${unit##*/}"
cp -f "$unit" systemd/unit/"${unit##*/}"
elif ! [ -f systemd/unit/"${unit##*/}" ]; then
echo "dots-systemd: backup unit ${unit##*/}"
cp "$unit" systemd/unit/"${unit##*/}"
fi
fi
done
for override in ~/.config/systemd/user/*.d/override.conf; do
override=$(echo "$override" | xargs dirname | xargs basename | sed 's/\(.*\)\.d/\1/')
if [[ -f ~/.config/systemd/user/"$override.d"/override.conf ]]; then
if [ -f systemd/override/"$override" ] && ! cmp --silent ~/.config/systemd/user/"$override.d"/override.conf systemd/override/"$override"; then
echo "dots-systemd: backup override ${unit##*/}"
cp -f ~/.config/systemd/user/"$override.d"/override.conf systemd/override/"$override"
elif ! [ -f systemd/override/"$override" ]; then
echo "dots-systemd: update override ${unit##*/}"
cp ~/.config/systemd/user/"$override.d"/override.conf systemd/override/"$override"
fi
fi
done
if [[ -f systemd/config/enabled.tsv ]]; then
while IFS=$'\t' read -r type unit; do
[[ $(systemctl --user is-enabled "$unit.$type") = enabled ]] || continue
echo "dots-systemd: enable $type ${unit##*/}" 1>&2
echo -e "$type\t$unit"
done < <(
(
basename -a ~/.config/systemd/user/*.target.*/* | awk -F. '{print $2"\t"$1}'
tail +2 systemd/config/enabled.tsv
) | sort -u
echo -e "TYPE\tNAME" > systemd/config/enabled.tsv
) > >(
sort -u >> systemd/config/enabled.tsv
)
fi
# if [[ -f systemd/config/links.tsv ]];then
# while IFS=$'\t' read -r target install name path;do
# [[ $target$install$name$path = TARGETINSTALLNAMEPATH ]] && continue
# [[ $(readlink ~/.config/systemd/user/"$target.$install/$name") == "$path" ]] && continue
# echo "systemd-user: enabling $name in $target"
# ln -Tfs "$path" ~/.config/systemd/user/"$target.$install/$name"
# ((mod++))
# done <<<"$(grep -vE '^([[:blank:]]*#|$)' systemd/config/links.tsv)"
# fi
elif [[ $PWD == "$dotdir" ]] && [[ $1 = systemd ]]; then
echo usage:
echo -e " sync\t\tsync systemd user config"
echo -e " update\tupdate systemd user config"
exit
elif [[ $PWD == "$dotdir" ]] && [[ $1 = rm ]]; then
shift
[[ $1 =~ ^(-f|--force)$ ]] && shift && FORCE=true
for n in "$@"; do
if [[ $n = "${0##*/}" ]]; then
echo Can\'t remove dotfiles manager >&2
elif [ -e "$n" ]; then
${FORCE:-false} || for p in 'Delete' 'Really delete' 'Really really delete'; do
unset REPLY
until [[ $REPLY =~ [YyNn$'\n'] ]]; do
read -i y -N 1 -rp"$p $n"'? [y/N]:'
case "$REPLY" in
n | N | $'\n')
[[ "$REPLY" != $'\n' ]] && echo
continue 3
;;
y | Y) [[ $p != 'Really really delete' ]] && {
printf '\r'
tput el
} || echo ;;
*)
printf '\r'
tput el
;;
esac
done
done
echo "Deleting $n"
t="${n#dot-}"
t="${t//$PATHSEP//}"
if [ ! -L "$HOME/.$t" ]; then
rm -fr "$HOME/.$t"
else
rm -f "$HOME/.$t"
fi
cp -a "$n" "$HOME/.$t"
rm -rf "$n"
else
echo "'$n' doesn't exist" >&2
fi
done
elif [[ $PWD == "$dotdir" ]] && [[ $1 = cp ]]; then
shift
cd "$OLDPWD" || exit
for n in "$@"; do
if [ -L "$n" ] && readlink "$n" | grep -q "^$dotdir"; then
echo "Already tracking '$n'" >&2
continue
fi
p="$(realpath --no-symlinks "$n")"
n="$(realpath "$n")"
if echo "$n" | grep -q "^$HOME/"; then
t="$(echo "$n" | sed "s@$HOME/@@;s@^\.@dot-@;s@/@$PATHSEP@g")"
if [ -d "$n" ] && [ ! -L "$n" ]; then
cp -r "$n" "$dotdir/$t"
rm -r "$n"
ln -s "$dotdir/$t" "$n"
elif [ -L "$n" ] && [ ! -d "$(realpath "$n")" ] && [ ! -f "$(realpath "$n")" ]; then
ln -fs "$(realpath "$n")" "$dotdir/$t"
rm "$n"
ln -s "$(realpath "$dotdir/$t")" "$n"
elif realpath "$n" | grep -q "^$HOME/" && [ -L "$n" ]; then
if [ -f "$dotdir/$(readlink "$n" | sed "s@$HOME/@@;s@^\.@dot-@;s@/@$PATHSEP@g")" ]; then
n="$(readlink "$n" | sed "s@$HOME/@@;s@^\.@dot-@;s@/@$PATHSEP@g")"
else
n="$(realpath "$n")"
fi
ln -fs "$n" "$dotdir/$t"
elif realpath "$n" | grep -q "^$HOME/" && [ -f "$n" ]; then
cp "$n" "$dotdir/$t"
ln -fs "$dotdir/$t" "$n"
fi
else
echo "$n is niether under '$HOME' nor a symlink." >&2
fi
done
elif [[ $1 = ls ]]; then
if [[ -n $(env which columns 2> /dev/null) ]]; then
cmd='columns -s'
elif [[ -n $(env which column 2> /dev/null) ]]; then
cmd='column -x'
else
cmd='cat'
fi
find . -maxdepth 1 -name 'dot-*' -print | cut -d/ -f2 |
sed 's@^dot-@.@;s@\(.*\)@~/\1@;s@'"$PATHSEP"'@/@g' | eval "$cmd"
elif (($# == 0)) || [[ $1 = help ]]; then
(
exec >&2
echo "${0##*/} help:"
echo " ln link dotfiles."
echo " ls list dotfiles."
echo " ps [chr] use chr as seperator."
echo " cp [path] track path."
echo " systemd update update systemd config."
echo " systemd sync sync systemd config."
echo " rm [dot] unlink and delete dotfile."
echo " help show this help."
)
elif (($# > 0)); then
echo "Invalid subcommand $1"
exit 1
else
echo "This repo should reside under '$HOME'"
exit 1
fi