#!/bin/bash
bash $(realpath $(dirname $0))/maneki-logo
menu(){
    dialog --clear --title "Which LibreOffice do you want?" \
        --menu "Choose one of the following options:" 15 40 4 \
        1 "libreoffice-still" \
        2 "libreoffice-fresh" \
        3 "LibreOffice (Flatpak)" \
        4 "Exit" 2>&1 >/dev/tty
}

check_flatpak() {
    if ! command -v flatpak &>/dev/null; then
        echo "Flatpak is not installed. Installing flatpak!"
        sudo pacman -S --needed flatpak
        flatpak --user remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
        exit 1
    fi
}

while true; do
    choice=$(menu)
    case $choice in
        1)
            echo "Downloading libreoffice-still..."
            sudo pacman -S libreoffice-still --noconfirm
            echo -e "\n==> Downloaded and Installed libreoffice-still."
            ;;
        2)
            echo "Downloading libreoffice-fresh..."
            sudo pacman -S libreoffice-fresh --noconfirm
            echo -e "\n==> Downloaded and Installed libreoffice-fresh."
            ;;
        3)
            check_flatpak
            echo "Downloading LibreOffice (Flatpak)..."
            flatpak install flathub org.libreoffice.LibreOffice
            echo -e "\n==> Downloaded and Installed LibreOffice (Flatpak)."
            ;;
        4)
            echo "Exiting..."
            exit 0
            ;;
    esac
done
