#!/bin/bash

# Authors:
#   Riccardo Leguti & Julian Del Vecchio <dev@ufficiozero.org>
#
# Description:
#   A post-installation bash script for Ufficio Zero Linux OS
#
# Legal Preamble:
#
# This script is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 3.
#
# This script is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <https://www.gnu.org/licenses/gpl-3.0.txt>

tabs 4
clear

richiedi_pwd=si

# $1=messaggio, $2=colore
function show_msg {
    tput bold;
    [ -n "$2" ] && tput setaf $2;
    echo -e "${1}";
    tput sgr0;
}

function get_msg {
    case "${LANG:0:2}_${1}" in
        it_title ) echo "Ufficio Zero Linux OS post-installazione";;
        *_title ) echo "Ufficio Zero Linux OS post-installation";;
        it_max_pwd )
            echo "Raggiunto il numero massimo di tentativi di inserimento della password.";;
        *_max_pwd )
            echo "Too many failed password attempts. Please try again.";;
        it_ins_pwd ) echo "Inserire la password";;
        *_ins_pwd) echo "Insert the password";;
        it_sel_tools ) echo "Selezionare gli strumenti da installare";;
        *_sel_tools ) echo "Select the tools to install";;
        it_close ) echo "Chiudi";;
        *_close ) echo "Close";;
        it_dev ) echo "sviluppo";;
        *_dev ) echo "development";;
        it_multimedia ) echo "multimedia";;
        *_multimedia ) echo "multimedia";;
        it_graph ) echo "grafica";;
        *_graph ) echo "graphic";;
        it_tech ) echo "tecnica";;
        *_tech ) echo "technology";;
        it_office ) echo "ufficio";;
        *_office ) echo "office";;
        it_school ) echo "scuola";;
        *_school ) echo "school";;
        it_crypto ) echo "criptovaluta";;
        *_crypto ) echo "crypto wallet";;
        it_press ) echo "Premere un tasto per continuare";;
        *_press ) echo "Press a key to continue";;
        it_err_inst ) echo "Errore nell'installazione del pacchetto";;
        *_err_inst ) echo "Error in the installation of the package";;
        it_installed ) echo "installato";;
        *_installed ) echo "was installed";;
        it_inst_pack ) echo "Installazione del pacchetto";;
        *_inst_pack ) echo "Installation of the package";;
        it_language_base) echo "Seleziona la lingua";;
        *_language_base) echo "Select the language";;
        it_language) echo "Completa l'installazione della lingua";;
        *_language) echo "Finish the installation of the language";;
        it_reboot) echo "Il sistema necessita di un riavvio.\nVuoi riavviare ora?";;
        *_reboot) echo "The system needs to be restarted.\nWould you like to restart now?";;
        it_reboot_title) echo "Riavvio necessario";;
        *_reboot_title) echo "Reboot needed";;
        it_restarting) echo "Riavvio in corso";;
        *_restarting) echo "Restarting";;
    esac
}

# Apre la finestra di dialogo per richiedere la password
# return: 0=OK, 1=errore di password, 2=annullato inserimento password, 3=password nulla
function get_pwd {
    [ $richiedi_pwd == "no" ] && return 0
    rimanenti=3;
    while : ;  do
        password=$(whiptail --title "$(get_msg ins_pwd)" --passwordbox "\nPassword:\n" 12 48 3>&1 1>&2 2>&3)
        [ "$?" -eq 1 ] && return 2
        [ -z $password ] && return 3
        sudo -S <<< "$password" echo " "
        if [ $? -eq 0 ]; then
            richiedi_pwd="no"
            return 0
        fi
        let rimanenti--
        if [ "$rimanenti" -eq 0 ]; then
            whiptail --msgbox "$(get_msg max_pwd)" --title "Oops" 8 56
            return 1
        fi
    done
}

distro="$(sed -r -n 's/PRETTY_NAME="(.*)"/\1/p' /etc/os-release)"

# rigenera la cache
if [ "${distro:0:6}" == "Devuan" -o "${distro}" == "UfficioZero GNU/Linux Roma" ]; then
    # richiede la password
    if [ $EUID -ne 0 ]; then
        get_pwd
        if [ $? -ne 0 ]; then
            echo -e "\n$(get_msg press)"
            read -n 1
        else
            sudo -S <<< "$password" sudo apt-get update
        fi
    else
        apt-get clean
    fi    
fi

