#!/usr/bin/env bash

# $1 Git only

GIT=${1:-false}
SUDO=${CH_SUDO:-sudo}

arch_packages=(git boost cmake make gcc gdb lib32-sdl2 lib32-glew lib32-freetype2 rsync lib32-libglvnd dialog)
ubuntu_packages=(software-properties-common build-essential git g++ g++-multilib libboost-dev gdb libsdl2-dev:i386 libglew-dev:i386 libfreetype6-dev:i386 cmake dialog rsync)
fedora_packages=(cmake dialog make gcc-c++ glibc-devel.i686 freetype-devel.i686 SDL2-devel.i686 glew-devel.i686 boost-devel rsync gdb git)

# Check if we should only install git
if [ "$GIT" == true ]; then
    arch_packages=(git)
    ubuntu_packages=(git)
    fedora_packages=(git)
fi

function requestPermissions {
    string=$@
    # Prefer GUI question
    if [ -x "$(command -v zenity)" ] && xset q &>/dev/null; then
        zenity --no-wrap --question --text="Do you want to install the following packages required for cathook?\n${string}?"
        out=$?
        if [ "$out" != 0 ]; then
            exit
        fi
        # Fall back to terminal
    elif [ -t 0 ]; then
        read -p "Do you want to install the following packages required for cathook? ${string} y/n " -r
        if ! [[ $REPLY =~ ^[Yy]$ ]]
        then
            exit
        fi
    else
        echo -e "\033[1;33m\nWarning! Automatic package installation is not supported! Zenity+XOrg or interactive terminal required!\n\033[0m"
        exit
    fi
}

# Determine distro
OSR="$(awk -F= '$1=="ID" { print $2 ;}' /etc/os-release)"
if [[ ("$OSR" == "manjaro" || "$OSR" == "arch") && -x "$(command -v pacman)" ]]; then
    OS="arch"
elif [ "$OSR" == "ubuntu" ] && [ -x "$(command -v apt-get)" ]; then
    OS="ubuntu"
elif [ "$OSR" == "fedora" ] && [ -x "$(command -v dnf)" ] && [ -x "$(command -v rpm)" ]; then
    OS="fedora"
elif [ -x "$(command -v pacman)" ]; then
    OS="arch"
elif [ -x "$(command -v apt-get)" ]; then
    OS="ubuntu"
elif [ -x "$(command -v dnf)" ] && [ -x "$(command -v rpm)" ]; then
    OS="fedora"
fi

if [ "$OS" == "arch" ]; then
    pacman -Qi "${arch_packages[@]}" > /dev/null 2>&1
    out=$?
    if [ "$out" == 1 ]; then
        requestPermissions "${arch_packages[@]}"
        $SUDO pacman -S --noconfirm --needed "${arch_packages[@]}"
    fi
elif [ "$OS" == "ubuntu" ]; then
    dpkg -s "${ubuntu_packages[@]}" > /dev/null 2>&1
    out=$?
    if [ "$out" == 0 ]; then
        dpkg --compare-versions "$(dpkg-query --show --showformat '${Version}' cmake)" ge 3.12
        out=$?
    fi
    if [ "$out" == 1 ]; then
        requestPermissions "${ubuntu_packages[@]}"
        pkgs="${ubuntu_packages[@]}"
        $SUDO bash -c "apt-get update;apt-get install -y software-properties-common;add-apt-repository ppa:ubuntu-toolchain-r/test -y;dpkg --add-architecture i386;apt-get update;apt-get install -y $pkgs"
        # Check if cmake is up to date enough
        dpkg --compare-versions "$(dpkg-query --show --showformat '${Version}' cmake)" ge 3.12
        out=$?
        if [ "$out" == 1 ]; then
            $SUDO bash -c "wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | apt-key add -; apt-add-repository -y 'deb https://apt.kitware.com/ubuntu/ bionic main'; apt-get install -y cmake"
        fi
    fi
elif [ "$OS" == "fedora" ]; then
    rpm -q "${fedora_packages[@]}" > /dev/null 2>&1
    out=$?
    if [ "$out" != 0 ]; then
        requestPermissions "${fedora_packages[@]}"
        $SUDO dnf install -y ${fedora_packages[@]}
    fi
else
    echo -e "\033[1;33m\nWarning! Automatic package installation is not supported!\n\033[0m"
fi
