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

config_file = "mv_config"
data_config = __import__(config_file, globals(), locals(), [])

def setup_ntp(usepool,ntpserver):
    if usepool:
        logging.info("Setting ntp server to %s",ntpserver)
        print '''sed -i "s/pool.ntp.org/$dbhost/g" ${BASE}/etc/ntp.conf'''
    else:
        logging.info("Not changing ntp configuration")

def setup_avahi(templates):
    file="/etc/avahi/services/mysql.service"
    if not os.path.exists(file):
        logging.debug("    copying in mysql.service for avahi")
        try:
            os.makedirs("/etc/avahi/services")
        except:
            logging.debug("    Couldn't create /etc/avahi/services")
        mv_common.cp_and_log(templates+"/mysql.service",file)
        cmd="sudo sv restart avahi"
        mv_common.runcmd(cmd)

def remove_avahi_service():
    file="/etc/avahi/services/mysql.service"
    if os.path.exists(file):
        logging.debug("    Removing avahi mysql.service file")
        cmd="rm -rf %s" %file
        mv_common.runcmd(cmd)
        cmd="sudo sv restart avahi"
        mv_common.runcmd(cmd)

def setup_db():
    logging.debug("    Setting up the database")

def setup_mysqlnetwork(EnableNetwork):
    logging.debug("    Setting up mysql network")
    mysqlconf = "/etc/mysql/my.cnf"
    cmd = '''grep "#skip-networking" %s ''' %mysqlconf
    status = mv_common.runcmd(cmd)

    if EnableNetwork :
        logging.debug("    Enabling mysql networking")
        if status == 0 :
            logging.debug("    Network is already enabled for mysql")
        else:
            cmd = '''sed -ie "s/^skip-networking/#skip-networking/g" %s ''' %mysqlconf
            mv_common.runcmd(cmd)
            mv_common.restart_service("mysql")
    else:
        logging.debug("    Disabling mysql networking")
        if status == 1 :
            logging.debug("    Network is already disabled for mysql")
        else:
            cmd = '''sed -ie "s/#skip-networking/skip-networking/g" %s ''' %mysqlconf
            mv_common.runcmd(cmd)
            mv_common.restart_service("mysql")





def setup_mysql_connection(systemconfig):

    logging.info("    Configuring database connection")
    mythip = systemconfig.get("mythip")
    systemtype = systemconfig.get("SystemType")
    dbhost = systemconfig.get("dbhost")
    mv_root = systemconfig.get("MVROOT")

    if systemtype == "Master_backend":
        logging.debug("    Setting dbhost to %s in systemconfig", mythip)
        dbhost = mythip
        cmd = '''sed -i "s/^dbhost=.*$/dbhost=\"%s\"/" /etc/systemconfig ''' %dbhost
        mv_common.runcmd(cmd)
        logging.debug("    Changing dbhost in settings table for master_backend")
        #cmd = '''%s/bin/restore_default_settings.sh -c BECONFIG -s master -a %s ''' %(mv_root, dbhost)
        cmd = '''%s/bin/myth_settings_wrapper.sh -c BECONFIG -s master -a %s ''' %(mv_root, dbhost)
        b=mv_common.runcmd_output(cmd)
        logging.debug(b)
        #mv_common.reloadfe(systemconfig.get("dbhost"),True)

    if systemtype == "Slave_backend":
        slavehost = mythip
        logging.debug("    Changing slave in settings: %s", slavehost)
        cmd = '''%s/bin/myth_settings_wrapper.sh -c BECONFIG -s slave -a %s ''' %(mv_root, slavehost)
        #cmd = '''%s/bin/restore_default_settings.sh -c BECONFIG -s slave  -a %s ''' %(mv_root, slavehost)
        b=mv_common.runcmd_output(cmd)
        logging.debug(b)

    if systemtype == "Frontend_only":
        fehost = mythip
        logging.debug("    Changing Frontend_only in settings BIP Null: %s", fehost)
        cmd = '''%s/bin/myth_settings_wrapper.sh -c BECONFIG -s frontendonly ''' %(mv_root)
        #cmd = '''%s/bin/restore_default_settings.sh -c BECONFIG -s slave  -a %s ''' %(mv_root, slavehost)
        b=mv_common.runcmd_output(cmd)
        logging.debug(b)




    if systemtype == "Standalone":
        #THIS IS NEVER CALLED ANYMORE, need to move the mysql.txt stuff
        logging.debug("   changing mysql.txt file to use localhost")
        cmd = '''sed -e "s/^DBHostName=.*$/DBHostName="localhost"/" %s > %s ''' %(mysqltemplate, mysqltxt)
        mv_common.runcmd(cmd)

        #mv_common.reloadfe(systemconfig.get("dbhost"),True)

        cmd = '''%s/bin/myth_settings_wrapper.sh -c BECONFIG -s standalone  ''' %(mv_root)
        b=mv_common.runcmd_output(cmd)
        logging.debug(b)

        #mv_common.reloadfe(systemconfig.get("dbhost"),True)

        logging.debug("   Running beconfig a 2nd time")
        cmd = '''%s/bin/myth_settings_wrapper.sh -c BECONFIG -s standalone  ''' %(mv_root)
        #mv_common.runcmd(cmd)
        b=mv_common.runcmd_output(cmd)
        logging.debug(b)





