#!/usr/bin/python3

# generate dependencies for debian/control, so packages we need are not removed from testing
# this is not yet used

import pathlib
import sys

class_exclude = {
    'BUSTER', # Ignore buster-specific dependencies as we're not packaged for buster.
    'DEVEL',
    'GCE_SDK',
}

class_arch = {
    'AMD64': 'amd64',
    'ARM64': 'arm64',
    'AZURE': 'amd64 arm64',
    'EC2': 'amd64 arm64',
    'GCE': 'amd64 arm64',
    'PPC64EL': 'ppc64el',
}

packages = set()

for i in pathlib.Path('../config_space/sid/package_config').glob('*'):
    if '.' in i.name or i.name in class_exclude:
        continue

    arch = class_arch.get(i.name)

    with i.open() as f:
        for line in f.readlines():
            line = line.strip()
            # Ignore empty lines and comments
            if not line or line.startswith('#'):
                pass

            elif line.startswith('PACKAGES'):
                l = line.split()
                # Ignore unknown commands
                if l[1] in ('install', 'install-norec'):
                    classes = frozenset(l[2:])
                    ignore = False
                    # Ignore entries if all of the extra classes are excluded
                    if classes and not (classes - class_exclude):
                        ignore = True

            elif not ignore:
                for package in line.split():
                    # Ignore to be removed packages
                    if package.endswith('-'):
                        continue
                    # https://piuparts.debian.org/sid/fail/debian-cloud-images-packages_0.0.5.log
                    if package == 'init':
                        continue
                    if arch:
                        packages.add(f'{package} [{arch}]')
                    else:
                        packages.add(package)

print('debian-cloud-images-packages:Depends=' + ', '.join(sorted(packages)))
