#!/bin/bash

# Function to check if a command exists
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# Function to install dependencies
install_dependencies() {
    # Download and verify dependencies
    deps_url="https://stellarmate.com/jdownloads/dependencies/$DEPS_FILE"
    echo "Downloading dependencies from $deps_url"
    deps_list=$(wget -qO- "$deps_url")

    if [ -z "$deps_list" ]; then
        echo "Error: Failed to download dependencies from $deps_url"
        exit 1
    fi

    echo "Dependencies to install: $deps_list"

    if [[ "$ID" == "arch" ]] || [[ "$ID_LIKE" == *"arch"* ]]; then
        echo "Detected Arch Linux. Installing dependencies using pacman..."
        sudo pacman -Syu --needed --noconfirm $deps_list
    else
        echo "Detected Debian/Ubuntu based system. Installing dependencies using apt-get..."
        # Configure APT options as an array
        APT_OPTIONS=(-o 'Dpkg::Options::=--force-overwrite' --fix-broken -y)

        sudo dpkg --configure -a
        sudo apt-get update
        # Install dependencies with proper option handling
        sudo apt-get "${APT_OPTIONS[@]}" install $deps_list
    fi
}

build_kstars()
{
    mkdir -p ~/Projects/build/kstars || return 1
    cd ~/Projects/build/kstars || return 1
    if [[ "$ID" == "arch" ]] || [[ "$ID_LIKE" == *"arch"* ]]; then
        cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=Off -DBUILD_WITH_QT6=ON -DCCACHE_SUPPORT=ON ~/Projects/kstars || return 1
    else
        cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=Off -DCCACHE_SUPPORT=ON ~/Projects/kstars || return 1
    fi
    cmake --build . || return 1
    sudo cmake --install . || return 1
    return 0
}

# Detect OS
if [ -f /etc/os-release ]; then
    . /etc/os-release
    # Check both ID and ID_LIKE for Ubuntu and its derivatives
    if [[ "$ID" == "ubuntu" ]] || [[ "$ID" == "kubuntu" ]] || [[ "$ID" == "neon" ]] || [[ "$ID_LIKE" == *"ubuntu"* ]]; then
        DEPS_FILE="kstars-ubuntu.txt" # Assuming a kstars-ubuntu.txt exists or will be created
    elif [[ "$ID" == "arch" ]] || [[ "$ID_LIKE" == *"arch"* ]]; then # Check for Arch Linux
        DEPS_FILE="kstars-arch.txt"
    else
        DEPS_FILE="kstars.txt" # Default for Debian and others
    fi
else
    echo "Cannot detect OS, defaulting to Debian configuration (kstars.txt)"
    DEPS_FILE="kstars.txt"
fi

# Install dependencies if essential tools are missing or if build directory doesn't exist
if ! command_exists git || ! command_exists cmake || [ ! -d ~/Projects/build/kstars ]; then
    echo "Installing/reinstalling dependencies..."
    install_dependencies
fi

# Try building if directory exists
if [ -d ~/Projects/build/kstars ]; then
    git -C ~/Projects/kstars pull
    if ! build_kstars; then
        echo "Build error. Cleaning KStars building directory and trying again"
        rm -rf ~/Projects/build/kstars
        if ! build_kstars; then
            echo "Build failed again after cleanup. Aborting."
            exit 1
        fi
    fi
    exit 0
fi
mkdir -p ~/Projects || true
cd ~/Projects
git clone --depth 1 https://invent.kde.org/education/kstars.git

# Try fresh build
if ! build_kstars; then
    echo "Build error. Cleaning and trying one more time"
    rm -rf ~/Projects/build/kstars
    if ! build_kstars; then
        echo "Build failed again after cleanup. Aborting."
        exit 1
    fi
fi