def setup_func_key():
    if data_config.SYSTEMTYPE == "MythVantage":
        logging.debug("    Grabbing the key for Func")
        cmd="/usr/MythVantage/bin/grabkey.py"
        mv_common.runcmd(cmd)

def setup_func_minion(dbhost,hostname):
    logging.info("    Setting up the func minion: %s", hostname)
    logging.debug("   certmaster is %s:",dbhost)
    #cmd='''sed -i "s/^certmaster.*$/certmaster = %s/" /etc/func/minion.conf ''' %dbhost
    #mv_common.runcmd(cmd)
    cmd='''sed -i "s/^certmaster.*$/certmaster = %s/" /etc/certmaster/minion.conf ''' %dbhost
    mv_common.runcmd(cmd)
    cmd='''sed -i "s/^minion_name.*$/minion_name = %s/" /etc/func/minion.conf ''' %hostname
    mv_common.runcmd(cmd)

def setup_syslog(dbhost):
    logging.debug("    Setting syslog to log to %s",dbhost)
    cmd ='''sed -i "s/^destination d_remote.*$/destination d_remote \{tcp\(\"%s\" port\(514\)\) \;\} \;/" /etc/syslog-ng.conf''' %dbhost
    return

def setup_rsyslog_server(enable_network):
    syslog_conf_file="/etc/rsyslog.d/network.conf"
    syslog_client_file="/etc/rsyslog.d/client.conf"
    contents='''
# ######### Receiving Messages from Remote Hosts ##########
# TCP Syslog Server:
# provides TCP syslog reception and GSS-API (if compiled to support it)
$ModLoad imtcp.so  # load module
$InputTCPServerRun 514 # start up TCP listener at port 514
# UDP Syslog Server:
$ModLoad imudp.so  # provides UDP syslog reception
$UDPServerRun 514 # start a UDP syslog server at standard port 514
'''
    mv_common.remove_file(syslog_client_file)
    if enable_network:
         logging.info("____Enabling rsyslog network____")
         try:
            f = open(syslog_conf_file,'w')
         except:
            print "%s could not be opened" %syslog_conf_file
            logging.debug("could not open %s" %syslog_conf_file)
            return
         f.write(contents)
         f.write("\n")
         f.close()
    else:
         logging.info("____Disabline rsyslog network____")
         mv_common.remove_file(syslog_conf_file)

    return

def setup_rsyslog_client(rsyslog_server):
    contents='''
# Remote Logging (we use TCP for reliable delivery)
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
$WorkDirectory /var/spool/rsyslog # where to place spool files
$ActionQueueFileName uniqName # unique name prefix for spool files
$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
$ActionQueueType LinkedList   # run asynchronously
$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
*.* @@%s:514
'''

    logging.info("____Enabling rsyslog network client____")
    logging.info("    syslog_server: %s", rsyslog_server)
    syslog_client_file="/etc/rsyslog.d/client.conf"
    syslog_conf_file="/etc/rsyslog.d/network.conf"
    try:
            f = open(syslog_client_file,'w')
    except:
            print "%s could not be opened" %syslog_client_file
            return
    f.write(contents %rsyslog_server)
    f.write("\n")
    f.close()
    mv_common.remove_file(syslog_conf_file)
    cmd="mkdir -p /var/spool/rsyslog"
    mv_common.runcmd(cmd)
    return

