#!/usr/bin/perl

#
# 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.
#

use strict;
use warnings;
use esmith::ConfigDB;
use JSON;

require '/usr/libexec/nethserver/api/lib/helper_functions.pl';

my $input = readInput();
my $cmd = $input->{'action'};

my $ret = {};


if ($cmd eq 'configuration') {

    my $db = esmith::ConfigDB->open_ro();
    $ret->{'configuration'}{'DefaultLocale'} = $db->get_prop('webtop', 'DefaultLocale') || 'en_US';
    $ret->{'configuration'}{'DefaultTimezone'} = $db->get_prop('webtop', 'DefaultTimezone') || 'Etc/UTC';
    $ret->{'configuration'}{'DefaultToolbarIconsSize'} = $db->get_prop('webtop', 'DefaultToolbarIconsSize') || 'medium';
    $ret->{'configuration'}{'PublicUrl'} = $db->get_prop('webtop', 'PublicUrl');
    $ret->{'configuration'}{'DavServerUrl'} = $db->get_prop('webtop', 'DavServerUrl');
    $ret->{'configuration'}{'PbxProvider'} = $db->get_prop('webtop', 'PbxProvider');
    $ret->{'configuration'}{'PbxProviderNethvoiceWebrestUrl'} = $db->get_prop('webtop', 'PbxProviderNethvoiceWebrestUrl');
    $ret->{'configuration'}{'MinMemory'} = $db->get_prop('webtop', 'MinMemory');
    $ret->{'configuration'}{'MaxMemory'} = $db->get_prop('webtop', 'MaxMemory');
    $ret->{'configuration'}{'VirtualHost'} = $db->get_prop('webtop', 'VirtualHost');
    $ret->{'configuration'}{'JitsiUrl'} = $db->get_prop('conference', 'JitsiUrl');
    
    my @localelist = split("\n", `/etc/e-smith/events/actions/nethserver-webtop5-locale-tz`);
    $ret->{'configuration'}{'LocaleList'} = \@localelist;
    
    my @timezones = split("\n", `java -classpath /usr/share/webtop/ ListTimeZones`);
    $ret->{'configuration'}{'TimezonesList'} = \@timezones;
    
    my $hostname = `/usr/bin/hostname`;
    $ret->{'configuration'}{'PlaceholderPublicUrl'} = 'http://' . $hostname . '/webtop';
    $ret->{'configuration'}{'PlaceholderDavServerUrl'} = 'https://' . $hostname . '/webtop-dav/server.php';
    
} else {
    error();
}

print encode_json($ret);
