#!/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.
#
#Raw popularity-contest results

# inst = number of people who installed this package
# vote = number of people who use this package regularly
# old = number of people who installed, but don't use this package regularly
# recent = number of people who upgraded this package recently
# no-files = number of people whose entry didn't contain enough information (atime and ctime were 0)
#
# # # of packages ~70K ( all-packages "74838")
# normal popcon     # = per #/1000
# too coarse for limited scope apps
#                                    74000   7400     740     74     7
# fine   popcon  per #/max*1000       1000    100      10      1     0 (base packages)
# fine   popcon1 per #/max*10000     10000   1000     100     10     1
# fine   popcon2 per #/max*100000   100000  10000    1000    100    10 (special packages)
#
# Shell script with bc etc. for floating point is nightmare
#
# Number of significant digits = 2
import sys
import re
import subprocess

def str_sig2(n):
    if [ n >= 1000 ]:
        s = "999"
    elif [ n >= 10 ]:
        s = "{:.0f}".format(n)
    elif [ n >= 1 ]:
        s = "{:.1f}".format(n)
    elif [ n >= 0.1 ]:
        s = "{:.2f}".format(n)
    else:
        s = "{:.3f}".format(n)
    return s

#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 = int(sys.argv[1])
#
    for i, line in enumerate(sys.stdin.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')
                vote = int(data6.group('vote'))
                old = int(data6.group('old'))
                new = int(data6.group('new'))
                nofile = int(data6.group('nofile'))
                inst = vote + old + new + nofile
                # package_norm = `echo $package | sed $(sed.placeholder)`
                result = subprocess.run("echo " + package + " | sed $(cat sed_drop_version)", shell=True, capture_output=True, text=True)
                package_tag = result.stdout.strip().lower().translate(str.maketrans("0123456789","abcdefghij", r" !#$%()=-~^|\/+*,.?;:@`\"'&><"))
                inst_normal = inst / submissions * 1000
                vote_normal = vote / submissions * 1000
                if inst_normal < 0.01:
                    print('<!ENTITY pop-{} "I: NONE">'.format(package_tag))
                elif (vote * 100) > inst and vote > 0:
                    if inst_normal > 10:
                        print('<!ENTITY pop-{} "V:{:.0f}, I:{:.0f}">'.format(package_tag, vote_normal, inst_normal))
                    elif inst_normal > 1:
                        print('<!ENTITY pop-{} "V:{:.1f}, I:{:.1f}">'.format(package_tag, vote_normal, inst_normal))
                    else:
                        print('<!ENTITY pop-{} "V:{:.2f}, I:{:.2f}">'.format(package_tag, vote_normal, inst_normal))
                else:
                    if inst_normal > 10:
                        print('<!ENTITY pop-{} "I:{:.0f}">'.format(package_tag, inst_normal))
                    elif inst_normal > 1:
                        print('<!ENTITY pop-{} "I:{:.1f}">'.format(package_tag, inst_normal))
                    else:
                        print('<!ENTITY pop-{} "I:{:.2f}">'.format(package_tag, inst_normal))
