#!/usr/bin/env bash
# avoid duplicates
set -x
set -euo pipefail

rm /etc/apt/sources.list
rm /etc/apt/sources.list.d/devuan.list

# If you leave the update too long the keyring expires and your apt breaks.
install_devuan_keyring() {
  local BASE_URL="https://pkgmaster.devuan.org/devuan/pool/main/d/devuan-keyring/"
  local DEB_DIR="/tmp"

  # Check for wget dependency
  if ! command -v wget &>/dev/null; then
    echo "Error: wget is not installed." >&2
    return 1
  fi

  echo "Searching for the latest devuan-keyring..."

  # Fetch directory listing, extract .deb links, filter, sort by version, take latest
  local LATEST_DEB
  LATEST_DEB=$(wget -q -O - "$BASE_URL" |
    grep -oP 'devuan-keyring_\d{4}\.\d{2}\.\d{2}_all\.deb' |
    sort -rV |
    head -n 1)

  if [ -z "$LATEST_DEB" ]; then
    echo "Error: Could not find any devuan-keyring_*.deb in listing" >&2
    return 1
  fi

  echo "Latest found: $LATEST_DEB"
  local FULL_URL="${BASE_URL}${LATEST_DEB}"
  local DEB_PATH="${DEB_DIR}/${LATEST_DEB}"

  echo "Downloading to $DEB_PATH..."
  wget -q --show-progress -O "$DEB_PATH" "$FULL_URL"

  echo "Installing package..."
  sudo dpkg -i "$DEB_PATH"

  # Clean up the downloaded file
  rm "$DEB_PATH"
  echo "Installation complete and cleanup finished."
}
install_devuan_keyring

sudo apt install -y usrmerge
sudo apt install -y apt-transport-tor apt-transport-https

TRANSPORT="https"
#MIRROR=deb
#MIRROR=pkgmaster
MIRROR=deb
MERGED_URI="${TRANSPORT}://${MIRROR}.devuan.org/merged/"
DEVUAN_URI="${TRANSPORT}://${MIRROR}.devuan.org/devuan"
ACTIVE=/etc/apt/sources.list.d
AVAILABLE=/etc/apt/sources.list-available
mkdir -p "${AVAILABLE}"

SECTION=(main contrib non-free non-free-firmware)

# For standard merged suites (main contrib non-free non-free-firmware)
SECTION_MERGED="main contrib non-free non-free-firmware"
#
# For experimental / proposed (usually just main)
SECTION_SPECIAL="main"

# ACTIVE_LIST=('freia')
# ACTIVE_LIST=('freia' 'freia-security' 'freia-updates' 'freia-backports')
# ACTIVE_LIST=('excalibur')
# ACTIVE_LIST=('excalibur' 'excalibur-security' 'excalibur-updates' 'excalibur-backports')
# ACTIVE_LIST=('daedalus')
# ACTIVE_LIST=('daedalus' 'daedalus-security' 'daedalus-updates' 'daedalus-backports')
# ACTIVE_LIST=('chimaera' 'chimaera-security' 'chimaera-updates' 'chimaera-backports')
# ACTIVE_LIST=('beowulf' 'beowulf-security' 'beowulf-updates' 'beowulf-backports')
# ACTIVE_LIST=('ascii' 'ascii-security' 'ascii-updates' 'ascii-backports')
# INACTIVE_LIST=('jessie' 'jessie-security' 'jessie-updates' 'jessie-backports' 'beowulf' 'beowulf-security' 'beowulf-updates' 'beowulf-backports' 'ceres')
INACTIVE_LIST=('jessie' 'jessie-security' 'jessie-updates' 'jessie-backports' 'ascii' 'ascii-security' 'ascii-updates' 'ascii-backports' 'beowulf' 'beowulf-security' 'beowulf-updates' 'beowulf-backports' 'chimaera' 'chimaera-security' 'chimaera-updates' 'chimaera-backports' 'ceres')

OTHER_LIST=('experimental')

SECTION=(main contrib non-free non-free-firmware)
ACTIVE_LIST=('excalibur' 'excalibur-security' 'excalibur-updates' 'excalibur-backports')

# New: proposed-updates suites (disabled by default - production risk)
PROPOSED_LIST=('excalibur-proposed-updates')

# Active (enabled) loop – all merged in your current choice
for RELEASE in "${ACTIVE_LIST[@]}"; do
  echo "${RELEASE}"
  bash -c "cat > ${AVAILABLE}/devuan_${RELEASE}.sources" <<EOF
	Enabled: yes
	Types: deb deb-src
	URIs: ${MERGED_URI}
	Suites: ${RELEASE}
	Components: ${SECTION_MERGED}
	Architectures: amd64
EOF
  ln -sf "${AVAILABLE}/devuan_${RELEASE}.sources" "${ACTIVE}/devuan_${RELEASE}.sources"
