#!/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/lib/x86_64-linux-gnu/dirsrv/perl);

use strict;

use AdminMigration;
use AdminUtil;
use AdminServer;
use Migration;
use Resource;
use DSMigration;
use SetupLog;

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

my $mig = new Migration($res); # parse cmd line etc.

$mig->msg('begin_dsadmin_migration', $mig->{oldsroot});

if (!$mig->{inf}->{General}->{ConfigDirectoryAdminPwd}) {
    $mig->msg('error_configds_adminpwd_required');
    $mig->doExit(1);
}

# first, migrate directory server instances
# either the config ds will be one of them, or we
# should have already migrated the config DS
$mig->msg('begin_ds_migration', $mig->{oldsroot});
if (!migrateDS($mig)) {
    $mig->doExit(1);
}

# next, migrate the admin server - this also registers the directory servers
$mig->msg('begin_as_migration', $mig->{oldsroot});
if (!migrateAdminServer($mig)) {
    $mig->doExit(1);
}

# next, register/update the new directory servers
# in the config ds
$mig->msg('registering_dirserver_instances');
my @errs;
if (!registerManyDSWithConfigDS($mig->{inf}, \@errs,
                                $mig->{configdir},
                                @{$mig->{instances}})) {
    $mig->msg($FATAL, @errs);
    $mig->doExit(1);
}

$mig->msg('end_dsadmin_migration');

$mig->doExit(0);

END {
    if ($mig and $mig->{keep}) {
        $mig->{inf}->write("__temp__");
    }
}

# emacs settings
# Local Variables:
# mode:perl
# indent-tabs-mode: nil
# tab-width: 4
# End:
