From 3c714486e1d1e4d85221937d72a15348b75704de Mon Sep 17 00:00:00 2001
From: tavo-wasd <gustavo@gustavocalvo.xyz>
Date: Fri, 17 May 2024 17:34:42 -0600
Subject: [PATCH] one menu

---
 04-main.sh | 154 ++++++++++++++++++++++++-----------------------------
 1 file changed, 71 insertions(+), 83 deletions(-)

diff --git a/04-main.sh b/04-main.sh
index 6930029..411debb 100644
--- a/04-main.sh
+++ b/04-main.sh
@@ -4,107 +4,95 @@
 # - Check if already installed
 # - Uninstall
 # - Allow to use already set variables
-
 # main
 set_lang
 set_version
 set_menu
 
+menu info "$PROMPT_WELCOME"
+
+if ! command -v curl > /dev/null ; then
+    menu error "$PROMPT_ERR_DEPS curl"
+    exit 1
+fi
+
+SERIAL="$(menu entry "$PROMPT_SERIAL")"
+if [ -z "$SERIAL" ] ; then
+    menu error "$PROMPT_ERR_SERIAL"
+    exit 1
+fi
+
+ARCHIVE="$(get_archive)"
+TEMPKEY="${ARCHIVE##* }"
+FILE="${ARCHIVE%% *}"
+
+DOWNLOAD_URL="$(
+    printf 'https://soportefirmadigital.com/sfdj/getiso.aspx?tempkey=%s' \
+        "$TEMPKEY"
+)"
+
+SIZE="$(curl -sI "$DOWNLOAD_URL" |
+    sed '/[Cc]ontent-[Ll]ength/!d;s/^.*: //g' |
+    awk '{$1/=1024;printf "%d",$1}'
+)"
+
+if [ "$SIZE" -lt 500 ] || [ -z "$FILE" ] ; then
+    menu error "$PROMPT_ERR_DOWNLOAD"
+    exit 1
+fi
+
+SAVE_DIR="/tmp/soportefirmadigital"
+SAVE_FILE="$SAVE_DIR/$FILE"
+mkdir -p "$SAVE_DIR"
+
 if [ "$MENU" = "zenity" ] ; then
-    echo_debug "Iniciando zenity" # DEBUG
-    MENU="zenity"
-    zenity --title "$TITLE" --text "$PROMPT_WELCOME" --info
-    ! command -v curl > /dev/null && zenity --title "$TITLE" --text "$PROMPT_ERR_DEPS curl" --error && exit 1
-
-    echo_debug "Pregunta serial" # DEBUG
-    SERIAL="$(zenity --title "$TITLE" --text "$PROMPT_SERIAL" --entry)"
-    [ -z "$SERIAL" ] && zenity --title "$TITLE" --text "$PROMPT_ERR_SERIAL" --error && exit 1
-
-    echo_debug "Generar tempkey y obtener URL de descarga" # DEBUG
-    ARCHIVE="$(get_archive)"
-    TEMPKEY="${ARCHIVE##* }"
-    FILE="${ARCHIVE%% *}"
-    DOWNLOAD_URL="$(printf 'https://soportefirmadigital.com/sfdj/getiso.aspx?tempkey=%s' "$TEMPKEY")"
-    [ -z "$DOWNLOAD_URL" ] && zenity --title "$TITLE" --text "$PROMPT_ERR_DOWNLOAD" --error && exit 1
-    [ -z "$FILE" ] && zenity --title "$TITLE" --text "$PROMPT_ERR_DOWNLOAD" --error && exit 1
-    SAVE_DIR="/tmp/soportefirmadigital"
-    SAVE_FILE="$SAVE_DIR/$FILE"
-    mkdir -p "$SAVE_DIR"
-    SIZE="$(curl -sI "$DOWNLOAD_URL" | sed '/[Cc]ontent-[Ll]ength/!d;s/^.*: //g' | awk '{$1/=1024;printf "%d",$1}')"
-
-    echo_debug "Descargar y mostrar progreso" # DEBUG
     (curl -sL "$DOWNLOAD_URL" -o "$SAVE_FILE") &
     while true ; do
         sleep 0.5
-        DOWN="$(du "$SAVE_FILE" 2>/dev/null | awk '{print $1}')" ; [ -z "$DOWN" ] && DOWN=0
+        DOWN="$(du "$SAVE_FILE" 2>/dev/null | awk '{print $1}')"
+        [ -z "$DOWN" ] && DOWN=0
         r=$(((DOWN*10000)/SIZE))
         printf '%d\n' ${r%??}
     done | zenity --title "$TITLE" --text "$PROMPT_DOWNLOAD" --progress --auto-close
-    while true ; do
-        ACTIVE="$(ps aux | grep 'curl.*soportefirmadigital' | sed '/grep/d')"
-        [ -z "$ACTIVE" ] && break
-        sleep 0.5
-    done | zenity --title "$TITLE" --text "$PROMPT_DOWNLOAD" --progress --pulsate --auto-close
-    ACTIVE="$(ps aux | grep 'curl.*soportefirmadigital' | sed '/grep/d')"
-    [ -n "$ACTIVE" ] && zenity --title "$TITLE" --text "$PROMPT_ERR_DOWNLOAD" --error && exit 1
 
