#!/usr/bin/env python

import argparse, pdb, simpledaemon, time, logging 
import pscripts.external_ip_address as eia

eia.yaml_file='/etc/external_ip_updater/urls.yaml'
eia.ip_cache_file=eia.get_yaml_setting("ip_cache_file")


root_logger = logging.getLogger()
# logging.info(str(root_logger.handlers))
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)



class ExtIpUpdaterDaemon(simpledaemon.Daemon):
    default_conf = '/etc/external_ip_updater/config.conf'
    section = 'external_ip_updater'

    def run(self):

        refresh_period_seconds = int(eia.get_yaml_setting("refresh_period_seconds"))
        eia.flush_ip_cache_file()
        while True:
            eia.update_ddns_server()
            logging.debug("Sleeping for {} seconds".format(refresh_period_seconds))
            time.sleep(refresh_period_seconds)

if __name__ == '__main__':
    ExtIpUpdaterDaemon().main()
