#!/usr/bin/python2

from edge.com import fs
from edge.webserver.sysconf.uiconf.time import TimeUiconf


def _main():
    def_uiconf = fs.load_json3(TimeUiconf.DEFCONF_PATH)
    def_uiconf_pub = def_uiconf['rw']  # type: dict

    uiconf = TimeUiconf.load_conf()
    uiconf_pub = uiconf['rw']  # type: dict

    tz = uiconf_pub['tz']
    ntp_enabled = uiconf_pub['mode'] == 'ntp'
    if ntp_enabled:
        ntp_servers = uiconf_pub.get('servers')
    else:
        ntp_servers = def_uiconf_pub['args']['sources'][0].get('servers', [])

    uiconf['rw'] = {
        'limits': def_uiconf_pub['limits'],
        'args': {
            'tz': tz,
            'sources': [
                {
                    'autosync': ntp_enabled,
                    'type': 'ntp',
                    'servers': ntp_servers
                }
            ]
        }
    }

    TimeUiconf.save_conf(uiconf)


if __name__ == '__main__':
    _main()
