#!perl
#
# Get 3.x server information

BEGIN 
{
    @INC = ( '../../perl' );
}

use Cgi;
use Magnus;
use Help;

$| = 1;
$perl = "../../perl/perl";                          
$isNT = -d '\\';
$rootDir = $ENV{'NETSITE_ROOT'};

# css selection and generate title and header
$title = "Migration Parameters";
open(CGI,"$perl includecss.pl \"$title\" \"$title\"|") or die "<FONT COLOR=\"red\">[Error]Cannot run includecss.pl: ",$! + 0,"</FONT>\n";
while (<CGI>)
{
	print;
}
close(CGI);
print "<BR/><DIV CLASS=\"TtlTxtDiv\">\<H2 CLASS=\"TtlTxt\">Please furnish the following specifications</H2></DIV>\n";
print "<TABLE BORDER=\"0\" TITLE=\"\" CELLPADDING=\"0\" CELLSPACING=\"0\">\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";
#java version attribute has been removed
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 "     { this.focus();";
print "       return false;\n";
print "  }}\n";
print "  return true;\n";
print "}\n\n";
print "</SCRIPT>\n";
# general parameters
print "<TR>\n";
print "<TD VALIGN=\"MIDDLE\" WIDTH=\"300\"><DIV CLASS=\"ConTblCl1Div\"><SPAN CLASS=\"LblLev2Txt\">\n";
print "Server name :\n";
print "\n</SPAN></DIV></TD>\n";
# server name:
$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 = `$rootDir/bin/proxy/admin/bin/getservice $cgiVars{'server'}`;
    if (($rez =~ /Service exists/))
    {
	$serviceTaken = 1;
	$serverId .= "-new";
	$dirNewSvr = "$rootDir/$cgiVars{'server'}-new";
    }
}

$name = $serverId;

if (-e $dirNewSvr)
{
    $x = 1;
    $name = $dirNewSvr . "-" . $x;
    while (-e $name)
    {
	$x++;
	$name = $dirNewSvr . "-" . $x;
    }
print "<TD WIDTH=\"300\"><DIV CLASS=\"ConTblCl2Div\"><INPUT TYPE=\"text\" NAME=\"name\" VALUE=\"$serverId-$x\" SIZE=\"40\" TITLE=\"Enter a server name\" onChange=\"stripSpaces(this)\">\n</DIV>\n</TD>\n";
}

else
{	
	print "<TD WIDTH=\"300\"><DIV CLASS=\"ConTblCl2Div\"><INPUT TYPE=\"text\" NAME=\"name\" VALUE=\"$serverId\"SIZE=\"40\" TITLE=\"Enter a server name\" onChange=\"stripSpaces(this)\">\n</DIV>\n</TD>\n";
}

	
if ($cgiVars{'name'}) 
{
$mysname = $cgiVars{'name'};
$name = "$rootDir/proxy-$mysname";
    if ($isNT)
    { 
			$rez = `$rootDir/bin/proxy/admin/bin/getservice $mysname`;
			if (($rez =~ /Service exists/))
 		   	{
				$serviceTaken = 1;
			}
			else 
			{
					$serviceTaken=0;
			}
   
			if ($serviceTaken||(-e $name))
        		{
					print "<TD WIDTH=\"300\"><div class=\"TtlHlpDiv\">";
					print "<div class=\"HlpPgeTxt\"><FONT COLOR=\"red\">(The server name specified is already available as either a service name or a directory)</font></div></div></td>";
    			} 
   } 
   elsif (-e $name) 
   {
		print "<TD WIDTH=\"300\"><div class=\"TtlHlpDiv\">";
		print "<div class=\"HlpPgeTxt\"><FONT COLOR=\"red\">(The server name specified has already been taken)</FONT></div></div></TD>";
   }

}

print "</TR>\n";

