#!/usr/bin/python

#
# Copyright (C) 2022 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer 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 any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer.  If not, see COPYING.
#

import sys
import subprocess
import simplejson
import os

def get_config():
    # ns8 config
    bash_command = "/sbin/e-smith/config getjson ns8"
    process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()
    ns8_config = simplejson.loads(output)
    bash_command = "/sbin/e-smith/config printjson DomainName"
    process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()
    domain_name = simplejson.loads(output)
    props = {"ns8": ns8_config,"account_provider_proposal": domain_name['type'], "slapd": {"props":{"status": "disabled"}}}

    # slapd config
    if os.path.isfile('/etc/e-smith/db/configuration/defaults/slapd/type'):
        bash_command = "/sbin/e-smith/config getjson slapd"
        process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
        slapd_config = simplejson.loads(output)
        props["slapd"] = slapd_config

    return props

try:
    config = get_config()
    output = simplejson.dumps({'configuration': config})
    print(output)
except Exception as e:
    print simplejson.dumps({'error': "%s" % e})
    sys.exit(1)