-    echo_debug "Consultar sudo pass" # DEBUG
-    SUDO_PASSWORD="$(zenity --title "$TITLE" --password)"
-    [ -z "$SUDO_PASSWORD" ] && zenity --title "$TITLE" --text "$PROMPT_ERR_DEPS_INSTALL" --error && exit 1
-    CORRECT_SUDO_PASSWORD="$(printf '%s' "$SUDO_PASSWORD" | sudo -Skp '' whoami >/dev/null 2>&1 || printf 'no')"
-    [ "$CORRECT_SUDO_PASSWORD" = "no" ] && zenity --title "$TITLE" --text "$PROMPT_ERR_DEPS_INSTALL" --error && exit 1
+elif [ "$MENU" = "term" ] ; then
+    menu info "$PROMPT_DOWNLOAD" && echo
+    curl "$DOWNLOAD_URL" -o "$SAVE_FILE" --progress-bar
 
-    # Attempt to install, forward output to zenity
+fi
+
+ACTIVE="$(ps -t | grep 'curl.*soportefirmadigital' | sed '/grep/d')"
+ACTIVE="${ACTIVE# }"
+ACTIVEID="${ACTIVE%% *}"
+if [ -n "$ACTIVE" ] ; then
+    menu error "$PROMPT_ERR_DOWNLOAD"
+    echo_debug "Killing process ID: $ACTIVEID from: $ACTIVE" # DEBUG
+    kill "$ACTIVEID"
+    exit 1
+fi
+
+SUDO_PASSWORD="$(menu pass "$PROMPT_PASS_DEPS_INSTALL")"
+if [ -z "$SUDO_PASSWORD" ] || ! tsudo whoami >/dev/null 2>&1 ; then
+    menu error "$PROMPT_ERR_DEPS_INSTALL"
+    exit 1
+fi
+
+if [ "$MENU" = "zenity" ] ; then
+    # Attempt to install, forward output to program
     # but keep exit code of install function
-    echo_debug "Funcion install_certs, corre de acuerdo al OS" # DEBUG
     ( ( ( (install_certs; echo $? >&3) |
         zenity --title "$TITLE" --text "$PROMPT_DEPS_INSTALL" --progress --pulsate --auto-close >&4) 3>&1 ) |
         (read -r xs; exit "$xs") ) 4>&1
     #install_certs # Just run this instead to see debug info # DEBUG
-
-    # install_certs fails (exit code != 0)
-    [ "$?" != "0" ] && zenity --title "$TITLE" --text "$PROMPT_ERR_DEPS_INSTALL" --error && exit 1
-
-    echo_debug "Termina correctamente" # DEBUG
-    zenity --title "$TITLE" --text "$PROMPT_END_SUCCESS" --info
-
+    if [ "$?" != "0" ] ; then
+        menu error "$PROMPT_ERR_DEPS_INSTALL"
+        exit 1
+    fi
 elif [ "$MENU" = "term" ] ; then
-    term_prompt info "$PROMPT_WELCOME"
-    read -r nil
-    ! command -v curl >/dev/null 2>&1 && term_prompt error "$PROMPT_ERR_DEPS curl" && exit 1
-
-    # Serial number is required for download
-    term_prompt entry "$PROMPT_SERIAL"
-    read -r SERIAL
-    [ -z "$SERIAL" ] && term_prompt error "$PROMPT_ERR_SERIAL" && exit 1
-
-    echo_debug "Generar tempkey y obtener URL de descarga" # DEBUG
-    ARCHIVE="$(get_archive)"
-    TEMPKEY="${ARCHIVE##* }"
-    FILE="${ARCHIVE%% *}"
-    DOWNLOAD_URL="$(printf 'https://soportefirmadigital.com/sfdj/getiso.aspx?tempkey=%s' "$TEMPKEY")"
-    [ -z "$DOWNLOAD_URL" ] && term_prompt error "$PROMPT_ERR_DOWNLOAD" && exit 1
-    [ -z "$FILE" ]         && term_prompt error "$PROMPT_ERR_DOWNLOAD" && exit 1
-    SAVE_DIR="/tmp/soportefirmadigital"
-    SAVE_FILE="$SAVE_DIR/$FILE"
-    mkdir -p "$SAVE_DIR"
-    SIZE="$(curl -sI "$DOWNLOAD_URL" | sed '/[Cc]ontent-[Ll]ength/!d;s/^.*: //g' | awk '{$1/=1024;printf "%d",$1}')"
-
-    term_prompt info "$PROMPT_DOWNLOAD" && echo
-    curl "$DOWNLOAD_URL" -o "$SAVE_FILE" --progress-bar
-
-    echo_debug "Consultar sudo pass" # DEBUG
-    term_prompt entry "$PROMPT_PASS_DEPS_INSTALL"
-    IFS= read -r SUDO_PASSWORD
-    [ -z "$SUDO_PASSWORD" ] && term_prompt error "$PROMPT_ERR_DEPS_INSTALL" && exit 1
-    CORRECT_SUDO_PASSWORD="$(tsudo whoami >/dev/null 2>&1 || printf 'no')"
-    [ "$CORRECT_SUDO_PASSWORD" = "no" ] && term_prompt error "$PROMPT_ERR_DEPS_INSTALL" && exit 1
-
-    # Install dependencies, components and
-    # certificates according to OS
-    term_prompt info "$PROMPT_DEPS_INSTALL" && echo
-    ! install_certs "$SAVE_FILE" && term_prompt error "$PROMPT_ERR_DEPS_INSTALL" && exit 1
-
-    term_prompt info "$PROMPT_END_SUCCESS" && echo
-
+    menu info "$PROMPT_DEPS_INSTALL" && echo
+    ! install_certs && menu error "$PROMPT_ERR_DEPS_INSTALL" && exit 1
 fi
+
+menu info "$PROMPT_END_SUCCESS"
+
+exit 0