# run server as:
$dirAdmin = "$cgiVars{'dir'}/admin-serv";
$adminMagnus = new Magnus("$rootDir/proxy-admserv/config/magnus.conf");
$oldMagnus = new Magnus("$cgiVars{'dir'}/$cgiVars{'server'}/config/magnus.conf");
if (!$isNT) 
{   print "<TR>\n<TD VALIGN=\"MIDDLE\" WIDTH=\"300\"><DIV CLASS=\"conTblCl1Div\"><SPAN CLASS=\"LblLev2Txt\">\n";
    print "User:\n</SPAN></DIV></TD>\n";
    
    if ($adminMagnus)
    {  
	$adminuser = $adminMagnus->value('User');
        if ($adminuser !~ /root/)
	{$user = $adminuser;
         print "<TD VALIGN=\"MIDDLE\" WIDTH=\"300\"><DIV CLASS=\"conTblCl1Div\"><SPAN CLASS=\"LblLev2Txt\">\n";
    	 print "$user\n</SPAN></DIV></TD>\n";
         print "<INPUT TYPE=\"hidden\" NAME=\"user\" VALUE=\"$user\">\n";
	 print "<INPUT TYPE=\"hidden\" NAME=\"nonroot\" VALUE=\"1\">\n"; 
	 goto nonroot;
         }
    }	 
    print "<INPUT TYPE=\"hidden\" NAME=\"nonroot\" VALUE=\"0\">\n";
    if ($oldMagnus)
    {
	$user = $oldMagnus->value( 'User' );
    }
    print "<TD WIDTH=\"300\">\n<DIV CLASS=\"ConTblCl2Div\"><INPUT TYPE=\"text\" NAME=\"user\" SIZE=\"40\" TITLE=\"Enter user name\" onChange=\"stripSpaces(this)\"";
    if ($user)
    {
	print " VALUE=\"$user\">";
    }
    else 
    {
	print ">";
    }
    print "\n</DIV></TD>\n";
    if ($cgiVars{'user'}&& !$cgiVars{'nonroot'})
    {
		$username = $cgiVars{'user'};
        	$idnum=0;
		$idnum=getpwnam("$username");
		if ($idnum eq "") 
		{
			print " <TD WIDTH=\"300\"><div class=\"TtlHlpDiv\">";
			print "<div class=\"HlpPgeTxt\"><FONT COLOR=\"red\">(The specified user does not exist)</FONT></div></div></TD>";
    		}
    }
 nonroot:	 
    print "</TR>\n";
}

#cron jobs not to be migrated
if (-f "$dirAdmin/config/cron.conf")
{
    open(F, "$dirAdmin/config/cron.conf");
    $lines = 0;
    while (<F>) 
    {
	if ($lines == 0)
        {
	    print "<TR>\n";
	    print "<TD align=center><BR/>";
	    print "<FONT COLOR=\"red\"><div class=\"TtlHlpDiv\">";
            print "<div class=\"HlpPgeTxt\"><B>Warning:</B></FONT> Cron jobs are not migrated.\n";
	    print "<PRE>";
	}
	print;
	$lines++;
    }
    if ($lines != 0) 
    {
	print "</PRE>\n";
	print " </div></div></TD></TR>\n";
    }
}
print "</TABLE>\n";


# 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 "<TABLE BORDER=\"0\" TITLE=\"\" WIDTH=\"100%\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n";
	print "<TR><TD><DIV CLASS=\"TtlTxtDiv\">\n";
	print "<H2 CLASS=\"TtlTxt\">Users and groups</H2>\n";
	print "</DIV></TD>\n</TR>\n";
	print "<INPUT TYPE=\"hidden\" NAME=\"isLocalDb\" VALUE=\"1\">\n";
        print "<TR><TD>";
        print "<div class=\"TtlHlpDiv\">";
        print "<div class=\"HlpPgeTxt\">";
        print "&nbsp;&nbsp;&nbsp;Localdb is no longer supported in this release.";
        print "Native keyfile authentication will be available for the 4.0 Proxy server.<BR/>&nbsp;&nbsp;&nbsp;Any Localdb, if to be retained, will have to be exported to the LDAP.<BR/>";
        print "&nbsp;&nbsp;&nbsp;You have the option of exporting the data in your localdb into an LDIF file,";
        print "which can later be used to export the data to an LDAP server.<BR/>";
        print "&nbsp;&nbsp;&nbsp;Note that this only needs to be done once per installation.<BR/>";
        print "&nbsp;&nbsp;&nbsp;You can skip this section if users and groups are already configured ";
        print "on your 4.0 Proxy Server Administration Server.<BR/>";
        print "</div></div></TD></TR>\n";
        print "</TABLE>";
        print "<TABLE BORDER=\"0\" TITLE=\"\" WIDTH=\"50%\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n";
        print "<BR/><TR>\n ";
        print "<TD align=right valign=top><DIV CLASS=\"conTblCl1Div\"><SPAN CLASS=\"LblLev2Txt\">\n";
        print "Export Localdb to LDIF File :\n</SPAN></DIV></TD>\n";
        print "<td align=right><font FACE=\"PrimaSans BT, Verdana, sans-serif\" SIZE=\"3\"><input type=radio name=ldif value=\"1\" title=\"convert to LDIF\"> <b>Yes</b></td><td> <input type=radio name=ldif value=\"0\" CHECKED title=\"Do not convert to LDIF\"> <b>No</b></font></td>";
        print "     </TR>\n";
        print "   </TABLE>\n";
}


