#!/bin/bash

set -euo pipefail

# Colors for output
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
RESET=$(tput sgr0)

# Detect codename
CODENAME=$(lsb_release --codename --short 2>/dev/null || grep VERSION_CODENAME /etc/os-release | cut -d= -f2)

if [[ -z "$CODENAME" ]]; then
    echo "${RED}Error: Unable to detect Debian codename (e.g., bullseye, bookworm).${RESET}"
    exit 1
fi

echo "${GREEN}Detected Debian codename: $CODENAME${RESET}"

# Add GPG key
echo "${GREEN}Adding VNV signing key...${RESET}"
curl --location --silent https://repo.vnv.dev/vnv-sa-signing-key.gpg | \
    sudo gpg --dearmor --output /usr/share/keyrings/vnv-sa-signing-key.gpg

# Add APT source
echo "${GREEN}Configuring APT source...${RESET}"
echo "deb [signed-by=/usr/share/keyrings/vnv-sa-signing-key.gpg] https://repo.vnv.dev/debian/ $CODENAME main" | \
    sudo tee /etc/apt/sources.list.d/vnvsa.list > /dev/null

# Update package index
echo "${GREEN}Updating package index...${RESET}"
sudo apt update

echo "${GREEN}Done. You can now install VNV packages with apt.${RESET}"

