Big loop refactoring

This commit is contained in:
zenobit 2023-03-31 01:37:15 +02:00
parent 95c3e6c228
commit 68e148b5fc

282
dh
View file

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