#!/bin/sh
#
# Writes the final sources configuration file in DEB822 format
#

CHROOT=$(mount | grep proc | grep calamares | awk '{print $3}' | sed -e "s#/proc##g")
RELEASE="sinambung"

# Which archive the installed system is pointed at is decided by the ISO, not
# by this package: live-build ships its choice as /etc/blankon/archive.conf.
# Read it from the live session we are running in -- note the deliberate lack
# of $CHROOT, the target has no such file yet. The fallback is the production
# archive, so a missing or unreadable file can only ever leave a development
# ISO pointing at production, never the reverse.
ARCHIVE_URI="http://arsip.blankonlinux.id/sinambung/"

if [ -r /etc/blankon/archive.conf ]; then
    . /etc/blankon/archive.conf
fi

cat <<EOF > "$CHROOT"/etc/apt/sources.list.d/blankon.sources
Types: deb deb-src
URIs: $ARCHIVE_URI
Suites: $RELEASE
Components: main extras restricted restricted-firmware
Signed-By: /usr/share/keyrings/blankon-archive-keyring.gpg

EOF

cat <<EOF > "$CHROOT"/etc/apt/sources.list.d/nvidia-cuda.sources
Types: deb
URIs: https://developer.download.nvidia.com/compute/cuda/repos/debian13/x86_64/
Suites: /
Signed-By: /usr/share/keyrings/cuda-archive-keyring.gpg

EOF

# Remove legacy sources.list if it exists to ensure clean APT configuration
if [ -f "$CHROOT/etc/apt/sources.list" ]; then
    echo "Removing legacy /etc/apt/sources.list"
    rm -f "$CHROOT/etc/apt/sources.list"
fi

# Remove legacy cuda-debian13-x86_64.list if it exists to ensure clean APT configuration
if [ -f "$CHROOT/etc/apt/sources.list.d/cuda-debian13-x86_64.list" ]; then
    echo "Removing legacy /etc/apt/sources.list.d/cuda-debian13-x86_64.list"
    rm -f "$CHROOT/etc/apt/sources.list.d/cuda-debian13-x86_64.list"
fi

exit 0
