#!/usr/bin/env python3
"""
Get the list of Debian mirrors from https://www.debian.org/mirror/list
The JSON file will be saved to /usr/lib/solydxk/system/data/debian_mirrors.json

Current content of page:
...
<h2 id="per-country">Debian Mirrors per Country</h2>
<table border="0" class="center" summary="">
<tr>
  <th>Country</th>
  <th>Site</th>
  <th>Architectures</th>
</tr>
<tr>
  <td>Armenia</td>
  <td><a rel="nofollow" href="http://ftp.am.debian.org/debian/">ftp.am.debian.org/debian/</a></td>
  <td><small><small>amd64 arm64 armel armhf i386 loong64 mips64el mipsel ppc64el riscv64 s390x</small></small></td>
</tr>
...

===============================================================================
Get a list of Debian releases from https://www.debian.org/releases/
The JSON file will be saved to /usr/lib/solydxk/system/data/debian_releases.json

Current content of page:
...
<h2>Index of releases</h2>
<p>See the <a href="../doc/manuals/debian-faq/">Debian FAQ</a> for an explanation of
<a href="../doc/manuals/debian-faq/ftparchives#sourceforcodenames">where
all these codenames came from</a>.</p>
<table border="1" summary="">
<tr>
  <th>Version</th>
  <th>Code Name</th>
  <th>Release Date</th>
  <th>End of Life (EOL)</th>
  <th>End of LTS</th>
  <th>End of ELTS</th>
  <th>Status</th>
</tr>
<tr>
  <td>15</td>
  <td>Duke</td>
  <td>TBA</td>
  <td>TBA</td>
  <td>TBA</td>
  <td>TBA</td>
  <td><a href="https://lists.debian.org/debian-devel-announce/2025/01/msg00004.html">Codename announced</a></td>
</tr>
...

Author: Arjen Balfoort
Date: 2026-03-15
"""

import os
import re
import json
import urllib.parse
import requests
from bs4 import BeautifulSoup   # python3-soupsieve

def _safe_int(s):
    try:
        return int(s)
    except ValueError:
        return None

def get_debian_mirrors():
    """
    Get the list of Debian mirrors from https://www.debian.org/mirror/list
    """
    url = "https://www.debian.org/mirror/list"
    try:
        response = requests.get(url, timeout=5)
    except requests.exceptions.RequestException:
        return {}

    soup = BeautifulSoup(response.text, 'html.parser')
    mirrors = {}

    # Add the default mirror
    mirrors['World'] = [
        {
            'domain': 'deb.debian.org',
            'uri': 'https://deb.debian.org/debian/'
        }
    ]

    # Find the table after the "Debian Mirrors per Country" header
    h2 = soup.find('h2', id='per-country')
    if not h2:
        return {}

    table = h2.find_next('table')
    if not table:
        return {}

    # Iterate over each row in the table (skip header)
    for row in table.find_all('tr')[1:]:
        cols = row.find_all('td')
        if len(cols) < 2:
            continue

        country = cols[0].get_text(strip=True)
        uri = cols[1].find('a').get('href')
        if not uri.startswith('http'):
            continue

        domain = urllib.parse.urlparse(uri).netloc

        if country not in mirrors:
            mirrors[country] = []

        mirrors[country].append({
            'domain': domain,
            'uri': uri
        })

    return mirrors

def get_debian_releases():
    """
    Get the list of Debian releases from https://www.debian.org/releases/
    """
    url = "https://www.debian.org/releases/"
    try:
        response = requests.get(url, timeout=5)
    except requests.exceptions.RequestException:
        return {}

    soup = BeautifulSoup(response.text, 'html.parser')
    releases = {}

    # Find the table after the "Index of releases" header
    h2 = soup.find('h2', string=re.compile(r'index of releases', re.IGNORECASE))
    if not h2:
        return {}

    table = h2.find_next('table')
    if not table:
        return {}

    # Iterate over each row in the table (skip header)
    for row in table.find_all('tr')[1:]:
        cols = row.find_all('td')
        if len(cols) < 2:
            continue

        # The first column is the version
        version = _safe_int(cols[0].get_text(strip=True))
        if version is None or version < 10:
            return releases
        # The second column is the debian release name
        name = cols[1].get_text(strip=True)
        if not name:
            continue

        # Add the release to the list
        if version not in releases:
            releases[version] = name

    return releases

if __name__ == "__main__":
    script_dir = os.path.abspath(os.path.dirname(__file__))

    # Get the list of Debian releases
    json_path = os.path.join(script_dir, 'usr/lib/solydxk/system/data', 'debian_releases.json')
    deb_releases = get_debian_releases()
    if deb_releases:
        with open(file=json_path, mode='w', encoding='utf-8') as f:
            json.dump(deb_releases, f, indent=2)
        print(f"Debian releases saved to {json_path}")
    else:
        print("Failed to get Debian releases")

    # Get the list of Debian mirrors
    json_path = os.path.join(script_dir, 'usr/lib/solydxk/system/data', 'debian_mirrors.json')
    deb_mirrors = get_debian_mirrors()
    if deb_mirrors:
        with open(file=json_path, mode='w', encoding='utf-8') as f:
            json.dump(deb_mirrors, f, indent=2)
        print(f"Debian mirrors saved to {json_path}")
    else:
        print("Failed to get Debian mirrors")
