#!/bin/python3
# vim: set sts=4 ts=4 sw=4 ai expandtab:
# Copyright (C) 2026 Osamu Aoki <osamu@debian.org>
#
# This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of
# the GNU General Public License version 2 or later.
#
# $ apt list "~n^fcitx5.*" "~n^ibus.*" | bin/popconhtml.py
#
#  NEEDS: all-popcon-results.txt"
#
import sys
import re

#<tr>
#  <td style="text-align: left">
#    <a class="ulink" href="http://packages.debian.org/sid/ibus"> ibus </a>
#  </td>
#  <td style="text-align: left">
#    <a class="ulink" href="http://qa.debian.org/popcon-graph.php?packages=ibus">V:219, I:264</a>
#  </td>
#  <td style="text-align: left">
#    <a class="ulink" href="https://tracker.debian.org/pkg/ibus">1874</a>
#  </td>
#  <td style="text-align: left"> input method framework using dbus </td>
#</tr>
def output_tr(package, vote, inst, extra):
    print('<tr>')
    print('<td style="text-align: left">{} </td>'.format(package))
    if inst < 0.01:
        print('<td style="text-align: right"> --- </td> <td style="text-align: right"> --- </td>')
    elif (vote * 100) > inst and vote > 0:
        print('<td style="text-align: right"> {:.2f} </td> <td style="text-align: right"> {:.2f} </td>'.format(vote, inst))
    else:
        print('<td style="text-align: right"> --- </td> <td style="text-align: right"> {:.2f} </td>'.format(inst))
    if extra:
        print('<td style="text-align: left"><a class="ulink" href="https://bugs.debian.org/cgi-bin/pkgreport.cgi?{}"> bugs.d.o link (bin) </a> </td>'.format(package))
        print('<td style="text-align: left"><a class="ulink" href="https://qa.debian.org/popcon-graph.php?packages={}"> popcon-graph link (bin) </a> </td>'.format(package))
        print('<td style="text-align: left"><a class="ulink" href="https://packages.debian.org/sid/{}"> packages.d.o link (src) </a> </td>'.format(package))
        print('<td style="text-align: left"><a class="ulink" href="https://tracker.debian.org/pkg/{}"> tracker.d.o link (src) </a> </td>'.format(package))
    print('</tr>')

#r_data = re.compile(r"(?P<lead>.+?)\S+(?P<package>.+)")
#r_data = re.compile(r"(?P<lead>.+)\S+(?P<package>.+)\S+.*")
r_pattern2_data = re.compile(r"^(?P<lead>\S+)\s+(?P<data>\S+)\s*")
r_pattern6_data = re.compile(r"^(?P<lead>\S+)\s+(?P<package>\S+)\s+(?P<vote>\S+)\s+(?P<old>\S+)\s+(?P<new>\S+)\s+(?P<nofile>\S+)\s*")
#r_data = re.compile(r".*")


if __name__ == '__main__':

    # Process first line
    submissions = 0

    if len(sys.argv) > 0:
        extra = True
    extra = False
    packages = []
    for pkg in sys.stdin.read().splitlines():
        #print("pkg = '{}'".format(pkg.strip()))
        packages.append(pkg.strip().split('/')[0])
    print(
'''
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Package summary</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  </head>
  <body>
              <table class="table" summary="Debian binary package information" border="1">
                <colgroup>
                  <col style="text-align: left"/>
                  <col style="text-align: right"/>
                  <col style="text-align: right"/>
'''
          )
    if extra:
        print(
'''
                  <col style="text-align: left"/>
                  <col style="text-align: left"/>
                  <col style="text-align: left"/>
                </colgroup>
                <thead>
                  <tr>
                    <th style="text-align: left"> deb </th>
                    <th style="text-align: right"> Vote </th>
                    <th style="text-align: right"> Install </th>
                    <th style="text-align: left"> bts link </th>
                    <th style="text-align: left"> popcon-graph link </th>
                    <th style="text-align: left"> packages.d.o </th>
                    <th style="text-align: left"> tracker.d.o </th>
                  </tr>
                </thead>
                <tbody>
'''
            )
    with open("all-popcon-results.txt", "r") as fd:
        for i, line in enumerate(fd.read().splitlines()):
            data2 = r_pattern2_data.fullmatch(line)
            data6 = r_pattern6_data.fullmatch(line)
            if data6 is not None:
                lead = data6.group('lead')
                if lead != "Package:":
                    print("# ERROR @{} : {}".format(i, line))
                else:
                    package = data6.group('package').strip()
                    if package in packages:
                        vote0 = int(data6.group('vote'))
                        old0 = int(data6.group('old'))
                        new0 = int(data6.group('new'))
                        nofile0 = int(data6.group('nofile'))
                        inst0 = vote0 + old0 + new0 + nofile0
                        inst = inst0 / submissions * 1000
                        vote = vote0 / submissions * 1000
                        output_tr(package, vote, inst, extra)
            elif data2 is not None:
                lead = data2.group('lead')
                if lead == "Submissions:":
                    submissions = int(data2.group('data'))
    print(
'''
                </tbody>
  </body>
</html>
'''
          )