done

# Special / other (experimental, and potentially *-proposed-updates later)
for RELEASE in "${OTHER_LIST[@]}"; do
  echo "${RELEASE}"
  bash -c "cat > ${AVAILABLE}/devuan_${RELEASE}.sources" <<EOF
	Enabled: yes
	Types: deb deb-src
	URIs: ${DEVUAN_URI}
	Suites: ${RELEASE}
	Components: ${SECTION_SPECIAL}
	Architectures: amd64
	# Signed-By: /usr/share/keyrings/devuan-archive-keyring.gpg  # optional, if needed
EOF
  ln -sf "${AVAILABLE}/devuan_${RELEASE}.sources" "${ACTIVE}/devuan_${RELEASE}.sources"
done

echo ${#OTHER_LIST[@]}
for RELEASE in "${OTHER_LIST[@]}"; do
  echo "${RELEASE}"
  bash -c "cat > ${AVAILABLE}/devuan_${RELEASE}.sources" <<EOF
Enabled: yes
Types: deb deb-src
URIs: ${TRANSPORT}://${MIRROR}.devuan.org/devuan
Suites: ${RELEASE}
Components: main
Architectures: amd64
EOF
done

# ────────────────────────────────────────────────
# Proposed-updates loop (disabled by default)
# ────────────────────────────────────────────────
echo ${#PROPOSED_LIST[@]}
for RELEASE in "${PROPOSED_LIST[@]}"; do
  echo "${RELEASE}"
  bash -c "cat > ${AVAILABLE}/devuan_${RELEASE}.sources" <<EOF
Enabled: no
Types: deb deb-src
URIs: ${DEVUAN_URI}
Suites: ${RELEASE}
Components: ${SECTION_SPECIAL}
Architectures: amd64
# Note: proposed-updates for upcoming point releases; not for production
EOF
  # Optional symlink even if disabled
  ln -sf "${AVAILABLE}/devuan_${RELEASE}.sources" "${ACTIVE}/devuan_${RELEASE}.sources"
done

# Onion setup (uncomment/adapt as needed)
ONION="devuanauxrkggcowgm2vcs6go3c5pgxdidd5wqjpg7zpfaxkmgspr6id.onion"
TOR_TRANSPORT="tor+http"
TOR_MERGED_URI="${TOR_TRANSPORT}://${ONION}/merged/"
TOR_DEVUAN_URI="${TOR_TRANSPORT}://${ONION}/devuan"

# Which suites get onion variants? (e.g. active + experimental/proposed)
# You can make ONION_LIST=("${ACTIVE_LIST[@]}" "${OTHER_LIST[@]}" "${PROPOSED_LIST[@]}")
ONION_LIST=('excalibur' 'excalibur-security' 'excalibur-updates' 'excalibur-backports' 'experimental' 'excalibur-proposed-updates')

# ────────────────────────────────────────────────
# Onion / Tor variants (optional - create separate files)
# ────────────────────────────────────────────────
# Uncomment the block below if you want Tor variants generated
#
echo "Generating onion variants for: ${ONION_LIST[*]}"
for RELEASE in "${ONION_LIST[@]}"; do
  echo "${RELEASE} (onion)"

  # Decide URI based on suite type (same logic as clearnet)
  if [[ "$RELEASE" == *proposed-updates || "$RELEASE" == "experimental" ]]; then
    TOR_URI="${TOR_DEVUAN_URI}"
    SECTION_USED="${SECTION_SPECIAL}"
  else
    TOR_URI="${TOR_MERGED_URI}"
    SECTION_USED="${SECTION_MERGED}" # assume you defined SECTION_MERGED earlier as "main contrib non-free non-free-firmware"
  fi

  bash -c "cat > ${AVAILABLE}/devuan_${RELEASE}-onion.sources" <<EOF
Enabled: no
Types: deb deb-src
URIs: ${TOR_URI}
Suites: ${RELEASE}
Components: ${SECTION_USED}
Architectures: amd64
# Tor / onion routing - requires apt-transport-tor installed and tor running
EOF

  ln -sf "${AVAILABLE}/devuan_${RELEASE}-onion.sources" "${ACTIVE}/devuan_${RELEASE}-onion.sources"
done

rm /etc/apt/sources.list

#bash -c "cat > /etc/apt/sources.list.d/x2go.list" <<'EOF'
## X2Go Repository (release builds)
#deb http://packages.x2go.org/debian jessie main
#
## X2Go Repository (sources of release builds)
#deb-src http://packages.x2go.org/debian jessie main
#
## X2Go Repository (nightly builds)
##deb http://packages.x2go.org/debian jessie heuler
#
## X2Go Repository (sources of nightly builds)
##deb-src http://packages.x2go.org/debian jessie heuler
#
## apt install -y x2goserver x2goserver-xsession
#EOF
