#!/bin/bash

# Authors:
#   Riccardo Leguti & Julian Del Vecchio <dev@ufficiozero.org>
#
# Description:
#   A post-installation bash script for Ufficio Zero
#
# 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 post-installazione ver 0.2";;
        *_title ) echo "Ufficio Zero post-installation ver 0.2";;
        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_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";;
        
    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 prova
        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
}
 
while :
do
    # mostra il menu
    linee=`tput lines`
    colonne=`tput cols`
    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) \
        3>&1 1>&2 2>&3) 
    [ -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
    sed -n -e 's/#.*//' -e 's/^[\t ]*//' -e "/\[${sel}\]/,/\[.*\]/ { /^[^\[].*/ p }" "$(dirname "$0")"/lista | \
    while read pacchetto; do
        show_msg  "\n$(get_msg inst_pack) $pacchetto ..." 7
        apt install -y  $pacchetto
        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

