#!/usr/bin/python3
#
# SPDX-License-Identifier: GPL-2.0-only
# (c) 2026 Gerd Hoffmann
#
""" download cloud images """
import os
import sys
import argparse

from virt.qemutest.distro.core import default_imgdir
from virt.qemutest.distro.rhel import FetchRHELCloud
from virt.qemutest.distro.centos import FetchCentosCloud
from virt.qemutest.distro.fedora import FetchFedoraCloud, FetchFedoraUKICloud

def main():
    defdir = default_imgdir()
    parser = argparse.ArgumentParser(description = "download cloud images")
    parser.add_argument('--imgdir', dest = 'imgdir',
                        type = str, default = defdir,
                        help = f'store downloads in DIR (default: {defdir})', metavar = 'DIR')
    parser.add_argument('--fedora', dest = 'fedora',
                        action = 'append', type = str, default = [],
                        help = 'download fedora VER', metavar = 'VER')
    parser.add_argument('--fedora-uki', dest = 'fedora_uki',
                        action = 'append', type = str, default = [],
                        help = 'download fedora VER (UKI variant)', metavar = 'VER')
    parser.add_argument('--centos', dest = 'centos',
                        action = 'append', type = str, default = [],
                        help = 'download centos stream VER', metavar = 'VER')
    parser.add_argument('--rhel', dest = 'rhel',
                        action = 'append', type = str, default = [],
                        help = 'download rhel VER', metavar = 'VER')
    options = parser.parse_args()

    if options.fedora:
        f = FetchFedoraCloud()
        for ver in options.fedora:
            f.fetch_image(ver, imgdir = options.imgdir)

    if options.fedora_uki:
        f = FetchFedoraUKICloud()
        for ver in options.fedora_uki:
            f.fetch_image(ver, imgdir = options.imgdir)

    if options.centos:
        f = FetchCentosCloud()
        for ver in options.centos:
            f.fetch_image(ver, imgdir = options.imgdir)

    if options.rhel:
        m = os.environ.get('REDHAT_MIRROR')
        if m is None:
            m = os.environ.get('BUILDURL') # beaker sets this
        if m is None:
            raise RuntimeError('REDHAT_MIRROR environment variable not set')
        f = FetchRHELCloud(m)
        for ver in options.rhel:
            f.fetch_image(ver, imgdir = options.imgdir)

    return 0

if __name__ == '__main__':
    sys.exit(main())
