-
Notifications
You must be signed in to change notification settings - Fork 4
/
distro-container-setup
executable file
·520 lines (473 loc) · 17.8 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
#!/data/data/com.termux/files/usr/bin/bash
function setup_distro() {
if [[ "$distro_answer" == "1" ]]; then
selected_distro="debian"
elif [[ "$distro_answer" == "2" ]]; then
selected_distro="ubuntu"
else
selected_distro="debian"
fi
banner
echo "${R}[${W}-${R}]${G}${BOLD}Installing ${C}proot-distro"${W}
echo
update_sys
package_install_and_check "proot-distro"
banner
echo "${R}[${W}-${R}]${G}${BOLD}Setup Selected 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}[${W}-${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/.${selected_distro}-sound-access"
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 distro_basic_task() {
banner
echo "${R}[${W}-${R}]${G}${BOLD}Updating ${C}$selected_distro ${G}And Install Required Packages"${W}
echo
proot-distro login $selected_distro --shared-tmp -- env DISPLAY=:1.0 apt update
if [[ "pd_audio_config_answer" == "n" ]]; then
distro_packs="sudo"
else
distro_packs="sudo pulseaudio"
distro_fix_sound_issue
fi
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
declare -f banner package_install_and_check >> $distro_path/root/pd_basic_setup.sh
cat <<EOF >> $distro_path/root/pd_basic_setup.sh
banner
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"
else
final_user_name="${user_name}"
fi
if [[ "$pd_pass_type" == "1" ]];then
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}:${pass}" | chpasswd
chmod u+rw /etc/sudoers
echo "$final_user_name ALL=(ALL:ALL) ALL" | tee -a /etc/sudoers
chmod u-w /etc/sudoers
rm useradd.sh
EOF
elif [[ "$pd_pass_type" == "2" ]];then
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}
chmod u+rw /etc/sudoers
echo "$final_user_name ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers
chmod u-w /etc/sudoers
rm useradd.sh
EOF
fi
proot-distro login $selected_distro -- /bin/sh -c 'bash useradd.sh'
}
#########################################################################
################## Setup Zsh And Terminal Utility #######################
#########################################################################
function distro_zsh_answer() {
banner
if [[ "$pd_useradd_answer" == "y" ]]; then
save_path="$distro_path/home/$final_user_name"
else
save_path="$distro_path/root"
fi
if [[ "$distro_zsh_answer" == "n" ]]; then
echo "${R}[${W}-${R}]${C}Canceling Distro Zsh Setup..."${W}
sleep 1.5
pd_shell_name="bash"
else
pd_shell_name="zsh"
echo "${R}[${W}-${R}]${G}${BOLD} Configuring Zsh for ${selected_distro}${G}.."${W}
echo
cat <<'EOF' > $save_path/pd_zsh_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
declare -f banner get_latest_release package_install_and_check >> $save_path/pd_zsh_setup.sh
cat <<'EOF' >> $save_path/pd_setup.sh
banner
package_install_and_check "git zsh wget"
wget https://raw.githubusercontent.com/sabamdarif/short-linux-scripts/main/install-zsh.sh && bash install-zsh.sh
rm pd_zsh_setup.sh
EOF
proot-distro login --user $final_user_name $selected_distro -- /bin/bash -c 'bash pd_zsh_setup.sh'
fi
}
function distro_terminal_utility_setup() {
if [[ "$distro_terminal_utility_setup_answer" == "n" ]];then
banner
echo "${R}[${W}-${R}]${C}Skipping Terminal Utility Setup For ${selected_distro}..."${W}
echo
else
banner
echo "${R}[${W}-${R}]${C}${BOLD}Configuring Terminal Utility..."${W}
echo
cat <<'EOF' > $distro_path/root/pd_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
declare -f banner get_latest_release package_install_and_check >> $distro_path/root/pd_setup.sh
cat <<'EOF' >> $distro_path/root/pd_setup.sh
banner
package_install_and_check "nala zoxide bat wget gpg"
device_arch=$(uname -m)
case "$device_arch" in
aarch64)
archtype="arm64"
;;
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 -O $distro_path/root/fastfetch.deb https://github.com/fastfetch-cli/fastfetch/releases/download/${version}/fastfetch-linux-${archtype}.deb
apt install ./fastfetch.deb -y
rm fastfetch.deb
mkdir -p /etc/apt/keyrings
wget -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
apt install -y eza
rm pd_setup.sh
EOF
proot-distro login $selected_distro -- /bin/bash -c 'bash pd_setup.sh'
cat <<'EOF' >> "$save_path/.${pd_shell_name}rc"
alias apt='sudo nala $@'
alias apt-get='sudo nala $@'
alias cat='bat $@'
alias ls='eza --icons $@'
alias mkdir='mkdir -p'
alias neofetch='fastfetch'
alias tx11start='echo please run from termux, not inside proot distro.'
alias tx11stop='echo please run from termux, not inside proot distro.'
#######################################################
# 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 ;;
*.tar.gz) tar xvzf $archive ;;
*.bz2) bunzip2 $archive ;;
*.rar) rar x $archive ;;
*.gz) gunzip $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 file with a progress bar
cpp() {
set -e
strace -q -ewrite cp -- "${1}" "${2}" 2>&1 |
awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i=0;i<=percent;i++)
printf "="
printf ">"
for (i=percent;i<100;i++)
printf " "
printf "]\r"
}
}
END { print "" }' total_size="$(stat -c '%s' "${1}")" count=0
}
# 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
cat <<EOF >> "$save_path/.${pd_shell_name}rc"
#set zoxide as cd
eval "\$(zoxide init --cmd cd ${pd_shell_name})"
EOF
fi
}
#########################################################################
#################### Create App Installer Shortcut ######################
#########################################################################
function distro_create_app_installer() {
banner
echo "${R}[${W}-${R}]${G}${BOLD}Creating App Launcher"${W}
if [[ "$pd_useradd_answer" == "y" ]]; then
save_user_path="$distro_path/home/$final_user_name"
fi
save_root_path="$distro_path/root"
cat <<TOP_EOF > "$PREFIX/bin/$selected_distro"
#!/data/data/com.termux/files/usr/bin/bash
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=:1.0 sudo apt 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
echo "${R}[${W}-${R}]${G} 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}[${W}-${R}]${G} Adding ${C}\${desktop_files_without_ext} ${G}To Termux Menu"
sudo cp "/usr/share/applications/\${desktop_files_with_ext}" "/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_with_ext}"
done
fi
done
rm packinstall.sh
EOF
proot-distro login $selected_distro -- /bin/bash -c 'bash packinstall.sh'
elif [[ "\$1" = "remove" ]] || [[ "\$1" = "remove" ]]; then
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:1.0 sudo apt \$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
echo "${R}[${W}-${R}]${G} 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}[${W}-${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_with_ext}
done
fi
done
rm packremove.sh
EOF
proot-distro login $selected_distro -- /bin/bash -c 'bash packremove.sh'
else
echo "sudo apt \$@
rm else.sh
" > $save_root_path/else.sh
proot-distro login $selected_distro -- /bin/sh -c 'bash else.sh'
fi
if [[ \$1 = "install" ]] || [[ \$1 = "remove" ]] || [[ \$1 = "autoremove" ]]; then
gtk-update-icon-cache
xfce4-panel -r >/dev/null 2>&1
fi
TOP_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
list_desktop_files_with_name() {
find "$1" -name "*.desktop" -type f -exec awk -F '=' '/^Name=/ {gsub(/.desktop$/,"",$2); print $2}' {} \;
}
action=$(zenity --list --title="Choose Action" --text="Select an action:" --checklist --column="" --column="Action" FALSE "Add Application To Termux Menu" FALSE "Remove Application From Termux Menu")
if [[ -z $action ]]; then
zenity --info --text="No action selected. Quitting..." --title="Operation Cancelled"
exit 0
fi
if [[ $action == *"Add Application To Termux Menu"* ]]; then
# Get the list of .desktop files with Name parameter
files=$(list_desktop_files_with_name "$distro_path/usr/share/applications/")
# Display the list of files and ask user to select
selected_names=$(zenity --list --title="Add To Termux Menu" --text="Select The App Name To Add:" --checklist --column="Include" --column="Name" --separator="," $files)
if [[ -z $selected_names ]]; then
zenity --info --text="No file selected. Quitting..." --title="Operation Cancelled"
exit 0
fi
IFS=',' read -ra selected_names_array <<< "$selected_names"
for selected_name in "${selected_names_array[@]}"; do
selected_file=$(find "$distro_path/usr/share/applications/" -name "*.desktop" -type f -exec awk -F '=' -v name="$selected_name" '$0 ~ "Name=" name {print FILENAME}' {} \;)
cp "$selected_file" "$PREFIX/share/applications/pd_added/"
sed -i 's/Exec=/Exec=pdrun /g' "$PREFIX/share/applications/pd_added/${selected_file##*/}"
gtk-update-icon-cache
xfce4-panel -r >/dev/null 2>&1
zenity --info --text="File '$selected_name' added successfully!" --title="Success"
done
fi
if [[ $action == *"Remove Application From Termux Menu"* ]]; then
# Get the list of .desktop files with Name parameter
files=$(list_desktop_files_with_name "$PREFIX/share/applications/pd_added/")
# Display the list of files and ask user to select
selected_names=$(zenity --list --title="Remove From Termux Menu" --text="Choose .desktop files to remove:" --checklist --column="Include" --column="Name" --separator="," $files)
if [[ -z $selected_names ]]; then
zenity --info --text="No file selected for removal. Quitting..." --title="Operation Cancelled"
exit 0
fi
IFS=',' read -ra selected_names_array <<< "$selected_names"
for selected_name in "${selected_names_array[@]}"; do
selected_file=$(find "$PREFIX/share/applications/pd_added/" -name "*.desktop" -type f -exec awk -F '=' -v name="$selected_name" '$0 ~ "Name=" name {print FILENAME}' {} \;)
rm "$selected_file"
gtk-update-icon-cache
xfce4-panel -r >/dev/null 2>&1
zenity --info --text="File '$selected_name' has been removed successfully!" --title="Success"
done
fi
gtk-update-icon-cache
EOF
chmod +x "$PREFIX/bin/add2menu"
check_and_create_directory "$PREFIX/share/applications/"
cat <<EOF > "$PREFIX/share/applications/add2menu.desktop"
[Desktop Entry]
Version=1.0
Type=Application
Name=Add To Menu
Comment=
Exec=add2menu
Icon=edit-move
Categories=System;
Path=
Terminal=false
StartupNotify=false
EOF
chmod +x $PREFIX/share/applications/add2menu.desktop
cp $PREFIX/share/applications/add2menu.desktop $HOME/Desktop/
}
#########################################################################
########################## App Launcher Setup ###########################
#########################################################################
function distro_app_launch_setup() {
cat <<EOF > "$PREFIX/bin/pdrun"
#!/data/data/com.termux/files/usr/bin/bash
bash ~/.${selected_distro}-sound-access
xhost +
proot-distro login --user $final_user_name $selected_distro --shared-tmp -- env DISPLAY=:1.0 $selected_pd_hw_method \$@
EOF
chmod +x "$PREFIX/bin/pdrun"
}
#########################################################################
############### Distro Directory , Theme And Icon Link ##################
#########################################################################
function distro_folders_link() {
echo "${R}[${W}-${R}]${G}${BOLD}Link Distro Directories With Internal Storage"${W}
cat <<EOF > "$save_path/setup-folders.sh"
#!/bin/bash
directories=(Desktop Music Downloads Pictures Videos)
for dir in "\${directories[@]}"; do
ln -s "/data/data/com.termux/files/home/\$dir" "\$HOME/"
done
rm setup-folders.sh
EOF
proot-distro login --user $final_user_name $selected_distro -- /bin/sh -c 'bash setup-folders.sh'
echo "${R}[${W}-${R}]${G}${BOLD}Link Distro Termux Themes And Icons With $selected_distro "${W}
sleep 0.5
check_and_create_directory "$HOME/.icons"
ln -s /data/data/com.termux/files/home/.icons $save_path
check_and_create_directory "$HOME/.themes"
ln -s /data/data/com.termux/files/home/.themes $save_path
}
setup_distro
distro_basic_task
distro_zsh_answer
distro_terminal_utility_setup
distro_create_app_installer
distro_app_launch_setup
distro_folders_link