#!/bin/bash

# Raweb Repository Installation Script
# This script adds the Raweb repository to your system

set -e

REPO_URL="https://repo.raweb.al"
REPO_NAME="raweb"

# Detect the distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
    DISTRO_ID="$ID"
    VERSION_CODENAME="$VERSION_CODENAME"
elif [ -f /etc/debian_version ]; then
    DISTRO_ID="debian"
    VERSION_CODENAME=$(lsb_release -cs 2>/dev/null || echo "unknown")
else
    echo "Error: Unable to detect your distribution"
    exit 1
fi

# Check if we support this distribution
case "$DISTRO_ID" in
    "ubuntu")
        case "$VERSION_CODENAME" in
            "jammy"|"noble")
                SUPPORTED_DISTRO="$VERSION_CODENAME"
                ;;
            *)
                echo "Error: Ubuntu $VERSION_CODENAME is not supported"
                echo "Supported Ubuntu versions: jammy (22.04), noble (24.04)"
                exit 1
                ;;
        esac
        ;;
    "debian")
        case "$VERSION_CODENAME" in
            "bullseye"|"bookworm")
                SUPPORTED_DISTRO="$VERSION_CODENAME"
                ;;
            *)
                echo "Error: Debian $VERSION_CODENAME is not supported"
                echo "Supported Debian versions: bullseye (11), bookworm (12)"
                exit 1
                ;;
        esac
        ;;
    *)
        echo "Error: $DISTRO_ID is not supported"
        echo "Supported distributions: Ubuntu (jammy, noble), Debian (bullseye, bookworm)"
        exit 1
        ;;
esac

echo "Detected: $DISTRO_ID $SUPPORTED_DISTRO"
echo "Installing Raweb Repository..."

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo "Error: This script must be run as root"
    echo "Please run: sudo $0"
    exit 1
fi

# Install required packages
echo "Installing required packages..."
apt-get update -qq
apt-get install -y curl gpg

# Add GPG key
echo "Adding repository GPG key..."
curl -fsSL "$REPO_URL/raweb-repo.gpg" -o /tmp/raweb-repo.gpg
gpg --dearmor < /tmp/raweb-repo.gpg > /usr/share/keyrings/raweb-repo.gpg
rm /tmp/raweb-repo.gpg

# Check if system uses DEB822 format (Ubuntu 24.04+ and newer Debian)
if [ -d "/etc/apt/sources.list.d" ] && [ "$DISTRO_ID" = "ubuntu" ] && [ "$VERSION_CODENAME" = "noble" ]; then
    # Use modern DEB822 format for Ubuntu 24.04+
    echo "Adding repository using DEB822 format..."
    cat > /etc/apt/sources.list.d/raweb.sources << EOF
Types: deb
URIs: $REPO_URL
Suites: $SUPPORTED_DISTRO
Components: main
Architectures: amd64
Signed-By: /usr/share/keyrings/raweb-repo.gpg
EOF
else
    # Use traditional format for older systems
    echo "Adding repository using traditional format..."
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/raweb-repo.gpg] $REPO_URL $SUPPORTED_DISTRO main" > /etc/apt/sources.list.d/raweb.list
fi

# Update package list
echo "Updating package list..."
apt-get update

echo "Raweb repository has been successfully added!"
echo "You can now install packages using: apt install <package-name>"
echo ""
echo "Available packages can be found at: $REPO_URL"
