#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Configura ejabberd con el fqdn del equipo en el que nos ejecutamos.

from hga.controlgrupos.common.netdev import NetDev
import os

folder = '/var/lib/ejabberd'
for the_file in os.listdir(folder):
    file_path = os.path.join(folder, the_file)
    try:
        if os.path.isfile(file_path):
            os.unlink(file_path)
    except Exception as e:
        print(e)

EJABBERD_TEMPLATE_CONFIG="/etc/ejabberd/ejabberd.yml.template"
EJABBERD_CONFIG="/etc/ejabberd/ejabberd.yml"

fqdn = NetDev().hostname()
config=""

with open(EJABBERD_TEMPLATE_CONFIG, "r") as f:
    config= f.read()
    print("Reemplazando '__FQDN__' por %s" % fqdn)
    config=config.replace("__FQDN__", fqdn)

with open(EJABBERD_CONFIG, "w") as f:
    f.write(config)
