#!perl
#
# Migrate from 4.1/6.0 to 6.1 server.

BEGIN {
    $rootDir = $ENV{'NETSITE_ROOT'};
    @INC = ( "$rootDir/bin/https/perl");
}
use Cgi;
use BinUtil;
use ES40lnf;
use Help;

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

$perl = "$rootDir/bin/https/perl/perl";
my @cgis;
@subtitles = ('Server Settings');
# optional subcomponents
if($isNT) {
    push(@cgis, "$perl -I$rootDir/bin/https/perl -I$rootDir/bin/https/perl/lib -I$rootDir/bin/https/perl/lib/site/5.6.1/lib importCore60");
    push(@cgis, "$perl -I$rootDir/bin/https/perl -I$rootDir/bin/https/perl/lib -I$rootDir/bin/https/perl/lib/site/5.6.1/lib importServerXml");
}
else {
    push(@cgis, "$perl -I$rootDir/bin/https/perl -I$rootDir/bin/https/perl/lib -I$rootDir/bin/https/perl/lib/site_perl importCore60");
    push(@cgis, "$perl -I$rootDir/bin/https/perl -I$rootDir/bin/https/perl/lib -I$rootDir/bin/https/perl/lib/site_perl importServerXml");
}
    push(@subtitles, 'Server Xml ');

# set up directory variables
$mysname = $cgiVars{'name'};
$mysname =~ s/^\s+//;  # to remove spaces in the begining and end
$mysname =~ s/\s+$//;
$cgiVars{'name'} = $mysname;
$sname = "https-$mysname";

$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;
my $fullservername = "https" . "-" . $cgiVars{'name'};
if ($isNT) {
    my $rez = `$rootDir/bin/https/bin/getservice $fullservername`;
    if (($rez =~ /Service exists/)) {
        $serviceTaken = 1;
    }
}

# check if the username is valid
my $validuser = 1;
if (!$isNT) {
if($cgiVars{'user'}) {
     $username = $cgiVars{'user'};
     $idnum = 0;
     $idnum = getpwnam("$username");
     if ($idnum == 0 && $cgiVars{'user'} ne "root") {
         $validuser = 0;
     } else {
         $validuser = 1;
     }
}
}
# make sure directory is available
if (!(-e $serverDir) && !$serviceTaken && $validuser == 1) {
    # create directory
    mkdir( $serverDir, 0755 ) or die die_horrible_death("Can't create $serverDir: $!\n");
    if(!($isNT)){
        chmod(0755,"$serverDir");
        chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome");
    }
} else {
    open( CGI, "$perl importInfo60 |" );
    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 "\n Assimilation complete.\n";
    } else {
        print "  <BR><FONT COLOR=\"red\"><B>Assimilation failed.</B></FONT>\n";
    }
    print " <P>\n";
}

# The below code needs to be tested for 4.0 - 6.1 migration
#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 "Before starting this server, you should either stop the $cgiVars{'version'} server\n";
print "or change this server's port number.\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";

if (!$isNT) {
    chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/start", "$newHome/stop", "$newHome/restart", "$newHome/reconfig", "$newHome/rotate");
    chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/logs", "$newHome/config", "$newHome/conf_bk");
    chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/ClassCache", "$newHome/SessionData");
    chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/conf_bk/backups.conf", "$newHome/conf_bk/magnus.conf", "$newHome/conf_bk/obj.conf", "$newHome/conf_bk/server.xml", "$newHome/conf_bk/login.conf","$newHome/conf_bk/keyfile","$newHome/conf_bk/server.policy", "$newHome/conf_bk/default-web.xml");
    chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/conf_bk/backups.conf", "$newHome/conf_bk/magnus.conf.1", "$newHome/conf_bk/obj.conf.1", "$newHome/conf_bk/server.xml.1", "$newHome/conf_bk/login.conf.1","$newHome/conf_bk/keyfile.1","$newHome/conf_bk/server.policy.1", "$newHome/conf_bk/default-web.xml.1");
    chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/backups.conf", "$newHome/config/magnus.conf", "$newHome/config/obj.conf", "$newHome/config/server.xml", "$newHome/config/login.conf","$newHome/config/keyfile","$newHome/config/server.policy", "$newHome/config/default-web.xml");
    chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/server.xml.clfilter", "$newHome/config/magnus.conf.clfilter", "$newHome/config/obj.conf.clfilter", "$newHome/config/server.xml.clfilter");
}
# remove the ".tmp" files created for 4.1 - 6.1 migration
if ($cgiVars{'version'} eq "4.1") {
    unlink("$newHome/config/magnus.conf.tmp");
    unlink("$newHome/config/obj.conf.tmp");
    unlink("$newHome/config/server.xml.tmp");
}
