#!/usr/bin/env python3
# Copyright 2019 Collabora Ltd.
# SPDX-License-Identifier: BSD-3-Clause
# (See debian/copyright)

import os
import shutil

try:
    from typing import (
        Dict,
        Set,
    )
    # placate pyflakes
    Dict, Set
except ImportError:
    pass

from steamrt_abi import AbiData


def main() -> None:
    abi = AbiData()

    for multiarch_tuple in abi.architectures:
        # { 'zlib1g': set(['libz.so.1']) }
        packages = {}   # type: Dict[str, Set[str]]

        output_dir = os.path.join(
            os.path.dirname(__file__),
            'expectations',
            multiarch_tuple,
        )

        try:
            shutil.rmtree(output_dir)
        except OSError:
            pass

        os.makedirs(output_dir)

        for lib in abi.shared_libraries.values():
            packages.setdefault(lib.deb, set()).add(lib.soname)

        for package, sonames in packages.items():
            for soname in sonames:
                lib = abi.shared_libraries[soname]

                if lib.check_loadable:
                    break
            else:   # didn't break
                continue

            with open(
                os.path.join(output_dir, package + '.symbols'),
                'w'
            ) as writer:
                for soname in sorted(sonames):
                    lib = abi.shared_libraries[soname]

                    if lib.check_loadable:
                        writer.write('{} {} #MINVER#\n'.format(
                            soname,
                            package,
                        ))
                        writer.write('# No symbols listed here yet\n')


if __name__ == '__main__':
    main()
