-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager
executable file
·247 lines (213 loc) · 6.7 KB
/
manager
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
#!/bin/sh
export CSI="$(printf '\033')["
echo() { printf "%s\n" "$*"; }
exists() { command -v "$1" >/dev/null 2>&1; }
unset IS_TTY
[ $FORCE_TTY ] || [ -t 1 ] && {
export IS_TTY=1
export BOLD="${CSI}1m"
export RESET="${CSI}m"
export RED=31 GREEN=32 YELLOW=33
for COLOR in RED GREEN YELLOW; do
export FG_$COLOR="${CSI}$(( $COLOR ))m"
export FG_${COLOR}_B="${CSI}$(( $COLOR + 60 ))m" # bright
done
}
[ $# -eq 0 ] || echo "$1" | grep -Eq '^-?-?h' && {
NAME="$(basename "$0")"
echo "${NAME} ${BOLD}save${RESET} -- Save current settings to repo"
echo "${NAME} ${BOLD}load${RESET} -- Load settings from repo"
echo "${NAME} ${BOLD}help${RESET} -- Print this help"
[ $# -gt 0 ]; exit $? # Clever, isn't it?
}
cd "$(dirname "$0")"
touch stdout stderr; chmod 0600 stdout stderr
trap 'rm -f stdout stderr' EXIT
trap exit INT HUP TERM
save() {
OS=common
[ $# = 2 ] && {
OS=os/"$1"
shift
}
DESTDIR="$(dirname "$1")"
if [ "${HOME}${DESTDIR#$HOME}" = "$DESTDIR" ]
then DESTDIR="home${DESTDIR#$HOME}"
else DESTDIR="root${DESTDIR}"
fi
DESTDIR="$OS/$DESTDIR"
[ ! -d "$DESTDIR" ] &&
mkdir -p "$DESTDIR"
if [ -d "$1" ]
then cp -R "$1" "$DESTDIR"
else cp "$1" "$DESTDIR"
fi
}
save_glirc_config() {
grep -Fq 'sasl-' "$HOME/.config/glirc/config" && {
echo "This is the old config format. Not supported."
echo "Please upgrade to latest glirc."
return 1
}
cp "$HOME/.config/glirc/config" "$HOME/.config/glirc/config.bak"
EC=$?; [ $EC -eq 0 ] || return $EC
sed >"$HOME/.config/glirc/config" \
<"$HOME/.config/glirc/config.bak" \
-e '/sasl:/{n;s/name:.*/name: "..."/;n;s/word:.*/word: "..."/;n}' \
-e '/port:/s/port:.*/port: 0/' \
-e '/password:/s/word:.*/word: "..."/'
EC=$?; [ $EC -eq 0 ] || return $EC
save "$HOME/.config/glirc/config"
EC=$?; [ $EC -eq 0 ] || return $EC
mv "$HOME/.config/glirc/config.bak" "$HOME/.config/glirc/config"
EC=$?; [ $EC -eq 0 ] || return $EC
}
save_pkglist() {
cd pkglist
# Void linux
exists xbps-query && {
set -- "$@" ^[^/] void
set -- "$@" ^/ void_custom
unset regex
for pkg in $(xbps-query -m); do
regex="${regex}|${pkg%-*}"
done; unset pkg
regex="${regex#|}"
regex="^($regex)$"
while [ $# -gt 0 ]; do
rm -f $2
xbps-query --regex -p repository -s "$1" \
| sed 's/: .*$//; s/-[^-]*$//' \
| grep -E "$regex" \
> $2
shift 2
done
unset regex
}
cd ..
}
_save() {
run rm -rf common/home
run save ~/.curlrc
run save ~/.gitconfig
run save ~/.profile
run save ~/.shrc
run save ~/.tmux.conf
run save ~/.vimrc
run save ~/.zsh
run save ~/.zshrc
run save ~/.ssh/config
run save ~/.config/alacritty
run save ~/.config/bat
run save ~/.config/fontconfig
run save ~/.config/foot
run save ~/.config/htop
run save ~/.config/hypr
run save ~/.config/lf
run save ~/.config/sway
run save ~/.config/swaylock
run save ~/.config/waybar
run save ~/.config/zathura
run save ~/.config/kanshi
run save ~/.config/nvim/init.lua
run save_glirc_config
run save ~/.vty
run rm -rf common/root
run save /etc/doas.conf
run save /etc/sudoers.d/allow_wheel
run save_pkglist
## OS/Distro-specific settings
# Linux
[ $(uname) = Linux ] && {
rm -rf os/linux
run save linux ~/.local/bin/light
run save linux /etc/tlp.d/BAT0.conf
run save linux /etc/elogind/logind.conf.d/custom.conf
run save linux /etc/modules-load.d # load modules automatically instead of having to `modprobe`
run save linux /etc/udev/rules.d
}
# Void Linux
[ -f /lib/dracut/dracut.conf.d/00-void.conf ] && {
rm -rf os/voidlinux
run save voidlinux /etc/rc.local
run save voidlinux /etc/acpi/handler.sh
}
}
_load() {
# TODO: Copy the files from this directory to the respective directories.
# Also, check before copying if the file exists already. If yes, show the diff
# to the user and ask if that file should be replaced/backed-up-and-replaced/kept.
# TODO: Since we strip out the sasl username and password in _save(), we should ensure that
# they are set to the correct values here (while loading back to $HOME), possibly by
# prompting the user to enter the sasl username and password here, or by printing a
# warning message.
echo >&2 Not implemented yet
return 1
groupadd -r backlight
usermod -aG backlight subnut
}
_test() {
DELAY=1
success() { true; }
failure() { false; }
success_delayed() { sleep $DELAY; true; }
failure_delayed() { sleep $DELAY; false; }
success_with_stdout() { echo It worked!; }
failure_with_stdout() { echo It failed!; return 1; }
failure_with_stderr() { echo error output goes here. >&2; return 1; }
failure_with_stdout_stderr() {
echo stdout message informing the user
echo >&2 error output showing more details
echo >&2 to help the developer identify the bug
return 1
}
run success
run success_delayed
run success_with_stdout
run failure
run failure_delayed
run failure_with_stderr
run failure_with_stdout
run failure_with_stdout_stderr
}
if [ "$1" = "dry_${1#dry_}" ]; then
run() { echo "$*"; }
elif [ ! $IS_TTY ]; then
run() { echo "$*"; "$@"; }
else
run() {
# CMD="$(echo "$*" | sed "s;$HOME;\$HOME;g")" # Show $HOME as "$HOME"
CMD="$(echo "$*" | sed "s;$HOME;~;g")" # Show $HOME as "~"
echo "[${FG_YELLOW}${BOLD}WAIT${RESET}] $CMD"
"$@" 1>stdout 2>stderr
if [ $? -eq 0 ]
then printf "${CSI}1A${CSI}1G[${FG_GREEN}%s${RESET}]\n" ' OK '
else printf "${CSI}1A${CSI}1G[${BOLD}${FG_RED_B}%s${RESET}]\n" 'FAIL'
if [ ! -s stdout ]
then cat stderr
else
print_output stdout
print_output stderr
fi
fi
}
print_output() {
FILE=$1; [ -s $FILE ] || return # empty
exec 4<$FILE
FILE=${FILE#std}
while read -r LINE <&4; do
printf "${BOLD}%s: ${RESET}" $FILE
echo "$LINE"
done
exec 4<&-
}
fi
case "${1#dry_}" in
(save) _save;;
(load) _load;;
(test) _test;;
(*) echo "Invalid command -- $1"
echo "Run ${BOLD:-'}$(basename "$0") help${RESET:-'} for help"
exit 1;;
esac
# vim: et ts=4 sts=4 sw=4 ft=sh