#!/usr/bin/env perl
# BEGIN COPYRIGHT BLOCK
# This Program 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; version 2 of the License.
# 
# 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.
# 
# Copyright (C) 2007 Red Hat, Inc.
# All rights reserved.
# END COPYRIGHT BLOCK
#

use lib qw(/usr/lib64/dirsrv/perl);

use strict;

use Mozilla::LDAP::API qw(ldap_explode_dn);
use CGI qw(:cgi :oldstyle_urls);
use Inf;
use AdminUtil;
use DSUtil;
use Resource;
use DSCreate;

my $res = new Resource("/usr/share/dirsrv/properties/ds_create.res",
                       "/usr/share/dirsrv/properties/setup-ds-admin.res",
                       "/usr/share/dirsrv/properties/setup-ds.res");

# parse the input parameters
my $query = new CGI;
# look at arguments
# save old start_server param
# set start_server=0
my $start_server = $query->param('start_server');

# create inf from CGI parameters
my $inf = createInfFromCGIParams($query);

# get the group from adm.conf before we create the instance
my $admConf = getAdmConf("/etc/dirsrv/admin-serv");
$inf->{General}->{SuiteSpotGroup} = $admConf->{sysgroup};

# create the instance
my @errs = createDSInstance($inf);
if (@errs) {
    print "Content-type: text/plain\n\n";
    print "NMC_ErrInfo: \n", $res->getText(@errs), "\n";
    print "NMC_Status: 1\n";
    exit 1;
}

# set up new DS to be managed by config DS - acis, pta config
# add the parmeters necessary to configure this DS to be managed
# by the console and to be registered with the config DS - these
# are usually passed in via the CGI params, or use reasonable
# default values
$inf->{General}->{ConfigDirectoryLdapURL} = $query->param('ldap_url') ||
    $admConf->{ldapurl};
$inf->{General}->{ConfigDirectoryAdminID} = $query->param('cfg_sspt_uid');
$inf->{General}->{ConfigDirectoryAdminPwd} = $query->param('cfg_sspt_uid_pw');
$inf->{General}->{AdminDomain} = $query->param('admin_domain') ||
    $admConf->{AdminDomain};

# need to get the admin uid
if (!$inf->{admin}->{ServerAdminID}) {
    my @rdns = ldap_explode_dn($inf->{General}->{ConfigDirectoryAdminID}, 1);
    $inf->{admin}->{ServerAdminID} = $rdns[0];
}

if (!createSubDSNoConn($inf, \@errs)) {
    print "Content-type: text/plain\n\n";
    print "NMC_ErrInfo: \n", $res->getText(@errs), "\n";
    print "NMC_Status: 1\n";
    exit 1;
}

my $servid = $query->param('servid');
# now start the server
$inf->{slapd}->{start_server} = 1;
if (@errs = DSCreate::startServer($inf)) {
    print "Content-type: text/plain\n\n";
    print "NMC_ErrInfo: \n", $res->getText(@errs), "\n";
    print "NMC_Status: 1\n";
    exit 1;
}

# add the aci that allows the admin user to administer the server
if (!addConfigACIsToSubDS($inf, \@errs)) {
    print "Content-type: text/plain\n\n";
    print "NMC_ErrInfo: \n", $res->getText(@errs), "\n";
    print "NMC_Status: 1\n";
    exit 1;
}

# register the new server with the configuration ds
if (!registerDSWithConfigDS($servid, \@errs, $inf)) {
    print "Content-type: text/plain\n\n";
    print "NMC_ErrInfo: \n", $res->getText(@errs), "\n";
    print "NMC_Status: 1\n";
    exit 1;
}

# if we got here, report success
print "Content-type: text/plain\n\n";
print "NMC_Status: 0\n";
exit 0;


sub createInfFromCGIParams {
    my $query = shift;
    my $inf = new Inf;
    $inf->{General}->{FullMachineName} = $query->param('servname');
    $inf->{General}->{SuiteSpotUserID} = $query->param('servuser');

    $inf->{slapd}->{ServerPort} = $query->param('servport');
    $inf->{slapd}->{RootDN} = $query->param('rootdn');
    $inf->{slapd}->{RootDNPwd} = $query->param('rootpw');
    $inf->{slapd}->{ServerIdentifier} = $query->param('servid');
    $inf->{slapd}->{Suffix} = $query->param('suffix');
    $inf->{slapd}->{start_server} = 0; # we will start it explicitly later

    return $inf;
}
