#!/usr/bin/env python3
#
# -*- python -*-
#
#  File: octofussd
#
#  Copyright (C) 2016 The FUSS Project <info@fuss.bz.it>
#  Copyright (C) 2016 Christopher R. Gabriel <cgabriel@truelite.it>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#

import sys
import os, os.path
from collections import defaultdict
import json

SYSTEM_CONF = "/etc/clusters"
USER_CONF = os.path.join(os.getenv("HOME"), ".clusters")
inventory = defaultdict(dict)

def load_file(fname):
    data = {}
    if not os.path.isfile(fname):
        return {}
    for l in open(fname).readlines():

        hosts = l.strip().split(" ")
        group_name = hosts.pop(0)
        data[group_name] = dict(hosts=hosts)
    return data

for f in (SYSTEM_CONF, USER_CONF):
    inventory.update(load_file(f))

sys.stdout.write(json.dumps(inventory))
