From fe736c8a2b64e4ec98e6e23d11b9b7d4c4046fa4 Mon Sep 17 00:00:00 2001 From: Dmitry Maksyoma Date: Sat, 24 Jul 2021 14:53:34 +1200 Subject: [PATCH] Easy start script: split shell code into modules --- builder/startup/deb/cli-processing.sh | 50 ++++++ builder/startup/deb/kasmvncserver-easy-start | 179 ++----------------- builder/startup/deb/select-de.sh | 127 +++++++++++++ 3 files changed, 189 insertions(+), 167 deletions(-) create mode 100644 builder/startup/deb/cli-processing.sh create mode 100644 builder/startup/deb/select-de.sh diff --git a/builder/startup/deb/cli-processing.sh b/builder/startup/deb/cli-processing.sh new file mode 100644 index 0000000..f0e7fc6 --- /dev/null +++ b/builder/startup/deb/cli-processing.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +debug() { + if [ -z "$debug" ]; then return; fi + + echo "$@" +} + +enable_debug() { + debug=1 + log_option="-log *:stderr:100" +} + +kill_vnc_server() { + vncserver -kill $display +} + +process_cli_options() { + for option in "$@"; do + case "$option" in + --help) + show_help + exit + ;; + -d|--debug) + enable_debug + ;; + -k|--kill) + kill_vnc_server + exit + ;; + -s|--select-de) + action=select-de-and-start + ;; + *) + echo >&2 "Unsupported argument: $option" + exit 1 + esac + done +} + +show_help() { + cat >&2 <<-USAGE +Usage: $(basename "$0") [options] + -d, --debug Debug output + -k, --kill Kill vncserver + -s, --select-de Select desktop environent to run + --help Show this help +USAGE +} diff --git a/builder/startup/deb/kasmvncserver-easy-start b/builder/startup/deb/kasmvncserver-easy-start index e2733d2..210b204 100755 --- a/builder/startup/deb/kasmvncserver-easy-start +++ b/builder/startup/deb/kasmvncserver-easy-start @@ -5,182 +5,27 @@ set -e display=:10 interface=0.0.0.0 cert_group=ssl-cert -xstartup_script=~/.vnc/xstartup -de_was_selected_file="$HOME/.vnc/.kasmvncserver-easy-start-de-was-selected" action=start -manual_xstartup_choice="Manually edit xstartup" -declare -A all_desktop_environments=( - [Cinnamon]=cinnamon-session - [Mate]="XDG_CURRENT_DESKTOP=MATE dbus-launch --exit-with-session mate-session" - [LXDE]=lxsession [Lxqt]=startlxqt - [KDE]=startkde - [Gnome]="XDG_CURRENT_DESKTOP=GNOME dbus-launch --exit-with-session /usr/bin/gnome-session" - [XFCE]=xfce4-session) +get_lib_path() { + local lib="$1" + local script_dir=$(dirname "$0") + local system_dir=/usr/share/kasmvnc -readarray -t sorted_desktop_environments < <(for de in "${!all_desktop_environments[@]}"; do echo "$de"; done | sort) - -all_desktop_environments[$manual_xstartup_choice]="" -sorted_desktop_environments+=("$manual_xstartup_choice") - -detected_desktop_environments=() -declare -A numbered_desktop_environments - -debug() { - if [ -z "$debug" ]; then return; fi - - echo "$@" -} - -print_detected_desktop_environments() { - declare -i i=1 - - echo "Please choose Desktop Environment to run:" - for detected_de in "${detected_desktop_environments[@]}"; do - echo "[$i] $detected_de" - numbered_desktop_environments[$i]=$detected_de - i+=1 - done -} - -detect_desktop_environments() { - for de_name in "${sorted_desktop_environments[@]}"; do - if [[ "$de_name" = "$manual_xstartup_choice" ]]; then - detected_desktop_environments+=("$de_name") - continue; - fi - - local executable=${all_desktop_environments[$de_name]} - executable=($executable) - executable=${executable[-1]} - - if detect_desktop_environment "$de_name" "$executable"; then - detected_desktop_environments+=("$de_name") - fi - done -} - -ask_user_to_choose_de() { - while : ; do - print_detected_desktop_environments - read -r de_number_to_run - de_name_from_number "$de_number_to_run" - if [[ -n "$de_name" ]]; then - break; - fi - - echo "Incorrect number: $de_number_to_run" - echo - done -} - -remember_de_choice() { - touch "$de_was_selected_file" -} - -de_was_selected_on_previous_run() { - [[ -f "$de_was_selected_file" ]] -} - -detect_desktop_environment() { - local de_name="$1" - local executable="$2" - - if command -v "$executable" &>/dev/null; then - return 0 + if [[ -f "$script_dir/$lib" ]]; then + echo "$script_dir/$lib" + return fi - return 1 -} - -did_user_forbid_replacing_xstartup() { - grep -q -v KasmVNC-safe-to-replace-this-file "$xstartup_script" -} - -de_cmd_from_name() { - de_cmd=${all_desktop_environments[$de_name]} -} - -de_name_from_number() { - local de_number_to_run="$1" - - de_name=${numbered_desktop_environments[$de_number_to_run]} -} - -warn_xstartup_will_be_overwriten() { - echo -n "WARNING: $xstartup_script will be overwritten y/N?" - read -r do_overwrite_xstartup - if [[ "$do_overwrite_xstartup" = "y" || "$do_overwrite_xstartup" = "Y" ]]; then - return 0 + if [[ -f "$system_dir/$lib" ]]; then + echo "$system_dir/$lib" + return fi - - return 1 } -setup_de_to_run_via_xstartup() { - warn_xstartup_will_be_overwriten - generate_xstartup "$de_name" -} - -generate_xstartup() { - local de_name="$1" - - de_cmd_from_name - - cat <<-SCRIPT > "$xstartup_script" - #!/bin/sh - exec $de_cmd -SCRIPT - chmod +x "$xstartup_script" -} - -enable_debug() { - debug=1 - log_option="-log *:stderr:100" -} - -kill_vnc_server() { - vncserver -kill $display -} - -process_cli_options() { - for option in "$@"; do - case "$option" in - --help) - show_help - exit - ;; - -d|--debug) - enable_debug - ;; - -k|--kill) - kill_vnc_server - exit - ;; - -s|--select-de) - action=select-de-and-start - ;; - *) - echo >&2 "Unsupported argument: $option" - exit 1 - esac - done -} - -user_asked_to_select_de() { - [[ "$action" = "select-de-and-start" ]] -} - -show_help() { - cat >&2 <<-USAGE -Usage: $(basename "$0") [options] - -d, --debug Debug output - -k, --kill Kill vncserver - -s, --select-de Select desktop environent to run - --help Show this help -USAGE -} +. $(get_lib_path cli-processing.sh) +. $(get_lib_path select-de.sh) process_cli_options "$@" diff --git a/builder/startup/deb/select-de.sh b/builder/startup/deb/select-de.sh new file mode 100644 index 0000000..b43c264 --- /dev/null +++ b/builder/startup/deb/select-de.sh @@ -0,0 +1,127 @@ +#!/bin/bash + +xstartup_script=~/.vnc/xstartup +de_was_selected_file="$HOME/.vnc/.kasmvncserver-easy-start-de-was-selected" + +manual_xstartup_choice="Manually edit xstartup" +declare -A all_desktop_environments=( + [Cinnamon]=cinnamon-session + [Mate]="XDG_CURRENT_DESKTOP=MATE dbus-launch --exit-with-session mate-session" + [LXDE]=lxsession [Lxqt]=startlxqt + [KDE]=startkde + [Gnome]="XDG_CURRENT_DESKTOP=GNOME dbus-launch --exit-with-session /usr/bin/gnome-session" + [XFCE]=xfce4-session) + +readarray -t sorted_desktop_environments < <(for de in "${!all_desktop_environments[@]}"; do echo "$de"; done | sort) + +all_desktop_environments[$manual_xstartup_choice]="" +sorted_desktop_environments+=("$manual_xstartup_choice") + +detected_desktop_environments=() +declare -A numbered_desktop_environments + +print_detected_desktop_environments() { + declare -i i=1 + + echo "Please choose Desktop Environment to run:" + for detected_de in "${detected_desktop_environments[@]}"; do + echo "[$i] $detected_de" + numbered_desktop_environments[$i]=$detected_de + i+=1 + done +} + +detect_desktop_environments() { + for de_name in "${sorted_desktop_environments[@]}"; do + if [[ "$de_name" = "$manual_xstartup_choice" ]]; then + detected_desktop_environments+=("$de_name") + continue; + fi + + local executable=${all_desktop_environments[$de_name]} + executable=($executable) + executable=${executable[-1]} + + if detect_desktop_environment "$de_name" "$executable"; then + detected_desktop_environments+=("$de_name") + fi + done +} + +ask_user_to_choose_de() { + while : ; do + print_detected_desktop_environments + read -r de_number_to_run + de_name_from_number "$de_number_to_run" + if [[ -n "$de_name" ]]; then + break; + fi + + echo "Incorrect number: $de_number_to_run" + echo + done +} + +remember_de_choice() { + touch "$de_was_selected_file" +} + +de_was_selected_on_previous_run() { + [[ -f "$de_was_selected_file" ]] +} + +detect_desktop_environment() { + local de_name="$1" + local executable="$2" + + if command -v "$executable" &>/dev/null; then + return 0 + fi + + return 1 +} + +did_user_forbid_replacing_xstartup() { + grep -q -v KasmVNC-safe-to-replace-this-file "$xstartup_script" +} + +de_cmd_from_name() { + de_cmd=${all_desktop_environments[$de_name]} +} + +de_name_from_number() { + local de_number_to_run="$1" + + de_name=${numbered_desktop_environments[$de_number_to_run]} +} + +warn_xstartup_will_be_overwriten() { + echo -n "WARNING: $xstartup_script will be overwritten y/N?" + read -r do_overwrite_xstartup + if [[ "$do_overwrite_xstartup" = "y" || "$do_overwrite_xstartup" = "Y" ]]; then + return 0 + fi + + return 1 +} + +setup_de_to_run_via_xstartup() { + warn_xstartup_will_be_overwriten + generate_xstartup "$de_name" +} + +generate_xstartup() { + local de_name="$1" + + de_cmd_from_name + + cat <<-SCRIPT > "$xstartup_script" + #!/bin/sh + exec $de_cmd +SCRIPT + chmod +x "$xstartup_script" +} + +user_asked_to_select_de() { + [[ "$action" = "select-de-and-start" ]] +}