#!/usr/bin/python3

# Copyright © 2013 Jakub Wilk <jwilk@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import collections

import apt_pkg

def main():
    base = os.path.join(os.path.dirname(__file__), os.pardir)
    tags = {}
    filename = '{base}/doc/tags.desc'.format(base=base)
    with open(filename, 'rt', encoding='UTF-8') as file:
        for section in apt_pkg.TagFile(file):
            tag = section['tag']
            tags[tag] = 0
    filename = '{base}/tests/testpkg/debian/control'.format(base=base)
    with open(filename, 'rt', encoding='UTF-8') as file:
        for n, section in enumerate(apt_pkg.TagFile(file)):
            if n == 0:
                continue
            description = section['description']
            emitted_tags = frozenset(
                line.split()[0]
                for line in description.splitlines()[1:]
            )
            for tag in emitted_tags:
                tags[tag] += 1
    filename = '{base}/tests/coverage.txt'.format(base=base)
    with open(filename, 'wt', encoding='UTF-8') as file:
        for tag, n in sorted(tags.items()):
            if n == 0:
                checkbox = '[ ]'
            elif n == 1:
                checkbox = '[x]'
            else:
                checkbox = '[{n}]'.format(n=n)
            print('{c} {tag}'.format(c=checkbox, tag=tag), file=file)

if __name__ == '__main__':
    main()

# vim:ts=4 sw=4 et
