#!perl
# Import core
#
BEGIN {
    @INC = ( '../../perl' );
}
use Cgi;
use BinUtil;

$| = 1;
$isNT = -d '\\';

$perl = "../../perl/perl";
$rootDir = $ENV{'NETSITE_ROOT'};
$newUserdb = "$rootDir/userdb";
$oldUserdb = "$cgiVars{'dir'}/userdb";


if ($cgiVars{'ldif'}) {
    print "Generating LDIF file.  This may take a while...\n";
    $options = "$cgiVars{'dir'}/userdb/ldap/tools $oldUserdb $cgiVars{'ldapbasedn'}";
    delete $ENV{SHLIB_PATH};
    open(OUTPUT, "$perl migrateLocalDB $options |") or 
	print "<FONT COLOR=\"red\">[Error]</FONT> Cannot run migrateLocalDB: $!.\n";
    @rez = ();
    $isEmpty = 0;
    while (<OUTPUT>) {
	if (/local db is empty/) {
	    print "<FONT COLOR=\"red\">Warning:</FONT> Localdb is empty.  No LDIF file generated.\n";
	    $isEmpty = 1;
	    last;
	}
    }
    close OUTPUT;
    if (!$isEmpty) {
      BinUtil::copyFile("$oldUserdb/localdb.ldif", "$newUserdb/$cgiVars{'name'}-localdb.ldif");
	print "LDIF file available at <CODE>$newUserdb/$cgiVars{'name'}-localdb.ldif</CODE>.\n";
    }
}
