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

from virt.qemutest.distro.rhel import FetchRHELNetinst, FetchRHELInstall
from virt.qemutest.distro.alpine import FetchAlpineIso
from virt.qemutest.distro.centos import FetchCentosNetinst
from virt.qemutest.distro.fedora import FetchFedoraNetinst

def main():
    parser = argparse.ArgumentParser(description = "download iso images")
    parser.add_argument('--alpine-live', dest = 'alpine_live',
                        action = 'store_true', default = False,
                        help = 'download alpine linux live iso')
    parser.add_argument('--centos-netinst', dest = 'centos_netinst',
                        action = 'append', type = str, default = [],
                        help = 'download centos stream VERSION netinst iso', metavar = 'VERSION')
    parser.add_argument('--fedora-netinst', dest = 'fedora_netinst',
                        action = 'append', type = str, default = [],
                        help = 'download fedora VERSION server netinst iso', metavar = 'VERSION')
    parser.add_argument('--rhel-netinst', dest = 'rhel_netinst',
                        action = 'append', type = str, default = [],
                        help = 'download rhel VERSION netinst iso', metavar = 'VERSION')
    parser.add_argument('--rhel-install', dest = 'rhel_install',
                        action = 'append', type = str, default = [],
                        help = 'download rhel VERSION full install iso', metavar = 'VERSION')
    parser.add_argument('--rhel-nightly', dest = 'rhel_nightly',
                        action = 'store_true', default = False,
                        help = 'download rhel nightly compose')
    options = parser.parse_args()

    if options.alpine_live:
        al = FetchAlpineIso()
        al.fetch_image()

    if options.centos_netinst:
        cn = FetchCentosNetinst()
        for ver in options.centos_netinst:
            cn.fetch_image(ver)

    if options.fedora_netinst:
        fn = FetchFedoraNetinst()
        for ver in options.fedora_netinst:
            fn.fetch_image(ver)

    m = os.environ.get('REDHAT_MIRROR')
    if options.rhel_netinst or options.rhel_install:
        if m is None:
            raise RuntimeError('REDHAT_MIRROR environment variable not set')

    if options.rhel_netinst:
        rn = FetchRHELNetinst(m, nightly = options.rhel_nightly)
        for ver in options.rhel_netinst:
            rn.fetch_image(ver)

    if options.rhel_install:
        ri = FetchRHELInstall(m, nightly = options.rhel_nightly)
        for ver in options.rhel_install:
            ri.fetch_image(ver)

    return 0

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