#cache settings 
print "<BR/><BR/>\n<TABLE BORDER=\"0\" TITLE=\"\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n";
print "<TR><TD><DIV CLASS=\"TtlTxtDiv\">\n";
print "<H2 CLASS=\"TtlTxt\">Cache settings</H2>\n";
print "</DIV></TD>\n</TR>\n";
print "<TR><TD>";
print "<div class=\"TtlHlpDiv\"><div class=\"HlpPgeTxt\">";
print "&nbsp;&nbsp;&nbsp;The default cache directory is under the instance root.<BR/>";
$size = 125;
if (!$isNT)
{
	print "&nbsp;&nbsp;&nbsp;Cache configuration parameters will be migrated over.The cache capacity displayed is as present in 3.6 Proxy Server, which may be edited. <BR/>"; 
	$objconf="$cgiVars{'dir'}/$cgiVars{'server'}/config/obj.conf";
	open(F,$objconf) or die "<FONT COLOR=\"red\">Cannot open $objconf: $!.</FONT>\n";
	while (<F>)
	{
   		if (/init-cache/)
		{
          		/ndirs=\"*(\d+)\"*\s+/;
			 $size = $1 *125;
			last;
		}
	}
	close(F);
}
print "</div></div></td></tr></table>";
print "<TABLE BORDER=\"0\" TITLE=\"\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n";
print "<TR>\n";
print "<TD VALIGN=\"MIDDLE\" WIDTH=\"300\"><DIV CLASS=\"ConTblCl1Div\"><SPAN CLASS=\"LblLev2Txt\">\n";
print "Cache directory : \n";
print "\n</SPAN></DIV></TD>\n";
$x1=1;
$cacheDir="<default>";
#while (-e $cacheDir)
#{
#	$cacheDir="$rootDir/cache";
#	$cacheDir .= "-$x1" ;
#	$x1++;
#}
print "<TD WIDTH=\"300\"><DIV CLASS=\"ConTblCl2Div\"><INPUT TYPE=\"text\" NAME=\"cachedir\" SIZE=\"40\" TITLE=\"Enter cache directory\" VALUE=\"$cacheDir\" onChange=\"stripSpaces(this)\"></DIV></TD>";
if ($cgiVars{'cachedir'}&& ($cgiVars{'cachedir'}ne$cacheDir))
{ 
	$cached = $cgiVars{'cachedir'};
	if (-e $cached)
       	{	
		 	                    print "<TD WIDTH=\"300\"><div class=\"TtlHlpDiv\">";
					    print "<div class=\"HlpPgeTxt\"><FONT COLOR=\"red\">The cache directory specified already exists</FONT></div></div></TD>";
	}	
        elsif ($cached =~ /(.*)\/[^\/]+/)
        { 
        if (!(-e $1))
	{
		print "<TD WIDTH=\"300\"><div class=\"TtlHlpDiv\"><div class=\"HlpPgeTxt\"><FONT COLOR=\"red\">The parent of the cache directory specified does not exist</FONT></div></div></TD>";
	}
	}
        elsif (($cached =~ /\s/)||($cached =~ /^[^\/]+$/))
	{
		print "<TD WIDTH=\"300\"><div class=\"TtlHlpDiv\"><div class=\"HlpPgeTxt\"><FONT COLOR=\"red\">Please specify an absolute path</FONT></div></div></TD>";
	}
}
print "</TR>\n";
print "<TR>\n";
print "<TD VALIGN=\"MIDDLE\" WIDTH=\"300\"><DIV CLASS=\"ConTblCl1Div\"><SPAN CLASS=\"LblLev2Txt\">\n";
print "Cache capacity :\n";
print "\n</SPAN></DIV></TD>\n";
print "<TD WIDTH=\"300\">\n<DIV CLASS=\"ConTblCl2Div\"><INPUT TYPE=\"text\" NAME=\"cachesize\" SIZE=\"40\" TITLE=\"Enter cache capacity\" VALUE=\"$size\" onChange=\"stripSpace(this)\">&nbsp;<b>MB</b></DIV></TD>";

if ($cgiVars{'cachesize'})
{
	$csize=$cgiVars{'cachesize'};
if (($csize =~ /[^\d]/)||($csize<125)||($csize>32000)||($csize%125) ||($csize!=125 && $csize%2))
	{ 
		print "\n<TD WIDTH=\"300\"><div class=\"TtlHlpDiv\"><div class=\"HlpPgeTxt\"><FONT COLOR=\"red\"> Must be in the range 125-32000, in even multiples of 125 </FONT></tr></tr></TD>";
	}
}	
print "</TR>\n";
print "\n</TABLE>";


