-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·305 lines (260 loc) · 5.95 KB
/
setup.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
298
299
300
301
302
303
304
305
#!/usr/bin/env zsh
set -e
DISTRO="$(sed -n 's/^NAME="\([^"]\+\)"/\1/p' /etc/os-release)"
SCRIPT_FILE="$(readlink -f -- "$0")"
SCRIPT_NAME="$(basename -- "$SCRIPT_FILE")"
SCRIPT_DIR="$(dirname -- "$SCRIPT_FILE")"
SOURCE_DIR="$SCRIPT_DIR"
DEST_DIR="$HOME"
PRINT_HELP=false
FORCE=false
REMOVE_EXISTING=false
IGNORE_EXISTING=false
ERROR=false
# Define packages
typeset -a PKGS
PKGS=(
"alacritty"
"dunst"
"fish"
"i3"
"i3lock"
"i3status"
"jq"
"lf"
"picom"
"rofi"
"startx"
"tmux"
"zsh"
)
# Define paths to symlink
typeset -a SYMLINKS
SYMLINKS=(
".config/alacritty"
".config/dolphinrc"
".config/dunst"
".config/fish"
".config/foot"
".config/frogminer"
".config/ghostty"
".config/i3"
".config/i3status"
".config/lf"
".config/kde-mimeapps.list"
".config/kglobalshortcutsrc"
".config/klipperrc"
".config/picom"
".config/rofi"
".config/tmux"
".config/wezterm"
".config/yay"
".local/bin"
".local/share/fonts"
".local/share/konsole"
".vimrc"
".xinit-scripts"
".xinitrc"
".Xresources"
)
typeset -a COPIES
COPIES=(
".config/gtk-3.0"
".config/gtk-4.0"
".gtkrc-2.0"
)
typeset -A SYMLINK_MAP
SYMLINK_MAP[zsh/rc]=".zshrc"
error() {
msg="$1"
ERROR=true
echo "Error: $msg" >&2
}
check_packages_installed() {
for pkg in "${PKGS[@]}"; do
if ! type "$pkg" >/dev/null; then
error "missing $pkg"
fi
done
}
remove_symlink() {
local link src
link="${DEST_DIR}/$1"
if test -L "$link"; then
src="$(readlink -f -- "$link")"
echo "Removing symlink: $link -> $src"
rm "$link"
elif test -e "$link"; then
error "object to be removed is not a symlink:"
error "${link}: $(stat -c '%F' -- "$link")"
return 1
fi
}
remove_all_symlinks() {
for link in "${SYMLINKS[@]}"; do
remove_symlink "$link"
done
for src dst in ${(kv)SYMLINK_MAP}; do
remove_symlink "$dst"
done
}
create_symlink() {
local rel_src rel_dst src dst dst_parent
rel_src="$1"
rel_dst="$2"
if test -z "$rel_src"; then
error "missing src argument:"
error "$0 $@"
return 1
fi
if test -z "$rel_dst"; then
error "missing dst argument:"
error "$0 $@"
return 1
fi
src="${SCRIPT_DIR}/$rel_src"
dst="${DEST_DIR}/$rel_dst"
dst_parent="$(dirname -- "$dst")"
if ! test -e "$src"; then
error "the following source path does not exist:"
error "$src"
return 1
fi
if ! test -d "$dst_parent"; then
echo "Creating parent: $dst_parent"
mkdir -p "$dst_parent"
fi
if test -L "$dst"; then
if $FORCE; then
if test "$(readlink -f "$dst")" != "$src"; then
remove_symlink "$2"
else
return 0
fi
elif $IGNORE_EXISTING; then
return 0
else
error "symbolic link already exists:"
error "$dst"
return 1
fi
elif test -e "$dst"; then
error "path already exists that is not a symlink:"
error "${dst}: $(stat -c '%F' -- "$dst")"
return 1
fi
echo "Creating link: $dst -> $rel_src"
ln -s "$src" "$dst"
}
remove_path() {
local target
target="$1"
echo "Trashing item: $target"
gio trash "$target"
}
copy_item() {
local rel_src rel_dst src dst dst_parent
rel_src="$1"
rel_dst="$2"
if test -z "$rel_src"; then
error "missing src argument:"
error "$0 $@"
return 1
fi
if test -z "$rel_dst"; then
error "missing dst argument:"
error "$0 $@"
return 1
fi
src="${SCRIPT_DIR}/$rel_src"
dst="${DEST_DIR}/$rel_dst"
dst_parent="$(dirname -- "$dst")"
if ! test -e "$src"; then
error "the following source path does not exist:"
error "$src"
return 1
fi
if ! test -d "$dst_parent"; then
echo "Creating parent: $dst_parent"
mkdir -p "$dst_parent"
fi
if test -e "$dst"; then
if $FORCE; then
remove_path "$dst"
elif $IGNORE_EXISTING; then
return 0
else
error "path already exists:"
error "${dst}"
return 1
fi
fi
echo "Copying item: from $rel_src to ${dst_parent}/"
cp -r "$src" "${dst_parent}/"
}
create_all_symlinks() {
for link in "${SYMLINKS[@]}"; do
create_symlink "$link" "$link"
done
for src dst in ${(kv)SYMLINK_MAP}; do
create_symlink "$src" "$dst"
done
}
copy_all_items() {
for item in "${COPIES[@]}"; do
copy_item "$item" "$item"
done
}
check_terminfo() {
if ! infocmp tmux-256color > /dev/null; then
error "Missing terminfo for tmux-256color. Try installing ncurses-term."
return 1
fi
}
print_help() {
echo "Usage: $SCRIPT_NAME [...]"
echo ""
echo "Options:"
echo " -h, --help Print this help message"
echo " -f, --force Overwrite any existing links"
echo " -i, --ignore-existing Ignore existing symlinks"
echo " -r, --remove-existing Remove any existing symlinks"
}
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
PRINT_HELP=true
shift
;;
-f|--force)
FORCE=true
shift
;;
-r|--remove-existing)
REMOVE_EXISTING=true
shift
;;
-i|--ignore-existing)
IGNORE_EXISTING=true
shift
;;
*)
error "unknown option: $1"
PRINT_HELP=true
shift
;;
esac
done
if $PRINT_HELP; then
print_help
elif $REMOVE_EXISTING; then
remove_all_symlinks
else
check_terminfo
check_packages_installed
create_all_symlinks
copy_all_items
fi
if $ERROR; then
exit 1
fi