#!/usr/bin/env bash
##makedesc: Install wine (latest) + ppa

set -eo pipefail

if [ "$(uname -s)" = "Darwin" ]; then
    echo "ERROR: wine on macOS requires Wine Skin, CrossOver, or manual setup" >&2
    exit 1
fi

installed() { command -v "$1" >/dev/null 2>&1; }

install() {
    echo
    echo "==============================================="
    echo "Installing wine"
    echo "==============================================="
    echo

    sudo dpkg --add-architecture i386
    if installed wget; then
        wget -qO- https://dl.winehq.org/wine-builds/winehq.key | sudo apt-key add -
    else
        curl -fsSL https://dl.winehq.org/wine-builds/winehq.key | sudo apt-key add -
    fi
    sudo add-apt-repository -y "deb https://dl.winehq.org/wine-builds/ubuntu/ $(lsb_release -cs 2>/dev/null) main"
    sudo apt install -y --autoremove winehq-stable

    echo
    echo "Finish!"
    wine --version
    echo
}

remove() {
    echo
    echo "==============================================="
    echo "Removing wine"
    echo "==============================================="
    echo

    sudo apt remove --purge -y "wine*"
    rm -rf "$HOME/.wine"
    rm -f "$HOME/.config/menus/applications-merged/"*wine*
    rm -rf "$HOME/.local/share/applications/wine"
    rm -f "$HOME/.local/share/desktop-directories/"*wine*
    rm -f "$HOME/.local/share/icons/"*wine*

    echo
    echo "Finish!"
    echo
}

case "$1" in
    r|remove|d|delete|p|purge) remove ;;
    *) install ;;
esac