# certificates info
$gotSecurity = 0;
if (($oldMagnus)&&($oldMagnus->value('Security') eq "on"))
{
	$gotSecurity = 1;
        print "<BR/><BR/>\n<TABLE BORDER=\"0\" TITLE=\"\" WIDTH=\"100%\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n";
print "<TR><TD><DIV CLASS=\"TtlTxtDiv\">\n";
print "<H2 CLASS=\"TtlTxt\">Keys and Certificates</H2>\n";
print "</DIV></TD>\n</TR>\n";
print "<INPUT TYPE=\"hidden\" NAME=\"security\" VALUE=\"1\">\n";
print "<SCRIPT LANGUAGE=\"JavaScript\">\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 "  <TR><TD>\n";
        print "<div class=\"TtlHlpDiv\"><div class=\"HlpPgeTxt\">";
        print "&nbsp;&nbsp;&nbsp;Encryption has been enabled for this server.<BR/>&nbsp;&nbsp;&nbsp;Please enter the password for the following key so that it can be migrated.";
        print "</div></div></td></TR>\n";
        $key = $oldMagnus->value('Keyfile');
        $key =~ /.*\/(\S*)-key\.db/;
        print "</TABLE><TABLE BORDER=\"0\" TITLE=\"\" WIDTH=\"85%\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n";

        print "<TR><TD VALIGN=\"TOP\"><DIV CLASS=\"ConTblCl1Div\"><SPAN CLASS=\"LblLev2Txt\">\n";
        print "Key \"$1\":</span></div></td>\n";
 
        print "<INPUT TYPE=\"hidden\" NAME=\"alias\" VALUE=\"$1\">\n";
        print "<TD><DIV CLASS=\"ConTblCl2Div\"><INPUT TYPE=\"password\" NAME=\"password\" SIZE=\"40\" TITLE=\"Enter password\" onChange=\"stripSpaces(this)\"></DIV></TD>";
        print "     </TR>\n";
        print "   </TABLE>\n";
}

    print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
print "function stripSpaces(textElement) {\n";
print "x = textElement.value;";
 print "while (x.substring(0,1) == ' ') x = x.substring(1);";
print "while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);";
print "textElement.value = x;}\n";
 print "function verifyfields(form) {\n";
 	print "  if (isEmpty(form.name.value)) {\n";
        print "      alert('Please enter a server name .');\n";
        print "      return false;\n";
        print "  }\n";
	print "      if (isEmpty(form.user.value)) {\n";
        print "      alert('Please enter a user name .');\n";
        print "      return false;\n";
        print "  }\n";
	print "  if (isEmpty(form.cachesize.value)) {\n";
        print "      alert('Please enter the cache size.');\n";
        print "      return false;\n";
        print "  }\n";
	print "  if (isEmpty(form.cachedir.value)) {\n";
        print "      alert('Please enter the cache directory .');\n";
        print "      return false;\n";
        print "  }\n";
	print "return true;\n";
	print "}\n";
        print "function verify(form) {\n";
    if ($gotSecurity) 
    {   	print "  if (!verifySecurity(form))\n";
	print "    return;\n";
    }
   	print "  if (!verifyfields(form)){\n";
	print "   return;}\n";
        print "  form.submit();\n";
        print "}\n";
        print "function cancelMigration(){\n";
        print "parent.content.location = '/proxy-admserv/bin/migrate';\n}\n";
        print "</SCRIPT>\n";
#print "<p>\n<input type=submit value=\"Migrate\" onclick=\"verify(this.form)\">\n";
    
print "<br/><br/><left><table width=\"50%\"><tr><td width=\"33%\" align=\"right\"><input type=\"submit\" value=\"Migrate\" class=\"Btn1\"  TITLE=\"Migrate\" ONCLICK=\"verify(this.form)\"></td><td width=\"33%\" align=\"right\"><input type=\"button\" value=\"Reset\" Title=\"Reset form values\" class=\"Btn1\" onClick=\"this.form.reset()\"></td><td width=\"33%\" align=\"right\"><input type=\"button\" value=\"Cancel\" class=\"Btn1\"  TITLE=\"Cancel\" ONCLICK=\"cancelMigration()\"></td>";
print "</tr></table></center>\n";
print "<SCRIPT>";
print "parent.tabs.helplocation='migrate-param'\n</SCRIPT>";


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