#!perl
#
# Get 3.x server information

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

$| = 1;
$perl = "../../perl/perl";
@cgis = ("$perl importWebPubInfo");

$isNT = -d '\\';
grep { s@/@\\@g } @cgis if $isNT;	# reverse those slashes
$rootDir = $ENV{'NETSITE_ROOT'};

# generate page
$title = "Migration Parameters";
ES40lnf::print_html($title);
ES40lnf::print_body();
ES40lnf::print_header($title);

foreach (values %cgiVars) {
    if (/[\|\>\;\<]/) {
        die "<FONT COLOR=\"red\">[Error]<\FONT> Invalid character in URI \n";
    }
}

print "<FORM ACTION=\"import\" METHOD=\"get\">\n";
print "<INPUT TYPE=\"hidden\" NAME=\"dir\" VALUE=\"$cgiVars{'dir'}\">\n";
print "<INPUT TYPE=\"hidden\" NAME=\"server\" VALUE=\"$cgiVars{'server'}\">\n";
print "<INPUT TYPE=\"hidden\" NAME=\"version\" VALUE=\"$cgiVars{'version'}\">\n";
print "<INPUT TYPE=\"hidden\" NAME=\"javaVersion\" VALUE=\"$cgiVars{'javaVersion'}\">\n";

# general parameters
print "<TABLE CELLPADDING=\"5\">\n";
print "  <TR>\n";
ES40lnf::print_OpenTD();
print "\n   <TABLE>\n";
# server name:
print "     <TR>\n";
ES40lnf::print_OpenTD();
print "Server Name:";
ES40lnf::print_CloseTD();
ES40lnf::print_OpenTD();
$cgiVars{'server'} =~ /-/;
my $serverId = $';
my $dirNewSvr = "$rootDir/$cgiVars{'server'}";
my $serviceTaken = 0;
if ($isNT) {
    # on NT, check that the name is still available as a service id
    my $rez = `../../bin/getservice $cgiVars{'server'}`;
    if (($rez =~ /Service exists/)) {
	$serviceTaken = 1;
	$serverId .= "-new";
	$dirNewSvr = "$rootDir/$cgiVars{'server'}-new";
    }
}
if (-e $dirNewSvr) {
    $x = 1;
    $name = $dirNewSvr . "-" . $x;
    while (-e $name) {
	$x++;
	$name = $dirNewSvr . "-" . $x;
    }
    print "<INPUT TYPE=\"text\" NAME=\"name\" VALUE=\"$serverId-$x\">";
} else {
    print "<INPUT TYPE=\"text\" NAME=\"name\" VALUE=\"$serverId\">";
}
if ($cgiVars{'name'}) {
    $name = "$rootDir/https-$cgiVars{'name'}";
    if ($serviceTaken) {
	print " <FONT COLOR=\"red\">(\"$cgiVars{'name'}\" is unavailable as a service name)</FONT>";
    } elsif (-e $name) {
	print " <FONT COLOR=\"red\">(\"$cgiVars{'name'}\" has already been taken)</FONT>";
    }
}
ES40lnf::print_CloseTD();
print "     </TR>\n";
# run server as:
$dirAdmin = "$cgiVars{'dir'}/admin-serv";
$oldMagnus = new Magnus("$cgiVars{'dir'}/$cgiVars{'server'}/config/magnus.conf");
if (!$isNT) {
    print "     <TR>\n";
    ES40lnf::print_OpenTD();
    if ($oldMagnus) {
	$user = $oldMagnus->value( 'User' );
    }
    print "Run Server As:";
    ES40lnf::print_CloseTD();
    ES40lnf::print_OpenTD();
    print "<INPUT TYPE=\"text\" NAME=\"user\"";
    if ($user) {
	print " VALUE=\"$user\">";
    } else {
	print ">";
    }
    ES40lnf::print_CloseTD();
    print "     </TR>\n";
}
if (-f "$dirAdmin/config/cron.conf") {
    open(F, "$dirAdmin/config/cron.conf");
    $lines = 0;
    while (<F>) {
	if ($lines == 0) {
	    print "     <TR>\n";
	    print "      <TD COLSPAN=\"2\">${ES40lnf::dfltFont}";
	    print "<P><FONT COLOR=\"red\"><B>Warning:</B></FONT> Cron jobs are not migrated.\n";
	    print "<XMP>";
	}
	print;
	$lines++;
    }
    if ($lines != 0) {
	print "</XMP>\n";
	ES40lnf::print_CloseTD();
	print "     </TR>\n";
    }
}
print "   </TABLE>\n";
print "   </TD>\n";
print "  </TR>\n";
print "</TABLE>\n";


