-
Notifications
You must be signed in to change notification settings - Fork 771
Expand file tree
/
Copy pathsetup-termux-desktop
More file actions
executable file
·6349 lines (5830 loc) · 201 KB
/
setup-termux-desktop
File metadata and controls
executable file
·6349 lines (5830 loc) · 201 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
#!/data/data/com.termux/files/usr/bin/bash
#
# Termux Desktop
# A simple script to easily install supported desktop environments and
# window managers, along with other essential configurations and tools.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author : @sabamdarif
# License : GPL-v3
# Description: This script automates the installation of various desktop environments and window managers
# Repository : https://github.com/sabamdarif/termux-desktop
#########################################################################
#
# Error Handling
#
#########################################################################
# set -euo pipefail
# Global trap handler for cleanup
function cleanup_on_exit() {
local exit_code=$?
if [ $exit_code -ne 0 ]; then
log_error "Script exited with error code: $exit_code"
fi
# Add any cleanup tasks here
return $exit_code
}
# Set up trap for cleanup
trap cleanup_on_exit EXIT
#########################################################################
#
# Configuration Constants
#
#########################################################################
# Main variables
readonly REPO_OWNER="sabamdarif"
readonly REPO_NAME="termux-desktop"
readonly REPO_BRANCH_MAIN="main"
readonly REPO_SETUP_FILE_BRANCH="setup-files"
readonly REPO_SETUP_FILES_FOLDER="setup-files"
readonly REPO_RAW_URL="https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}"
readonly TERMUX_HOME=${HOME:-/data/data/com.termux/files/home}
readonly TERMUX_PREFIX=${PREFIX:-/data/data/com.termux/files/usr}
# Retry configuration
readonly MAX_DOWNLOAD_RETRIES=5
readonly MAX_INSTALL_RETRIES=5
readonly DOWNLOAD_TIMEOUT=15
#########################################################################
#
# Initial Values
#
#########################################################################
readonly TERMUX_DESKTOP_PATH="$TERMUX_PREFIX/etc/termux-desktop"
readonly CONFIG_FILE="$TERMUX_DESKTOP_PATH/configuration.conf"
readonly LOG_FILE="$TERMUX_HOME/termux-desktop.log"
readonly LATEST_APPSTORE_VERSION="1.0.0"
CALL_FROM_CHANGE_DISTRO=false
CALL_FROM_CHANGE_STYLE=false
IS_TERMUX_API_INSTALLED=false
LITE_MODE="${LITE,,}"
#########################################################################
#
# Color Setup
#
#########################################################################
# shellcheck disable=SC2154
if [ -t 1 ] && [ -n "${TERM:-}" ]; then
R="$(printf '\033[0m\033[1m\033[31m')" # RST + bold + red
G="$(printf '\033[0m\033[32m')" # RST + green
Y="$(printf '\033[0m\033[33m')" # RST + yellow
B="$(printf '\033[0m\033[34m')" # RST + blue
C="$(printf '\033[0m\033[36m')" # RST + cyan
NC="$(printf '\033[0m')" # RST
BOLD="$(printf '\033[1m')" # bold
else
R=""
G=""
Y=""
B=""
C=""
NC=""
BOLD=""
fi
cd "$TERMUX_HOME" || exit 1
##################################################################
# create detailed log output
function debug_msg() {
local debug_message="[DEBUG] running: $BASH_COMMAND"
echo "$debug_message"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $debug_message" >>"$LOG_FILE"
}
function banner() {
clear
printf "%s############################################################\n" "$C"
printf "%s# #\n" "$C"
printf "%s# ▀█▀ █▀▀ █▀█ █▀▄▀█ █ █ ▀▄▀ █▀▄ █▀▀ █▀ █▄▀ ▀█▀ █▀█ █▀█ #\n" "$C"
printf "%s# █ ██▄ █▀▄ █ █ █▄█ █ █ █▄▀ ██▄ ▄█ █ █ █ █▄█ █▀▀ #\n" "$C"
printf "%s# #\n" "$C"
printf "%s######################### Termux Gui #######################%s\n" "$C" "$NC"
echo " "
}
# check if the script is running on termux or not
function check_termux() {
if [ ! -f /system/build.prop ]; then
echo "${R}[${R}☓${R}]${R}${BOLD} Not running on Android."
exit 1
fi
local tracer_pid
tracer_pid=$(grep TracerPid "/proc/$$/status" | cut -d $'\t' -f 2)
if [ "$tracer_pid" != "0" ]; then
local tracer_name
tracer_name=$(grep Name "/proc/${tracer_pid}/status" | cut -d $'\t' -f 2)
if [ "$tracer_name" = "proot" ]; then
echo "${R}[${R}☓${R}]${R}${BOLD} Must not be executed under PRoot."
exit 1
fi
fi
if [[ -z "$PREFIX" || "$PREFIX" != *"/com.termux/"* ]]; then
echo "${R}[${R}☓${R}]${R}${BOLD} Please run this script inside Termux."
exit 1
fi
}
#########################################################################
#
# Base Functions
#
#########################################################################
function print_log() {
local timestamp
timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
local log_level="${2:-INFO}"
local message="$1"
echo "[${timestamp}] ${log_level}: ${message}" >>"$LOG_FILE" 2>/dev/null || true
}
function log_warn() {
local timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local message="$1"
echo "[${timestamp}] WARN: ${message}" >>"$LOG_FILE" 2>/dev/null || true
}
function log_error() {
local timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local message="$1"
echo "[${timestamp}] ERROR: ${message}" >>"$LOG_FILE" 2>/dev/null || true
}
function log_debug() {
local timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local message="$1"
echo "[${timestamp}] DEBUG: ${message}" >>"$LOG_FILE" 2>/dev/null || true
}
function print_success() {
local msg
msg="$1"
echo -e "${R}[${G}✓${R}]${G} $msg ${NC}"
log_debug "$msg"
}
function print_failed() {
local msg
msg="$1"
echo -e "${R}[${R}☓${R}]${R} $msg ${NC}"
log_debug "$msg"
}
function print_warn() {
local msg
msg="$1"
echo -e "${R}[${Y}!${R}]${Y} $msg ${NC}"
log_debug "$msg"
}
function print_msg() {
local msg
msg="$1"
echo -e "${R}[${C}-${R}]${B} $msg ${NC}"
log_debug "$msg"
}
function wait_for_keypress() {
read -n1 -s -r -p "${R}[${C}-${R}]${G} Press any key to continue, CTRL+c to cancel...${NC}"
echo
}
function check_and_create_directory() {
if [[ -n "$1" && ! -d "$1" ]]; then
mkdir -p "$1" 2>/dev/null || {
log_error "Failed to create directory: $1"
return 1
}
log_debug "Created directory: $1"
fi
}
# first check then delete
function check_and_delete() {
local files_folders
for files_folders in "$@"; do
if [[ -e "$files_folders" ]]; then
rm -rf "$files_folders" >/dev/null 2>&1 || {
log_error "Failed to delete: $files_folders"
continue
}
log_debug "Deleted: $files_folders"
fi
done
}
# first check then backup
function check_and_backup() {
log_debug "Starting backup for: $*"
# shellcheck disable=SC2206
local files_folders_list=($@)
local files_folders
local date_str
date_str=$(date +"%d-%m-%Y")
for files_folders in "${files_folders_list[@]}"; do
if [[ -e "$files_folders" ]]; then
local backup="${files_folders}-${date_str}.bak"
if [[ -e "$backup" ]]; then
print_msg "Backup $backup already exists"
else
print_msg "Backing up $files_folders"
mv "$files_folders" "$backup"
log_debug "$files_folders $backup"
fi
else
print_msg "Path $files_folders does not exist"
fi
done
}
# find a backup file which end with a number pattern and restore it
function check_and_restore() {
log_debug "Starting restore for: $*"
# shellcheck disable=SC2206
local files_folders_list=($@)
local files_folders
for files_folders in "${files_folders_list[@]}"; do
local latest_backup
latest_backup=$(find "$(dirname "$files_folders")" -maxdepth 1 -name "$(basename "$files_folders")-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9].bak" 2>/dev/null | sort | tail -n 1)
if [[ -z "$latest_backup" ]]; then
print_msg "No backup found for $files_folders"
continue
fi
if [[ -e "$files_folders" ]]; then
print_msg "File $files_folders already exists"
else
print_msg "Restoring $files_folders"
mv "$latest_backup" "$files_folders"
log_debug "$latest_backup $files_folders"
fi
done
}
function download_file() {
local dest
local url
local max_retries=$MAX_DOWNLOAD_RETRIES
local attempt=1
local download_success=false
# Parse arguments
if [[ -z "$2" ]]; then
url="$1"
dest="$(basename "$url")"
else
dest="$1"
url="$2"
fi
# Validate URL
if [[ -z "$url" ]]; then
print_failed "No URL provided!"
return 1
fi
# Retry loop
while [[ $attempt -le $max_retries ]]; do
print_msg "Downloading $dest... (Attempt $attempt/$max_retries)"
# Clean up any existing empty or partial file
check_and_delete "$dest"
# Perform download
if command -v wget &>/dev/null; then
if wget --tries=3 --timeout=$DOWNLOAD_TIMEOUT --retry-connrefused -O "$dest" "$url"; then
download_success=true
fi
else
if curl -f -L --connect-timeout $DOWNLOAD_TIMEOUT --max-time $((DOWNLOAD_TIMEOUT * 3)) "$url" -o "$dest"; then
download_success=true
fi
fi
# Validate downloaded file
if [[ "$download_success" == true ]] && [[ -f "$dest" ]] && [[ -s "$dest" ]]; then
# Additional validation: check if file is not an HTML error page
if file "$dest" | grep -qi "html\|xml" && grep -qi "error\|404\|403\|not found" "$dest" 2>/dev/null; then
print_failed "Downloaded file appears to be an error page"
download_success=false
check_and_delete "$dest"
else
if [[ $attempt -eq 1 ]]; then
print_success "File downloaded successfully."
else
print_success "File downloaded successfully on attempt $attempt."
fi
return 0
fi
fi
# Download failed, prepare for retry
if [[ $attempt -lt $max_retries ]]; then
print_failed "Download failed. Retrying..."
check_and_delete "$dest"
fi
download_success=false
((attempt++))
done
# All attempts failed
check_and_delete "$dest"
print_failed "Failed to download the file after $max_retries attempts."
return 1
}
function detect_package_manager() {
# shellcheck disable=SC1091
if [[ -f "$TERMUX_PREFIX/bin/termux-setup-package-manager" ]]; then
source "$TERMUX_PREFIX/bin/termux-setup-package-manager"
fi
if [[ "$TERMUX_APP_PACKAGE_MANAGER" == "apt" ]]; then
PACKAGE_MANAGER="apt"
elif [[ "$TERMUX_APP_PACKAGE_MANAGER" == "pacman" ]]; then
PACKAGE_MANAGER="pacman"
else
PACKAGE_MANAGER="pkg"
print_failed "${C} Could not detect your package manager, Switching To ${C}pkg ${NC}"
fi
log_debug "Package manager: $PACKAGE_MANAGER"
}
# will check if the package is already installed or not, if it installed then reinstall and print success/failed message
function package_install_and_check() {
log_debug "Starting package installation for: $*"
local packs_list
# Properly splits on spaces
IFS=' ' read -r -a packs_list <<<"$*"
if [[ "$PACKAGE_MANAGER" == "pacman" ]]; then
for package_name in "${packs_list[@]}"; do
if [[ "$package_name" == *"*"* ]]; then
log_debug "Processing wildcard pattern: $package_name"
local packages
packages=$(pacman -Ssq 2>/dev/null | grep -E "^${package_name//\*/.*}$")
log_debug "matched packages: ${C}$packages"
for package in $packages; do
install_package_with_retry "$package" "pacman"
done
else
install_package_with_retry "$package_name" "pacman"
fi
done
else
for package_name in "${packs_list[@]}"; do
if [[ "$package_name" == *"*"* ]]; then
log_debug "Processing wildcard pattern: $package_name"
local packages
packages=$(apt-cache pkgnames 2>/dev/null | grep -E "^${package_name//\*/.*}$" | sort -u)
log_debug "matched packages: $packages"
for package in $packages; do
install_package_with_retry "$package" "apt"
done
else
install_package_with_retry "$package_name" "apt"
fi
done
fi
}
# Helper function for package_install_and_check
# to handle package installation with retry
function install_package_with_retry() {
local package="$1"
local manager="$2"
local retry_count=0
local max_retries=$MAX_INSTALL_RETRIES
local install_success=false
while [[ "$retry_count" -lt "$max_retries" && "$install_success" == false ]]; do
retry_count=$((retry_count + 1))
if [[ "$manager" == "pacman" ]]; then
check_and_delete "$TERMUX_PREFIX/var/lib/pacman/db.lck"
print_msg "Installing Package : ${C}$package"
pacman -S --noconfirm "$package" 2>/dev/null
if pacman -Qi "$package" >/dev/null 2>&1; then
print_success "Successfully install package: ${C}$package"
install_success=true
else
print_warn "Failed to install package: ${C}${package}. ${Y}Trying again... ($retry_count)"
fi
else
print_msg "Installing package: ${C}$package"
apt install "$package" -y 2>/dev/null
if dpkg -s "$package" >/dev/null 2>&1; then
print_success "Successfully install package: ${C}$package"
install_success=true
else
print_warn "Failed to install package: ${C}${package}. ${Y}Trying again... ($retry_count)"
# Only run recovery commands on failure
dpkg --configure -a 2>/dev/null
apt --fix-broken install -y 2>/dev/null
apt install --fix-missing -y 2>/dev/null
fi
fi
done
}
# will check the package is installed or not then remove it
function package_check_and_remove() {
log_debug "Starting package removal for: $*"
local packs_list
# Properly splits on spaces
IFS=' ' read -r -a packs_list <<<"$*"
if [[ "$PACKAGE_MANAGER" == "pacman" ]]; then
for package_name in "${packs_list[@]}"; do
if [[ "$package_name" == *"*"* ]]; then
log_debug "Processing wildcard pattern: $package_name"
local packages
packages=$(pacman -Qsq 2>/dev/null | grep -E "^${package_name//\*/.*}$")
if [[ -z "$packages" ]]; then
print_success "No installed packages found matching: ${C}$package_name"
continue
fi
log_debug "matched packages: $packages"
for package in $packages; do
remove_package_with_retry "$package" "pacman"
done
else
# Check if package is installed before attempting removal
if pacman -Qi "$package_name" >/dev/null 2>&1; then
remove_package_with_retry "$package_name" "pacman"
else
print_success "Package ${C}$package_name${G} is not installed. Skipping...${NC}"
fi
fi
done
else
for package_name in "${packs_list[@]}"; do
if [[ "$package_name" == *"*"* ]]; then
log_debug "Processing wildcard pattern: $package_name"
local packages
packages=$(dpkg-query -W -f='${binary:Package}\n' "${package_name}" 2>/dev/null | grep -v ' ')
if [[ -z "$packages" ]]; then
print_success "No installed packages found matching: ${C}$package_name"
continue
fi
log_debug "matched packages: $packages"
for package in $packages; do
remove_package_with_retry "$package" "apt"
done
else
if dpkg -s "$package_name" >/dev/null 2>&1; then
remove_package_with_retry "$package_name" "apt"
else
print_success "Package ${C}$package_name${G} is not installed. Skipping...${NC}"
fi
fi
done
fi
}
# Helper function for package_check_and_remove
# to handle package removal with retry
function remove_package_with_retry() {
local package="$1"
local manager="$2"
local retry_count=0
local max_retries=$MAX_INSTALL_RETRIES
local remove_success=false
while [[ "$retry_count" -lt "$max_retries" && "$remove_success" == false ]]; do
retry_count=$((retry_count + 1))
if [[ "$manager" == "pacman" ]]; then
check_and_delete "$TERMUX_PREFIX/var/lib/pacman/db.lck"
print_msg "Removing Package : ${C}$package"
pacman -Rnds --noconfirm "$package" 2>/dev/null
if ! pacman -Qi "$package" >/dev/null 2>&1; then
print_success "Successfully removed package: ${C}$package"
remove_success=true
else
print_warn "Failed to remove package: ${C}${package}. ${Y}Trying again...($retry_count)"
fi
else
print_msg "Removing package: ${C}$package"
dpkg --configure -a >/dev/null 2>&1
apt autoremove "$package" -y 2>/dev/null
if ! dpkg -s "$package" >/dev/null 2>&1; then
print_success "Successfully removed package: ${C}$package"
remove_success=true
else
print_warn "Failed to remove package: ${C}${package}. ${Y}Trying again...($retry_count)"
fi
fi
done
}
function get_file_name_number() {
local current_file
current_file=$(basename "$0")
local folder_name="${current_file%.sh}"
local theme_number
theme_number=$(echo "$folder_name" | grep -oE '[1-9][0-9]*')
log_debug "Theme number: $theme_number"
echo "$theme_number"
}
function extract_archive() {
local archive="$1"
if [[ ! -f "$archive" ]]; then
print_failed "$archive doesn't exist"
return 1
fi
case "$archive" in
*.tar.gz | *.tgz)
print_success "Extracting ${C}$archive"
tar xzvf "$archive" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.tar.xz)
print_success "Extracting ${C}$archive"
tar xJvf "$archive" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.tar.bz2 | *.tbz2)
print_success "Extracting ${C}$archive"
tar xjvf "$archive" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.tar)
print_success "Extracting ${C}$archive"
tar xvf "$archive" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.bz2)
print_success "Extracting ${C}$archive"
bunzip2 -v "$archive" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.gz)
print_success "Extracting ${C}$archive${NC}"
gunzip -v "$archive" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.7z)
print_success "Extracting ${C}$archive"
7z x "$archive" -y 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.zip)
print_success "Extracting ${C}$archive"
unzip "${archive}" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*.rar)
print_success "Extracting ${C}$archive"
unrar x "$archive" 2>/dev/null || {
print_failed "Failed to extract ${C}$archive"
return 1
}
;;
*)
print_failed "Unsupported archive format: ${C}$archive"
return 1
;;
esac
print_success "Successfully extracted ${C}$archive"
log_debug "Extracted: $archive"
}
# download a archive file and extract it in a folder
function download_and_extract() {
local url="$1"
local target_dir="$2"
local filename="${url##*/}"
if [[ -n "$target_dir" ]]; then
check_and_create_directory "$target_dir"
cd "$target_dir" || return 1
fi
if download_file "$filename" "$url"; then
if [[ -f "$filename" ]]; then
echo
extract_archive "$filename"
check_and_delete "$filename"
fi
else
print_failed "Failed to download ${C}${filename}"
print_msg "${C}Please check your internet connection"
fi
log_debug "Downloaded and extracted: $url to $target_dir"
}
count_subfolders() {
local owner="$1"
local repo="$2"
local path="$3"
local branch="$4"
local response
response=$(curl -s "https://api.github.com/repos/$owner/$repo/contents/$path?ref=$branch" 2>/dev/null)
# Use jq to extract directories and count them; if none found then set to 0
local subfolder_count
subfolder_count=$(echo "$response" | jq -r '[.[] | select(.type == "dir")] | length' 2>/dev/null)
echo "${subfolder_count:-0}"
log_debug "Subfolder count: ${subfolder_count:-0}"
}
# create a yes / no confirmation prompt
function confirmation_y_or_n() {
while true; do
# prompt
read -r -p "${R}[${C}-${R}]${Y} $1 ${Y}(y/n) ${NC}" response
# apply default
response="${response:-y}"
# lowercase
response="${response,,}"
# reject spaces or slashes
if [[ "$response" =~ [[:space:]/] ]]; then
echo
print_failed "Invalid input: no spaces or slashes allowed. Enter only 'y' or 'n'."
echo
continue
fi
# normalize full words to single letters
if [[ "$response" =~ ^(yes|y)$ ]]; then
response="y"
elif [[ "$response" =~ ^(no|n)$ ]]; then
response="n"
else
echo
print_failed "Invalid input. Please enter 'y', 'yes', 'n', or 'no'."
echo
continue
fi
# store in the caller's variable
eval "$2='$response'"
# handle it
case $response in
y)
echo
print_success "Continuing with answer: $response"
echo
sleep 0.2
break
;;
n)
echo
print_msg "${C}Skipping this step${NC}"
echo
sleep 0.2
break
;;
esac
done
log_debug "Confirmation: $1 - response: $response"
}
# get the latest version from a github releases
# ex. latest_tag=$(get_latest_release "$repo_owner" "$repo_name")
function get_latest_release() {
local repo_owner="$1"
local repo_name="$2"
curl --silent \
--location \
--retry 5 \
--retry-delay 1 \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${repo_owner}/${repo_name}/releases/latest" | jq -r '.tag_name'
}
function install_font_for_style() {
local style_number="$1"
print_msg "Installing Fonts..."
check_and_create_directory "$TERMUX_HOME/.fonts"
download_and_extract "$REPO_RAW_URL/refs/heads/$REPO_SETUP_FILE_BRANCH/$REPO_SETUP_FILES_FOLDER/$de_name/look_${style_number}/font.tar.gz" "$TERMUX_HOME/.fonts"
fc-cache -f 2>/dev/null
cd "$TERMUX_HOME" || return 1
}
# Use:- select_an_option 8 1 variable_name
function select_an_option() {
local max_options=$1
local default_option=${2:-1}
local response_var=$3
local response
while true; do
read -r -p "${Y}select an option (Default ${default_option}): ${NC}" response
response=${response:-$default_option}
if [[ $response =~ ^[0-9]+$ ]] && ((response >= 1 && response <= max_options)); then
echo
print_success "Continuing with answer: $response"
sleep 0.2
eval "$response_var=$response"
break
else
echo
print_failed " Invalid input, Please enter a number between 1 and $max_options"
fi
done
}
function read_conf() {
if [[ ! -f "$CONFIG_FILE" ]]; then
print_failed " Configuration file $CONFIG_FILE not found"
exit 0
fi
# shellcheck disable=SC1090
source "$CONFIG_FILE"
print_success "Configuration variables loaded"
validate_required_vars
}
# Use atomic write pattern to prevent background execution issues
function print_to_config() {
local var_name="$1"
local var_value="${2:-${!var_name}}"
local IFS=$' \t\n'
local temp_file="${CONFIG_FILE}.tmp.$$"
# Ensure config file exists
if [[ ! -f "$CONFIG_FILE" ]]; then
touch "$CONFIG_FILE" 2>/dev/null || {
log_error "Cannot access $CONFIG_FILE"
return 1
}
fi
if grep -q "^${var_name}=" "$CONFIG_FILE" 2>/dev/null; then
# Atomic write: write to temp file then move
sed "s|^${var_name}=.*|${var_name}=${var_value}|" "$CONFIG_FILE" >"$temp_file" && mv "$temp_file" "$CONFIG_FILE"
else
echo "${var_name}=${var_value}" >>"$CONFIG_FILE"
fi
log_debug "$var_name = $var_value"
}
function validate_required_vars() {
local required_vars=(
# Basic system variables
"HOME"
"PREFIX"
"TMPDIR"
"PACKAGE_MANAGER"
# Display and GUI variables
"display_number"
"gui_mode"
"de_name"
"de_startup"
"de_on_startup"
# Hardware acceleration variables
"enable_hw_acc"
# Configuration paths
"CONFIG_FILE"
"themes_folder"
"icons_folder"
# apps
"installed_browser"
"installed_ide"
"installed_media_player"
"installed_photo_editor"
"installed_wine"
# extra
"chosen_shell_name"
"terminal_utility_setup_answer"
"fm_tools"
# Distro
"distro_add_answer"
)
# If hardware acceleration is enabled, add required variables
if [[ "$enable_hw_acc" == "y" ]]; then
required_vars+=(
"GPU_NAME"
"exp_termux_vulkan_hw_answer"
"termux_hw_answer"
)
fi
# If a distro is enabled, add required variables
# shellcheck disable=SC2154
if [[ "$distro_add_answer" == "y" ]]; then
required_vars+=(
"selected_distro_type"
"selected_distro"
"pd_audio_config_answer"
"pd_useradd_answer"
)
fi
# shellcheck disable=SC2154
if [[ "$distro_add_answer" == "y" ]] && [[ "$pd_useradd_answer" == "y" ]]; then
required_vars+=(
"user_name"
)
fi
if [[ "$enable_hw_acc" == "y" ]] && [[ "$distro_add_answer" == "y" ]]; then
required_vars+=(
"pd_hw_answer"
)
fi
print_msg "Validating required configuration values..."
for var in "${required_vars[@]}"; do
if [[ -z "${!var}" ]]; then
print_msg "Missing variable: $var - attempting to set it..."
case "$var" in
"display_number")
display_number=0
;;
"gui_mode")
question_gui_mode
;;
"de_name")
question_select_de_from_list
set_desktop_variables
;;
"de_startup")
question_select_de_from_list
set_desktop_variables
;;
"de_on_startup")
question_de_on_startup
;;
"enable_hw_acc")
question_enable_hw_acc
;;
"GPU_NAME")
setup_device_gpu_model
;;
"exp_termux_vulkan_hw_answer")
exp_vulkan_support
;;
"termux_hw_answer")
if [[ "$exp_termux_vulkan_hw_answer" == "freedreno_kgsl" ]] || [[ "$exp_termux_vulkan_hw_answer" == "mesa_freedreno" ]] || [[ "$exp_termux_vulkan_hw_answer" == "mesa_zink_freedreno" ]]; then
termux_hw_answer="${exp_termux_vulkan_hw_answer}"
fi
quetion_termux_hw_answer
;;
"themes_folder")
question_select_de_from_list
set_desktop_variables
;;
"icons_folder")
question_select_de_from_list
set_desktop_variables
;;
"installed_browser")
question_install_browser
;;
"installed_ide")
question_install_ide
;;
"installed_media_player")
question_install_media_player
;;
"installed_photo_editor")
question_install_photo_editor
;;
"installed_wine")
question_install_wine
;;
"chosen_shell_name")
question_chosen_shell_name
;;
"terminal_utility_setup_answer")
question_terminal_utility_setup
;;
"fm_tools")
question_fm_tools
;;
"distro_add_answer")
question_distro_add
;;
"selected_distro_type")
distro_type_select
;;
"selected_distro")
choose_distro
;;
"pd_audio_config_answer")
question_pd_audio_config_answer
;;
"pd_useradd_answer")
question_pd_useradd_answer
;;
"user_name")
set_account_by_type
;;
"pd_hw_answer")
distro_hw_questions
;;
# importent variables - critical if missing
"HOME" | "PREFIX" | "TMPDIR" | "PACKAGE_MANAGER" | "CONFIG_FILE")
print_failed "Critical system variable $var is not set!"
exit 1
;;
*)
print_failed "Unknown variable $var - no handler defined"
log_debug "Unknown variable: $var"
exit 1
;;
esac
# Verify the variable was set after handling
if [[ -z "${!var}" ]]; then
print_failed "Failed to set variable: $var"