#!/bin/bash

# Universal Setup Script for GTK2 MPV Player
# Identifies the Linux distribution and installs required build dependencies.

set -e

PRINT_INFO() { echo -e "\e[34m[INFO]\e[0m $1"; }
PRINT_SUCCESS() { echo -e "\e[32m[SUCCESS]\e[0m $1"; }
PRINT_ERROR() { echo -e "\e[31m[ERROR]\e[0m $1"; }
PRINT_WARNING() { echo -e "\e[33m[WARNING]\e[0m $1"; }

OS_TYPE=""
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS_TYPE=$ID
else
    PRINT_ERROR "Could not detect operating system type. /etc/os-release missing."
    exit 1
fi

PRINT_INFO "Detected operating system: $OS_TYPE"

install_debian_deps() {
    PRINT_INFO "Installing dependencies for Debian/Ubuntu/Mint..."
    sudo apt-get update
    sudo apt-get install -y build-essential pkg-config libgtk2.0-dev libmpv-dev
}

install_fedora_deps() {
    PRINT_INFO "Installing dependencies for Fedora/RHEL/CentOS..."
    sudo dnf groupinstall -y "Development Tools"
    sudo dnf install -y pkg-config gtk2-devel mpv-devel
}

install_arch_deps() {
    PRINT_INFO "Installing dependencies for Arch/Manjaro..."
    sudo pacman -S --needed base-devel pkg-config gtk2 mpv
}

install_opensuse_deps() {
    PRINT_INFO "Installing dependencies for openSUSE..."
    sudo zypper install -t pattern devel_C_C++
    sudo zypper install -y pkg-config gtk2-devel mpv-devel
}

install_slackware_deps() {
    PRINT_INFO "Checking dependencies for Slackware..."
    PRINT_WARNING "Slackware dependency management varies. Please ensure the following are installed:"
    echo "  - gtk+2"
    echo "  - mpv (with client library support)"
    echo "  - pkg-config"
    echo ""
    PRINT_INFO "If you have slackpkg+ or similar, you can try searching for 'gtk+2' and 'mpv'."
}

case "$OS_TYPE" in
    ubuntu|debian|linuxmint|pop|kali|raspbian)
        install_debian_deps
        ;;
    fedora|centos|rhel|almalinux|rocky)
        install_fedora_deps
        ;;
    arch|manjaro)
        install_arch_deps
        ;;
    opensuse*|suse)
        install_opensuse_deps
        ;;
    slackware)
        install_slackware_deps
        ;;
    *)
        PRINT_WARNING "Unsupported or unknown distribution: $OS_TYPE"
        PRINT_INFO "Please manually install the development files for: gtk2 and mpv."
        exit 1
        ;;
esac

PRINT_SUCCESS "Dependency installation step complete."
PRINT_INFO "You can now build the player using 'make'."
