forked from cfgnunes/nautilus-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
1630 lines (1392 loc) · 53.4 KB
/
install.sh
File metadata and controls
1630 lines (1392 loc) · 53.4 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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# =============================================================================
# Project: Enhanced File Manager Actions for Linux
# Author: Cristiano Fraga G. Nunes
# Repository: https://github.com/cfgnunes/fm-scripts
# License: MIT License
# Version: 30.13
# =============================================================================
set -u
#------------------------------------------------------------------------------
#region Constants
#------------------------------------------------------------------------------
APP_NAME="Enhanced File Manager Actions for Linux"
APP_VERSION="30.13"
# Used in:
# - Directory where scripts are installed located at:
# "$HOME/.local/share/$INSTALL_NAME_DIR".
# - Directory where shortcuts (application menu) are stored located at:
# "$HOME/.local/share/applications/$INSTALL_NAME_DIR".
INSTALL_NAME_DIR="scripts"
INSTALL_PATH=".local/share/$INSTALL_NAME_DIR"
INSTALL_APPS_SHORTCUTS_PATH=".local/share/applications/$INSTALL_NAME_DIR"
# Constants for logging.
if [[ -z "${TEMP_DIR:-}" ]]; then
TEMP_DIR=$(mktemp --directory)
export TEMP_DIR
fi
INSTALL_LOG_NAME="install.log"
INSTALL_LOG_TMP="$TEMP_DIR/$INSTALL_LOG_NAME"
# List of supported file managers.
COMPATIBLE_FILE_MANAGERS=(
"nautilus"
"dolphin"
"nemo"
"caja"
"thunar"
"pcmanfm-qt"
"pcmanfm"
"krusader"
)
# Ignored application menu shortcuts during install.
IGNORE_APPS_SHORTCUTS=(
! -iname "Code editor"
! -iname "Disk usage analyzer"
! -iname "Terminal"
! -iname "Extract here"
! -iname "Create hard link here"
! -iname "Create symbolic link here"
! -iname "Paste as hard link"
! -iname "Paste as symbolic link"
! -iname "Paste clipboard content"
)
# Directories to be ignored during install.
IGNORE_FIND_PATHS=(
! -path "*/.helpers*"
! -path "*/.git*"
! -path "*/.po*"
)
# Define the directory of this script. If '$BASH_SOURCE' is available,
# use its path. Otherwise, create a temporary directory for cases like remote
# execution via curl.
if [[ -n "${BASH_SOURCE[0]:-}" ]]; then
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
else
SCRIPT_DIR="."
fi
I18N_DIR="$SCRIPT_DIR/.po"
# Mark constants as read-only to prevent accidental modification.
readonly \
APP_NAME \
APP_VERSION \
COMPATIBLE_FILE_MANAGERS \
I18N_DIR \
IGNORE_FIND_PATHS \
INSTALL_APPS_SHORTCUTS_PATH \
INSTALL_LOG_NAME \
INSTALL_LOG_TMP \
INSTALL_NAME_DIR \
INSTALL_PATH \
SCRIPT_DIR
# Use current username if '$USER' is undefined.
USER=${USER:-$(id -un)}
#endregion
#------------------------------------------------------------------------------
#region Global variables
#------------------------------------------------------------------------------
FILE_MANAGER="" # Current file manager being processed.
I18N_FILE="" # Current translation file being used.
INSTALL_DIR="" # Target installation directory for scripts.
INSTALL_HOME="" # User's home directory where scripts will be installed.
INSTALL_OWNER="" # Owner of the installation directory.
INSTALL_GROUP="" # Group of the installation directory.
# Default main menu options.
OPT_INSTALL_BASIC_DEPS="true"
OPT_REMOVE_SCRIPTS="true"
OPT_INSTALL_ACCELS="true"
OPT_CLOSE_FILE_MANAGER="true"
OPT_INSTALL_APP_SHORTCUTS="false"
OPT_INSTALL_HOMEBREW="false"
OPT_CHOOSE_CATEGORIES="false"
# Default core options.
OPT_INTERACTIVE_INSTALL="true"
OPT_QUIET_INSTALL="false"
# Import helper script for interactive multi-selection menus.
#shellcheck source=.helpers/.multiselect-menu.sh
if [[ -f "$SCRIPT_DIR/.helpers/.multiselect-menu.sh" ]]; then
source "$SCRIPT_DIR/.helpers/.multiselect-menu.sh"
fi
#endregion
#------------------------------------------------------------------------------
#region Main flow
#------------------------------------------------------------------------------
_on_exit() {
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
_log "[ERR] Installation terminated with exit code $exit_code."
fi
}
trap _on_exit EXIT
# shellcheck disable=SC2034
_main() {
local cat_defaults=()
local cat_dirs=()
local cat_selected=()
local menu_defaults=()
local menu_labels=()
local menu_selected=()
_log "[INF] Installation started."
# Prevent running the installer with sudo/root.
if [[ "$(id -u)" -eq 0 ]]; then
_echo_error "Do NOT run as root! Use: 'bash install.sh'"
exit 1
fi
_get_parameters_command_line "$@"
# If quiet mode is enabled, automatically disable interactive mode to
# ensure a fully silent and non-interactive installation.
if [[ "$OPT_QUIET_INSTALL" == "true" ]]; then
OPT_INTERACTIVE_INSTALL="false"
fi
# If the file ".common-functions.sh" is missing, it means the installer is
# being executed remotely (e.g., via 'curl'). In that case, download the
# tarball and continue the installation from the extracted files.
if [[ ! -f "$SCRIPT_DIR/.common-functions.sh" ]]; then
_log "[INF] Running remote installer."
_bootstrap_repository "$@"
exit 0
fi
_log_system_info
_i18n_initialize
# Available options presented in the interactive menu.
menu_labels=(
"$(_i18n 'Check for basic dependencies')"
"$(_i18n 'Remove previously installed scripts')"
"$(_i18n 'Install keyboard accelerators')"
"$(_i18n 'Close the file manager to reload configurations')"
"$(_i18n 'Add shortcuts to the application menu')"
"$(_i18n 'Install Homebrew (optional)')"
"$(_i18n 'Choose which script categories to install')"
)
# Default states for the menu options.
menu_defaults=(
"$OPT_INSTALL_BASIC_DEPS"
"$OPT_REMOVE_SCRIPTS"
"$OPT_INSTALL_ACCELS"
"$OPT_CLOSE_FILE_MANAGER"
"$OPT_INSTALL_APP_SHORTCUTS"
"$OPT_INSTALL_HOMEBREW"
"$OPT_CHOOSE_CATEGORIES"
)
_echo "$APP_NAME v$APP_VERSION"
_echo ""
_log_variable "APP_VERSION"
if [[ "$OPT_INTERACTIVE_INSTALL" == "true" ]]; then
_echo "$(_i18n 'Select the options (<SPACE> to check, <ENTER> to confirm):')"
# Display the interactive menu and capture user selections.
_multiselect_menu menu_selected menu_labels menu_defaults
else
_echo_info "$(_i18n 'Installing in non-interactive mode...')"
menu_selected=("${menu_defaults[@]}")
fi
# Map menu selections into a comma-separated string of options.
OPT_INSTALL_BASIC_DEPS=${menu_selected[0]}
OPT_REMOVE_SCRIPTS=${menu_selected[1]}
OPT_INSTALL_ACCELS=${menu_selected[2]}
OPT_CLOSE_FILE_MANAGER=${menu_selected[3]}
OPT_INSTALL_APP_SHORTCUTS=${menu_selected[4]}
OPT_INSTALL_HOMEBREW=${menu_selected[5]}
OPT_CHOOSE_CATEGORIES=${menu_selected[6]}
_log_variable "OPT_INSTALL_BASIC_DEPS"
_log_variable "OPT_REMOVE_SCRIPTS"
_log_variable "OPT_INSTALL_ACCELS"
_log_variable "OPT_CLOSE_FILE_MANAGER"
_log_variable "OPT_INSTALL_APP_SHORTCUTS"
_log_variable "OPT_INSTALL_HOMEBREW"
_log_variable "OPT_CHOOSE_CATEGORIES"
_log_variable "OPT_INTERACTIVE_INSTALL"
_log_variable "OPT_QUIET_INSTALL"
# Collect all available script categories (directories).
local dir=""
while IFS= read -r -d $'\0' dir; do
cat_dirs+=("$dir")
done < <(_list_scripts_categories)
# If requested, let the user select which categories to install.
if [[ "$OPT_CHOOSE_CATEGORIES" == "true" ]]; then
_echo ""
_echo "$(_i18n 'Select the options (<SPACE> to check, <ENTER> to confirm):')"
_multiselect_menu cat_selected cat_dirs cat_defaults
fi
#region Step 1: Check for basic dependencies
[[ "$OPT_INSTALL_BASIC_DEPS" == "true" ]] && _check_dependencies
#endregion
#region Step 2: Install the scripts
INSTALL_HOME=$HOME
INSTALL_OWNER=$(stat -c "%U" "$INSTALL_HOME")
INSTALL_GROUP=$(stat -c "%G" "$INSTALL_HOME")
_log_variable "INSTALL_OWNER"
_log_variable "INSTALL_GROUP"
INSTALL_DIR="$INSTALL_HOME/$INSTALL_PATH"
_echo ""
_echo_info "$(_i18n 'Installing:')"
_echo_info "> $(_i18n 'User'): $INSTALL_OWNER"
_echo_info "> $(_i18n 'Directory'): $INSTALL_DIR"
_install_scripts cat_selected cat_dirs
#endregion
#region Step 3: Install file manager configurations
# Install the actions and the keyboard accelerators for
# each detected file manager.
local file_manager=""
for file_manager in "${COMPATIBLE_FILE_MANAGERS[@]}"; do
FILE_MANAGER=$file_manager
if ! _command_exists "$FILE_MANAGER"; then
continue
fi
#region Step 3.1: Install the the actions
_install_actions
#endregion
#region Step 3.2: Install the keyboard accelerators
[[ "$OPT_INSTALL_ACCELS" == "true" ]] && _install_accels
#endregion
#region Step 3.3: Reload file manager to apply changes
[[ "$OPT_CLOSE_FILE_MANAGER" == "true" ]] && _close_filemanager
_echo_info "> $(_i18n 'Done!')"
#endregion
done
#endregion
#region Step 4: Install the shortcuts (application menu)
if [[ "$OPT_INSTALL_APP_SHORTCUTS" == "true" ]]; then
_echo ""
_echo_info "$(_i18n 'Installing application menu shortcuts:')"
_install_application_shortcuts
_create_gnome_application_folder
_echo_info "> $(_i18n 'Done!')"
fi
#endregion
#region Step 5: Install Homebrew (optional)
if [[ "$OPT_INSTALL_HOMEBREW" == "true" ]]; then
_install_homebrew
fi
#endregion
_echo ""
_echo_info "$(_i18n 'Installation completed successfully!')"
_log_finish
}
#endregion
#------------------------------------------------------------------------------
#region Printing
#------------------------------------------------------------------------------
_echo() {
local message=$1
if [[ -n "$message" ]]; then
_log "[MSG] $message"
fi
if [[ "$OPT_QUIET_INSTALL" == "true" ]]; then
return
fi
echo "$message"
}
_echo_info() {
local message=$1
_log "[MSG] $message"
if [[ "$OPT_QUIET_INSTALL" == "true" ]]; then
return
fi
local msg_info="[\033[0;32m INFO \033[0m]"
echo -e "$msg_info $message"
}
_echo_error() {
local message=$1
_log "[ERR] $message"
local msg_error="[\033[0;31mFAILED\033[0m]"
echo -e "$msg_error $message"
}
#endregion
#------------------------------------------------------------------------------
#region Log functions
#------------------------------------------------------------------------------
_print_date() {
date +"%Y-%m-%d %T %Z"
}
_log() {
local message=$1
printf "%s\n" "$(_print_date) $message" >>"$INSTALL_LOG_TMP"
}
_log_finish() {
local log_file="$INSTALL_DIR/$INSTALL_LOG_NAME"
touch -- "$log_file"
cat -- "$INSTALL_LOG_TMP" >>"$log_file"
}
_log_system_info() {
local sys_kernel=""
local sys_os=""
if _command_exists "lsb_release"; then
# shellcheck disable=SC2034
sys_os=$(lsb_release -ds 2>/dev/null)
fi
if _command_exists "uname"; then
# shellcheck disable=SC2034
sys_kernel=$(uname -srmo 2>/dev/null)
fi
_log_variable "USER"
_log_variable "HOME"
_log_variable "SHELL"
_log_variable "LANG"
_log_variable "sys_kernel"
_log_variable "sys_os"
_log_variable "XDG_CURRENT_DESKTOP"
_log_variable "XDG_SESSION_TYPE"
}
_log_variable() {
local var_name=$1
local var_value="${!var_name:-}"
_log "[VAR] $var_name=\"$var_value\""
}
#endregion
#------------------------------------------------------------------------------
#region Internationalization (i18n)
#------------------------------------------------------------------------------
_i18n_print_desktop_name() {
local prefix=$1
local name=$2
local po_file=""
while IFS= read -r -d $'\0' po_file; do
local msg=""
msg=$(_i18n_get_translation "$I18N_DIR/$po_file" "$name")
printf "%s\n" "${prefix}[${po_file%.po}]=${msg}"
done < <(_list_traslation_files)
}
# Translate each directory component in the path.
_i18n_translate_path() {
local path=$1
local IFS='/'
# shellcheck disable=SC2206
local components=($path)
local translated_components=()
for component in "${components[@]}"; do
translated_components+=("$(_i18n "$component")")
done
translated_path=$(
IFS='/'
echo "${translated_components[*]}"
)
printf "%s" "$translated_path"
}
_i18n() {
local msgid=$1
_i18n_get_translation "$I18N_FILE" "$msgid"
}
_i18n_get_translation() {
local po_file=$1
local msgid=$2
local msgstr=""
msgstr=$(grep -A1 "msgid \"$msgid\"" "$po_file" 2>/dev/null |
grep "msgstr" | cut -d '"' -f 2)
if [[ -n "$msgstr" ]]; then
printf "%s" "$msgstr"
else
printf "%s" "$msgid"
fi
}
_i18n_initialize() {
# LANG not set: nothing to load
if [[ -z "${LANG:-}" ]]; then
return
fi
local lang_full="${LANG%%.*}" # e.g. 'pt_BR.UTF-8' to 'pt_BR'.
local lang_base="${lang_full%%_*}" # e.g. 'pt_BR to' 'pt'.
local po_file=""
# Try full locale first (e.g. pt_BR.po).
po_file="$I18N_DIR/$lang_full.po"
if [[ -f "$po_file" ]]; then
I18N_FILE="$po_file"
return
fi
# Fallback to base language (e.g. pt.po).
po_file="$I18N_DIR/$lang_base.po"
if [[ -f "$po_file" ]]; then
I18N_FILE="$po_file"
fi
}
#endregion
#------------------------------------------------------------------------------
#region Validation and checks
#------------------------------------------------------------------------------
# Function: _check_exist_filemanager
#
# Description:
# This function checks if at least one compatible file manager exists in the
# system by iterating through a predefined list of supported file managers
# defined in '$COMPATIBLE_FILE_MANAGERS'.
#
# Returns:
# 0 (true): If at least one compatible file manager is found.
# 1 (false): If no compatible file manager is found.
_check_exist_filemanager() {
local file_manager=""
for file_manager in "${COMPATIBLE_FILE_MANAGERS[@]}"; do
if _command_exists "$file_manager"; then
return 0
fi
done
return 1
}
#endregion
#------------------------------------------------------------------------------
#region File and directory management
#------------------------------------------------------------------------------
_list_scripts() {
find -L "$INSTALL_DIR" -mindepth 2 -type f \
"${IGNORE_FIND_PATHS[@]}" \
-print0 2>/dev/null | sort --zero-terminated
}
_list_scripts_installed() {
local dir=$1
find -L "$dir" -mindepth 2 -type f \
"${IGNORE_FIND_PATHS[@]}" \
-print0 2>/dev/null | sort --zero-terminated
}
_list_scripts_application() {
find -L "$INSTALL_DIR" -mindepth 2 -type f \
"${IGNORE_FIND_PATHS[@]}" "${IGNORE_APPS_SHORTCUTS[@]}" \
-print0 2>/dev/null | sort --zero-terminated
}
_list_scripts_categories() {
find -L "$SCRIPT_DIR" -mindepth 1 -maxdepth 1 -type d \
"${IGNORE_FIND_PATHS[@]}" -print0 2>/dev/null |
sed -z "s|^.*/||" | sort --zero-terminated
}
_list_traslation_files() {
find -L "$I18N_DIR" -name "*.po" -type f \
-printf "%f\0" 2>/dev/null | sort --zero-terminated
}
_chown_file() {
chown "$INSTALL_OWNER:$INSTALL_GROUP" -- "$1"
}
_chmod_x_file() {
chmod +x -- "$1"
}
_tee_file() {
tee -- "$1" >/dev/null
}
# Function: _item_create_backup
#
# Description:
# This function creates a backup of a file (append .bak) if it exists.
_item_create_backup() {
local item=$1
if [[ -e "$item" ]] && [[ ! -e "$item.bak" ]]; then
mv -- "$item" "$item.bak" 2>/dev/null
fi
}
# Function: _delete_items
#
# Description:
# This function deletes or trash items, using the best available method.
_delete_items() {
local items=$1
# Attempt to remove empty directories directly (rmdir only removes empty
# dirs, silently fails otherwise) This avoids sending empty folders to the
# trash and deletes them outright.
# shellcheck disable=SC2086
rmdir -- $items &>/dev/null
# shellcheck disable=SC2086
if _command_exists "gio"; then
gio trash -- $items 2>/dev/null
elif _command_exists "kioclient"; then
kioclient move -- $items trash:/ 2>/dev/null
elif _command_exists "gvfs-trash"; then
gvfs-trash -- $items 2>/dev/null
else
rm -rf -- $items 2>/dev/null
fi
}
#endregion
#------------------------------------------------------------------------------
#region System information and parameters
#------------------------------------------------------------------------------
_command_exists() {
local command_check=$1
command -v "$command_check" &>/dev/null || return 1
}
_get_parameters_command_line() {
local expanded_args=()
# Expand combined short options (e.g. -ia -> -i -a).
while [[ $# -gt 0 ]]; do
if [[ "$1" =~ ^-[a-zA-Z]{2,}$ ]]; then
# Split combined short options (e.g. -ia -> -i -a).
local arg="${1#-}"
local i=""
for ((i = 0; i < ${#arg}; i++)); do
expanded_args+=("-${arg:i:1}")
done
else
expanded_args+=("$1")
fi
shift
done
# Replace positional parameters with expanded arguments.
if ((${#expanded_args[@]})); then
set -- "${expanded_args[@]}"
else
set --
fi
# Read parameters from command line.
while [[ $# -gt 0 ]]; do
case "$1" in
-b | --install-homebrew) OPT_INSTALL_HOMEBREW="true" ;;
-B | --no-install-homebrew) OPT_INSTALL_HOMEBREW="false" ;;
-c | --check-dependencies) OPT_INSTALL_BASIC_DEPS="true" ;;
-C | --no-check-dependencies) OPT_INSTALL_BASIC_DEPS="false" ;;
-d | --remove-scripts) OPT_REMOVE_SCRIPTS="true" ;;
-D | --no-remove-scripts) OPT_REMOVE_SCRIPTS="false" ;;
-f | --close-filemanager) OPT_CLOSE_FILE_MANAGER="true" ;;
-F | --no-close-filemanager) OPT_CLOSE_FILE_MANAGER="false" ;;
-k | --install-shortcuts) OPT_INSTALL_ACCELS="true" ;;
-K | --no-install-shortcuts) OPT_INSTALL_ACCELS="false" ;;
-n | --non-interactive) OPT_INTERACTIVE_INSTALL="false" ;;
-q | --quiet) OPT_QUIET_INSTALL="true" ;;
-s | --install-app-shortcuts) OPT_INSTALL_APP_SHORTCUTS="true" ;;
-S | --no-install-app-shortcuts) OPT_INSTALL_APP_SHORTCUTS="false" ;;
-h | --help)
echo "Usage: $0 [options]"
echo
echo " -b, --install-homebrew Install Homebrew."
echo " -B, --no-install-homebrew Do not install Homebrew."
echo " -c, --check-dependencies Check for basic dependencies."
echo " -C, --no-check-dependencies Do not check for basic dependencies."
echo " -d, --remove-scripts Remove previously installed scripts."
echo " -D, --no-remove-scripts Do not remove previously installed scripts."
echo " -f, --close-filemanager Close file manager after install."
echo " -F, --no-close-filemanager Do not close file manager after install."
echo " -k, --install-shortcuts Install keyboard accelerators."
echo " -K, --no-install-shortcuts Do not install keyboard accelerators."
echo " -n, --non-interactive Run without prompts."
echo " -q, --quiet Suppress all output (silent mode)."
echo " -s, --install-app-shortcuts Add shortcuts to the application menu."
echo " -S, --no-install-app-shortcuts Do not add shortcuts to the application menu."
echo " -h, --help Show this help message and exit."
echo
exit 0
;;
*)
echo "Unknown option: $1" >&2
echo "Use --help for usage information." >&2
exit 1
;;
esac
shift
done
}
# Function: _get_par_value
#
# Description:
# This function extracts the value of a given parameter from a script file.
# It searches for "parameter=value" inside the file, then returns only the
# value.
_get_par_value() {
local filename=$1
local parameter=$2
grep --only-matching -m 1 "$parameter=.*" "$filename" |
cut -d "=" -f 2- 2>/dev/null
}
#endregion
#------------------------------------------------------------------------------
#region Installation functions
#------------------------------------------------------------------------------
_sanitize_string() {
tr -cd ";[:alnum:] -" | tr " " "-" | tr -s "-" | tr "[:upper:]" "[:lower:]"
}
_generate_desktop_filename() {
local name=$1
name=$(_sanitize_string <<<"$name")
printf "%s" "$name.desktop"
}
#------------------------------------------------------------------------------
#region Dependencies
#------------------------------------------------------------------------------
# shellcheck disable=SC2086
_check_dependencies() {
_echo ""
_echo_info "$(_i18n 'Checking for basic dependencies:')"
local packages=""
# Basic packages to run the script '.common-functions.sh'.
_command_exists "basename" || packages+="coreutils "
_command_exists "file" || packages+="file "
_command_exists "pstree" || packages+="psmisc "
_command_exists "xdg-open" || packages+="xdg-utils "
# Packages for dialogs.
if [[ -n "${XDG_CURRENT_DESKTOP:-}" ]]; then
if ! _command_exists "zenity" && ! _command_exists "yad"; then
if [[ "${XDG_CURRENT_DESKTOP,,}" == *"gnome"* ||
"${XDG_CURRENT_DESKTOP,,}" == *"cosmic"* ]]; then
packages+="zenity "
else
packages+="yad "
fi
fi
fi
if _command_exists "nix-env"; then
# Package manager 'nix-env': For Nix-based systems.
_command_exists "pgrep" || packages+="procps "
_install_packages "nix-env" "$packages"
elif _command_exists "apt-get"; then
# Package manager 'apt-get': For Debian/Ubuntu systems.
_command_exists "pgrep" || packages+="procps "
_install_packages "apt-get" "$packages"
elif _command_exists "rpm-ostree"; then
# Package manager 'rpm-ostree': For Fedora/RHEL atomic systems.
_command_exists "pgrep" || packages+="procps-ng "
_install_packages "rpm-ostree" "$packages"
elif _command_exists "dnf"; then
# Package manager 'dnf': For Fedora/RHEL systems.
_command_exists "pgrep" || packages+="procps-ng "
_install_packages "dnf" "$packages"
elif _command_exists "pacman"; then
# Package manager 'pacman': For Arch Linux systems.
_command_exists "pgrep" || packages+="procps "
# NOTE: Force update GTK4 packages on Arch Linux.
if [[ "$packages" == *"zenity"* ]]; then
packages+="gtk4 zlib glib2 "
fi
_install_packages "pacman" "$packages"
elif _command_exists "zypper"; then
# Package manager 'zypper': For openSUSE systems.
_command_exists "pgrep" || packages+="procps-ng "
_install_packages "zypper" "$packages"
elif _command_exists "guix"; then
# Package manager 'guix': For GNU Guix systems.
_command_exists "pgrep" || packages+="procps "
_install_packages "guix" "$packages"
elif _command_exists "xbps-install"; then
# Package manager 'xbps': For Void Linux systems.
_command_exists "pgrep" || packages+="procps-ng "
# NOTE: Update dependencies on Void Linux.
if [[ "$packages" == *"yad"* ]]; then
packages+="libavcodec6 libheif "
fi
_install_packages "xbps-install" "$packages"
else
if [[ -n "$packages" ]]; then
_log "[ERR] Missing package manager."
_echo_error "$(_i18n 'Could not find a package manager!')"
exit 1
fi
fi
if [[ -z "$packages" ]]; then
_echo_info "> $(_i18n 'All dependencies are already satisfied.')"
fi
_echo_info "> $(_i18n 'Done!')"
}
_install_packages() {
local pkg_manager=$1
local packages=$2
local cmd_admin=""
local cmd_admin_available=""
local cmd_inst=""
# Remove the last space char.
packages=${packages% }
_log "[INF] Package manager: $pkg_manager"
[[ -z "$packages" ]] && return
_echo_info "> $(_i18n 'The following packages are missing:') $packages"
_command_exists "sudo" && cmd_admin_available="sudo"
cmd_inst=""
cmd_admin="$cmd_admin_available"
case "$pkg_manager" in
"apt-get")
cmd_inst+="apt-get update;"
cmd_inst+="apt-get -y install $packages"
;;
"dnf")
cmd_inst+="dnf check-update;"
cmd_inst+="dnf -y install $packages"
;;
"guix")
cmd_inst="guix package -i $packages"
;;
"nix-env")
local nix_packages=""
local nix_channel="nixpkgs"
if grep --quiet "ID=nixos" /etc/os-release 2>/dev/null; then
nix_channel="nixos"
fi
# Prefix packages with their channel namespace.
nix_packages="$nix_channel.$packages"
# shellcheck disable=SC2001
nix_packages=$(sed "s| $||g" <<<"$nix_packages")
# shellcheck disable=SC2001
nix_packages=$(sed "s| | $nix_channel.|g" <<<"$nix_packages")
cmd_inst+="nix-env -iA $nix_packages"
# Nix does not require root for installing user packages.
cmd_admin=""
;;
"pacman")
cmd_inst+="pacman -Syy;"
cmd_inst+="pacman --noconfirm -S $packages"
;;
"rpm-ostree")
cmd_inst+="rpm-ostree install $packages"
;;
"xbps-install")
cmd_inst+="xbps-install -S;"
cmd_inst+="xbps-install -y -u xbps;"
cmd_inst+="xbps-install -y $packages"
;;
"zypper")
cmd_inst+="zypper refresh;"
cmd_inst+="zypper --non-interactive install $packages"
;;
esac
# Execute installation.
if [[ -n "$cmd_inst" ]]; then
_echo_info "> $(_i18n 'Installing the packages. Please, wait...')"
# If root privileges are required, prepend with 'sudo'.
$cmd_admin bash -c "$cmd_inst"
fi
}
#endregion
#------------------------------------------------------------------------------
#region Install scripts
#------------------------------------------------------------------------------
# Function: _install_scripts
#
# Description:
# This function installs scripts into the target directory.
#
# Steps:
# 1. Optionally remove any previously installed scripts.
# 2. Copy common and category-specific script files.
# 3. Set proper ownership and permissions.
_install_scripts() {
local -n _cat_selected=$1
local -n _cat_dirs=$2
rm -rf -- "$INSTALL_DIR" 2>/dev/null
rm -rf -- "${INSTALL_HOME:?}/$INSTALL_APPS_SHORTCUTS_PATH" 2>/dev/null
_echo_info "> $(_i18n 'Copying files...')"
mkdir --parents -- "$INSTALL_DIR"
# Always copy important files and directories.
cp -- "$SCRIPT_DIR/.common-functions.sh" "$INSTALL_DIR"
cp -- "$SCRIPT_DIR/.pkg-map.sh" "$INSTALL_DIR"
cp -r -- "$SCRIPT_DIR/.po" "$INSTALL_DIR"
# Copy scripts by category. If the user selected specific categories, only
# those are installed. Otherwise, all categories are copied by default.
local i=0
for i in "${!_cat_dirs[@]}"; do
if [[ ${_cat_selected[$i]+_} ]]; then
if [[ ${_cat_selected[i]} == "true" ]]; then
cp -r -- "$SCRIPT_DIR/${_cat_dirs[i]}" "$INSTALL_DIR"
fi
else
cp -r -- "$SCRIPT_DIR/${_cat_dirs[i]}" "$INSTALL_DIR"
fi
done
# Adjust ownership and permissions. Ensures all files belong to
# the correct user/group and are executable.
chown -R "$INSTALL_OWNER:$INSTALL_GROUP" -- "$INSTALL_DIR"
find -L "$INSTALL_DIR" -mindepth 2 -type f \
"${IGNORE_FIND_PATHS[@]}" -exec chmod +x -- {} +
}
_create_links() {
local destination_dir=$1
# Remove previous scripts if requested.
if [[ "$OPT_REMOVE_SCRIPTS" == "true" ]]; then
_delete_items "$destination_dir"
else
# Remove broken links.
find "$destination_dir" -type l -delete 2>/dev/null
# Remove empty dirs.
find "$destination_dir" -type d -empty -delete 2>/dev/null
fi
mkdir --parents -- "$destination_dir"
ln -sf -- "$INSTALL_DIR/.po" "$destination_dir/.po"
local dir=""
local name=""
local relative_path=""
# Process all files and directories recursively.
local file_path=""
while IFS= read -r -d $'\0' file_path; do
relative_path="${file_path#"$INSTALL_DIR"/}"
dir=$(dirname -- "$relative_path")
name=$(basename -- "$file_path")
# Skip the .po directory (already linked).
[[ "$relative_path" == ".po" ]] && continue
# Create translated directory path.
local translated_dir=""
translated_dir=$(_i18n_translate_path "$dir")
# Create destination path.
local destination_path="$destination_dir"
if [[ -n "$translated_dir" ]]; then
destination_path="$destination_dir/$translated_dir"
fi
if [[ -d "$file_path" ]]; then
# Create translated directory.
mkdir --parents -- "$destination_path/$(_i18n "$name")"
else
# Create parent directories and link file.
mkdir --parents -- "$destination_path"
ln -sf -- "$file_path" "$destination_path/$(_i18n "$name")"
fi
done < <(find -L "$INSTALL_DIR" \
-mindepth 1 "${IGNORE_FIND_PATHS[@]}" -print0 2>/dev/null)
}
#endregion
#------------------------------------------------------------------------------
#region Keyboard accellerators
#------------------------------------------------------------------------------
# Function: _install_accels
#
# Description:
# Install keyboard accelerators for specific file managers.
_install_accels() {
_echo_info "> ($FILE_MANAGER) $(_i18n 'Installing keyboard accelerators...')"
case "$FILE_MANAGER" in
"nautilus") _install_accels_nautilus "$INSTALL_HOME/.config/nautilus/scripts-accels" ;;
"caja") _install_accels_gnome2 "$INSTALL_HOME/.config/caja/accels" "$INSTALL_HOME/.config/caja/scripts" ;;
"nemo") _install_accels_gnome2 "$INSTALL_HOME/.gnome2/accels/nemo" "$INSTALL_HOME/.local/share/nemo/scripts" ;;
"thunar") _install_accels_thunar "$INSTALL_HOME/.config/Thunar/accels.scm" ;;
esac
}
_install_accels_nautilus() {
local accels_file=$1
mkdir --parents -- "$(dirname -- "$accels_file")"
# Create a backup of older custom actions.
_item_create_backup "$accels_file"
rm -f -- "$accels_file"
{
local filename=""
while IFS= read -r -d $'\0' filename; do
local keyboard_shortcut=""
keyboard_shortcut=$(_get_par_value \
"$filename" "install_keyboard_shortcut")
if [[ -n "$keyboard_shortcut" ]]; then
local name=""
name=$(basename -- "$filename")
printf "%s\n" "$keyboard_shortcut $(_i18n "$name")"
fi