Big loop refactoring

This commit is contained in:
zenobit 2023-03-31 01:37:15 +02:00
parent 95c3e6c228
commit 68e148b5fc
1 changed files with 489 additions and 487 deletions

282
dh
View File

@ -4,8 +4,14 @@
TEXTDOMAIN=distrohopper
TEXTDOMAINDIR=/usr/share/locale
export "TEXTDOMAINDIR" "TEXTDOMAIN"
# bugs notice
function some_bugs() {
trap 'exit' SIGINT SIGTERM SIGQUIT
set -e
echo $"DistroHopper is running... Press Ctrl-Q anytime to exit."
while true
do
function bugs_notice_show() {
echo ""
echo $"Done"
echo ""
@ -19,7 +25,7 @@ function some_bugs() {
echo " zenobit"
}
function show_help() {
function help_show() {
echo "DistroHopper v. $version"
echo "quickemu v. $("$portable_prefix"quickemu --version)"
printf $"\texample for First run from terminal: ./dh i && dh s g\n"
@ -50,11 +56,11 @@ function show_help() {
echo $"Chat group on SimpleX: https://tinyurl.com/7hm4kcjx"
}
function portable() {
[ -f "$DH_CONFIG" ] && mode_installed || mode_portable
function check_and_set_mode() {
[ -f "$DH_CONFIG" ] && work_in_choosed_dir || work_in_current_dir
}
function mode_portable() {
function work_in_current_dir() {
VMS_DIR="$(pwd)"
DH_CONFIG_DIR="$(pwd)"
DH_ICON_DIR="$(pwd)/icons"
@ -62,25 +68,24 @@ function mode_portable() {
export "VMS_DIR" "DH_CONFIG_DIR" "DH_ICON_DIR" "TERMINAL" "replace" "portable_prefix"
}
function mode_installed() {
function work_in_choosed_dir() {
# shellcheck source=distrohopper.conf
source "$DH_CONFIG"
portable_prefix=''
export "portable_prefix"
}
# installation ---------------------------
function check_gui_dependencies() {
function gui_dependencies_check() {
[ -f "$PATH_PREFIX/yad" ] || echo $"Missing yad!"
}
trap 'exit' SIGINT SIGTERM SIGQUIT
set -e
echo $"DistroHopper is running... Press Ctrl-Q anytime to exit."
function check_tui_dependencies() {
function tui_dependencies_check() {
[ -f "$PATH_PREFIX/fzf" ] || echo $"Missing fzf!"
}
function set_variables() {
function variables_set() {
# DEBUG mod
#bash -x ./dh 2>&1 | tee output.log
#progname="${progname:="${0##*/}"}"
@ -95,13 +100,13 @@ function set_variables() {
TERMINAL="sakura"
replace='"!"'
export "DH_CONFIG_DIR" "DH_CONFIG" "replace" "DH_ICON_DIR" "PATH_PREFIX" "TMP_DIR" "TERMINAL"
portable
check_and_set_mode
# Set traps to catch the signals and exit gracefully
trap "exit" INT
trap "exit" EXIT
}
function create_desktop_entry() {
function desktop_entry_create() {
cat <<EOF > ${DESKTOP_FILE}
[Desktop Entry]
Version=$version
@ -122,7 +127,7 @@ Keywords=$keyword
EOF
}
function create_structure() {
function directory_structure_create() {
source distrohopper.conf
echo $"creating config dir..."
mkdir -p "$DH_CONFIG_DIR"
@ -130,39 +135,25 @@ function create_structure() {
mkdir -p "$DH_ICON_DIR" >/dev/null 2>&1 || sudo mkdir -p "$DH_ICON_DIR"
}
function dh_desktop_entry() {
function desktop_entry_distrohopper() {
echo $"Do you want DistroHopper to run in the terminal? (y/n)"
run_in_terminal
DESKTOP_FILE="${TMP_DIR}/dh.desktop"
type='Application'
name='DistroHopper'
comment='Quickly download, create and run VM of any#TODO operating system.'
version='0.77b'
execmd="sh -c 'cd ${VMS_DIR} && dh g'"
if [ "$interminal" == "yes" ]; then
cat <<EOF > ${TMP_DIR}/dh.desktop
[Desktop Entry]
Type=Application
Name=DistroHopper
Description=Quickly download, create and run VM of any#TODO operating system.
version=0.77b
Terminal=true
Exec=sh -c 'cd ${VMS_DIR} && dh g'
Icon=/home/zen/git/distrohopper/icons/hop
Categories=System;Virtualization;
EOF
else
cat <<EOF > ${TMP_DIR}/dh.desktop
[Desktop Entry]
Type=Application
Name=DistroHopper
Description=Quickly download, create and run VM of any#TODO operating system.
version=0.77b
Exec=sh -c 'cd ${VMS_DIR} && dh g'
Icon=/home/zen/git/distrohopper/icons/hop
Categories=System;Virtualization;
EOF
terminal='true'
fi
icon='hop'
categories='System;Virtualization;'
desktop_entry_create
sudo cp ${TMP_DIR}/dh.desktop /usr/share/applications/
}
function set_dir() {
function virtual_machines_directory_choose() {
NEWDIR="$(yad --width=900 --height=900 --file --directory --title="Where to save VMs?")"
VMS_DIR="$NEWDIR"
echo "VMS_DIR=\"$VMS_DIR\"
@ -170,7 +161,7 @@ function set_dir() {
export "VMS_DIR"
}
function install_prereq() {
function dependencies_install() {
# Find the current distribution and install dependecies
if [ -f /etc/os-release ]; then
if grep -q arch /etc/os-release; then
@ -198,7 +189,7 @@ function install_prereq() {
fi
}
function install_dh() {
function distrohopper_install() {
cp dh quickget quickemu macrecovery windowskey "$PATH_PREFIX" >/dev/null 2>&1 || sudo cp dh quickget quickemu macrecovery windowskey "$PATH_PREFIX"
# quickget also to config directory for adding new distros...
cp quickget "$DH_CONFIG_DIR/"
@ -210,21 +201,22 @@ function install_dh() {
cp -r locale "$DH_CONFIG_DIR/"
}
function install_process() {
check_tui_dependencies
check_gui_dependencies
function installation_process() {
tui_dependencies_check
gui_dependencies_check
#check_quickemu_dependencies
echo $"Creating directory structure..." \
&& create_structure \
&& directory_structure_create \
&& echo $"Setting up directory..." \
&& set_dir \
&& virtual_machines_directory_choose \
&& echo $"Installing needed..." \
&& install_prereq \
&& dependencies_install \
&& echo $"Installing DistroHopper to bin..." \
&& install_dh \
&& distrohopper_install \
&& echo $"Copying desktop entry..." \
&& dh_desktop_entry
&& desktop_entry_distrohopper
}
# basic -----------------------------------
function run_in_terminal() {
@ -236,7 +228,7 @@ function run_in_terminal() {
fi
}
function renew_ready() {
function virtual_machines_update_ready() {
cd "$VMS_DIR" || exit 1
# for files in "$VMS_DIR"/*; do
# if [ ! -e *.conf ]; then
@ -258,20 +250,19 @@ function renew_ready() {
elif [ -z "$icon_file" ]; then
icon_file="$DH_ICON_DIR/tux.svg"
fi
# content of desktop files (ready to run VMs)
cat <<EOF > "$DH_CONFIG_DIR"/ready/"$vm_desktop".desktop
[Desktop Entry]
Type=Application
Name=$vm_desktop
Exec=sh -c 'cd ${VMS_DIR} && ${portable_prefix}quickemu -vm ${vm_conf}'
Icon=$icon_file
Categories=System;Virtualization;
EOF
DESKTOP_FILE="$DH_CONFIG_DIR/ready/$vm_desktop.desktop"
type='Application'
name="$vm_desktop"
comment='Quickly download, create and run VM of any#TODO operating system.'
version='0.77b'
execmd="sh -c 'cd ${VMS_DIR} && ${portable_prefix}quickemu -vm ${vm_conf}'"
icon="$icon_file"
categories='System;Virtualization;'
desktop_entry_create
done
}
function renew_supported() {
function virtual_machines_update_supported() {
echo $"Do you want quickget to show outputs in the terminal? (y/n)"
run_in_terminal
# get supported VMs
@ -291,45 +282,54 @@ function renew_supported() {
echo $"Creating $vm_desktop desktop entry..."
echo ""
# Check if there are editions
DESKTOP_FILE="$DH_CONFIG_DIR/supported/$vm_desktop.desktop"
type='Application'
name="$get_name"
releases=$releases
replace=$replace
if [ -z "$editions" ]; then
cat <<EOF > "$DH_CONFIG_DIR"/supported/"$vm_desktop".desktop
[Desktop Entry]
Type=Application
Name=$get_name
releases=$releases
replace=$replace
Exec=sh -c 'cd "$VMS_DIR" && yad --form --field="Release:CB" "${releases// /$replace}" | cut -d"|" -f1 | xargs -I{} sh -c "${portable_prefix}quickget $get_name {}"'
Icon=$icon_file
Categories=System;Virtualization;
EOF
execmd="sh -c 'cd $VMS_DIR && yad --form --field=\"Release:CB\" \"${releases// /$replace}\" | cut -d\'|\' -f1 | xargs -I{} sh -c \"${portable_prefix}quickget $get_name {}\"'"
elif [ "$interminal" == "yes" ]; then
terminal='true'
else
cat <<EOF > "$DH_CONFIG_DIR"/supported/"$vm_desktop".desktop
[Desktop Entry]
Type=Application
Name=$get_name
releases=$releases
editions=$editions
replace=$replace
Exec=sh -c 'cd $VMS_DIR && yad --form --separator=" " --field="Release:CB" "${releases// /$replace}" --field="Edition:CB" "${editions// /$replace}" | xargs -I{} sh -c "${portable_prefix}quickget $get_name {}"'
Icon=$icon_file
Categories=System;Virtualization;
EOF
execcmd="sh -c 'cd $VMS_DIR && yad --form --separator=\" \" --field=\"Release:CB\" \"${releases// /$replace}\" --field=\"Edition:CB\" \"${editions// /$replace}\" | xargs -I{} sh -c \"${portable_prefix}quickget $get_name {}\"'"
fi
icon='hop'
categories='System;Virtualization;'
desktop_entry_create
done < "$DH_CONFIG_DIR"/supported.md
}
function run_gui() {
check_gui_dependencies
function distrohopper_run_gui() {
gui_dependencies_check
key=$((RANDOM % 9000 + 1000))
yad --plug="$key" --tabnum=1 --monitor --icons --listen --read-dir="$DH_CONFIG_DIR"/ready --sort-by-name --no-buttons --borders=0 --icon-size=46 --item-width=76 &
yad --plug="$key" --tabnum=2 --monitor --icons --listen --read-dir="$DH_CONFIG_DIR"/supported --sort-by-name --no-buttons --borders=0 --icon-size=46 --item-width=76 &
yad --dynamic --notebook --key="$key" --monitor --listen --window-icon="$DH_ICON_DIR"/hop.svg --width=900 --height=900 --title="DistroHopper" --tab="run VM" --tab="download VM"
}
function run_tui() {
check_tui_dependencies
function distrohopper_run_tui() {
tui_dependencies_check
tui_run
if [ "$todo" = "create" ]; then
virtual_machine_create
tui_windows
tui_get_releases_and_editions
tui_get_releases
if [ "$(echo "$choices" | wc -l)" = 1 ]; then
tui_downloading
else
tui_get_editions
tui_downloading_with_edition
fi
fi
virtual_machine_choose_to_run
virtual_machine_run
}
tui_run() {
cd "$VMS_DIR" || exit 1
vms=(*.conf)
printf $" Prepared VMs:\n-------------\n\n"
@ -350,17 +350,41 @@ function run_tui() {
todo="create"
;;
esac
}
# If the user chose to create a new VM
if [ "$todo" = "create" ]; then
virtual_machine_create() {
os=$("$portable_prefix"quickget | sed 1d | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose OS to download
or CTRL-c or ESC to quit')
}
# If the OS is Windows
tui_get_releases_and_editions() {
choices=$("$portable_prefix"quickget "$os" | sed 1d)
}
tui_get_releases() {
release=$(echo "$choices" | grep 'Releases' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Release
or CTRL-c or ESC to quit')
}
tui_get_editions() {
edition=$(echo "$choices" | grep 'Editions' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Edition
or CTRL-c or ESC to quit')
}
tui_downloading() {
printf $"\n Trying to download %s %s...\n\n" "$os" "$release"
"$portable_prefix"quickget "$os" "$release"
}
tui_downloading_with_edition() {
printf $"\n Trying to download %s %s %s...\n\n" "$os" "$release" "$edition"
"$portable_prefix"quickget "$os" "$release" "$edition"
}
tui_windows() {
if [ "$os" = windows ]; then
answer=$(echo "Default English
Choose other language" | fzf --cycle)
# If the user wants another windows language
if [ "$answer" = "Choose other language" ]; then
wrelease=$(echo "8
@ -379,46 +403,21 @@ Choose other language" | fzf --cycle)
"$portable_prefix"quickget "windows" "$wrelease" "$wlang"
fi
fi
}
# Get the release and edition to download, if necessary
choices=$("$portable_prefix"quickget "$os" | sed 1d)
if [ "$(echo "$choices" | wc -l)" = 1 ]; then
# get release
release=$(echo "$choices" | grep 'Releases' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Release
virtual_machine_choose_to_run() {
choosed=$(echo "$(ls ***.conf 2>/dev/null | sed 's/\.conf$//')" | fzf --cycle --header='Choose VM to run
or CTRL-c or ESC to quit')
}
# downloading
printf $"\n Trying to download %s %s...\n\n" "$os" "$release"
"$portable_prefix"quickget "$os" "$release"
else
# get release
release=$(echo "$choices" | grep 'Releases' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Release
or CTRL-c or ESC to quit')
# get edition
edition=$(echo "$choices" | grep 'Editions' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Edition
or CTRL-c or ESC to quit')
# downloading
printf $"\n Trying to download %s %s %s...\n\n" "$os" "$release" "$edition"
"$portable_prefix"quickget "$os" "$release" "$edition"
fi
# choose VM to run
choosed=$(echo "$(ls ./***.conf 2>/dev/null | sed 's/\.conf$//')" | fzf --cycle --header='Choose VM to run
or CTRL-c or ESC to quit')
# Run choosed VM
virtual_machine_run() {
printf $"\n Starting %s...\n\n" "$choosed"
"$portable_prefix"quickemu -vm "$choosed.conf"
fi
}
# more
function isos_to_dir() {
function isos_copy_to_dir() {
yad --width=900 --height=900 --file --directory > target
cd "$VMS_DIR" || exit 1
# shellcheck disable=2154
@ -506,11 +505,12 @@ function translate() {
}
# run --------------------------------------
set_variables
variables_set
if [[ $# -eq 0 ]]; then
printf $"No argumet provided!\n\n"
show_help
help_show
fi
while [[ $# -gt 0 ]]
@ -518,41 +518,41 @@ do
key="$1"
case $key in
h|help)
show_help
help_show
shift
;;
d|dir)
set_dir
virtual_machines_directory_choose
shift
;;
i|install)
echo $"Starting installation..."
install_process
installation_process
shift
;;
m|mode)
echo $"Switching to portable mode!"
mode_portable
work_in_current_dir
shift
;;
s|supported)
echo $"Updating supported VMs..."
renew_supported
virtual_machines_update_supported
shift
;;
r|ready)
echo $"Updating ready VMs..."
renew_ready
virtual_machines_update_ready
shift
;;
t|tui)
echo $"Running DistroHopper TUI..."
run_tui
distrohopper_run_tui
shift
;;
g|gui)
echo $"Starting DistroHopper GUI..."
run_gui
distrohopper_run_gui
shift
;;
a|add)
@ -572,7 +572,7 @@ do
;;
c|copy)
echo $"Copying ISOs to dir. It will take some time..."
isos_to_dir
isos_copy_to_dir
shift
;;
l|language)
@ -580,18 +580,20 @@ do
shift
;;
z)
WIP
run_tui_new
shift
;;
*)
echo $"Invalid option: $1"
echo ""
show_help
help_show
exit 1
;;
esac
done
some_bugs
bugs_notice_show
exit 0
done