#!/usr/bin/perl

#
# Copyright (C) 2013 Nethesis S.r.l.
# http://www.nethesis.it - support@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 <http://www.gnu.org/licenses/>.
#

use esmith::DB::db;
use esmith::event;
use strict;

my $event = shift;
my $sourceDir = shift;
my $esmithDbDir = '/home/e-smith/db';
my $errors = 0;
if( ! -d $sourceDir) {
    die("Could not find dir $sourceDir");
}

my $srcConfigDb = esmith::DB::db->open_ro(join('', $sourceDir, $esmithDbDir, '/configuration')) || die("Could not open source configuration database in $sourceDir \n");
my $srcAccountsDb = esmith::DB::db->open_ro(join('', $sourceDir, $esmithDbDir, '/accounts')) || die("Could not open source accounts database in $sourceDir \n");

my $dstConfigDb = esmith::DB::db->open('configuration') || die("Could not open destination account db\n");
my $dstProxyPassDb = esmith::DB::db->open('proxypass') || die("Could not open destination proxypass db\n");

foreach ($srcAccountsDb->get_all_by_prop('type'=>'ProxyPass')) {
    $dstProxyPassDb->new_record($_->key, { $_->props, 'type' => 'ProxyPass' });
}

#
# Apply configuration
# 
if( ! esmith::event::event_signal('nethserver-httpd-update')) {
    exit(1);
}

exit 0;