if ($cgiVars{'version'} eq "3.6") {
    # users info
    $dbswitch = "$cgiVars{'dir'}/userdb/dbswitch.conf";
    open(DBSWITCH, $dbswitch) or 
        die "<FONT COLOR=\"red\">Cannot open $dbswitch: $!.</FONT>\n";
    $isLocalDb = 0;
    while (<DBSWITCH>) {
        if (/directory default/) {
            if (/ldapdb:\/\//) {
                $isLocalDb = 1;
            }
            last;
        }
    }
    close DBSWITCH;
    if ($isLocalDb) {
        print "\n<P>\n";
        ES40lnf::print_subheader("Users and Groups");
        print "<INPUT TYPE=\"hidden\" NAME=\"isLocalDb\" VALUE=\"1\">\n";
        print "<TABLE CELLPADDING=\"5\">\n  <TR>\n";
        ES40lnf::print_OpenTD();
        print "Localdb is no longer supported in this release.\n";
        print "An LDAP server must be used to manage users and groups.\n";
        print "You have the option of exporting the data in your localdb into an LDIF file,\n";
        print "which can later be used to export the data to an LDAP server.\n";
        print "Note that this only needs to be done once per installation.\n";
        print "You can skip this section if users and groups are already configured\n";
        print "on your 4.x Web Server Administration Server.\n";
        print "   <P>\n";
        print "   <TABLE CELLPADDING=\"0\" CELLSPACING=\"0\">\n";
        print "     <TR>\n ";
        ES40lnf::print_OpenTD();
        print " Export Localdb to LDIF File:";
        ES40lnf::print_CloseTD();
        ES40lnf::print_OpenTD();
        print "<INPUT TYPE=\"radio\" NAME=\"ldif\" VALUE=\"1\"> Yes\n";
        print "<INPUT TYPE=\"radio\" NAME=\"ldif\" VALUE=\"0\" CHECKED> No\n";
        ES40lnf::print_CloseTD();
        print "     </TR>\n";
        print "   </TABLE>\n";
        print "   </TD>\n  </TR>\n</TABLE>\n";
    }
}


# certificates info
$gotSecurity = 0;
if ($cgiVars{'version'} eq "3.6") {
    if ($oldMagnus->value('Security') eq "on") {
        $gotSecurity = 1;
        print "\n<P>\n";
        ES40lnf::print_subheader('Keys and Certificates');
        print "<INPUT TYPE=\"hidden\" NAME=\"security\" VALUE=\"1\">\n";
        
        print "<SCRIPT>\n";
        print "function isEmpty(line) {\n";
        print "  for (var x = 0; x < line.length; x++) {\n";
        print "    var c = line.charAt(x);\n";
        print "    if ((c != ' ') && (c != '\\n') && (c != '\\t'))\n";
        print "      return false;\n";
        print "  }\n";
        print "  return true;\n";
        print "}\n\n";
        print "function verifySecurity(form) {\n";
        $key = $oldMagnus->value('Keyfile');
        $key =~ /.*\/(\S*)-key\.db/;
        print "  if (isEmpty(form.password.value)) {\n";
        print "      alert('Please enter the password for key \"$1\".');\n";
        print "      return false;\n";
        print "  }\n";
        print "  return true;;\n";
        print "}\n";
        print "</SCRIPT>\n";
        
        print "<TABLE CELLPADDING=\"5\">\n";
        print "  <TR>\n";
        print "   <TD>${ES40lnf::dfltFont}\n";
        print "Encryption has been enabled for this server.  Please enter the \n";
        print "password for the following key so that it can be migrated:\n";
        print "   <TABLE>\n";
        print "     <TR>\n";
        $key = $oldMagnus->value('Keyfile');
        $key =~ /.*\/(\S*)-key\.db/;
        ES40lnf::print_OpenTD();
        print "Key \"$1\":";
        ES40lnf::print_CloseTD();
        ES40lnf::print_OpenTD();
        print "<INPUT TYPE=\"hidden\" NAME=\"alias\" VALUE=\"$1\">\n";
        print "<INPUT TYPE=\"password\" NAME=\"password\">\n";
        ES40lnf::print_CloseTD();
        print "     </TR>\n";
        print "   </TABLE>\n";
        print "   </TD>\n";
        print "  </TR>\n";
        print "</TABLE>\n";
    }
}

# all other subcomponents:
print "\n<P>\n";
foreach $cgi ( @cgis ) {
    $fluff = 1;
    open(CGI, "$cgi |");
    while ( <CGI> ) {
	if ( $fluff ) {
	    $fluff = 0 if /^<!-- Start Form -->/;
	} else {	# in the meat of the CGI output
	    if ( /^<!-- End Form -->$/ ) {
		$fluff = 1;
	    } else {
		print;
	    }
	}
    }
    close( CGI );
}
if ($gotSecurity) {
    print "<SCRIPT>\n";
    print "function verify(form) {\n";
    if ($gotSecurity) {
	print "  if (!verifySecurity(form))\n";
	print "    return;\n";
    }
    print "  form.submit();\n";
    print "}\n";
    print "</SCRIPT>\n";
    print "<P>\n<INPUT TYPE=\"button\" VALUE=\"Migrate\" ONCLICK=\"verify(this.form)\">\n";
} else {
    print "<P>\n<INPUT TYPE=\"submit\" VALUE=\"Migrate\">\n";
}
    print "<input type=button value=\"Help\" onClick=\"";
    print &helpJavaScriptForTopic("migrate-param");
    print "\">\n";

print "</FORM>\n</BODY>\n</HTML>\n";
