#!/usr/bin/python3
# -*- mode: python; coding: utf-8 -*-
#
##########################################################################
# 00-network - manage networkd override structure
# Copyright © 2018 Pôle de compétences EOLE <eole@ac-dijon.fr>
#
# License CeCILL:
#  * in french: http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
#  * in english http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
##########################################################################

"""Populate a per .network override

"""

from __future__ import absolute_import, print_function, unicode_literals

import sys
import os

from pyeole.i18n import i18n
from pyeole.log import init_logging

from creole.client import CreoleClient

_ = i18n('00-network')

NETPLAN_CONFIG_DIR = '/etc/netplan'

log = None

def clean_netplan_configs(config):
    """Remove all files under `/etc/netplan/`

    """
    if config['eth0_method'] != 'non géré':
        log.debug(_('Cleanup netplan configuration'))
        for filename in os.listdir(NETPLAN_CONFIG_DIR):
            log.debug(_("Remove netplan file '{0}'").format(filename))
            os.remove(os.path.join(NETPLAN_CONFIG_DIR, filename))

def main():
    global log
    log = init_logging(name=sys.argv[0], as_root=True, level='info')

    client = CreoleClient()
    config = client.get_creole()
    clean_netplan_configs(config)


if __name__ == "__main__":
    try:
        main()
    except Exception as error:
        log.error(_('Unable to setup per interface override: {error}').format(error=error))
