#!/usr/bin/python3
# SPDX-FileCopyrightText: 2018-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

"""
Univention Setup:
Setting primary network interface.
"""

from univention.config_registry.interfaces import Interfaces
from univention.management.console.modules.setup.setup_script import SetupScript, _, main


class NoConfiguredInterfaceError(Exception):
    pass


class PrimaryInterfaceSetup(SetupScript):
    name = _("Setting primary network interface")

    def up(self):
        self.interfaces = Interfaces()

    def inner_run(self):
        primary_interface = self.ucr.get("interfaces/primary")
        if primary_interface in [interface[0] for interface in self.interfaces.all_interfaces]:
            self.message("Primary interface already set")
            return True
        try:
            primary_interface = self._choose_primary_interface()
        except NoConfiguredInterfaceError:
            self.message("No configured interface found. No primary interface set.")
            return True
        self.message(f"Setting primary interface to: {primary_interface}")
        self.ucr.set("interfaces/primary", primary_interface)
        return True

    def _choose_primary_interface(self):
        for interface in self.interfaces.all_interfaces:
            if "address" in interface[1] and interface[1]["address"]:
                return interface[0]
            if "ipv6/default/address" in interface[1] and interface[1]["ipv6/default/address"]:
                return interface[0]
        raise NoConfiguredInterfaceError


if __name__ == "__main__":
    main(PrimaryInterfaceSetup())
