#!perl
#
# Migrate from 3.x to 4.0 server.

BEGIN {
    @INC = ( '../../perl' );
}
use Cgi;
use BinUtil;
use ES40lnf;
use Help;

$| = 1;
$isNT = -d '\\';
open(STDERR, ">&STDOUT");

$perl = "../../perl/perl";
@cgis = ("$perl importCore");
@subtitles = ('Server Settings');
# optional subcomponents
if ($cgiVars{'version'} eq "3.6") {
    push(@cgis, "$perl importUsers");
    push(@subtitles, 'Users and Groups');
}
if ($cgiVars{'security'}) {
    push(@cgis, "$perl importSecurity");
    push(@subtitles, 'Keys and Certificates');
}
my %pkgList = BinUtil::getPkgList($ENV{'NETSITE_ROOT'});
if ($pkgList{'webpub'}) {
    push(@cgis, "$perl importWebPub");
    push(@subtitles, 'Web Publishing and Search Collections');
}


# set up directory variables
$sname = "https-$cgiVars{'name'}";
$rootDir = $ENV{'NETSITE_ROOT'};
$serverDir = "$rootDir/$sname";
$ENV{'SERVER_NAMES'} = $sname;
$trail = substr($cgiVars{'dir'}, length($cgiVars{'dir'})-1, length($cgiVars{'dir'}));
if ($trail eq "/" || $trail eq "\\") {
    chop($cgiVars{'dir'});
}
$newHome = "$rootDir/$sname";

# on NT, check that the name is still available as a service id
my $serviceTaken = 0;
if ($isNT) {
    my $rez = `../../bin/getservice $cgiVars{'name'}`;
    if (($rez =~ /Service exists/)) {
	$serviceTaken = 1;
    }
}
# make sure directory is available
if (!(-e $serverDir) && !$serviceTaken) {
    # create directory
    mkdir( $serverDir, 0755 ) or die die_horrible_death("Can't create $serverDir: $!\n");
} else {
    open( CGI, "$perl importInfo |" );
    while ( <CGI> ) {
	print;
    }
    close( CGI );
    exit;
}


# perform upgrade
$title = "Migrate $cgiVars{'server'}";
ES40lnf::print_html($title);
ES40lnf::print_body();
ES40lnf::print_header($title);

print "<TABLE CELLPADDING=\"5\"><TR>\n";
ES40lnf::print_OpenTD();
print "\n<DL>\n";
foreach $cgi (@cgis) {
    $subtitle = shift(@subtitles);

    print " <DT><B>Migrating $subtitle...</B></DT>\n";
    print "  <DD>";
    open(CGI, "$cgi |") or print "<FONT COLOR=\"red\">Can\'t run $cgi: ", $! + 0, "</FONT>\n";

    $error = 0;
    while (<CGI>) {
	if (/<FONT COLOR=\"red\">\[Error\]<\/FONT>/) {
	    print;
	    $error = 1;
	    last;
	}
	print;
	print "  <BR>";
    }
    close(CGI);
    if (!$error) {
	print "Assimilation complete.\n";
    } else {
	print "  <BR><FONT COLOR=\"red\"><B>Assimilation failed.</B></FONT>\n";
    }
    print " <P>\n";
}

if ($cgiVars{'version'} eq "4.0") {
    `$perl $rootDir/bin/https/bin/upgradeFrom40 -r $rootDir -i $sname -w`;
}

print "</DL>\n";


# Success!
print "<FONT COLOR=\"#666699\"><B>New server created, but not started.</B></FONT><BR>\n";
print "You should either stop the 3.x server, and start this one, or\n";
print "change the port number for this one, then start it.\n";
print "<SCRIPT>\n";
print "function configureServer() {\n";
print "  this.location = '/$sname/bin/index';\n";
print "}\n";
print "</SCRIPT>\n";
print "<FORM>\n";
print "<INPUT TYPE=\"button\" VALUE=\"Configure Migrated Server\" ONCLICK=\"configureServer();\">\n";
print "<INPUT TYPE=\"button\" VALUE=\"Close\" ONCLICK=\"window.close();\">\n";
#print "<input type=button value=\"Help\" onClick=\"";
#print &helpJavaScriptForTopic("migrate-result");
#print "\">\n";

print "</FORM>\n";

ES40lnf::print_CloseTD();
print "</TR></TABLE>\n";
print "</BODY>\n";
print "</HTML>\n";
