#!/usr/bin/perl
#
# Copyright (c) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 3. Neither the name of the company nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# This product includes software developed by Luke Howard.

#
# Usage:
#   ultramigration [-h <host>] [-b <basedn>] [-D <rootdn>] [-w <rootpw>] 
#
# need ldapdiff and migrationtools
#

use File::Temp qw/ tempfile /;
use Getopt::Std;
eval q{
  use UltraPossum::Conf;
};
my $ultrapossum = 1 if ! $@;

sub error
{
  print STDERR <<EOF;
Error: $_[0]
Usage:
 $0 [-h <host>] [-b <basedn>] [-D <rootdn>] [-w <rootpw>] 
EOF
  exit 1;
}

my ( $tmpfh, $tmp ) = tempfile();
my ( $dirfh, $dirtmp ) = tempfile();

my ( $master, $suffix, $rootdn, $rootpw );

if( $ultrapossum ) {
  my $ul = UltraPossum::Conf->new;
  $master = $ul->{LDAPMASTER};
  $suffix = $ul->{SUFFIX};
  $rootdn = $ul->{ROOTDN};
  $rootpw = $ul->{ROOTPW};
}

getopt('hbDw');
                                                                           
$master = $opt_h if $opt_h;
$suffix = $opt_b if $opt_b;
$rootdn = $opt_D if $opt_D;
$rootpw = $opt_w if $opt_w;

$master or die error("can't determine host");
$suffix or die error("can't determine basedn"); 
$rootdn or die error("can't determine rootdn");
$rootpw or die error("can't determine rootpw");

print STDERR "master: $master, suffix: $suffix, rootdn: $rootdn\n";

print $tmpfh <<EOF;
dn: ou=People,$suffix
objectClass: organizationalUnit
ou: People

dn: ou=Group,$suffix
objectClass: organizationalUnit
ou: Group

EOF

close( $tmpfh );
close( $dirfh );

chdir "/usr/share/migrationtools";
$ENV{LDAP_BASEDN} = $suffix;
system( "./migrate_passwd.pl /etc/passwd >> $tmp ") == 0 or die;
system( "./migrate_group.pl /etc/group >> $tmp" ) == 0 or die;

system( "ldapsearch -x -D $rootdn -w $rootpw -h $master -b ou=People,$suffix -LLL > $dirtmp" );
system( "ldapsearch -x -D $rootdn -w $rootpw -h $master -b ou=Group,$suffix -LLL >> $dirtmp" );
#system( "cat $dirtmp 1>&2 ");
system( "ldifdiff $dirtmp $tmp | ldapmodify -x -h $master -D $rootdn -w $rootpw" );

