# -*- coding: utf-8 -*-
import logging, mv_common
import os, re

config_file = "mv_config"

def setup_ddns(systemconfig):
    if  mv_common.read_config(mv_common.module_config,"ddns")  == False  :
        logging.info("____Skipping of DDNS, config disabled____")
        return
    logging.info("____Start of setup_ddns____")

    if systemconfig.get("DDnsEnable") == "1":
        logging.debug("    Enabling DDNS")
	mv_common.pacinstall("ddclient")
	mv_common.cp_and_log("/etc/ddclient/ddclient.conf.dynamic.dyndns.sample",
	  		     "/etc/ddclient/ddclient.conf")
	cmd = ''' sed -i -e "s/your-dynamic-dyndns-login/%s/g" /etc/ddclient/ddclient.conf''' %(systemconfig.get("DDnslogin"))
	mv_common.runcmd(cmd)
	cmd = ''' sed -i -e "s/your-dynamic-dyndns-password/%s/g" /etc/ddclient/ddclient.conf''' %(systemconfig.get("DDnspassword"))
	mv_common.runcmd(cmd)
	cmd = ''' sed -i -e "s/your-dynamic-dyndns-host/%s/g" /etc/ddclient/ddclient.conf''' %(systemconfig.get("DDnshostname"))
	mv_common.runcmd(cmd)
	mv_common.add_service("ddclient")
    else:
        logging.debug("    Disabling DDNS")
	mv_common.remove_service("ddclient")

    logging.info("__End of ddns\n")
