-
Notifications
You must be signed in to change notification settings - Fork 771
Expand file tree
/
Copy pathdistro-container-setup
More file actions
executable file
·2367 lines (2068 loc) · 85.4 KB
/
distro-container-setup
File metadata and controls
executable file
·2367 lines (2068 loc) · 85.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
#!/data/data/com.termux/files/usr/bin/bash
#
# This script is a module of the 'termux-desktop' project.
# It handles the setup of the chroot/proot containers.
#
# 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: setup the distro part of termux-desktop
# Repository : https://github.com/sabamdarif/termux-desktop
# shellcheck disable=SC2154
# shellcheck disable=SC2120
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
}
function setup_proot_distro() {
print_msg "${BOLD}Setting up ${C}proot-distro"
package_install_and_check "proot-distro"
banner
print_msg "${BOLD}Setting up Selected Linux Distro: ${C}${selected_distro}"
echo
print_to_config "selected_distro"
pd install "$selected_distro"
}
function check_su_access() {
if command -v su >/dev/null 2>&1; then
if su -c true >/dev/null 2>&1; then
return 0
fi
fi
return 1
}
function check_root_access() {
if ! check_su_access; then
return 1
fi
local whoami_out
if whoami_out=$(su -c 'whoami' 2>/dev/null) && [ "$whoami_out" = "root" ]; then
return 0
fi
return 1
}
function check_chroot_file() {
if ! check_su_access; then
return 1
fi
if su -c 'ls /system/bin/chroot-distro' >/dev/null 2>&1; then
return 0
fi
return 1
}
function setup_chroot_distro() {
print_msg "Checking if you have root access or not"
package_install_and_check "sudo"
local retry_count
local max_retries
retry_count=0
max_retries=3
while [ $retry_count -lt $max_retries ]; do
if check_root_access; then
print_success "Root access is enabled"
break
else
((retry_count++))
if [[ "$retry_count" -lt "$max_retries" ]]; then
print_failed "Please grant root access to termux"
print_msg "Want to check again (Attempt $retry_count/$max_retries)"
wait_for_keypress
else
print_failed "Failed to get root access after $max_retries attempts"
print_failed "Exiting..."
exit 1
fi
fi
done
print_msg "Checking access to chroot-distro"
retry_count=0
while [ $retry_count -lt $max_retries ]; do
if check_chroot_file; then
print_success "chroot-distro binary exist"
break
else
((retry_count++))
if [ $retry_count -lt $max_retries ]; then
print_failed "Cannot access /system/bin/chroot-distro file"
print_msg "Please flash:- ${B}https://github.com/sabamdarif/chroot-distro"
print_msg "Want to check again (Attempt $retry_count/$max_retries)"
wait_for_keypress
else
print_failed "Failed to access chroot-distro after $max_retries attempts"
print_msg "Exiting..."
exit 1
fi
fi
done
print_msg "Creating a chroot-distro launcher..."
cat <<-'EOF' >"$TERMUX_PREFIX/bin/chroot-distro"
#!/data/data/com.termux/files/usr/bin/bash
function check_su_access() {
if command -v su >/dev/null 2>&1; then
if su -c true >/dev/null 2>&1; then
return 0
fi
fi
return 1
}
function check_chroot_file() {
if ! check_su_access; then
return 1
fi
if su -c 'ls /system/bin/chroot-distro' >/dev/null 2>&1; then
return 0
fi
return 1
}
function wait_for_keypress() {
read -n1 -s -r -p " Press any key to continue, CTRL+c to cancel..."
echo
}
function checkup() {
local retry_count
local max_retries
retry_count=0
max_retries=3
while [ $retry_count -lt $max_retries ]; do
if check_chroot_file; then
break
else
((retry_count++))
if [ $retry_count -lt $max_retries ]; then
echo "Cannot access /system/bin/chroot-distro file"
echo "Please flash:- ${B}https://github.com/sabamdarif/chroot-distro${NC}"
echo "Want to check again (Attempt $retry_count/$max_retries)"
wait_for_keypress
else
echo "Failed to access chroot-distro after $max_retries attempts"
echo "Exiting..."
exit 1
fi
fi
done
}
checkup
# Fix: Use printf to properly escape arguments and pass them to su
args=""
for arg in "$@"; do
# Escape single quotes in arguments and wrap each argument in single quotes
escaped_arg=$(printf '%s' "$arg" | sed "s/'/'\\\\''/g")
args="$args '$escaped_arg'"
done
su -c "/system/bin/chroot-distro $args"
EOF
chmod +x "$TERMUX_PREFIX/bin/chroot-distro"
chroot-distro install "$selected_distro"
print_to_config "selected_distro"
}
function set_distro_paths {
banner
print_msg "Setiing up distro related paths"
if [[ "$selected_distro_type" == "proot" ]]; then
distro_path="$TERMUX_PREFIX/var/lib/proot-distro/installed-rootfs/$selected_distro"
elif [[ "$selected_distro_type" == "chroot" ]]; then
distro_path="/data/local/chroot-distro/installed-rootfs/$selected_distro"
else
print_failed "Unable to determine what distro type to use"
print_msg "Fallback to:- proot-distro"
selected_distro_type=proot
distro_path="$TERMUX_PREFIX/var/lib/proot-distro/installed-rootfs/$selected_distro"
fi
save_path="$distro_path/root"
}
function setup_distro() {
banner
if [[ "$selected_distro_type" == "proot" ]]; then
setup_proot_distro
elif [[ "$selected_distro_type" == "chroot" ]]; then
setup_chroot_distro
fi
print_to_config "selected_distro_type"
}
#########################################################################
#
# Update Distro | Install Required Packages | Add User | Fix audio issue
#
#########################################################################
function pd_package_install_and_check() {
# Check for --root flag
local use_sudo=true
if [[ "$1" == "--root" ]]; then
use_sudo=false
shift
fi
# shellcheck disable=SC2206
packs_list=($@)
# Get distro id
# shellcheck disable=SC1091
if [ -f /etc/os-release ]; then
. /etc/os-release
fi
# Install package
for package_name in "${packs_list[@]}"; do
print_msg "${BOLD}Installing package: ${C}$package_name"
# Install package and handle errors
install_failed=false
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if $use_sudo; then
if ! sudo apt install "$package_name" -y; then
install_failed=true
fi
else
if ! apt install "$package_name" -y; then
install_failed=true
fi
fi
elif [[ "$ID" == arch* ]]; then
if $use_sudo; then
if ! sudo pacman -Sy --noconfirm "$package_name"; then
install_failed=true
fi
else
if ! pacman -Sy --noconfirm "$package_name"; then
install_failed=true
fi
fi
elif [[ "$ID" == "fedora" ]]; then
if $use_sudo; then
if ! sudo dnf install "$package_name" -y; then
install_failed=true
fi
else
if ! dnf install "$package_name" -y; then
install_failed=true
fi
fi
fi
# Handle error in installation
if $install_failed; then
print_failed "${BOLD} Error detected during installation of: ${C}$package_name${NC}"
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if $use_sudo; then
sudo apt --fix-broken install -y
sudo dpkg --configure -a
sudo apt install "$package_name" -y
else
apt --fix-broken install -y
dpkg --configure -a
apt install "$package_name" -y
fi
elif [[ "$ID" == arch* ]]; then
if $use_sudo; then
sudo pacman -Syu --noconfirm
sudo pacman -Sy --noconfirm "$package_name"
else
pacman -Syu --noconfirm
pacman -Sy --noconfirm "$package_name"
fi
elif [[ "$ID" == "fedora" ]]; then
if $use_sudo; then
sudo dnf --refresh install -y
sudo rpm --rebuilddb
sudo dnf install "$package_name" -y
else
dnf --refresh install -y
rpm --rebuilddb
dnf install "$package_name" -y
fi
fi
fi
# Reinstall if necessary
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if ! dpkg -s "$package_name" >/dev/null 2>&1; then
if $use_sudo; then
sudo apt install "$package_name" -y
else
apt install "$package_name" -y
fi
fi
elif [[ "$ID" == arch* ]]; then
if ! pacman -Qi "$package_name" >/dev/null 2>&1; then
if $use_sudo; then
sudo pacman -Sy --noconfirm "$package_name"
else
pacman -Sy --noconfirm "$package_name"
fi
fi
elif [[ "$ID" == "fedora" ]]; then
if ! rpm -q "$package_name" >/dev/null 2>&1; then
if $use_sudo; then
sudo dnf install "$package_name" -y
else
dnf install "$package_name" -y
fi
fi
fi
# Check installation
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if dpkg -s "$package_name" >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
else
if command -v wget &>/dev/null || ls "$TERMUX_PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
fi
fi
elif [[ "$ID" == arch* ]]; then
if pacman -Qi "$package_name" >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
else
if command -v wget &>/dev/null || ls "$TERMUX_PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
fi
fi
elif [[ "$ID" == "fedora" ]]; then
if rpm -q "$package_name" >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
else
if command -v wget &>/dev/null || ls "$TERMUX_PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
fi
fi
fi
done
echo ""
}
#############################
# Create Shell Setup Script #
#############################
function create_shell_script() {
local script_path="$1"
local shell_setup_content="$2"
# Create directory if it doesn't exist
if [[ "$selected_distro_type" == "chroot" ]]; then
sudo mkdir -p "$(dirname "$script_path")"
else
mkdir -p "$(dirname "$script_path")"
fi
if [[ "$selected_distro_type" == "chroot" ]]; then
sudo tee "$script_path" >/dev/null <<-EOF
#!/data/data/com.termux/files/usr/bin/bash
# Repository URLs
readonly REPO_OWNER="$REPO_OWNER"
readonly REPO_NAME="$REPO_NAME"
readonly REPO_BRANCH_MAIN="$REPO_BRANCH_MAIN"
readonly REPO_SETUP_FILE_BRANCH="$REPO_SETUP_FILE_BRANCH"
readonly REPO_SETUP_FILES_FOLDER="$REPO_SETUP_FILES_FOLDER"
readonly REPO_RAW_URL="$REPO_RAW_URL"
readonly TERMUX_HOME="$TERMUX_HOME"
readonly TERMUX_PREFIX="$TERMUX_PREFIX"
# Retry configuration
readonly MAX_DOWNLOAD_RETRIES="$MAX_DOWNLOAD_RETRIES"
readonly MAX_INSTALL_RETRIES="$MAX_INSTALL_RETRIES"
readonly DOWNLOAD_TIMEOUT="$DOWNLOAD_TIMEOUT"
#########################################################################
#
# Initial Values
#
#########################################################################
readonly TERMUX_DESKTOP_PATH="$TERMUX_DESKTOP_PATH"
readonly CONFIG_FILE="$CONFIG_FILE"
readonly LOG_FILE="$LOG_FILE"
LITE_MODE="$LITE_MODE"
#########################################################################
#
# Color Setup
#
#########################################################################
# shellcheck disable=SC2154
R="$R"
G="$G"
Y="$Y"
B="$B"
C="$C"
NC="$NC"
BOLD="$BOLD"
EOF
else
cat <<-EOF >"$script_path"
#!/data/data/com.termux/files/usr/bin/bash
# Repository URLs
readonly REPO_OWNER="$REPO_OWNER"
readonly REPO_NAME="$REPO_NAME"
readonly REPO_BRANCH_MAIN="$REPO_BRANCH_MAIN"
readonly REPO_SETUP_FILE_BRANCH="$REPO_SETUP_FILE_BRANCH"
readonly REPO_SETUP_FILES_FOLDER="$REPO_SETUP_FILES_FOLDER"
readonly REPO_RAW_URL="$REPO_RAW_URL"
readonly TERMUX_HOME="$TERMUX_HOME"
readonly TERMUX_PREFIX="$TERMUX_PREFIX"
# Retry configuration
readonly MAX_DOWNLOAD_RETRIES="$MAX_DOWNLOAD_RETRIES"
readonly MAX_INSTALL_RETRIES="$MAX_INSTALL_RETRIES"
readonly DOWNLOAD_TIMEOUT="$DOWNLOAD_TIMEOUT"
#########################################################################
#
# Initial Values
#
#########################################################################
readonly TERMUX_DESKTOP_PATH="$TERMUX_DESKTOP_PATH"
readonly CONFIG_FILE="$CONFIG_FILE"
readonly LOG_FILE="$LOG_FILE"
LITE_MODE="$LITE_MODE"
#########################################################################
#
# Color Setup
#
#########################################################################
# shellcheck disable=SC2154
R="$R"
G="$G"
Y="$Y"
B="$B"
C="$C"
NC="$NC"
BOLD="$BOLD"
EOF
fi
if [[ "$selected_distro_type" == "chroot" ]]; then
typeset -f check_termux log_debug print_success print_failed print_warn print_msg wait_for_keypress check_and_create_directory check_and_delete check_and_backup download_file check_and_restore detect_package_manager package_install_and_check install_package_with_retry package_check_and_remove remove_package_with_retry get_file_name_number extract_zip_with_progress extract_archive download_and_extract count_subfolders confirmation_y_or_n get_latest_release install_font_for_style select_an_option read_conf print_to_config pd_package_install_and_check set_distro_paths setup_distro | sudo tee -a "$script_path" >/dev/null
echo "$shell_setup_content" | sudo tee -a "$script_path" >/dev/null
else
typeset -f check_termux log_debug print_success print_failed print_warn print_msg wait_for_keypress check_and_create_directory check_and_delete check_and_backup download_file check_and_restore detect_package_manager package_install_and_check install_package_with_retry package_check_and_remove remove_package_with_retry get_file_name_number extract_zip_with_progress extract_archive download_and_extract count_subfolders confirmation_y_or_n get_latest_release install_font_for_style select_an_option read_conf print_to_config pd_package_install_and_check set_distro_paths setup_distro >>"$script_path"
echo "$shell_setup_content" >>"$script_path"
fi
# Make script executable
if [[ "$selected_distro_type" == "chroot" ]]; then
sudo chmod +x "$script_path"
else
chmod +x "$script_path"
fi
# Verify script was created
if [[ "$selected_distro_type" == "chroot" ]]; then
if ! su -c "ls '$script_path'" >/dev/null 2>&1; then
print_failed "Failed to create script at $script_path"
fi
else
if [[ ! -f "$script_path" ]]; then
print_failed "Failed to create script at $script_path"
fi
fi
}
#################
# Update distro #
#################
function update_distro_repo() {
print_msg "${BOLD}Updating $selected_distro And Installing Required Packages"
echo
if [[ "$selected_distro" == "debian" ]] || [[ "$selected_distro" == "ubuntu" ]]; then
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" apt update
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" apt upgrade -y -o Dpkg::Options::="--force-confnew"
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" apt install sudo apt-file -y
elif [[ "$selected_distro" == "archlinux" ]]; then
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" pacman -Syu --noconfirm
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" pacman -S --noconfirm sudo
elif [[ "$selected_distro" == "fedora" ]]; then
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/bash -c "echo 'max_parallel_downloads=10' >> '/etc/dnf/dnf.conf'"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/bash -c "echo 'fastestmirror=True/' >> '/etc/dnf/dnf.conf'"
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" dnf update -y
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" dnf upgrade -y
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" dnf install sudo -y
fi
if [[ "$selected_distro_type" == "proot" ]]; then
chmod -R +w "$distro_path/"
fi
}
###################
# Setup time zone #
###################
function set_distro_time_zone() {
timezone=$(getprop persist.sys.timezone)
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" rm /etc/localtime
"${selected_distro_type}"-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:"$display_number" cp "/usr/share/zoneinfo/$timezone" /etc/localtime
}
###################
# Fix Sound Issue #
###################
function distro_fix_sound_issue() {
banner
print_msg "${BOLD}Fixing ${selected_distro_type} distro Sound Problem..."
echo
audio_related_packs="pulseaudio"
install_audio_related_packs=$(
cat <<-EOF
pd_package_install_and_check "$audio_related_packs"
check_and_delete "install_audio_related_packs.sh"
EOF
)
create_shell_script "$save_path/install_audio_related_packs.sh" "$install_audio_related_packs"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/bash -c 'bash /root/install_audio_related_packs.sh'
if [[ "$selected_distro_type" == "chroot" ]]; then
echo "export PULSE_SERVER=127.0.0.1" | sudo tee -a "$distro_path/etc/profile" >/dev/null
else
echo "export PULSE_SERVER=127.0.0.1" >>"$distro_path/etc/profile"
fi
print_to_config "pd_audio_config_answer"
}
###########################################
# create a user account inside the distro #
###########################################
function distro_setup_user() {
# set user name value
if [[ "$pd_useradd_answer" == "n" ]]; then
final_user_name="root"
elif [[ "$pd_useradd_answer" == "y" ]]; then
final_user_name="${user_name}"
fi
print_to_config "pd_useradd_answer"
print_to_config "user_name"
# set password value
if [[ "$pd_pass_type" == "2" ]]; then
final_pass="${pass}"
else
final_pass="root"
fi
print_to_config "pd_pass_type"
print_to_config "pass"
if [[ "$pd_useradd_answer" == "y" ]]; then
# first setup the user
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c 'groupadd storage'
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c 'groupadd wheel'
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "useradd -m -g users -s \$(which bash) ${final_user_name}"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "usermod -aG wheel,polkitd,audio,video,storage ${final_user_name}"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "echo '${final_user_name}:${final_pass}' | chpasswd"
# setup user without password confirmation
if [[ "$pd_pass_type" == "1" ]]; then
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "chmod u+rw /etc/sudoers"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "echo '$final_user_name ALL=(ALL) NOPASSWD:ALL' | tee -a /etc/sudoers > /dev/null 2>&1"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "chmod u-w /etc/sudoers"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c 'sudo -k'
# setup user with password
elif [[ "$pd_pass_type" == "2" ]]; then
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "chmod u+rw /etc/sudoers"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "echo '$final_user_name ALL=(ALL:ALL) ALL' | tee -a /etc/sudoers > /dev/null 2>&1"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c "chmod u-w /etc/sudoers"
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/sh -c 'sudo -k'
fi
fi
}
function distro_basic_task() {
banner
update_distro_repo
set_distro_time_zone
distro_fix_sound_issue
distro_setup_user
}
#########################################################################
#
# Setup Zsh And Terminal Utility
#
#########################################################################
#############
# ZSH Setup #
#############
function distro_zsh_setup() {
if [[ "$chosen_shell_name" == "zsh" ]]; then
zsh_setup_content=$(
cat <<-EOF
pd_package_install_and_check "git zsh wget"
if [[ "$selected_distro" == "debian" ]] || [[ "$selected_distro" == "ubuntu" ]]; then
chsh -s /usr/bin/zsh ${final_user_name}
else
chsh -s /bin/zsh ${final_user_name}
fi
check_and_delete "pd_zsh_install.sh"
EOF
)
create_shell_script "$distro_path/root/pd_zsh_install.sh" "$zsh_setup_content"
banner
print_msg "${BOLD}Setting up shell for ${selected_distro}..."
echo
"${selected_distro_type}"-distro login "$selected_distro" -- /bin/bash -c 'bash /root/pd_zsh_install.sh'
fi
}
##########################
# Terminal Utility Setup #
##########################
function distro_terminal_utility_setup() {
if [[ "$terminal_utility_setup_answer" == "y" ]]; then
if [[ "$selected_distro" == "debian" ]] || [[ "$selected_distro" == "ubuntu" ]]; then
terminal_utility_content=$(
cat <<-'EOF'
pd_package_install_and_check "zoxide bat wget gpg fzf eza fastfetch nala"
check_and_delete "pd_setup.sh"
EOF
)
else
terminal_utility_content=$(
cat <<-'EOF'
pd_package_install_and_check "zoxide bat wget gpg eza fastfetch fzf"
check_and_delete "pd_setup.sh"
EOF
)
fi
create_shell_script "$save_path/pd_setup.sh" "$terminal_utility_content"
banner
print_msg "Configuring Terminal Utility For ${selected_distro}..."
echo
"${selected_distro_type}"-distro login --user "$final_user_name" "$selected_distro" -- /bin/bash -c 'bash /root/pd_setup.sh'
fi
}
#########################################################################
#
# Create App Installer Shortcut
#
#########################################################################
function add_installer_common() {
cat <<-EOF >"$TERMUX_PREFIX/bin/$selected_distro"
#!/data/data/com.termux/files/usr/bin/bash
source $TERMUX_PREFIX/etc/termux-desktop/common_functions
xhost + >/dev/null 2>&1
# --- USB auto-bind helpers ---
usb_mounts() {
# Candidate external filesystems under /mnt/media_rw/<UUID>
local candidates seen="" media uuid acc
candidates=\$(awk '(\$2 ~ "^/mnt/media_rw/[0-9A-Fa-f-]+\$") && (\$3 ~ /vfat|exfat|ntfs|fuseblk/) {print \$2}' /proc/mounts | sort -u)
while IFS= read -r media; do
[ -d "\$media" ] || continue
uuid="\${media##*/}"
acc="/storage/\${uuid}"; [ -r "\$acc" ] || acc="\$media"
[ -r "\$acc" ] || continue
case ",\$seen," in *",\$uuid,"*) ;; *) printf '%s\n' "\$acc"; seen="\${seen},\${uuid}";; esac
done <<< "\$candidates"
}
urldecode() { local s="\${1//+/ }"; printf '%b' "\${s//%/\\x}"; }
normalize_arg() {
# Decode file:// and %XX to the real path (do not rewrite the path itself)
local a="$1"
case "\$a" in file://*) a="\${a#file://}";; esac
if [[ "\$a" == *%* || "\$a" == *+* ]]; then a="\$(urldecode "\$a")"; fi
echo "\$a"
}
enum_media=0
usb_bind_args() {
if [ -n "\${PDRUN_DISABLE_USB_BIND:-}" ]; then printf ''; return 0; fi
local args="--bind /storage:/storage --bind /mnt/media_rw:/mnt/media_rw"
# Convenience bind (/mnt/usb/<UUID>)
local list; if [ -n "\${PDRUN_USB_BIND_PATH:-}" ]; then list="\${PDRUN_USB_BIND_PATH}"; else list="\$(usb_mounts 2>/dev/null)"; fi
if [ -n "\$list" ]; then
local include="\${PDRUN_USB_INCLUDE:-}"; local exclude="\${PDRUN_USB_EXCLUDE:-}"; local limit="\${PDRUN_USB_LIMIT:-0}"
local count=0
while IFS= read -r p; do
[ -n "\$p" ] || continue
local uuid="\${p##*/}"
if [ -n "\$include" ]; then case ",\$include," in *",\$uuid,"*) ;; *) continue;; esac; fi
if [ -n "\$exclude" ]; then case ",\$exclude," in *",\$uuid,"*) continue;; esac; fi
if [ "\$limit" -gt 0 ] && [ "\$count" -ge "\$limit" ]; then break; fi
args="\${args} --bind \${p}:/mnt/usb/\${uuid}"
count=\$((count+1))
done <<< "\$list"
fi
# If needed: self-bind individual paths (to prevent /mnt/media_rw from being omitted in file dialogs)
if [ "\${enum_media:-0}" -eq 1 ] || [[ "\${PDRUN_MEDIA_RW_ENUM:-}" =~ ^(1|true|yes)$ ]]; then
while IFS= read -r p; do
[ -n "\$p" ] || continue
args="\${args} --bind \${p}:\${p}" # /mnt/media_rw/<UUID>
uuid="\${p##*/}"; st="/storage/\${uuid}"
[ -r "\$st" ] && args="\${args} --bind \${st}:\${st}" # /storage/<UUID>
done <<<"\$(usb_mounts 2>/dev/null | sed -E 's|^/storage/([0-9A-Fa-f-]+)$|/mnt/media_rw/\\1|')"
fi
printf '%s' "\$args"
}
if [ "\$#" -eq 0 ]; then
if [ "\$PWD" = "$TERMUX_HOME" ]; then
${selected_distro_type}-distro login \$(usb_bind_args) --user "$final_user_name" "$selected_distro" --shared-tmp $termux_home_link
else
${selected_distro_type}-distro login \$(usb_bind_args) --user "$final_user_name" "$selected_distro" --shared-tmp --work-dir "\$PWD" $termux_home_link
fi
check_and_create_directory "$TERMUX_PREFIX/share/applications/pd_added"
fi
EOF
}
#######################################################
################ Debian Distro Command ################
#######################################################
function proot_distro_debian_based_app_installer() {
cat <<-TOP_EOF >>"$TERMUX_PREFIX/bin/$selected_distro"
package_install_and_add_to_menu() {
#################################################
## Handle install package and add to menu part ##
#################################################
# Create the packinstall.sh script
cat <<'EOF' >"${save_root_path}/packinstall.sh"
#!/bin/bash
source $TERMUX_PREFIX/etc/termux-desktop/common_functions
# First configure dpkg and update apt
dpkg --configure -a
apt update
apt-file update
# Install the packages - get arguments directly
apt install "\$@"
# Process desktop files for non-option arguments
for arg in "\$@"; do
# Skip options (starting with -)
if [[ "\$arg" != -* ]]; then
package_name="\$arg"
# Find desktop files for this package
echo "\${R}[\${C}-\${R}]\${G} Searchling for .desktop file… \${W}"
echo
desktop_files=\$(dpkg-query -W -f='\${binary:Package}\n' | grep "^\$package_name\(-.*\)\?\$" | xargs dpkg-query -L | grep "^/usr/share/applications/.*\.desktop\$")
# If not found
if [ -z "\$desktop_files" ]; then
if ! apt-file list "\$package_name" >/dev/null 2>&1; then
echo "\${R}[\${C}-\${R}]\${G} Updating apt-file cache… \${W}"
apt-file update
fi
if command -v apt-file >/dev/null; then
desktop_files=\$(apt-file list "\$package_name" | awk -F': ' -v pkg="\$package_name" '\$1 == pkg && \$2 ~ /\.desktop\$/ { print \$2 }')
fi
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package \${C} \$package_name \${R}/usr/share/applications."
fi
else
for desktop_files_name in \$desktop_files; do
desktop_files_with_ext=\$(basename "\$desktop_files_name")
desktop_files_without_ext="\${desktop_files_with_ext%.desktop}"
if dpkg -s "\$package_name" >/dev/null 2>&1; then
print_success "Adding \${C}\${desktop_files_without_ext} \${G}To Termux Menu"
cp "/usr/share/applications/\${desktop_files_with_ext}" "$TERMUX_PREFIX/share/applications/pd_added/"
sed -i 's/Exec=/Exec=pdrun /g' "$TERMUX_PREFIX/share/applications/pd_added/\${desktop_files_with_ext}"
update-desktop-database $TERMUX_PREFIX/share/applications
fi
done
fi
fi
done
check_and_delete 'packinstall.sh'
EOF
# Make the script executable
proot-distro login "${selected_distro}" --shared-tmp -- /bin/bash -c "chmod +x /root/packinstall.sh"
# Execute the script with all arguments
proot-distro login "${selected_distro}" --shared-tmp -- /bin/bash -c "export DISPLAY=:${display_number}; export XDG_RUNTIME_DIR=${TMPDIR}; bash /root/packinstall.sh \$*"
}
package_uninstall_and_remove_from_menu() {
###################################################
# Handle remove package and remove from menu part #
###################################################
# Create the packremove.sh script
cat <<'EOF' >"${save_root_path}/packremove.sh"
#!/bin/bash
source $TERMUX_PREFIX/etc/termux-desktop/common_functions
# Get the removal command (remove or autoremove)
removal_command="\$1"
shift
# Process each non-option argument (package)
for arg in "\$@"; do
# Skip options
if [[ "\$arg" == -* ]]; then
continue
fi
package_name="\$arg"
desktop_files=""
# Find desktop files for this package
echo "\${R}[\${C}-\${R}]\${G} Searchling for .desktop file… \${W}"
echo
desktop_files=\$(dpkg-query -W -f='\${binary:Package}\n' | grep "^\$package_name\(-.*\)\?\$" | xargs dpkg-query -L | grep "^/usr/share/applications/.*\.desktop\$" || true)
# If not found
if [[ -z "\$desktop_files" ]]; then
if command -v apt-file >/dev/null; then
if ! apt-file list "\$package_name" >/dev/null 2>&1; then
echo "\${R}[\${C}-\${R}]\${G} Updating apt-file cache… \${W}"
apt-file update
fi
desktop_files=\$(apt-file list "\$package_name" | awk -F': ' -v pkg="\$package_name" '\$1 == pkg && \$2 ~ /\.desktop\$/ { print \$2 }')
fi
fi
# Remove the package
echo "\${R}[\${C}-\${R}]\${G} Removing packages...\${W}"
echo
apt "\$removal_command" "\$package_name"
# Handle desktop files
if [[ -z "\$desktop_files" ]]; then
print_failed "No .desktop files found for package \${C}\$package_name\${R} /usr/share/applications"
else
for desktop_files_name in \$desktop_files; do
desktop_files_with_ext=\$(basename "\$desktop_files_name")
desktop_files_without_ext="\${desktop_files_with_ext%.desktop}"
# Check if package is really uninstalled
if ! dpkg -s "\$package_name" >/dev/null 2>&1; then
print_success "Removing \${C}\${desktop_files_without_ext} \${G}From Termux Menu"
check_and_delete "$TERMUX_PREFIX/share/applications/pd_added/\${desktop_files_with_ext}"
update-desktop-database $TERMUX_PREFIX/share/applications
else
print_failed "Package \${C}\$package_name\${R} is still installed; skipping desktop removal."
fi
done
fi
done
check_and_delete 'packremove.sh'
EOF
# Make the script executable
proot-distro login "${selected_distro}" --shared-tmp -- /bin/bash -c "chmod +x /root/packremove.sh"
# Execute the script with all arguments
proot-distro login "${selected_distro}" --shared-tmp -- /bin/bash -c "export DISPLAY=:${display_number}; export XDG_RUNTIME_DIR=$TERMUX_PREFIX/tmp; bash /root/packremove.sh \$1 \${*:2}"
}
update_distro() {
#################################################
############### Handle update part ##############
#################################################
proot-distro login --user "$final_user_name" "$selected_distro" --shared-tmp --termux-home -- env DISPLAY=:"$display_number" sudo apt update
proot-distro login --user "$final_user_name" "$selected_distro" --shared-tmp --termux-home -- env DISPLAY=:"$display_number" sudo apt-file update
}
TOP_EOF
}
function chroot_distro_debian_based_app_installer() {
cat <<-TOP_EOF >>"$TERMUX_PREFIX/bin/$selected_distro"
package_install_and_add_to_menu() {
#################################################
## Handle install package and add to menu part ##
#################################################
# Create the packinstall.sh script
sudo tee "${save_root_path}/packinstall.sh" >/dev/null <<'EOF'
#!/bin/bash
source $TERMUX_PREFIX/etc/termux-desktop/common_functions
# First configure dpkg and update apt
dpkg --configure -a
apt update
apt-file update
# Install the packages - get arguments directly
apt install "\$@"
# Process desktop files for non-option arguments
for arg in "\$@"; do
# Skip options (starting with -)
if [[ "\$arg" != -* ]]; then
package_name="\$arg"
# Find desktop files for this package
echo "\${R}[\${C}-\${R}]\${G} Searchling for .desktop file… \${W}"
echo
desktop_files=\$(dpkg-query -W -f='\${binary:Package}\n' | grep "^\$package_name\(-.*\)\?\$" | xargs dpkg-query -L | grep "^/usr/share/applications/.*\.desktop\$")
# If not found
if [ -z "\$desktop_files" ]; then
if ! apt-file list "\$package_name" >/dev/null 2>&1; then
echo "\${R}[\${C}-\${R}]\${G} Updating apt-file cache… \${W}"
apt-file update
fi
if command -v apt-file >/dev/null; then
desktop_files=\$(apt-file list "\$package_name" | awk -F': ' -v pkg="\$package_name" '\$1 == pkg && \$2 ~ /\.desktop\$/ { print \$2 }')
fi
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package \${C} \$package_name \${R}/usr/share/applications."
fi
else
for desktop_files_name in \$desktop_files; do
desktop_files_with_ext=\$(basename "\$desktop_files_name")
desktop_files_without_ext="\${desktop_files_with_ext%.desktop}"
if dpkg -s "\$package_name" >/dev/null 2>&1; then
print_success "Adding \${C}\${desktop_files_without_ext} \${G}To Termux Menu"
cp "/usr/share/applications/\${desktop_files_with_ext}" "$TERMUX_PREFIX/share/applications/pd_added/"
sed -i 's/Exec=/Exec=pdrun /g' "$TERMUX_PREFIX/share/applications/pd_added/\${desktop_files_with_ext}"
update-desktop-database $TERMUX_PREFIX/share/applications
fi
done
fi
fi
done
check_and_delete 'packinstall.sh'
EOF
# Make the script executable
chroot-distro login "${selected_distro}" --shared-tmp -- /bin/bash -c "chmod +x /root/packinstall.sh"
# Execute the script with all arguments
chroot-distro login "${selected_distro}" --shared-tmp -- /bin/bash -c "export DISPLAY=:${display_number}; export XDG_RUNTIME_DIR=${TMPDIR}; bash /root/packinstall.sh \$*"
}
package_uninstall_and_remove_from_menu() {
###################################################
# Handle remove package and remove from menu part #
###################################################
# Create the packremove.sh script
sudo tee "${save_root_path}/packremove.sh" >/dev/null <<'EOF'
#!/bin/bash
source $TERMUX_PREFIX/etc/termux-desktop/common_functions
# Get the removal command (remove or autoremove)
removal_command="\$1"