-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_common.sh
More file actions
332 lines (291 loc) · 8.59 KB
/
_common.sh
File metadata and controls
332 lines (291 loc) · 8.59 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
326
327
328
329
330
331
332
#!/usr/bin/env bash
# Error handler - shows what failed and where
trap_err() {
local exit_code=$?
local line_no=${BASH_LINENO[0]}
local cmd="${BASH_COMMAND}"
local script="${BASH_SOURCE[1]:-$0}"
echo_red "ERROR: Command failed with exit code $exit_code"
echo_red " Script: $script"
echo_red " Line: $line_no"
echo_red " Command: $cmd"
exit $exit_code
}
trap 'trap_err' ERR
CACHE_DIR=$HOME/.cache
SRC_CONFIG_DIR=$(pwd)/config
DEST_CONFIG_DIR=$HOME/.config
SRC_SERVICE_DIR=$(pwd)/service
DEST_SERVICE_DIR=$DEST_CONFIG_DIR/systemd/user
RUN_MODE=""
echo_gray() { echo -e "\033[90m$1\033[0m"; }
echo_amber() { echo -e "\033[33m$1\033[0m"; }
echo_red() { echo -e "\033[31m$1\033[0m"; }
echo_green() { echo -e "\033[32m$1\033[0m"; }
echo_blue() { echo -e "\033[34m$1\033[0m"; }
echo_cyan() { echo -e "\033[36m$1\033[0m"; }
echo_magenta() { echo -e "\033[35m$1\033[0m"; }
link_bin_dir() { ln -sfn "$(pwd)/bin" "$HOME/bin"; }
link_home() { ln -sfn "$SRC_CONFIG_DIR/$1" "$HOME/${2:-$1}"; }
link_config() { ln -sfn "$SRC_CONFIG_DIR/$1" "$DEST_CONFIG_DIR/${2:-$1}"; }
cp_config() { cp -rf "$SRC_CONFIG_DIR/$1" "$DEST_CONFIG_DIR/${2:-$1}"; }
link_alacritty() {
local filepath
filepath="$SRC_CONFIG_DIR/alacritty/$OS_ALIAS.toml"
[[ ! -f "$filepath" ]] && filepath="$SRC_CONFIG_DIR/alacritty/linux.toml"
[[ ! -f "$filepath" ]] && filepath="$SRC_CONFIG_DIR/alacritty/archlinux.toml"
ln -sfn "$filepath" "$SRC_CONFIG_DIR/alacritty/alacritty.toml"
link_config "alacritty"
}
link_ghostty() {
local filepath
filepath="$SRC_CONFIG_DIR/ghostty/$OS_ALIAS"
[[ ! -f "$filepath" ]] && filepath="$SRC_CONFIG_DIR/ghostty/linux"
[[ ! -f "$filepath" ]] && filepath="$SRC_CONFIG_DIR/ghostty/archlinux"
ln -sfn "$filepath" "$SRC_CONFIG_DIR/ghostty/config"
link_config "ghostty"
}
enable_service() { sudo systemctl enable --now "$1"; }
setup_user_service() {
local svc
svc="$1"
mkdir -p "$DEST_SERVICE_DIR"
ln -sfn "$SRC_SERVICE_DIR/$svc" "$DEST_SERVICE_DIR/$svc"
systemctl --user daemon-reload
systemctl --user enable --now "$svc"
}
setup_timezone() {
local tz
tz="${TIMEZONE:-Europe/Berlin}"
echo_gray "setting timezone to $tz..."
sudo ln -sf /usr/share/zoneinfo/"$tz" /etc/localtime
sudo hwclock --systohc
}
create_cache_dir() { mkdir -p "$CACHE_DIR"; }
create_config_dir() { mkdir -p "$DEST_CONFIG_DIR"; }
setup_terraform_plugin_cache() {
mkdir -p "$CACHE_DIR/terraform-plugins"
link_home "terraformrc" ".terraformrc"
}
read_package_file_to_array() {
local filename=$1
local -n _arr=$2 # nameref to caller's array
local filepath
filepath="$(pwd)/pkg/$filename.txt"
[[ ! -f "$filepath" ]] && return
while IFS= read -r line || [[ -n "$line" ]]; do
# Trim leading/trailing whitespace
item=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# Skip empty lines or lines starting with '#' or ';'
if [[ -z "$item" || "$item" == \#* || "$item" == \;* ]]; then
continue
fi
_arr+=("$item")
done <"$filepath"
}
install_or_update_rust() {
! command -v rustup &>/dev/null && return
echo_gray "updating rust toolchain..."
rustup default stable
rustup update
}
install_or_update_aws_cli_linux() (
echo_gray "installing/updating aws cli..."
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
cd "$dir" || exit 1
local url filename
filename="awscli-exe-linux-x86_64.zip"
url="https://awscli.amazonaws.com/$filename"
curl -sS "$url" -o "$filename"
unzip -q "$filename"
sudo ./aws/install \
--bin-dir /usr/local/bin \
--install-dir /usr/local/aws-cli \
--update
)
install_or_update_aws_cli_mac() (
echo_gray "installing/updating aws cli..."
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
cd "$dir" || exit 1
local url filename
filename="AWSCLIV2.pkg"
url="https://awscli.amazonaws.com/$filename"
curl -sS "$url" -o "$filename"
sudo installer -pkg "$filename" -target /
)
install_or_update_aws_sam_cli_linux() (
echo_gray "installing/updating aws sam cli..."
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
cd "$dir" || exit 1
local url filename dir_name
dir_name="sam-installation"
filename="aws-sam-cli-linux-x86_64.zip"
url="https://github.com/aws/aws-sam-cli/releases/latest/download/$filename"
curl -sSL "$url" -o "$filename"
unzip -q "$filename" -d "$dir_name"
sudo ./$dir_name/install --update
)
install_or_update_aws_sam_cli_mac() (
echo_gray "installing/updating aws sam cli..."
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
cd "$dir" || exit 1
local arch url filename dir_name
arch=$(uname -m)
dir_name="sam-installation"
filename="aws-sam-cli-macos-arm64.zip"
[[ "$arch" != "arm64" ]] && filename="aws-sam-cli-macos-x86_64.zip"
url="https://github.com/aws/aws-sam-cli/releases/latest/download/$filename"
curl -sSL "$url" -o "$filename"
unzip -q "$filename" -d "$dir_name"
sudo ./$dir_name/install --update
)
install_gtk_theme() (
if [[ -d ~/.themes ]]; then
for d in ~/.themes/*; do
[[ ${d##*/} =~ [Cc]atppuccin ]] && return
done
fi
local name="Catppuccin-GTK-Theme"
echo_gray "installing $name..."
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
cd "$dir" || exit 1
git clone --depth=1 https://github.com/Fausto-Korpsvart/$name.git
cd "$name/themes" || exit 1
./install.sh -c dark -l -t all
)
# WARN: only linux
install_devpod() (
echo_gray "installing/updating devpod..."
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
cd "$dir" || exit 1
local url
url="https://github.com/loft-sh/devpod/releases/latest/download/devpod-linux-amd64"
curl -sSL "$url" -o devpod &&
sudo install -c -m 0755 devpod /usr/local/bin
)
setup_libvirt() {
! command -v libvirtd &>/dev/null && return
echo_gray "configuring libvirt/qemu-kvm..."
# Add user to libvirt group for system-mode access
sudo usermod -aG libvirt "$USER"
# Enable and start libvirtd service
echo_gray "enabling libvirtd service..."
sudo systemctl enable --now libvirtd.service
# Set default network to autostart
echo_gray "enabling default virtual network..."
sudo virsh net-autostart default 2>/dev/null || true
sudo virsh net-start default 2>/dev/null || true
# Set ACL on images directory for user access
local images_dir="/var/lib/libvirt/images"
if [[ -d "$images_dir" ]]; then
echo_gray "setting ACL on $images_dir..."
sudo setfacl -R -m "u:$USER:rwX" "$images_dir"
sudo setfacl -m "d:u:$USER:rwx" "$images_dir"
fi
# virsh -c qemu:///system list --all
# virt-manager
}
setup_zsh_as_default() {
if [[ "$(realpath "$SHELL")" != "$(command -v zsh)" ]]; then
echo_amber "changing default shell to zsh..."
sudo chsh -s "$(command -v zsh)" "$USER"
fi
}
add_user_to_docker_group() {
# Add user to docker group for running docker without sudo
sudo usermod -aG docker "$USER"
}
build_bat_cache() {
# Rebuild bat cache for syntax highlighting
bat cache --build &>/dev/null
}
fix_btop_graphics() {
# Fix btop Intel graphics issue (set performance monitoring capability)
sudo setcap cap_perfmon=+ep "$(command -v btop)" 2>/dev/null || true
}
setup_tmux() {
link_config "tmux"
local tmux_tpm="$DEST_CONFIG_DIR/tmux/plugins/tpm"
[[ -d "$tmux_tpm" ]] || {
echo_gray "installing tmux plugin manager..."
git clone https://github.com/tmux-plugins/tpm "$tmux_tpm" &&
"$tmux_tpm"/bin/install_plugins
}
}
pre_config_generic() {
create_cache_dir
create_config_dir
setup_terraform_plugin_cache
link_home "gitconfig" ".gitconfig"
}
post_config_generic() {
link_bin_dir
link_home "vim" ".vim"
link_home "vimrc" ".vimrc"
link_config "nvim"
link_config "eza"
link_config "lazydocker"
link_config "lazygit"
link_config "starship"
link_config "kitty"
link_alacritty
link_ghostty
link_config "bat"
build_bat_cache
setup_tmux
}
post_config_linux() {
post_config_generic
setup_zsh_as_default
add_user_to_docker_group
fix_btop_graphics
link_config "btop"
link_config "k9s"
# link_config "gtk-3.0"
# link_config "gtk-4.0"
# link_config "qt5ct"
# link_config "qt6ct"
enable_service "docker"
setup_user_service "wl-paste.service"
}
usage() {
echo "Usage: $0 [update|config|full]"
echo " update - Update packages only"
echo " config - Apply configuration only"
echo " full - Full install (default)"
}
set_run_mode() {
case "${1:-}" in
update)
RUN_MODE="update"
;;
config)
RUN_MODE="config"
;;
full)
RUN_MODE="full"
;;
-h | --help)
usage
exit 0
;;
"")
usage
exit 1
;;
*)
echo_red "Unknown option: $1"
usage
exit 1
;;
esac
}
should_update() { [[ "$RUN_MODE" == "update" ]]; }
should_config() { [[ "$RUN_MODE" == "config" ]]; }
should_full() { [[ "$RUN_MODE" == "full" ]]; }