#!/usr/bin/python3 -B

import os
import sys
from textwrap import dedent

from config import Signing

(series, source_name, generate_name, source_version, unsigned_name, unsigned_version, abi_version) = sys.argv[1:]

signing = Signing.load("debian/package.config")

with open("debian/control.stub") as tfd, open("debian/control", "w") as cfd:
    for line in tfd:
        line = line.replace("@SRCPKGNAME@", source_name)
        line = line.replace("@SERIES@", series)
        if "@DEPENDS@" in line:
            for flavour, archs in signing.flavour_archs:
                print(f' linux-image-unsigned-{abi_version}-{flavour} (= {unsigned_version}) [{" ".join(archs)}],', file=cfd)
                print(f' linux-buildinfo-{abi_version}-{flavour} (= {unsigned_version}) [{" ".join(archs)}],', file=cfd)
            print(f" {generate_name} (= {source_version}),", file=cfd)
        else:
            print(line, end='', file=cfd)

    for flavour, archs in signing.package_flavour_archs("image"):
        print(dedent(f"""\

            Package: linux-image-{abi_version}-{flavour}
            Architecture: {" ".join(archs)}
            Depends: ${{unsigned:Depends}}
            Recommends: ${{unsigned:Recommends}}
            Suggests: ${{unsigned:Suggests}}
            Conflicts: ${{unsigned:Conflicts}}
            Provides: ${{unsigned:Provides}}
            Built-Using: {unsigned_name} (= {unsigned_version})
            Description: Signed kernel image {flavour}
             A kernel image for {flavour}.  This version of it is signed with
             Canonical's signing key.
            """).rstrip(), file=cfd)
    for flavour, archs in signing.package_flavour_archs("di"):
        print(dedent(f"""\

            Package: kernel-signed-image-{abi_version}-{flavour}-di
            Package-Type: udeb
            Section: debian-installer
            Priority: extra
            Provides: kernel-signed-image
            Architecture: {" ".join(archs)}
            Built-Using: {unsigned_name} (= {unsigned_version})
            Description: Signed kernel image generic for the Debian installer
             A kernel image for {flavour}.  This version of it is signed with
             Canonical's UEFI signing key.  It is intended for the Debian installer,
             it does _not_ provide a usable kernel for your full Debian system.
        """).rstrip(), file=cfd)
    for flavour, archs in signing.package_flavour_archs("hmac"):
        print(dedent(f"""\

            Package: linux-image-hmac-{abi_version}-{flavour}
            Build-Profiles: <!stage1>
            Architecture: {" ".join(archs)}
            Section: kernel
            Priority: optional
            Depends: ${{misc:Depends}}, ${{shlibs:Depends}}, linux-image-{abi_version}-{flavour}
            Suggests: fips-initramfs-generic
            Description: HMAC file for linux kernel image {abi_version}-{flavour}
             This package contains the HMAC file for Linux kernel image for version
             {abi_version}-{flavour}
            """).rstrip(), file=cfd)
    # XXX: all dbgsym packages _must_ be at the end of debian/control else the
    # build will hang forever on the builder.
    for flavour, archs in signing.package_flavour_archs("image"):
        print(dedent(f"""\

            Package: linux-image-{abi_version}-{flavour}-dbgsym
            Section: devel
            Architecture: {" ".join(archs)}
            Depends: linux-image-unsigned-{abi_version}-{flavour}-dbgsym
            Description: Signed kernel image {flavour}
             A link to the debugging symbols for the {flavour} signed kernel.
            """).rstrip(), file=cfd)
