import configparser
import os

def read_config(config_path):
    config = configparser.ConfigParser()
    config.read(config_path)

    network_configs = []

    try:
        # Collect information from each topic / network
        for topic in config.sections():
            network = config[topic]
            server = network.get("server")
            port = network.getint("port")
            channels = network.get("channels", fallback=None)
            nickname = network.get("nickname")
            password = network.get("password", fallback=None)

            network_config = {
                "net_name": network.name,
                "server": server,
                "port": port,
                "channels": channels,
                "nickname": nickname,
                "password": password,
            }
            network_configs.append(network_config)
        return network_configs
    except Exception as e:
        print(f"Failure while reading configuration file. {e}")
        exit(1)

def make_files(path, net_name):

    os.makedirs(path, exist_ok=True)
    server_dir = os.path.join(path, net_name)
    os.makedirs(server_dir, exist_ok=True)
    try:
        os.mkfifo(f"{server_dir}/in")
    except FileExistsError:
        pass
    try:
        os.mkfifo(f"{server_dir}/out")
    except FileExistsError:
        pass
    fifo_files = []
    fifo_files.append(f"{server_dir}/in")
    fifo_files.append(f"{server_dir}/out")
    return fifo_files