-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.bash_functions
More file actions
491 lines (361 loc) · 13.6 KB
/
.bash_functions
File metadata and controls
491 lines (361 loc) · 13.6 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#HACK: Ideally this should be an export in '.profile., but it's not working properly on Debian 11.
OS_NAME="$(uname -s)"
#os_target="$(cat ${HOME}/.dotfiles_os_target)"
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="$(dotver | cut -d ' ' -f 1)"
local 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=()
if [[ -f "/etc/apt/sources.list" ]]; then
files+=("/etc/apt/sources.list")
fi
local sources_list_dir="/etc/apt/sources.list.d"
if [[ -d "$sources_list_dir" ]]; then
for file in "$sources_list_dir/"*.{list,sources}; do
if [[ -f "$file" ]]; then
files+=("$file")
fi
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
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
local i=1
echo "Files available:"
for file in "${sorted_files[@]}"; do
echo " $i) $file"
((i++))
done
echo ""
read -rp "Select file you want to edit (1-${num_files}): " selection
if ! [[ "$selection" =~ ^[0-9]+$ ]] || [[ "$selection" -lt 1 ]] || [[ "$selection" -gt "$num_files" ]]; then
echo -e "\nError: Invalid selection! Next time enter a number between 1 and ${num_files}."
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 or Linux distribution!"
return 1
fi
}
get-manual ()
{
local parent_pid="$(ps -p $$ -o ppid= | xargs)"
local current_pid="$parent_pid"
local is_unicode_term="true"
while [[ -n "$current_pid" && "$current_pid" -ne "1" ]]; do
local cmd="$(ps -p $current_pid -o command= | xargs)"
local term="${cmd%% *}"
if [[ "$term" == "xterm" ]] || [[ "$term" == "/usr/X11R6/bin/xterm" ]] || [[ "$term" == "/usr/bin/X11/xterm" ]] || [[ "$term" == "/usr/bin/xterm" ]]; then
if [[ "$cmd" == "$term -class UXTerm" ]] || [[ "$cmd" == "$term -class UXTerm -u8" ]]; then
is_unicode_term="true"
break
else
is_unicode_term="false"
break
fi
fi
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, since current terminal dosen't support UTF-8..."
(xterm -class "UXTerm" -e "sh -c \"man $* && echo Press RETURN to continue. && read DUMMY\"" &)
else
man "$@"
fi
}
get-new-dotfiles ()
{
if [[ -d "./.git" ]]; then
echo -e "ERROR: You are in the git repository directory! Quitting..."
return 1
fi
local force_update="false"
if [[ "$1" == "-f" ]] || [[ "$1" == "--force" ]]; then
local force_update="true"
fi
if $force_update || check-dotfiles-update; then
echo -e "\nGetting new dotfiles..."
repo_name="Matriks404/dotfiles"
curl --silent "https://raw.githubusercontent.com/${repo_name}/refs/heads/master/build/update.sh" | sh
else
echo -e "\nThere is no need to update dotfiles."
echo -e "Use -f or --force option to force update them."
fi
}
get-repos ()
{
if [[ -f "/etc/debian_version" ]]; then
local files=()
sources_list_dir="/etc/apt/sources.list.d"
if [[ -f /etc/apt/sources.list ]]; then
files+=("/etc/apt/sources.list")
fi
if [[ -d $sources_list_dir ]]; then
for file in "${sources_list_dir}"/*.{list,sources}; do
if [[ -f "$file" ]]; then
files+=("$file")
fi
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
fi
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
local temp_file=$(mktemp)
for file in "${sorted_files[@]}"; do
echo -e "=== File: $file ===" >> $temp_file
cat "$file" >> $temp_file
echo "" >> $temp_file
done
less $temp_file
rm $temp_file
elif [[ "$OS_NAME" == "OpenBSD" ]]; then
cat /etc/installurl
else
echo -e "Error: Unsupported operating system or Linux distribution!"
fi
}
show-binaries ()
{
if [[ -z "$1" ]]; then
echo -e "Error: You need to 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 operating system or Linux distribution!"
return 1
fi
local output=""
local package_exists="false"
if [[ "$($cmdline_local $1 2> /dev/null)" ]]; then
# Show different message on OpenBSD, because it actually looks up remote package files, if they are not installed.
if [[ "$OS_NAME" == "OpenBSD" ]]; then
echo -e "Info: Found package '$1'."
else
echo -e "Info: Found local package '$1'."
fi
local package_exists=true
local 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'."
local package_exists=true
# Check if apt-file command exists.
if [[ ! "$(command -v apt-file 2> /dev/null)" ]]; then
echo -e "\nError: 'apt-file' not found."
echo -e "To list binaries for remote package you might want to run: 'sudo apt update && sudo apt install apt-file'"
echo -e "After installation, remember to run: 'sudo apt-file update' to update list of files for remote packages.\n"
return 1
fi
local output="$(apt-file list $1 | grep -E '/(s?bin|games|libexec)/' | sort)"
fi
if [[ "$package_exists" == "false" ]]; then
echo -e "Error: Package '$1' does not exist or could not be found!"
return 1
fi
if [[ -n "$output" ]]; then
echo -e "\nBinary files:"
echo "$output"
else
echo -e "Info: No binary files found for package '$1'."
fi
}
toggle-disabled ()
{
file_path=$1
if [[ -z "$file_path" ]]; then
echo -e "Error: No file path provided."
return 1
fi
if [[ ! -e "$file_path" ]]; then
echo -e "Error: File '$file_path' does not exist!"
return 1
fi
if [[ "$file_path" == *".disabled" ]]; then
# If it ends with .disabled, remove it.
local new_file_path="${file_path%.disabled}"
mv "$file_path" "$new_file_path"
if [[ $? -eq 0 ]]; then
echo "Successfully renamed to '$new_file_path'."
else
echo "Error: Failed to rename '$file_path' to '$new_file_path'."
return 1
fi
else
# If it doesn't end with .disabled, add it.
local new_file_path="${file_path}.disabled"
mv "$file_path" "$new_file_path"
if [[ $? -eq 0 ]]; then
echo "Successfully renamed to '$new_file_path'."
else
echo "Error: Failed to rename '$file_path' to '$new_file_path'."
return 1
fi
fi
}
# Functions available only if git is installed
if [[ "$(command -v git)" ]]; then
clone-dotfiles-repository ()
{
local repos_dir=$HOME/repos
local dotfiles_repo_dir=$HOME/repos/dotfiles
# Return prematurely if dotfiles repository already exists.
if [[ -d $dotfiles_repo_dir ]]; then
return 1
fi
mkdir -p $repos_dir
#git clone --branch $OS_TARGET $github_base_url.git $dotfiles_repo_dir
git clone $github_base_url.git $dotfiles_repo_dir
cd $dotfiles_repo_dir
tools/initial_repository_setup.sh
cd $OLDPWD
}
copy-dotfiles-to-repos-directory ()
{
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 "? "
read 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="$(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
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
rsync -ciRv $dotfiles_to_copy $dotfiles_repo_dir
echo -e "\n=== Copying OS-specific dotfiles... ==="
local 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
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
# 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
# Linux-specific functions
if [[ "$OS_NAME" == "Linux" ]]; then
# Debian-specific functions
if [[ -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 valid sources.list found!"
return 1
fi
}
fi
fi