-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.bash_functions
More file actions
434 lines (329 loc) · 12.2 KB
/
.bash_functions
File metadata and controls
434 lines (329 loc) · 12.2 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#HACK: Ideally this should be an export in '.profile', but it's not working properly on Debian 11.
OS_NAME="$(uname -s)"
GITHUB_REPO="Matriks404/dotfiles"
GITHUB_BASE_URL="https://github.com/${GITHUB_REPO}"
check-dotfiles-update() {
local url="https://raw.githubusercontent.com/${GITHUB_REPO}/refs/heads/master/.dotfiles_version"
local current_version
local latest_version
current_version="$(dotver | cut -d ' ' -f 1)"
latest_version="$(curl -s "${url}" | cut -d ' ' -f 1)"
if [[ "$current_version" == "$latest_version" ]] || [[ "$current_version" > "$latest_version" ]]; then
echo -e "You have the latest version of dotfiles! (${current_version})"
return 1
else
echo -e "There is a dotfiles update available! (${current_version} --> ${latest_version})"
return 0
fi
}
edit-repos() {
if [[ -f "/etc/debian_version" ]]; then
if [[ "$1" == "-f" || "$1" == "-fl" || "$1" == "-fs" || "$1" == "--file" ]]; then
if [[ -z "$2" ]]; then
echo -e "Error: You need to provide a filename!"
return 1
fi
local file
if [[ "$1" == "-fl" ]]; then
file="/etc/apt/sources.list.d/${2}.list"
elif [[ "$1" == "-fs" ]]; then
file="/etc/apt/sources.list.d/${2}.sources"
else
file="/etc/apt/sources.list.d/${2}"
fi
if [[ ! -f "$file" ]]; then
echo -e "Error: File '${file}' doesn't exist!"
return 1
fi
"$SU_CMD" "$EDITOR" "$file"
return $?
fi
# Logic for interactive file selection
local files=()
[[ -f "/etc/apt/sources.list" ]] && files+=("/etc/apt/sources.list")
local sources_list_dir="/etc/apt/sources.list.d"
local file
if [[ -d "$sources_list_dir" ]]; then
# Using a loop to avoid issues if no files match glob
for file in "${sources_list_dir}/"*.{list,sources}; do
[[ -f "$file" ]] && files+=("$file")
done
fi
local num_files="${#files[@]}"
if [[ "$num_files" -eq 0 ]]; then
echo -e "Error: You don't have valid sources.list available!"
return 1
elif [[ "$num_files" -eq 1 ]]; then
"$SU_CMD" "$EDITOR" "${files[0]}"
return 0
fi
local sorted_files
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
local i=1
echo -e "Files available:"
for file in "${sorted_files[@]}"; do
echo -e " $i) $file"
((i++))
done
local selection
read -rp "Select file you want to edit (1-${num_files}): " selection
if ! [[ "$selection" =~ ^[0-9]+$ ]] || (( selection < 1 || selection > num_files )); then
echo -e "\nError: Invalid selection!"
return 1
fi
local selected_file="${sorted_files[selection - 1]}"
"$SU_CMD" "$EDITOR" "$selected_file"
elif [[ "$OS_NAME" == "OpenBSD" ]]; then
"$SU_CMD" "$EDITOR" "/etc/installurl"
return $?
else
echo -e "Error: Unsupported operating system!"
return 1
fi
}
get-manual() {
local parent_pid
parent_pid="$(ps -p $$ -o ppid= | xargs)"
local current_pid="$parent_pid"
local is_unicode_term="true"
local cmd
local term
while [[ -n "$current_pid" && "$current_pid" -ne "1" ]]; do
cmd="$(ps -p "$current_pid" -o command= | xargs)"
term="${cmd%% *}"
case "$term" in
"xterm"|"/usr/X11R6/bin/xterm"|"/usr/bin/X11/xterm"|"/usr/bin/xterm")
if [[ "$cmd" == "$term -class UXTerm"* ]]; then
is_unicode_term="true"
else
is_unicode_term="false"
fi
break
;;
esac
current_pid="$(ps -p "$current_pid" -o ppid= | xargs)"
done
if [[ -z "$1" ]]; then
man
elif [[ "$is_unicode_term" == "false" ]]; then
echo -e "Running external UXTerm (non-UTF8 terminal detected)..."
(xterm -class "UXTerm" -e "sh -c \"man $* && echo Press RETURN to continue. && read -r _\"" &)
else
man "$@"
fi
}
get-new-dotfiles() {
if [[ -d "./.git" ]]; then
echo -e "ERROR: You are in a git repository directory! Quitting..."
return 1
fi
local force_update="false"
[[ "$1" == "-f" || "$1" == "--force" ]] && force_update="true"
if [[ "$force_update" == "true" ]] || check-dotfiles-update; then
echo -e "\nGetting new dotfiles..."
local repo_name="Matriks404/dotfiles"
curl --silent "https://raw.githubusercontent.com/${repo_name}/refs/heads/master/build/update.sh" | sh
else
echo -e "\nNo update needed. Use -f to force."
fi
}
get-repos() {
if [[ -f "/etc/debian_version" ]]; then
local files=()
local sources_list_dir="/etc/apt/sources.list.d"
local file
[[ -f /etc/apt/sources.list ]] && files+=("/etc/apt/sources.list")
if [[ -d "$sources_list_dir" ]]; then
for file in "${sources_list_dir}"/*.{list,sources}; do
[[ -f "$file" ]] && files+=("$file")
done
fi
if [[ "${#files[@]}" -eq 0 ]]; then
echo -e "Error: No sources found."
return 1
fi
local sorted_files
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
local temp_file
temp_file=$(mktemp)
for file in "${sorted_files[@]}"; do
{
echo -e "=== File: $file ==="
cat "$file"
echo -e ""
} >> "$temp_file"
done
less "$temp_file"
rm "$temp_file"
elif [[ "$OS_NAME" == "OpenBSD" ]]; then
cat /etc/installurl
fi
}
show-binaries() {
if [[ -z "$1" ]]; then
echo -e "Error: Provide a package name!"
return 1
fi
local cmdline_local=""
if [[ -f /etc/debian_version ]]; then
cmdline_local="dpkg -L"
elif [[ "$OS_NAME" == "OpenBSD" ]]; then
cmdline_local="pkg_info -L"
else
echo -e "Error: Unsupported OS."
return 1
fi
local package_exists="false"
local output=""
if $cmdline_local "$1" &>/dev/null; then
echo -e "Info: Found local package '$1'."
package_exists="true"
output="$($cmdline_local "$1" | grep -E '/(s?bin|games|libexec)/' | sort)"
elif [[ -f /etc/debian_version ]] && apt-cache show "$1" &>/dev/null; then
echo -e "Info: Found remote package '$1'."
package_exists="true"
if ! command -v apt-file &>/dev/null; then
echo -e "Error: 'apt-file' not found."
return 1
fi
output="$(apt-file list "$1" | grep -E '/(s?bin|games|libexec)/' | sort)"
fi
if [[ "$package_exists" == "false" ]]; then
echo -e "Error: Package '$1' not found!"
return 1
fi
if [[ -n "$output" ]]; then
echo -e "\nBinary files:\n$output"
else
echo -e "Info: No binary files found for package '$1'."
fi
}
toggle-disabled() {
local file_path="$1"
[[ -z "$file_path" ]] && { echo -e "Error: No path."; return 1; }
[[ ! -e "$file_path" ]] && { echo -e "Error: Not found."; return 1; }
local new_file_path
if [[ "$file_path" == *".disabled" ]]; then
new_file_path="${file_path%.disabled}"
else
new_file_path="${file_path}.disabled"
fi
if mv "$file_path" "$new_file_path"; then
echo -e "Successfully renamed to '$new_file_path'."
else
echo -e "Error: Rename failed."
return 1
fi
}
if command -v git &>/dev/null; then
clone-dotfiles-repository() {
local repos_dir="$HOME/repos"
local dotfiles_repo_dir="$repos_dir/dotfiles"
[[ -d "$dotfiles_repo_dir" ]] && return 1
mkdir -p "$repos_dir"
git clone "${GITHUB_BASE_URL}.git" "$dotfiles_repo_dir"
# Using subshell to avoid directory bleed
(
cd "$dotfiles_repo_dir" || exit 1
tools/initial_repository_setup.sh
)
}
copy-dotfiles-to-repos-directory () {
local short_name
if [[ "$OS_NAME" == "Linux" ]]; then
short_name='linux'
elif [[ "$OS_NAME" == "OpenBSD" ]]; then
short_name='openbsd'
fi
local repos_dir="$HOME/repos"
local dotfiles_repo_dir="$HOME/repos/dotfiles"
# Clone dotfiles repository if it doesn't exist.
if [[ ! -d "$dotfiles_repo_dir" ]]; then
echo -e "=== Cloning the dotfiles repository, because it doesn't exist yet... ==="
clone-dotfiles-repository
fi
echo -e "\n=== Updating dotfiles repository to the latest commit... ==="
echo -e "Info: This script won't copy dotfiles, unless dotfiles git repository is updated."
echo -e "Are you OK with pulling the latest git commit? If so, enter \"Yes.\" (without quotes):"
echo -en "? "
local answer
read -r answer
echo
if [[ ! "$answer" == "Yes." ]]; then
echo -e "Quitting..."
return 1
fi
if ! update-dotfiles-repository; then
return 1
fi
echo -e "\n=== Copying dotfiles lists... ==="
local txtfiles_to_copy="$HOME/.dotfiles_lists/*"
local txtfiles_repo_dir="$dotfiles_repo_dir/.dotfiles_lists/"
rsync -civ $txtfiles_to_copy "$txtfiles_repo_dir"
echo -e "\n=== Copying dotfiles... ==="
local dotfiles_to_copy
dotfiles_to_copy="$(cat "$HOME/.dotfiles_lists/common.txt")"
if [[ -f "$HOME/.dotfiles_lists/${short_name}.txt" ]]; then
dotfiles_to_copy="$dotfiles_to_copy $(cat "$HOME/.dotfiles_lists/${short_name}.txt")"
fi
if [[ -f /etc/debian_version ]]; then
dotfiles_to_copy="$dotfiles_to_copy $(cat "$HOME/.dotfiles_lists/debian.txt")"
fi
if [[ "$USER" == "marcin" ]]; then
local full_username
full_username=$(getent passwd "marcin" | cut -d ':' -f 5)
if [[ "$full_username" =~ ^Marcin\ Kralka ]]; then
dotfiles_to_copy="$dotfiles_to_copy $(cat "$HOME/.dotfiles_lists/private.txt")"
if [[ -f /etc/debian_version ]]; then
dotfiles_to_copy="$dotfiles_to_copy $(cat "$HOME/.dotfiles_lists/private_debian.txt")"
fi
fi
fi
# Logic check: $dotfiles_to_copy must remain unquoted here to allow rsync to see multiple files
rsync -ciRv $dotfiles_to_copy "$dotfiles_repo_dir"
echo -e "\n=== Copying OS-specific dotfiles... ==="
local os_specific_dotfiles_list
os_specific_dotfiles_list="$(cat "$HOME/.dotfiles_lists/os_specific.txt")"
local os_specific_repo_dir="$dotfiles_repo_dir/os_specific"
mkdir -p "$os_specific_repo_dir"
local entry
local filename
local final_name
for entry in $os_specific_dotfiles_list; do
filename="${entry}.1"
if [[ -f "$filename" ]]; then
final_name="${entry}.${short_name}"
rsync -ciRv "$filename" "${os_specific_repo_dir}/${final_name}"
fi
done
}
update-dotfiles-repository () {
local dotfiles_repo_dir="$HOME/repos/dotfiles"
local exit_status
# Clone repository if it doesn't exist.
if [[ ! -d "$dotfiles_repo_dir" ]]; then
clone-dotfiles-repository
fi
cd "$dotfiles_repo_dir"
git pull
exit_status=$?
cd "$OLDPWD"
if [[ $exit_status -ne 0 ]]; then
echo -e "Error: Couldn't pull the latest commit in the dotfiles git repository!"
fi
return $exit_status
}
fi
# Debian-specific shorthand
if [[ "$OS_NAME" == "Linux" && -f /etc/debian_version ]]; then
edit-debian-sources() {
if [[ -f /etc/apt/sources.list ]]; then
edit-repos
elif [[ -f /etc/apt/sources.list.d/debian.sources ]]; then
edit-repos --file debian.sources
else
echo -e "Error: No sources found!"
return 1
fi
}
fi