-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdistro-container-setup
executable file
·1224 lines (1073 loc) · 43.9 KB
/
distro-container-setup
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
function check_termux() {
if [[ $HOME != *termux* ]]; then
echo "${R}[${R}☓${R}]${R}${BOLD}Please run it inside termux${W}"
exit 0
fi
}
call_from_change_pd="n"
function setup_distro() {
banner
echo "${R}[${C}-${R}]${G}${BOLD} Setting up ${C}proot-distro${W}"
echo
update_sys
package_install_and_check "proot-distro"
banner
echo "${R}[${C}-${R}]${G}${BOLD} Setting up Selected Linux Distro: ${C}${selected_distro}${W}"
echo
pd install "$selected_distro"
distro_path="$PREFIX/var/lib/proot-distro/installed-rootfs/$selected_distro"
}
#########################################################################
#
# Fix Sound Issue
#
#########################################################################
function distro_fix_sound_issue() {
banner
echo "${R}[${C}-${R}]${G}${BOLD} Fixing Proot Distro Sound Problem...${W}"
echo
echo "export PULSE_SERVER=127.0.0.1" >> "$distro_path/etc/profile"
cat <<EOF > "$HOME/.pd-sound-service"
pulseaudio --start --exit-idle-time=-1
pacmd load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1
EOF
}
#########################################################################
#
# Update Distro | Install Required Packages | Add User
#
#########################################################################
function pd_package_install_and_check() {
packs_list=($@)
# Get distro id
if [ -f /etc/os-release ]; then
. /etc/os-release
fi
# Install package
for package_name in "${packs_list[@]}"; do
echo "${R}[${C}-${R}]${G}${BOLD} Installing package: ${C}$package_name ${W}"
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
sudo apt install "$package_name" -y
elif [[ "$ID" == arch* ]]; then
sudo pacman -Sy --noconfirm "$package_name"
elif [[ "$ID" == "alpine" ]]; then
sudo apk add "$package_name"
elif [[ "$ID" == "fedora" ]]; then
sudo dnf install "$package_name" -y
fi
# Handle error in installation
if [ $? -ne 0 ]; then
print_failed "${BOLD} Error detected during installation of: ${C}$package_name"
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
sudo apt --fix-broken install -y
sudo dpkg --configure -a
sudo apt install "$package_name" -y
elif [[ "$ID" == arch* ]]; then
sudo pacman -Syu --noconfirm
sudo pacman -Sy --noconfirm "$package_name"
elif [[ "$ID" == "alpine" ]]; then
sudo apk fix
sudo apk add "$package_name"
elif [[ "$ID" == "fedora" ]]; then
sudo dnf --refresh install -y
sudo rpm --rebuilddb
sudo dnf install "$package_name" -y
fi
fi
# Reinstall if necessary
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if ! dpkg -s "$package_name" >/dev/null 2>&1; then
sudo apt install "$package_name" -y
fi
elif [[ "$ID" == arch* ]]; then
if ! pacman -Qi "$package_name" >/dev/null 2>&1; then
sudo pacman -Sy --noconfirm "$package_name"
fi
elif [[ "$ID" == "alpine" ]]; then
if ! apk info -e "$package_name"; then
sudo apk add "$package_name"
fi
elif [[ "$ID" == "fedora" ]]; then
if ! rpm -q "$package_name" >/dev/null 2>&1; then
sudo dnf install "$package_name" -y
fi
fi
# Check installation
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if dpkg -s "$package_name" >/dev/null 2>&1; then
print_success "$package_name installed successfully"
else
if type -p "$package_name" &>/dev/null || ls "$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 installed successfully"
else
if type -p "$package_name" &>/dev/null || ls "$PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
fi
fi
elif [[ "$ID" == "alpine" ]]; then
if apk info -e "$package_name"; then
print_success "$package_name installed success"
else
if type -p "$package_name" &>/dev/null || ls "$PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed success"
fi
fi
elif [[ "$ID" == "fedora" ]]; then
if rpm -q "$package_name" >/dev/null 2>&1; then
print_success "$package_name installed success"
else
if type -p "$package_name" &>/dev/null || ls "$PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed success"
fi
fi
fi
done
echo ""
}
function pd_root_package_install_and_check() {
local packs_list
mapfile -d ' ' -t packs_list <<< "$1"
# Get distro id
if [ -f /etc/os-release ]; then
. /etc/os-release
fi
# Install package
for package_name in "${packs_list[@]}"; do
echo "${R}[${C}-${R}]${G}${BOLD} Installing package: ${C}$package_name ${W}"
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
apt install "$package_name" -y
elif [[ "$ID" == arch* ]]; then
pacman -Sy --noconfirm "$package_name"
elif [[ "$ID" == "alpine" ]]; then
apk add "$package_name"
elif [[ "$ID" == "fedora" ]]; then
dnf install "$package_name" -y
fi
# Handle error in installation
if [ $? -ne 0 ]; then
print_failed "${BOLD} Error detected during installation of: ${C}$package_name"
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
apt --fix-broken install -y
dpkg --configure -a
apt install "$package_name" -y
elif [[ "$ID" == arch* ]]; then
pacman -Syu --noconfirm
pacman -Sy --noconfirm "$package_name"
elif [[ "$ID" == "alpine" ]]; then
apk fix
apk add "$package_name"
elif [[ "$ID" == "fedora" ]]; then
dnf --refresh install -y
rpm --rebuilddb
dnf install "$package_name" -y
fi
fi
# Reinstall if necessary
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if ! dpkg -s "$package_name" >/dev/null 2>&1; then
apt install "$package_name" -y
fi
elif [[ "$ID" == arch* ]]; then
if ! pacman -Qi "$package_name" >/dev/null 2>&1; then
pacman -Sy --noconfirm "$package_name"
fi
elif [[ "$ID" == "alpine" ]]; then
if ! apk info -e "$package_name"; then
apk add "$package_name"
fi
elif [[ "$ID" == "fedora" ]]; then
if ! rpm -q "$package_name" >/dev/null 2>&1; then
dnf install "$package_name" -y
fi
fi
# Check installation
if [[ "$ID" == "debian" ]] || [[ "$ID" == "ubuntu" ]]; then
if dpkg -s "$package_name" >/dev/null 2>&1; then
print_success "$package_name installed successfully"
else
if type -p "$package_name" &>/dev/null || ls "$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 installed successfully ${W}"
else
if type -p "$package_name" &>/dev/null || ls "$PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
fi
fi
elif [[ "$ID" == "alpine" ]]; then
if apk info -e "$package_name"; then
print_success "$package_name installed successfully ${W}"
else
if type -p "$package_name" &>/dev/null || ls "$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 installed successfully ${W}"
else
if type -p "$package_name" &>/dev/null || ls "$PREFIX/bin/"*"$package_name"* >/dev/null 2>&1; then
print_success "$package_name ${G}installed successfully"
fi
fi
fi
done
echo ""
}
function distro_basic_task() {
banner
echo "${R}[${C}-${R}]${G}${BOLD} Updating ${C}$selected_distro ${G}And Installing Required Packages${W}"
echo
if [[ "$selected_distro" == "debian" ]] || [[ "$selected_distro" == "ubuntu" ]]; then
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 apt update
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 apt upgrade -y
elif [[ "$selected_distro" == "archlinux" ]]; then
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 pacman -Syu --noconfirm
elif [[ "$selected_distro" == "alpine" ]]; then
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 apk update
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 apk upgrade
elif [[ "$selected_distro" == "fedora" ]]; then
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 dnf update -y
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 dnf upgrade -y
fi
chmod -R +w "$distro_path/"
# Set proot timezone
timezone=$(getprop persist.sys.timezone)
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 rm /etc/localtime
proot-distro login "$selected_distro" --shared-tmp -- env DISPLAY=:0 cp "/usr/share/zoneinfo/$timezone" /etc/localtime
if [[ "$pd_audio_config_answer" == "y" ]]; then
distro_packs="sudo pulseaudio"
distro_fix_sound_issue
elif [[ "$pd_audio_config_answer" == "n" ]]; then
distro_packs="sudo"
fi
echo "pd_audio_config_answer=\"${pd_audio_config_answer}\"" >> "$config_file"
cat <<'EOF' > "$distro_path/root/pd_basic_setup.sh"
#!/bin/bash
R="$(printf '\033[1;31m')"
G="$(printf '\033[1;32m')"
Y="$(printf '\033[1;33m')"
B="$(printf '\033[1;34m')"
C="$(printf '\033[1;36m')"
W="$(printf '\033[0m')"
BOLD="$(printf '\033[1m')"
EOF
typeset -f banner pd_root_package_install_and_check >> "$distro_path/root/pd_basic_setup.sh"
cat <<EOF >> "$distro_path/root/pd_basic_setup.sh"
banner
pd_root_package_install_and_check "${distro_packs}"
rm pd_basic_setup.sh
EOF
proot-distro login "$selected_distro" -- /bin/bash -c 'bash pd_basic_setup.sh'
if [[ "$pd_useradd_answer" == "n" ]]; then
final_user_name="root"
elif [[ "$pd_useradd_answer" == "y" ]]; then
final_user_name="${user_name}"
fi
echo "final_user_name=\"$final_user_name\"" >> "$config_file"
if [[ "$pd_pass_type" == "2" ]]; then
final_pass="${pass}"
else
final_pass="root"
fi
if [[ "$pd_useradd_answer" == "y" ]]; then
if [[ "$selected_distro" == "alpine" ]]; then
cat <<EOF > "$distro_path/root/useradd.sh"
#!/bin/bash
groupadd storage
groupadd wheel
mkdir -p /home/$final_user_name
adduser -h /home/$final_user_name -s \$(which bash) -G users -D $final_user_name
chown ${final_user_name}:users /home/$final_user_name
echo "${final_user_name}:${final_pass}" | chpasswd
rm useradd.sh
EOF
else
cat <<EOF > "$distro_path/root/useradd.sh"
#!/bin/bash
groupadd storage
groupadd wheel
useradd -m -g users -s \$(which bash) ${final_user_name}
usermod -aG wheel,polkitd,audio,video,storage ${final_user_name}
echo "${final_user_name}:${final_pass}" | chpasswd
rm useradd.sh
EOF
fi
proot-distro login "$selected_distro" -- /bin/sh -c 'bash useradd.sh'
if [[ "$pd_pass_type" == "1" ]];then
cat <<EOF > "$distro_path/root/user_permission.sh"
#!/bin/bash
chmod u+rw /etc/sudoers
echo "$final_user_name ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers > /dev/null 2>&1
chmod u-w /etc/sudoers
rm user_permission.sh
EOF
elif [[ "$pd_pass_type" == "2" ]];then
cat <<EOF > "$distro_path/root/user_permission.sh"
#!/bin/bash
chmod u+rw /etc/sudoers
echo "$final_user_name ALL=(ALL:ALL) ALL" | tee -a /etc/sudoers > /dev/null 2>&1
chmod u-w /etc/sudoers
rm user_permission.sh
EOF
fi
proot-distro login "$selected_distro" -- /bin/sh -c 'bash user_permission.sh'
proot-distro login "$selected_distro" -- /bin/sh -c 'sudo -k'
fi
}
#########################################################################
#
# Setup Zsh And Terminal Utility
#
#########################################################################
function assigne_value() {
if [[ "$pd_useradd_answer" == "y" ]]; then
save_path="$distro_path/home/$final_user_name"
elif [[ "$pd_useradd_answer" == "n" ]]; then
save_path="$distro_path/root"
fi
if [[ "$distro_zsh_answer" == "y" ]]; then
pd_shell_name="zsh"
else
pd_shell_name="bash"
fi
}
# Create Shell Setup Script
function create_shell_script() {
local script_path
script_path="$1"
local shell_setup_content
shell_setup_content="$2"
cat << 'EOF' > "$script_path"
#!/bin/bash
R="$(printf '\033[1;31m')"
G="$(printf '\033[1;32m')"
Y="$(printf '\033[1;33m')"
C="$(printf '\033[1;36m')"
W="$(printf '\033[0m')"
BOLD="$(printf '\033[1m')"
EOF
typeset -f banner print_log print_success print_failed check_and_delete get_latest_release pd_root_package_install_and_check pd_package_install_and_check >> "$script_path"
echo "$shell_setup_content" >> "$script_path"
}
function distro_zsh_answer() {
# ZSH Setup
if [[ "$distro_zsh_answer" == "y" ]]; then
zsh_setup_content=$(cat <<'EOF'
pd_package_install_and_check "git zsh wget"
wget --tries=5 --retry-connrefused https://raw.githubusercontent.com/sabamdarif/short-linux-scripts/main/install-zsh.sh && bash install-zsh.sh
check_and_delete "pd_zsh_install.sh"
EOF
)
create_shell_script "$save_path/pd_zsh_install.sh" "$zsh_setup_content"
banner
echo "${R}[${C}-${R}]${G}${BOLD} Setting up shell for ${selected_distro}...${W}"
echo
proot-distro login --user "$final_user_name" "$selected_distro" -- /bin/bash -c 'bash pd_zsh_install.sh'
else
echo "${R}[${C}-${R}]${C} Canceling ZSH setup...${W}"
fi
}
function distro_terminal_utility_setup() {
# Terminal Utility Setup
if [[ "$distro_terminal_utility_setup_answer" == "y" ]]; then
if [[ "$selected_distro" == "debian" ]] || [[ "$selected_distro" == "ubuntu" ]]; then
terminal_utility_content=$(cat <<'EOF'
pd_package_install_and_check "nala zoxide bat wget gpg"
device_arch=$(uname -m)
case "$device_arch" in
aarch64) archtype="aarch64" ;;
arm) archtype="armhf" ;;
amd64|x86_64) archtype="amd64" ;;
i*86|x86) archtype="i386" ;;
*) echo "${R} Unknown architecture${W}" ;;
esac
version=$(get_latest_release "fastfetch-cli" "fastfetch")
wget --tries=5 --retry-connrefused -O fastfetch.deb https://github.com/fastfetch-cli/fastfetch/releases/download/${version}/fastfetch-linux-${archtype}.deb
apt install ./fastfetch.deb -y
check_and_delete "fastfetch.deb"
mkdir -p /etc/apt/keyrings
wget --tries=5 --retry-connrefused -qO- https://raw.githubusercontent.com/eza-community/eza/main/deb.asc | sudo gpg --dearmor -o /etc/apt/keyrings/gierens.gpg
echo "deb [signed-by=/etc/apt/keyrings/gierens.gpg] http://deb.gierens.de stable main" | sudo tee /etc/apt/sources.list.d/gierens.list
chmod 644 /etc/apt/keyrings/gierens.gpg /etc/apt/sources.list.d/gierens.list
apt update
pd_package_install_and_check "eza"
check_and_delete "pd_setup.sh"
EOF
)
else
terminal_utility_content=$(cat <<'EOF'
pd_package_install_and_check "zoxide bat wget gpg eza fastfetch"
check_and_delete "pd_setup.sh"
EOF
)
fi
create_shell_script "$save_path/pd_setup.sh" "$terminal_utility_content"
banner
echo "${R}[${C}-${R}]${C} Configuring Terminal Utility For $selected_distro ...${W}"
echo
proot-distro login --user "$final_user_name" "$selected_distro" -- /bin/bash -c 'bash pd_setup.sh'
shell_rc_content=$(cat <<'EOF'
alias cat='bat $@'
alias ls='eza --icons $@'
alias mkdir='mkdir -p'
alias neofetch='fastfetch'
#######################################################
# SPECIAL FUNCTIONS
#######################################################
# Extracts any archive(s) (if unp isn't installed)
extract() {
for archive in "$@"; do
if [ -f "$archive" ]; then
case $archive in
*.tar.bz2) tar xvjf $archive ;;
*.tar.xz) tar -xvf $archive ;;
*.tar.gz) tar -xzvf $archive ;;
*.bz2) bunzip2 $archive ;;
*.rar) rar x $archive ;;
*.tar) tar xvf $archive ;;
*.tbz2) tar xvjf $archive ;;
*.tgz) tar xvzf $archive ;;
*.zip) unzip $archive ;;
*.Z) uncompress $archive ;;
*.7z) 7z x $archive ;;
*) echo "don't know how to extract '$archive'..." ;;
esac
else
echo "'$archive' is not a valid file!"
fi
done
}
# Searches for text in all files in the current folder
ftext() {
# -i case-insensitive
# -I ignore binary files
# -H causes filename to be printed
# -r recursive search
# -n causes line number to be printed
# optional: -F treat search term as a literal, not a regular expression
# optional: -l only print filenames and not the matching lines ex. grep -irl "$1" *
grep -iIHrn --color=always "$1" . | less -r
}
# Copy and go to the directory
cpg() {
if [ -d "$2" ]; then
cp "$1" "$2" && cd "$2"
else
cp "$1" "$2"
fi
}
# Move and go to the directory
mvg() {
if [ -d "$2" ]; then
mv "$1" "$2" && cd "$2"
else
mv "$1" "$2"
fi
}
# Create and go to the directory
mkdirg() {
mkdir -p "$1"
cd "$1"
}
EOF
)
# Preserve old shell rc file
cp "$save_path/.${pd_shell_name}rc" "$save_path/.${pd_shell_name}rc-2"
check_and_backup "$save_path/.${pd_shell_name}rc"
mv "$save_path/.${pd_shell_name}rc-2" "$save_path/.${pd_shell_name}rc"
echo "$shell_rc_content" >> "$save_path/.${pd_shell_name}rc"
if [[ "$selected_distro" == "debian" ]] || [[ "$selected_distro" == "ubuntu" ]]; then
cat <<'EOF' >> "$save_path/.${pd_shell_name}rc"
alias apt='sudo nala $@'
alias apt-get='sudo nala $@'
EOF
fi
cat <<EOF >> "$save_path/.${pd_shell_name}rc"
#set zoxide as cd
eval "\$(zoxide init --cmd cd ${pd_shell_name})"
EOF
fi
cat <<'EOF' >> "$save_path/.${pd_shell_name}rc"
export DISPLAY=:0
case $1 in
tx11start|tx11stop|vncstart|vncstop)
case $2 in
--help|-h|--nogpu|-f) ;;
esac
echo "please run it from termux, not inside proot distro."
;;
esac
EOF
echo "pd_shell_name=\"$pd_shell_name\"" >> "$config_file"
}
#########################################################################
#
# Create App Installer Shortcut
#
#########################################################################
function handel_debian_based_app_installer() {
if [[ "$distro_terminal_utility_setup_answer" == "y" ]]; then
local package_manager="nala"
else
local package_manager="apt"
fi
cat <<TOP_EOF > "$PREFIX/bin/$selected_distro"
#!/data/data/com.termux/files/usr/bin/bash
. $PREFIX/etc/termux-desktop/common_functions
xhost + > /dev/null 2>&1
if [[ "\$#" -eq 0 ]]; then
proot-distro login --user "$final_user_name" "$selected_distro" --shared-tmp
elif [[ "\$1" = "install" ]]; then
if [[ ! -d "/data/data/com.termux/files/usr/share/applications/pd_added" ]]; then
mkdir -p "/data/data/com.termux/files/usr/share/applications/pd_added"
fi
proot-distro login --user "$final_user_name" "$selected_distro" --shared-tmp -- env DISPLAY=:0 sudo "$package_manager" install \${@:2}
cat <<EOF > "$save_root_path/packinstall.sh"
packages=(\${@:2})
EOF
cat <<'EOF' >> "$save_root_path/packinstall.sh"
for package_name in "\${packages[@]}"; do
desktop_files=\$(dpkg-query -W -f='\${binary:Package}\n' | grep "^\$package_name\(-.*\)\?\$" | xargs dpkg-query -L | grep "^/usr/share/applications/.*\.desktop\$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /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}"
echo "${R}[${C}-${R}]${G} Adding ${C}\${desktop_files_without_ext} ${G}To Termux Menu"
cp "/usr/share/applications/\${desktop_files_with_ext}" "/data/data/com.termux/files/usr/share/applications/pd_added/"
sed -i 's/Exec=/Exec=pdrun /g' "/data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_with_ext}"
done
fi
done
rm packinstall.sh
EOF
proot-distro login "$selected_distro" --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packinstall.sh'
elif [[ "\$1" = "remove" ]] || [[ "\$1" = "autoremove" ]]; then
proot-distro login --user "$final_user_name" "$selected_distro" --shared-tmp -- env DISPLAY=:0 sudo "$package_manager" \$1 \${@:2}
cat <<EOF > "$save_root_path/packremove.sh"
packages=(\${@:2})
EOF
cat <<'EOF' >> "$save_root_path/packremove.sh"
for package_name in "\${packages[@]}"; do
desktop_files=\$(dpkg-query -W -f='\${binary:Package}\n' | grep "^\$package_name\(-.*\)\?\$" | xargs dpkg-query -L | grep "^/usr/share/applications/.*\.desktop\$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /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}"
echo "${R}[${C}-${R}]${G} Removing ${C}\${desktop_files_without_ext} ${G}From Termux Menu"
rm /data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_with_ext}
done
fi
done
rm packremove.sh
EOF
proot-distro login "$selected_distro" --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packremove.sh'
elif [[ "\$1" = "update" ]]; then
proot-distro login --user $final_user_name "$selected_distro" --shared-tmp -- env DISPLAY=:0 sudo $package_manager update
fi
if [[ \$1 = "install" ]] || [[ \$1 = "remove" ]] || [[ \$1 = "autoremove" ]]; then
gtk-update-icon-cache
update-desktop-database -q "$PREFIX/share/applications/pd_added"
fi
TOP_EOF
}
function handel_arch_based_app_installer() {
cat <<TOP_EOF > "$PREFIX/bin/$selected_distro"
#!/data/data/com.termux/files/usr/bin/bash
. $PREFIX/etc/termux-desktop/common_functions
xhost + > /dev/null 2>&1
if [[ "\$#" -eq 0 ]]; then
proot-distro login --user $final_user_name "$selected_distro" --shared-tmp
elif [[ "\$1" = "install" ]]; then
if [[ ! -d "/data/data/com.termux/files/usr/share/applications/pd_added" ]]; then
mkdir -p "/data/data/com.termux/files/usr/share/applications/pd_added"
fi
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:0 sudo pacman -S --noconfirm "\${@:2}"
cat <<EOF > "$save_root_path/packinstall.sh"
packages=("\${@:2}")
EOF
cat <<'EOF' >> "$save_root_path/packinstall.sh"
for package_name in "\${packages[@]}"; do
desktop_files=\$(pacman -Ql \$package_name | grep -oP "/usr/share/applications/.*\.desktop$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /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}"
echo "${R}[${C}-${R}]${G} Adding ${C}\${desktop_files_without_ext} ${G}To Termux Menu"
cp "/usr/share/applications/\${desktop_files_with_ext}" "/data/data/com.termux/files/usr/share/applications/pd_added/"
sed -i 's/Exec=/Exec=pdrun /g' "/data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_with_ext}"
done
fi
done
rm packinstall.sh
EOF
proot-distro login $selected_distro --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packinstall.sh'
elif [[ "\$1" = "remove" ]]; then
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:0 sudo pacman -R --noconfirm "\${@:2}"
cat <<EOF > "$save_root_path/packremove.sh"
packages=("\${@:2}")
EOF
cat <<'EOF' >> "$save_root_path/packremove.sh"
for package_name in "\${packages[@]}"; do
desktop_files=\$(pacman -Ql \$package_name | grep -oP "/usr/share/applications/.*\.desktop$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /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}"
echo "${R}[${C}-${R}]${G} Removing ${C}\${desktop_files_without_ext} ${G}From Termux Menu"
rm /data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_with_ext}
done
fi
done
rm packremove.sh
EOF
proot-distro login $selected_distro --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packremove.sh'
elif [[ "\$1" = "update" ]]; then
proot-distro login --user $final_user_name "$selected_distro" --shared-tmp -- env DISPLAY=:0 sudo pacman -Su
fi
if [[ \$1 = "install" ]] || [[ \$1 = "remove" ]]; then
gtk-update-icon-cache
update-desktop-database -q "$PREFIX/share/applications/pd_added"
fi
TOP_EOF
}
function handel_alpine_based_app_installer() {
cat <<TOP_EOF > "$PREFIX/bin/$selected_distro"
#!/data/data/com.termux/files/usr/bin/bash
. $PREFIX/etc/termux-desktop/common_functions
xhost + > /dev/null 2>&1
if [[ "\$#" -eq 0 ]]; then
proot-distro login --user $final_user_name $selected_distro --shared-tmp
elif [[ "\$1" = "install" ]]; then
if [[ ! -d "/data/data/com.termux/files/usr/share/applications/pd_added" ]]; then
mkdir -p "/data/data/com.termux/files/usr/share/applications/pd_added"
fi
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:0 sudo apk add "\${@:2}"
cat <<EOF > "$save_root_path/packinstall.sh"
packages=("\${@:2}")
EOF
cat <<'EOF' >> "$save_root_path/packinstall.sh"
for package_name in "\${packages[@]}"; do
desktop_files=\$(ls /usr/share/applications/ | grep "\$package_name.*\.desktop$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /usr/share/applications."
else
for desktop_files_name in \$desktop_files; do
desktop_files_without_ext="\${desktop_files_with_ext%.desktop}"
echo "${R}[${C}-${R}]${G} Adding ${C}\${desktop_files_without_ext} ${G}To Termux Menu"
sudo cp "/usr/share/applications/\${desktop_files_name}" "/data/data/com.termux/files/usr/share/applications/pd_added/"
sudo sed -i 's/Exec=/Exec=pdrun /g' "/data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_name}"
done
fi
done
rm packinstall.sh
EOF
proot-distro login $selected_distro --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packinstall.sh'
elif [[ "\$1" = "remove" ]]; then
proot-distro login --user $final_user_name "$selected_distro" --shared-tmp -- env DISPLAY=:0 sudo apk del "\${@:2}"
cat <<EOF > "$save_root_path/packremove.sh"
packages=("\${@:2}")
EOF
cat <<'EOF' >> "$save_root_path/packremove.sh"
for package_name in "\${packages[@]}"; do
desktop_files=\$(ls /usr/share/applications/ | grep "\$package_name.*\.desktop\$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /usr/share/applications."
else
for desktop_files_name in \$desktop_files; do
desktop_files_without_ext="\${desktop_files_with_ext%.desktop}"
echo "${R}[${C}-${R}]${G} Removing ${C}\${desktop_files_without_ext} ${G}From Termux Menu"
sudo rm /data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_name}
done
fi
done
rm packremove.sh
EOF
proot-distro login $selected_distro --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packremove.sh'
elif [[ "\$1" = "update" ]]; then
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:0 sudo apk update
fi
if [[ \$1 = "install" ]] || [[ \$1 = "remove" ]]; then
gtk-update-icon-cache
update-desktop-database -q "$PREFIX/share/applications/pd_added"
fi
TOP_EOF
}
function handel_fedora_based_app_installer() {
cat <<TOP_EOF > "$PREFIX/bin/$selected_distro"
#!/data/data/com.termux/files/usr/bin/bash
. $PREFIX/etc/termux-desktop/common_functions
xhost + > /dev/null 2>&1
if [[ "\$#" -eq 0 ]]; then
proot-distro login --user $final_user_name $selected_distro --shared-tmp
elif [[ "\$1" = "install" ]]; then
if [[ ! -d "/data/data/com.termux/files/usr/share/applications/pd_added" ]]; then
mkdir -p "/data/data/com.termux/files/usr/share/applications/pd_added"
fi
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:0 sudo dnf install \${@:2}
cat <<EOF > "$save_root_path/packinstall.sh"
packages=(\${@:2})
EOF
cat <<'EOF' >> "$save_root_path/packinstall.sh"
for package_name in "\${packages[@]}"; do
package_query=\$(rpm -q "\$package_name" | sed 's/-[0-9].*//')
desktop_files=\$(ls /usr/share/applications/ | grep "\$package_query.*\.desktop\$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /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}"
echo "${R}[${C}-${R}]${G} Adding ${C}\${desktop_files_without_ext} ${G}To Termux Menu"
cp "/usr/share/applications/\${desktop_files_with_ext}" "/data/data/com.termux/files/usr/share/applications/pd_added/"
sed -i 's/Exec=/Exec=pdrun /g' "/data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_with_ext}"
done
fi
done
rm packinstall.sh
EOF
proot-distro login $selected_distro --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packinstall.sh'
elif [[ "\$1" = "remove" ]]; then
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:0 sudo dnf remove \${@:2}
cat <<EOF > "$save_root_path/packremove.sh"
packages=(\${@:2})
EOF
cat <<'EOF' >> "$save_root_path/packremove.sh"
for package_name in "\${packages[@]}"; do
package_query=\$(rpm -q "\$package_name" | sed 's/-[0-9].*//')
desktop_files=\$(ls /usr/share/applications/ | grep "\$package_query.*\.desktop\$")
if [ -z "\$desktop_files" ]; then
print_failed "No .desktop files found for package ${C}\$package_name ${G}in /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}"
echo "${R}[${C}-${R}]${G} Removing ${C}\${desktop_files_without_ext} ${G}From Termux Menu"
rm /data/data/com.termux/files/usr/share/applications/pd_added/\${desktop_files_with_ext}
done
fi
done
rm packremove.sh
EOF
proot-distro login $selected_distro --shared-tmp -- /bin/bash -c 'export DISPLAY=:0; export XDG_RUNTIME_DIR=$TMPDIR; bash packremove.sh'
fi
if [[ \$1 = "install" ]] || [[ \$1 = "remove" ]] || [[ \$1 = "autoremove" ]]; then
gtk-update-icon-cache
update-desktop-database -q "$PREFIX/share/applications/pd_added"
fi
TOP_EOF
}
function distro_create_app_installer() {
banner
echo "${R}[${C}-${R}]${G}${BOLD} Creating App Launcher${W}"
save_root_path="$distro_path/root"
if [[ "$selected_distro" == "debian" ]] || [[ "$selected_distro" == "ubuntu" ]]; then
handel_debian_based_app_installer
elif [[ "$selected_distro" == "archlinux" ]]; then
handel_arch_based_app_installer
elif [[ "$selected_distro" == "alpine" ]]; then
handel_alpine_based_app_installer
elif [[ "$selected_distro" == "fedora" ]]; then
handel_fedora_based_app_installer
fi
cat <<EOF >> "$PREFIX/bin/$selected_distro"
function remove_distro_container() {
confirmation_y_or_n "Do you want to remove the distro${R}it can't be undone" ans_remove_distro
case \$ans_remove_distro in
[yY]* )
print_success "Continuing with answer: \$ans_remove_distro"
sleep 0.2
if [[ -d $distro_path ]]; then
proot-distro remove "$selected_distro"
distro_file_list=(
"$PREFIX/bin/pdrun"
"$PREFIX/share/applications/pd_added"
"$PREFIX/bin/add2menu"
"$PREFIX/share/applications/add2menu.desktop"
"$PREFIX/bin/"$selected_distro""
)
for distro_file in "\${distro_file_list[@]}"; do
if [[ -e "\$distro_file" ]]; then
check_and_delete "\$distro_file"
else
print_failed "File does not exist: \$distro_file"
fi
done
print_success "$selected_distro removed successfully"
echo "distro_add_answer=\"n\"" >> $config_file
else
print_failed "$selected_distro does not exist"
fi
;;
[nN]* )
echo "${R}[${C}-${R}]${C} Skipping distro remove${W}"
sleep 0.2
;;
* )
print_failed "Invalid input, Please enter y or n"
;;
esac
}
function help_distro_container() {
echo -e "
"$selected_distro" install packagename to install packages and add them to termux desktop\n
"$selected_distro" remove packagename to remove packages and also remove them to termux desktop\n
"$selected_distro" update to update the Linux distro\n
"$selected_distro" -r to login as root\n
"$selected_distro" --remove to complitely remove the Linux distro and delete all files and data related to it\n
"
}
case \$1 in
--root|-r)
proot-distro sh "$selected_distro"
;;
--remove)
remove_distro_container
;;
--help|-h)
help_distro_container
;;
esac
EOF
chmod +x "$PREFIX/bin/$selected_distro"
#########################################################################
########################### Add To Menu Setup ###########################
#########################################################################
banner
package_install_and_check "zenity"
check_and_create_directory "$PREFIX/share/applications/pd_added"
echo "#!/data/data/com.termux/files/usr/bin/bash
distro_path=$distro_path
" > "$PREFIX/bin/add2menu"
cat <<'EOF' >> "$PREFIX/bin/add2menu"
function add2menu_f() {
# Function to list displayable .desktop files
list_desktop_files_with_name() {
find "$1" -name "*.desktop" -type f -exec awk -F '=' '
/^Name=/ {name=$2}
/^NoDisplay=/ {no_display=$2}
END {
if (no_display != "true") {
gsub(/.desktop$/, "", name);
print name ";;" FILENAME
}
}' {} \;
}
# Main action selection
action=$(zenity --list --title="Choose Action" --text="Select an action:" --radiolist --column="Select" --column="Action" \
TRUE "Add Application To Termux Menu" FALSE "Remove Application From Termux Menu")
# Exit if no action is selected
if [[ -z $action ]]; then
zenity --info --text="No action selected. Quitting..." --title="Operation Cancelled"
exit 0
fi