#!/usr/bin/perl

# Copyright (C) 2009-2011 Zentyal S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use EBox;
use EBox::Global;
use EBox::Sudo;
use EBox::EGroupware;

use strict;
use warnings;

EBox::init();

my $pass = EBox::EGroupware::getPassword();

chdir('/usr/share/egroupware/setup');

EBox::Sudo::root("./setup-cli.php --install default,ebox,$pass");

my $configCmd = "./setup-cli.php --config default,ebox,$pass";
my @cmds;
push(@cmds, "$configCmd --webserver-url /egroupware");
push(@cmds, "$configCmd --files-dir /var/lib/egroupware/default/files");
push(@cmds, "$configCmd --backup-dir /var/lib/egroupware/default/backup");
push(@cmds, "$configCmd --temp-dir /tmp");
push(@cmds, "$configCmd --smtpserver localhost --postfix yes");
EBox::Sudo::root(@cmds);

my $users = EBox::Global->modInstance('users');
my $ldap = $users->ldap();
my $usersDn = $users->usersDn();
my $groupsDn = $users->groupsDn();
my $rootDn = $ldap->rootDn();
my $ldapPass = $ldap->getPassword();
my $ldapHost = '127.0.0.1:';
unless ($users->mode() eq 'slave') {
    $ldapHost .= $ldap->ldapConf->{'port'};
} else {
    $ldapHost .= $ldap->ldapConf->{'translucentport'};
}

my $ldapCmd = "$configCmd --account-auth ldap,ldap "
            . "--ldap-host $ldapHost "
            . "--ldap-root-dn $rootDn "
            . "--ldap-root-pw $ldapPass "
            . "--ldap-context $usersDn "
            . "--ldap-group-context $groupsDn";
my $output = EBox::Sudo::silentRoot($ldapCmd);
unless ($? == 0) {
    my $error = join ("\n", @{$output});
    EBox::error("ebox-egroupware LDAP config error\n: $error");
}

# Set minimum account id
my $psqlCmd = "psql egroupware -c \"INSERT INTO egw_config VALUES ('phpgwapi','account_min_id',2001)\"";
EBox::Sudo::sudo($psqlCmd, 'postgres');

# Set system charset as utf-8
$psqlCmd = "psql egroupware -c \"INSERT INTO egw_config VALUES ('phpgwapi','system_charset','utf-8')\"";
EBox::Sudo::sudo($psqlCmd, 'postgres');

# Set default preferences (some egroupware apps doesn't work if they are not set)
#$psqlCmd = "psql egroupware -c \"INSERT INTO egw_preferences VALUES (-2, 'common', 'a:6:{s:12:\\\"template_set\\\";s:6:\\\"jerryr\\\";s:5:\\\"theme\\\";s:6:\\\"jerryr\\\";s:9:\\\"tz_offset\\\";s:1:\\\"0\\\";s:10:\\\"dateformat\\\";s:5:\\\"m/d/Y\\\";s:10:\\\"timeformat\\\";s:2:\\\"24\\\";s:11:\\\"default_app\\\";s:4:\\\"home\\\";}');\"";
#EBox::Sudo::sudo($psqlCmd, 'postgres');

1;
