#!/usr/bin/python3
#
# repology-openmamba.py - find upstream packages updates from repology.org
# Copyright (c) 2021-2024 by Silvan Calarco <silvan.calarco@mambasoft.it>
#

import json
import rpm
import sys
import time
import urllib.request


with open("/var/ftp/pub/openmamba/base/legacy") as f:
    legacy_packages = f.read().splitlines()

with open("/var/ftp/pub/openmamba/base/updates-conf.json") as f:
    data = json.loads(f.read())
    ignored_packages = data["repology"]["ignore"]

from_package = ""
last_from_package = "NONE"
results = []

while True:
    if from_package == last_from_package:
        break
    requrl = "https://repology.org/api/v1/projects/"
    if from_package != "":
        requrl += from_package +"/"
    requrl += "?inrepo=openmamba&outdated=1"
    sys.stderr.write("Requesting: " + requrl + "\n")
    url = urllib.request.urlopen(requrl)
    #print(requrl)
    data = json.loads(url.read().decode())

    if from_package != "" and len(data.keys()) <= 1:
        break
    for k in data.keys():
        if k == from_package:
            continue
        package = k
        openmamba_srcname = None
        openmamba_version = None
        openmamba_altnames = []
        newest_version = None
        newest_repos = []
        for p in data[k]:
            if p["repo"] == "openmamba":
                if not p["srcname"] in ignored_packages and \
                    not p["srcname"] in legacy_packages:
                        if openmamba_srcname is None:
                            openmamba_version = p["version"]
                            openmamba_srcname = p["srcname"]
                        else:
                            openmamba_altnames.append(p["srcname"])
            elif p["status"] == "newest":
               # kaos_build provides unreleased kde package versions
               if p["repo"] != "kaos_build":
                   newest_version = p["version"]
                   newest_repos.append(p["repo"])
        newest_repos = list(dict.fromkeys(newest_repos))
        if openmamba_srcname and newest_version is not None and len(newest_repos) > 0:
            results.append(openmamba_srcname + " " + newest_version)
            for altname in openmamba_altnames:
                results.append(altname + " " + newest_version)
    last_from_package = from_package
    from_package = package
    time.sleep(1)

results_sorted = sorted(set(results), key=str.casefold)
#results_unique = set(results_sorted)
for result in results_sorted:
    print(result)