# cliclo del menu 
while :
do
    # mostra il menu
    linee=`tput lines`
    colonne=`tput cols`
    
    if [ "${distro:0:10}" != "Linux Mint" -a "${distro}" != "LMDE 4 (debbie)" -a "${distro:0:34}" != "UfficioZero customized using Cubic" ]; then
        sel=$(whiptail \
            --notags \
            --title "$(get_msg title)" \
            --menu "\n$(get_msg sel_tools)" \
            --cancel-button "$(get_msg close)" \
            $linee $colonne $(( $linee - 12 )) \
             'language_base' "$(get_msg language_base)" \
             'language' "$(get_msg language)" \
            'development'         $(get_msg dev) \
            'graphic'         $(get_msg graph) \
            'multimedia'         $(get_msg multimedia) \
            'office'         $(get_msg office) \
            'school'       $(get_msg school) \
            'technical'         $(get_msg tech) \
            'crypto wallet'         $(get_msg crypto) \
            3>&1 1>&2 2>&3)
    else
        sel=$(whiptail \
            --notags \
            --title "$(get_msg title)" \
            --menu "\n$(get_msg sel_tools)" \
            --cancel-button "$(get_msg close)" \
            $linee $colonne $(( $linee - 12 )) \
            'development'         $(get_msg dev) \
            'graphic'         $(get_msg graph) \
            'multimedia'         $(get_msg multimedia) \
            'office'         $(get_msg office) \
            'school'       $(get_msg school) \
            'technical'         $(get_msg tech) \
            'crypto wallet'         $(get_msg crypto) \
            3>&1 1>&2 2>&3)
    fi
    [ -z "$sel" ] && exit
    # richiede la password
    if [ $EUID -ne 0 ]; then
        get_pwd
        if [ $? -ne 0 ]; then
            echo -e "\n$(get_msg press)"
            read -n 1
            continue
        fi
    fi
    # installa i pacchetti
    language=$(locale language | tr '[[:upper:]]' '[[:lower:]]')
    if [ "$(echo $language | wc -w)" -eq 2 ]; then
        language="$(echo $language | awk '{ print $2 }')"
    fi
    if [ "${distro:0:10}" == "Linux Mint" -o "${distro}" == "LMDE 4 (debbie)" ]; then # pulisce la cache 
        if [ $EUID -ne 0 ]; then
            sudo -S <<< "$password" sudo apt-get clean
        else
            sudo apt-get clean
        fi
    fi
    if [ "$sel"  ==  "language_base" ]; then
            if [ $EUID -ne 0 ]; then
                sudo -S <<< "$password" dpkg-reconfigure locales
            else
                dpkg-reconfigure locales
            fi
            if ( whiptail --title "$(get_msg reboot_title)" \
                --yesno "$(get_msg reboot)" 10 64 ) then
            # reboot
                echo "$(get_msg restarting)"
                if [ $EUID -ne 0 ]; then
                    sudo -S <<< "$password" reboot
                else
                    reboot
                fi
            fi
    fi
    sed -n -e 's/#.*//' -e 's/^[\t ]*//' -e "/\[${sel}\]/,/\[.*\]/ { /^[^\[].*/ p }" "$(dirname "$0")"/lista | \
    while read pacchetto; do
        [ "${distro:0:6}" == "Devuan" -o  "${distro}" == "UfficioZero GNU/Linux Roma" -o "${distro}" == "LMDE 4 (debbie)" ] \
            && [ "$pacchetto" == "netbeans" -o "$pacchetto" == "obs-studio" -o "$pacchetto" == "exodus" ] && continue
        
        [ "$pacchetto" == "nemwallet" ] && [  "${distro:0:10}" == "Linux Mint" -o "${distro:0:34}" == "UfficioZero customized using Cubic" ] \
            && continue
        
        if [ "$sel" == "language" ]; then
            if [ "$language" == "english" ] && [  "$pacchetto"  ==  "task-language-desktop" ]; then
                pacchetto="task-desktop"
            else
                pacchetto=$( echo "$pacchetto" | sed -e "s/language/${language}/")
            fi
        fi
        show_msg  "\n$(get_msg inst_pack) $pacchetto ..." 7
        if [ $EUID -ne 0 ]; then
            sudo -S <<< "$password" apt install -y  $pacchetto
        else
            apt install -y  $pacchetto
        fi
        if [ $? -eq 0 ]; then
            show_msg "$pacchetto $(get_msg installed)" 2
            continue
        fi
        show_msg "###### $(get_msg err_inst) $pacchetto ######" 1
    done
    # attende la pressione di un tasto
    echo -e "\n$(get_msg press)"
    read -n 1
done


