#!/usr/bin/python

#
# Copyright (C) 2019 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 fileinput
import subprocess
import simplejson

def read_config(config_name):
    bash_command = "/sbin/e-smith/config getjson %s" % config_name
    process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()
    config_json = simplejson.loads(output)
    return config_json

try:
    line = fileinput.input()[0].rstrip()
    input_json = simplejson.loads(line)
    config = input_json["config"]

    if config == 'asterisk':
        asterisk_config = read_config('asterisk')
        output = simplejson.dumps({ 'configuration': asterisk_config })
        print (output)
    elif config == 'httpd-fpbx':
        httpd_fpbx_config = read_config('httpd-fpbx')
        output = simplejson.dumps({ 'configuration': httpd_fpbx_config })
        print (output)
    else:
        raise ValueError('config must be one of \'asterisk\' or \'httpd-fpbx\'')
except Exception, e:
    print simplejson.dumps({ 'error': "%s" % e })
    sys.exit(1)