def hostypeprint(systemconfig):
    if  mv_common.read_config(mv_common.module_config,"hostype")  == False  :
        logging.info("____Skipping of hostype, config disabled____")
        return
    logging.info("______Start of hostype config_____")

    url = 'http://%s:1337/extra.cfg.txt' %systemconfig.get("dbhost")
    try:
        ex = mv_common.config_extra_reader(url)
        xymon_supported=ex.read_config('xymon')
        rsyslog_network=ex.read_config('rsyslog_network')
        func_supported=ex.read_config('func')
    except:
        xymon_supported=False
        rsyslog_network=False
        func_supported=False

    mv_common.services(systemconfig)
    install_list=''
    remove_list=''
    daemon_list=''
    daemon_remove_list=''
    run_mysqlnetwork = False
    restart_mbe = False
    profile_status="/etc/profile.d/z_myth_status.sh"
    myth_status="/usr/LH/bin/myth_status.sh"

    if systemconfig.get("SystemType") == "Standalone":
        logging.info("Stand alone system being configured")
        setup_ntp(False,"null")
        remove_avahi_service()
        EnableNetwork = False
        run_mysqlnetwork = True
        if systemconfig.get("hostypec"):
            logging.info(" Host type change detected, queuing restart of backend")
            restart_mbe = True


        install_list=["nss-mdns", "mysql", "mythdb-initial", "avahi", "rpcbind", "nfs-utils", "supplemental-web", "udevil"]
        daemon_list=["mysql", "mythbackend", "avahi", "rpcbind", "nfs-common", "netfs", "lighttpd" ,"msg_daemon", "udevil"]


        mv_common.link_file(myth_status,profile_status)
        setup_rsyslog_server(False)
        if func_supported:
            install_list.append('func')
            daemon_list.append('funcd')
            install_list.append('certmaster')
            daemon_list.append('certmaster')


    elif systemconfig.get("SystemType") == "Master_backend":
        logging.info("Master backend system being configured")
        setup_ntp(False,"null")
        setup_avahi(systemconfig.get("TEMPLATES"))
        install_list=["nss-mdns", "mysql", "mythdb-initial", "avahi", "rpcbind", "nfs-utils", "supplemental-web", "udevil"]
        daemon_list=["mysql", "mythbackend", "avahi", "rpcbind", "nfs-common", "netfs", "lighttpd" ,"msg_daemon" ,"udevil" ]
        EnableNetwork = True
        run_mysqlnetwork = True
        mv_common.link_file(myth_status,profile_status)

        setup_rsyslog_server(True)
        if func_supported:
        #if data_config.SYSTEMTYPE == "MythVantage":
            install_list.append('func')
            daemon_list.append('funcd')
            install_list.append('certmaster')
            daemon_list.append('certmaster')

        if systemconfig.get("hostypec"):
            logging.info(" Host type change detected, queuing restart of backend")
            restart_mbe = True

    elif systemconfig.get("SystemType") == "Slave_backend":
        logging.info("Slave backend  system being configured")
        setup_ntp(True,systemconfig.get("dbhost"))
        remove_avahi_service()
        mv_common.link_file(myth_status,profile_status)

        install_list = [ "nss-mdns", "rpcbind", "nfs-utils", "avahi", "libmysqlclient","udevil","supplemental-web-slave"]
        daemon_list = ["mythbackend", "rpcbind", "nfs-common", "netfs", "avahi" ,"msg_daemon","udevil","supplemental-slave" ]

        daemon_remove_list=["mysql", "lighttpd", "certmaster"]

        if func_supported:
            install_list.append('func')
            daemon_list.append('funcd')

        if xymon_supported:
            install_list.append('xymonclient')
            daemon_list.append('xymon-client')

        setup_rsyslog_client(systemconfig.get("dbhost"))
        if systemconfig.get("hostypec"):
            logging.info(" Host type change detected, queuing restart of backend")
            restart_mbe = True


    elif systemconfig.get("SystemType") == "Frontend_only":
        logging.info("Frontend only system being configured")
        setup_ntp(True,systemconfig.get("dbhost"))
        remove_avahi_service()
        install_list=["mysql-clients", "libmysqlclient",  "avahi", "rpcbind", "nfs-utils","udevil"]
        remove_list=["mysql","lighttpd"]

        daemon_list=["nss-mdns",  "mysql", "mythbackend", "avahi", "rpcbind", "nfs-common", "netfs" ,"msg_daemon","udevil"]
        daemon_remove_list=["mysql", "mythbackend","lighttpd"]
        if xymon_supported:
            install_list.append('xymonclient')
            daemon_list.append('xymon-client')

        if func_supported:
            install_list.append('func')
            daemon_list.append('funcd')
            #install_list.append('ghosd')

        setup_rsyslog_client(systemconfig.get("dbhost"))

    mv_common.restart_service("rsyslog")
    for pkg in install_list:
        mv_common.pacinstall(pkg)
    for pkg in remove_list:
        mv_common.pacinstall(pkg)
    for daemon in daemon_list:
        mv_common.add_service(daemon)
    for daemon in daemon_remove_list:
        mv_common.remove_service(daemon)

    if systemconfig.get("RunFrontend") == "1":
        logging.debug("   Will run the frontend")
        mv_common.add_service("frontend")
        #mv_common.add_service("hal")
        if data_config.SYSTEMTYPE == "MythVantage":
            mv_common.add_service("ghosd")
    else:
        logging.debug("   Will NOT run the frontend")
        mv_common.remove_service("frontend")
        #mv_common.add_service("hal")
        if data_config.SYSTEMTYPE == "MythVantage":
            mv_common.remove_service("ghosd")

    logging.debug("   Running mythwelcome-config.py")
    cmd="/usr/LH/bin/mythwelcome-config.py"
    mv_common.runcmd(cmd)

    if restart_mbe == True :
        setup_mysql_connection(systemconfig)
        mv_common.stop_service("mythbackend")


    if run_mysqlnetwork:
        setup_mysqlnetwork(EnableNetwork)
    else:
        logging.debug("    Not running setup_mysql_connection")

    setup_mysql_connection(systemconfig)

    if restart_mbe == True :
        mv_common.start_service("mythbackend")


    if func_supported:
        setup_func_minion(systemconfig.get("dbhost"),systemconfig.get("hostname"))


    logging.info("__End of hostype config\n")
