#!/usr/bin/python3
#
# Univention System Setup
# cleanup script to shut down the local browser in the appliance scenario
#
# SPDX-FileCopyrightText: 2014-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

import re
import subprocess

import univention.config_registry


RE_IPV4_TYPE = re.compile('^interfaces/[^/]*/type$')


def unset_temporary_interfaces():
    ucr = univention.config_registry.ConfigRegistry()
    ucr.load()

    # unset the temporary interface if set
    for var in ucr.keys():
        if RE_IPV4_TYPE.match(var) and ucr.get(var) == 'appliance-mode-temporary':
            print('unset %s' % var)
            keys = [var]
            for k in ['netmask', 'address', 'broadcast', 'network']:
                keys.append(var.replace('/type', '/%s' % k))
            univention.config_registry.handler_unset(keys)
            # Shut down temporary interface
            subprocess.call(['ifdown', var.split('/')[1].replace('_', ':')])


if __name__ == "__main__":
    unset_temporary_interfaces()
