#!perl


use Cgi;
use BinUtil;
use Magnus;
use XML::Simple;

$| = 1;
$isNT = -d '\\';
$rootDir = $ENV{'NETSITE_ROOT'};
$rootDir =~ s/\\/\//g if $isNT;

@qos_string = ("maxbps", "enforcebandwidth", "maxconn", "enforceconnections");
@var_string=();
@auth_dbs=();

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

$webAppMsg = 0;

$oldHome = "$cgiVars{'dir'}/$cgiVars{'server'}";
$oldHome =~ tr/\/*/\//s;
$newHome = "$rootDir/$sname";

$oldsname="$cgiVars{'server'}";

$newRoot = "$rootDir";
$trail = substr($cgiVars{'dir'}, length($cgiVars{'dir'})-1, length($cgiVars{'dir'}));
if ($trail eq "/" || $trail eq "\\") {
    chop($cgiVars{'dir'});
}
$oldRoot = "$cgiVars{'dir'}";
$oldRoot =~ tr/\/*/\//s;
$docRoot = "$newRoot/docs";

$magnus = new Magnus("$oldHome/config/magnus.conf");
$filename = $magnus->value('ServerConfigurationFile');
if ($filename) {
    if ($isNT) { # windows has the path as drive: for absolute path
        $tmpfilename = substr($filename, 1, length($filename)-1);
        if ($tmpfilename !~ /^:/) {
           # the filename does not have absolute path 
           $filename = "$oldHome/config/$filename";
        }
   } else { # unix has the path starting with a / for absolute path
       if ($filename !~ /^\//) {
           # the filename does not have absolute path 
            $filename = "$oldHome/config/$filename";
       }
   }

#Warning indicating that migrated instance will have its server.xml under config.
print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> ServerConfigurationFile $filename is now migrated to $newHome/config/server.xml\n";
} else {
    $filename = "$oldHome/config/server.xml";
}

($https,$oldservername)=split(/\-/, $oldsname);

if ($isNT) {
    chomp($hostName = `hostname`);
} else {
    chomp($hostName = `uname -n`);
  }

getvars();

if ($cgiVars{'version'} eq "6.0") {
    $config = XMLin("$filename", forcearray=>1, keyattr=>[]);
} else {
  chmod(0644,"$newHome/config/server.xml.tmp");
   $config = XMLin("$newHome/config/server.xml.tmp", forcearray=>1, keyattr=>[]);
}

open(SXML,">$newHome/config/server.xml") or die "server.xml cannot be created\n";
chmod(0644,"$newHome/config/server.xml");

print SXML <<__UP_TO_THIS_POINT_;
<?xml version="1.0" encoding="UTF-8"?>

<!--
  Copyright (c) 2003 Sun Microsystems, Inc. All rights reserved.
  Use is subject to license terms.
-->

__UP_TO_THIS_POINT_

$dtdRoot = $newRoot;
$dtdRoot =~ s/\\/\//g;

if ($isNT) {
$dtdFile="file:///$dtdRoot/bin/https/dtds/sun-web-server_6_1.dtd";
} else {
$dtdFile="file://$newRoot/bin/https/dtds/sun-web-server_6_1.dtd";
}
print SXML <<__UP_TO_THIS_POINT_;
<!DOCTYPE SERVER PUBLIC "-//Sun Microsystems Inc.//DTD Sun ONE Web Server 6.1//EN" "$dtdFile">
__UP_TO_THIS_POINT_


$qosmetricsinterval = ${config}->{qosmetricsinterval};
$qosrecomputeinterval = ${config}->{qosrecomputeinterval};
$qosactive = ${config}->{qosactive};


print SXML "<SERVER ";
if (defined $qosactive) {
    print SXML "qosactive=\"$qosactive\" ";
}
if (defined $qosmetricsinterval) {
    print SXML "qosmetricsinterval=\"$qosmetricsinterval\" ";
}
if (defined $qosrecomputeinterval) {
    print SXML "qosrecomputeinterval=\"$qosrecomputeinterval\" ";
}
print SXML ">\n";

# Following parses <VARS> and places the required values in <PROPERTY>
#*********************************************************************
 for ($k=0; $var_string[$k]; $k++) {
   %myhash = ($var_string[$k], ${config}->{VARS}->[0]->{$var_string[$k]});
   if (defined $myhash{$var_string[$k]}) {
        if ( $var_string[$k] eq "docroot") {
           if ( $cgiVars{'docroot'} eq "old") {
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"$myhash{$var_string[$k]}\"/> \n";
           }
           else {
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"$docRoot\"/> \n";
           }
        }
        else {
            if ($isNT) {
                 $myhash{$var_string[$k]} =~ s/$oldRoot/$newRoot/i;
                 $myhash{$var_string[$k]} =~ s/$oldsname/$sname/i;
            }
            else {
                 $myhash{$var_string[$k]} =~ s/$oldRoot/$newRoot/;
                 $myhash{$var_string[$k]} =~ s/$oldsname/$sname/;
            }
        
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"$myhash{$var_string[$k]}\"/> \n";
       }
   } else {
     if ($var_string[$k] eq "user" || $var_string[$k] eq "group" || $var_string[$k] eq "chroot" || $var_string[$k] eq "dir" || $var_string[$k] eq "nice" ) {
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"\"/> \n";
         }
     elsif ($var_string[$k] eq "docroot") {
                $vsclass_def_docroot = ${config}->{VSCLASS}->[0]->{VARS}->[0]->{'docroot'};
                if ( $cgiVars{'docroot'} eq "old") {
                     print SXML "<PROPERTY name=\"docroot\" value=\"$vsclass_def_docroot\"/> \n";
           } else {
                print SXML "<PROPERTY name=\"docroot\" value=\"$docRoot\"/> \n";
           }
     }
     elsif ($var_string[$k] eq "accesslog") {
                     print SXML "<PROPERTY name=\"accesslog\" value=\"$newHome/logs/access\"/> \n";
         }

   }
 }

#Following parses the <LS> of server.xml
#******************************************
for ( $i=0; ${config}->{LS}->[$i]->{id}; $i++) {
        $ls_id = ${config}->{LS}->[$i]->{id};
        $ls_ip = ${config}->{LS}->[$i]->{ip};
        $ls_port = ${config}->{LS}->[$i]->{port};
        $ls_security = ${config}->{LS}->[$i]->{security};
        $ls_acceptorthreads = ${config}->{LS}->[$i]->{acceptorthreads};
        $ls_family = ${config}->{LS}->[$i]->{family};
        $ls_blocking = ${config}->{LS}->[$i]->{blocking};

        for ($j=0; ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{id}; $j++) {

        $connection_id = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{id};
        $connection_matchingip = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{matchingip};
        $connection_matchingip =~ s/default/$ls_ip/;
        $connection_servername = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{servername};
        $connection_defaultvs = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{defaultvs};

#       if ($isNT) {
#             $connection_defaultvs =~ s/$oldsname/$sname/i;
#       } else {
#             $connection_defaultvs =~ s/$oldsname/$sname/;
#       }

     print SXML "    <LS ";
     if (defined $connection_id) {
         print SXML "id=\"$connection_id\" ";
     }
     if (defined $connection_matchingip) {
         print SXML "ip=\"$connection_matchingip\" ";
     }
     if (defined $ls_port) {
         print SXML "port=\"$ls_port\" ";
     }
     if (defined $ls_acceptorthreads) {
         print SXML "acceptorthreads=\"$ls_acceptorthreads\" ";
     }
     if (defined $ls_blocking) {
         print SXML "blocking=\"$ls_blocking\" ";
     }
     if (defined $ls_security) {
         print SXML "security=\"$ls_security\" ";
     }
     if (defined $ls_family) {
         print SXML "family=\"$ls_family\" ";
     }
     if (defined $connection_defaultvs) {
         print SXML "defaultvs=\"$connection_defaultvs\" ";
     }
     if (defined $connection_servername) {
         print SXML "servername=\"$connection_servername\" ";
     }

     print SXML ">\n";

      if(${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}) {
         $ssl_servercertnickname = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{servercertnickname};
         $ssl_ssl2 = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{ssl2};
         $ssl_ssl2ciphers = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{ssl2ciphers};
         $ssl_ssl3 = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{ssl3};
         $ssl_ssl3tlsciphers = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{ssl3tlsciphers};
         $ssl_tls = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{tls};
         $ssl_tlsrollback = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{tlsrollback};
         $ssl_clientauth = ${config}->{LS}->[$i]->{CONNECTIONGROUP}->[$j]->{SSLPARAMS}->[$0]->{clientauth};

     print SXML "    <SSLPARAMS ";
     if (defined $ssl_servercertnickname) {
         print SXML "servercertnickname=\"$ssl_servercertnickname\" ";
     }
     if (defined $ssl_ssl2) {
         print SXML "ssl2=\"$ssl_ssl2\" ";
     }
     if (defined $ssl_ssl2ciphers) {
         print SXML "ssl2ciphers=\"$ssl_ssl2ciphers\" ";
     }
     if (defined $ssl_ssl3) {
         print SXML "ssl3=\"$ssl_ssl3\" ";
     }
     if (defined $ssl_ssl3tlsciphers) {
         print SXML "ssl3tlsciphers=\"$ssl_ssl3tlsciphers\" ";
     }
     if (defined $ssl_tls) {
         print SXML "tls=\"$ssl_tls\" ";
     }
     if (defined $ssl_tlsrollback) {
         print SXML "tlsrollback=\"$ssl_tlsrollback\" ";
     }
     if (defined $ssl_clientauth) {
         print SXML "clientauth=\"$ssl_clientauth\" ";
     }
     print SXML "/>\n";
      }
      print SXML "    </LS>\n"
      }
}
#******************************************
#Following parses the <MIME> of server.xml
#******************************************
for ( $i=0; ${config}->{MIME}->[$i]->{id}; $i++) {
        $mime_id = ${config}->{MIME}->[$i]->{id};
        $mime_file = ${config}->{MIME}->[$i]->{file};
     print SXML "    <MIME ";
     if (defined $mime_id) {
         print SXML "id=\"$mime_id\" ";
     }
     if (defined $mime_file) {
         print SXML "file=\"$mime_file\" ";
     }
     print SXML "/>\n";
}
#******************************************
#Following parses the <TYPE> of server.xml
#******************************************
for ( $i=0; ${config}->{TYPE}->[$i]; $i++) {
        $type_type = ${config}->{TYPE}->[$i]->{type};
        $type_language = ${config}->{TYPE}->[$i]->{language};
        $type_encoding = ${config}->{TYPE}->[$i]->{encoding};
        $type_extensions = ${config}->{TYPE}->[$i]->{extensions};
        print SXML "    <TYPE ";
        if (defined $type_type) {
           print SXML "type=\"$type_type\" ";
        }
        if (defined $type_language) {
           print SXML "type=\"$type_language\" ";
        }
        if (defined $type_encoding) {
           print SXML "type=\"$type_encoding\" ";
        }
        if (defined $type_extensions) {
           print SXML "type=\"$type_extensions\" ";
        }

        print SXML " />\n";
}
#******************************************
#Following parses the <ACLFILE> of server.xml
#******************************************
for ( $i=0; ${config}->{ACLFILE}->[$i]->{id}; $i++) {
        $acl_id = ${config}->{ACLFILE}->[$i]->{id};
        $acl_file = ${config}->{ACLFILE}->[$i]->{file};
# file attribute of ACLFILE in server.xml is already with new server root and
# servername while migrating from 4.1 as this is taken care in 4.1-6.0
# migration. Hence skipping this for 4.1 - 6.1 migration.
  if ($cgiVars{'version'} eq "6.0") {
       if ($isNT) {
           $acl_file =~ s/$oldRoot/$newRoot/i;
           $acl_file =~ s/$oldsname/$sname/i;
       } else {
           $acl_file =~ s/$oldRoot/$newRoot/;
           $acl_file =~ s/$oldsname/$sname/;
       }
  }
     print SXML "    <ACLFILE ";
     if (defined $acl_id) {
         print SXML "id=\"$acl_id\" ";
     }
     if (defined $acl_file) {
         print SXML "file=\"$acl_file\" ";
     }
     print SXML "/>\n";
}

#******************************************
# Following parses and places the value for VSCLASS & VS
# ***************************************************

for ( $i=0; ${config}->{VSCLASS}->[$i]->{id}; $i++) {
        $vsclass_id = ${config}->{VSCLASS}->[$i]->{id};
        $vsclass_objectfile = ${config}->{VSCLASS}->[$i]->{objectfile};
        $vsclass_rootobject = ${config}->{VSCLASS}->[$i]->{rootobject};
        $vsclass_acceptlanguage = ${config}->{VSCLASS}->[$i]->{acceptlanguage};

     print SXML "    <VSCLASS ";
     if (defined $vsclass_id) {
         print SXML "id=\"$vsclass_id\" ";
     }
     if (defined $vsclass_objectfile) {
         print SXML "objectfile=\"$vsclass_objectfile\" ";
     }
     if (defined $vsclass_rootobject) {
         print SXML "rootobject=\"$vsclass_rootobject\" ";
     }
     if (defined $vsclass_acceptlanguage) {
         print SXML "acceptlanguage=\"$vsclass_acceptlanguage\" ";
     }
     print SXML ">\n";


#******************************************
# Following parses <VARS> and places the required values in <PROPERTY>
#*********************************************************************

if (${config}->{VSCLASS}->[$i]->{VARS}) {
 for ($k=0; $var_string[$k]; $k++) {
   %myhash = ($var_string[$k], ${config}->{VSCLASS}->[$i]->{VARS}->[0]->{$var_string[$k]});
   if (defined $myhash{$var_string[$k]}) {
        if ( $var_string[$k] eq "docroot") {
           if ( $cgiVars{'docroot'} eq "old") {
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"$myhash{$var_string[$k]}\"/> \n";
           }
           else {
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"$docRoot\"/> \n";
           }
        }
      else {
           if ($isNT) {
               $myhash{$var_string[$k]} =~ s/$oldRoot/$newRoot/i;
               $myhash{$var_string[$k]} =~ s/$oldsname/$sname/i;
           } else {
               $myhash{$var_string[$k]} =~ s/$oldRoot/$newRoot/;
               $myhash{$var_string[$k]} =~ s/$oldsname/$sname/;
           }
      print SXML "    <PROPERTY name=\"$var_string[$k]\" value=\"$myhash{$var_string[$k]}\"/> \n";
      }
   }
  }
}

for ( $j=0; ${config}->{VSCLASS}->[$i]->{VS}->[$j]; $j++) {
  $vs_id = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{id};
  $vs_connections = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{connections};
  $vs_mime = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{mime};
  $vs_urlhosts = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{urlhosts};
  $vs_aclids = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{aclids};
  $vs_state = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{state};
  $vs_acceptlanguage = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{acceptlanguage};
  $vs_errorlog = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{errorlog};

  if (defined $vs_errorlog) {
  $old_vs_errorlog = $vs_errorlog;
     if ($isNT) {
         if ($vs_errorlog =~ m/$oldHome/i) {
                 $vs_errorlog =~ s/$oldHome/$newHome/i;
                 BinUtil::copyFile("$old_vs_errorlog", "$vs_errorlog");
                 chmod(0644,"$vs_errorlog");
         }
     } else {
             if ($vs_errorlog =~ m/$oldHome/) {
                 $vs_errorlog =~ s/$oldHome/$newHome/;
                 BinUtil::copyFile("$old_vs_errorlog", "$vs_errorlog");
                 chmod(0644,"$vs_errorlog");
            }
            chown((getpwnam($cgiVars{'user'}))[2, 3], "$vs_errorlog");
     }
 }
  
  if (defined $vs_state) {
     $vs_state =~ s/disable/disabled/;
  }
     print SXML "      <VS ";
     if (defined $vs_id) {
         print SXML "id=\"$vs_id\" ";
     }
     if (defined $vs_connections) {
         print SXML "connections=\"$vs_connections\" ";
     }
     if (defined $vs_urlhosts) {
         print SXML "urlhosts=\"$vs_urlhosts\" ";
     }
     if (defined $vs_objectfile) {
         print SXML "objectfile=\"$vs_objectfile\" ";
     }
     if (defined $vs_rootobject) {
         print SXML "rootobject=\"$vs_rootobject\" ";
     }
     if (defined $vs_mime) {
         print SXML "mime=\"$vs_mime\" ";
     }
     if (defined $vs_aclids) {
         print SXML "aclids=\"$vs_aclids\" ";
     }
   if (defined $vs_errorlog) {
       print SXML " errorlog=\"$vs_errorlog\" ";
   }
   if (defined $vs_acceptlanguage) {
       print SXML " acceptlanguage=\"$vs_acceptlanguage\" ";
   }
   if (defined $vs_state) {
       print SXML " state=\"$vs_state\" ";
   }
   print SXML ">\n";


# Following parses <VARS> and places the required values in <PROPERTY>
#*********************************************************************

if (${config}->{VSCLASS}->[$i]->{VS}->[$j]->{VARS}) {
 for ($k=0; $var_string[$k]; $k++) {
   %myhash = ($var_string[$k], ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{VARS}->[0]->{$var_string[$k]});
   if (defined $myhash{$var_string[$k]}) {
        if ( $var_string[$k] eq "docroot") {
           if ( $cgiVars{'docroot'} eq "old") {
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"$myhash{$var_string[$k]}\"/> \n";
           }
           else {
      print SXML "<PROPERTY name=\"$var_string[$k]\" value=\"$docRoot\"/> \n";
           }
        }
      else {
           if ($isNT) {
               $myhash{$var_string[$k]} =~ s/$oldRoot/$newRoot/i;
               $myhash{$var_string[$k]} =~ s/$oldsname/$sname/i;
           } else {
               $myhash{$var_string[$k]} =~ s/$oldRoot/$newRoot/;
               $myhash{$var_string[$k]} =~ s/$oldsname/$sname/;
           }
        print SXML "      <PROPERTY name=\"$var_string[$k]\" value=\"$myhash{$var_string[$k]}\"/> \n";
      }
   }
  }
}

#Following parses the <QOSPARAMS> of <VS>
#******************************************
if (${config}->{VSCLASS}->[$i]->{VS}->[$j]->{QOSPARAMS}) {
 print SXML "       <QOSPARAMS ";
 for ($k=0; $qos_string[$k]; $k++) {
   %myqoshash = ($qos_string[$k], ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{QOSPARAMS}->[0]->{$qos_string[$k]});
   if (defined $myqoshash{$qos_string[$k]}) {
      print SXML "$qos_string[$k]=\"$myqoshash{$qos_string[$k]}\" ";
   }
 }
  print SXML " />\n";
}


if (${config}->{VSCLASS}->[$i]->{VS}->[$j]->{USERDB}) {
  $userdb_id = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{USERDB}->[0]->{id};
  $userdb_database = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{USERDB}->[0]->{database};
  $userdb_basedn = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{USERDB}->[0]->{basedn};
  $userdb_certmaps = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{USERDB}->[0]->{certmaps};

  print SXML "       <USERDB "; 
  if (defined $userdb_id) {
        print SXML " id=\"$userdb_id\"";
  }
  if (defined $userdb_database) {
        print SXML " database=\"$userdb_database\"";
  }
  if (defined $userdb_basedn) {
        print SXML " basedn=\"$userdb_basedn\"";
  }
  if (defined $userdb_certmaps) {
        print SXML " certmaps=\"$userdb_certmaps\"";
  }
  print SXML " />\n";
}

if ($webAppFile = ${config}->{VSCLASS}->[$i]->{VS}->[$j]->{VARS}->[0]->{"webapps_file"}) {
   migrateWebApps();
 }

print SXML <<__UP_TO_THIS_POINT_;
      </VS>
__UP_TO_THIS_POINT_
}

#Following parses the <QOSPARAMS> of VSCLASS
#******************************************
if (${config}->{VSCLASS}->[$i]->{QOSPARAMS}) {
 print SXML "   <QOSPARAMS ";
 for ($k=0; $qos_string[$k]; $k++) {
   %myqoshash = ($qos_string[$k], ${config}->{VSCLASS}->[$i]->{QOSPARAMS}->[0]->{$qos_string[$k]});
   if (defined $myqoshash{$qos_string[$k]}) {
      print SXML "$qos_string[$k]=\"$myqoshash{$qos_string[$k]}\" ";
   }
 }
  print SXML " />\n";
}
print SXML <<__UP_TO_THIS_POINT_;
    </VSCLASS>
__UP_TO_THIS_POINT_
print SXML "\n";
}
# ***************************************************
#Following parses the <QOSPARAMS> of <SERVER>
#******************************************
if (${config}->{QOSPARAMS}) {
  print SXML "   <QOSPARAMS ";
 for ($k=0; $qos_string[$k]; $k++) {
   %myqoshash = ($qos_string[$k], ${config}->{QOSPARAMS}->[0]->{$qos_string[$k]});
   if (defined $myqoshash{$qos_string[$k]}) {
      print SXML "$qos_string[$k]=\"$myqoshash{$qos_string[$k]}\" ";
   }
 }
  print SXML " />\n";

}

####
# Call the importJDK function
####

importJDK();


####
# Print the close of the server element
####
print SXML <<__UP_TO_THIS_POINT_;
</SERVER>
__UP_TO_THIS_POINT_
close(SXML);

BinUtil::backupFile($newHome, $sname, "server.xml");
chmod(0644,"$newHome/conf_bk/server.xml");


# Warning in case of VARS getting migrated.
print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> All variables defined in VARS of server.xml have reference to 6.1 server root and 6.1 server name.<BR>\n";

sub getvars()
{
if ($cgiVars{'version'} eq "6.0") {
open(XML,"<$filename") or die "$filename cannot be opened \n";
} else {
open(XML,"<$newHome/config/server.xml.tmp") or die " server.xml.tmp cannot be opened\n";
}
while(<XML>) {
  if($_ =~ /ATTLIST VARS/) {
     chomp($line);
     $i = 0;
     next;
  }
  s/^\s*//;
  ($a,$b) = split(/ /,$_);
  last if ($a =~ /\>/);
  if ($a ne 'webapps_file' && $a ne 'webapps_enable' ){
      $var_string[$i] = $a;
      $i++;
  }
}
  close(XML);
}

sub importJDK()
{

    my $useDefault = 1;
    my $jrePath = "";
    my $jreVersion = "";
    my $jreMajVer = "";
    my $jreMinVer = "";
    my $jreDotVer = "";
    my $jrePatchVer = "";
    my @nativeLibs;
    my @serverCPprefix;
    my $debugAttrib = "false";
    my $profilerEnabled = "false";
    my $profilerClasspath;
    my $profilerNative;
    my $loglevel;
    my $debugOptions;
    my $envClasspathIgnored = "true";
    my $CPSep = ":";
    if($isNT) {
        $CPSep = ";";
    }


#####
#   Migrate the JVM from 6.0 to 6.1
#   6.1 changes the way the JVM is started, all of the information for the 
#   JVM is in the server.xml.  In 6.0 the start-jvm script in the admin 
#   server's directory pointed to the JVM and set up the runtime libraries.
#
#   The per instance setting in 6.0 are in the jvm12.conf file.  These 
#   translate to options that go in the server.xml.
#####

    if ($isNT) {
####
# Windows uses the registry to point to the JMV
# For now, we will always use the default until Win32::registry is added to 
# the build
####



    } else {

#### 
#  Unix uses the start-jvm script under the admserv instance
####
        open(STARTJVM, "<$oldRoot/https-admserv/start-jvm") or die "<Font COLOR=RED>Error:</FONT> Cannot open the file $oldHome/https-admserv/start-jvm.\n<BR>";


        while(<STARTJVM>)
        {
            if($_ =~ /^NSES_JDK=/) {
                if($_ =~ m/\=/g){
                    my $equalPos = pos $_;
                    if($_ =~ m/\;/g) {
                        my $semiPos = pos $_;
                        $jrePath = substr($_, $equalPos, $semiPos - $equalPos - 1);
                        chomp($os = `uname -s`);
                        open(VERS, "$jrePath/bin/java -version 2>&1 |");
                        while(<VERS>) {
                            if ($_ =~ /java version/) {
                                my @ver = split /\"/, $_;
                                $jreVersion = @ver[1];
                                my @subVer = split/\./, $jreVersion;
                                $jreMajVer = @subVer[0];
                                $jreMinVer = @subVer[1];
                                if ($os =~ /HP-UX/ )  {
                                  $jreDotVer = @subVer[2];
                                  $jrePatchVer = @subVer[3];
                                } elsif ($os =~ /AIX/ ) {
                                  $jreDotVer = @subVer[2];
                                  $jrePatchVer = 0;
                                } else {
                                  my @patchVer = split/_/, @subVer[2];
                                  $jreDotVer = @patchVer[0];
                                  $jrePatchVer = @patchVer[1];
                                }
                            }
                        }
                        close(VERS);
                        if ($jreMajVer >= 1) {
                            if ($jreMinVer >= 4) {
                                if ($jreDotVer == 1) {
                                    if ($os =~ /HP-UX/ )  {
                                      if ($jrePatchVer >= 05) {
                                        $useDefault = 0;
                                      }
                                    } elsif ($os =~ /AIX/ ) {
                                         $useDefault = 0;
                                    } else {
                                    if ($jrePatchVer >= 03) {
                                        $useDefault = 0;
                                    }
                                   }
                                 } elsif ($jreDotVer > 2) {
                                     $useDefault = 0;
                                 }
                             }
                        }
                      }
                    }
            } elsif ($_ =~ /^NSES_JRE_RUNTIME_LIBPATH/ ){
####
#  Find the Native LibPath from the startjvm script and save it 
#  to put in the nativelibrarysuffix attribute.  Ignore the entries that
#  are part of the JRE
####
                if($_ =~ m/\=/g){
                    my $equalPos = pos $_;
                    if($_ =~ m/\;/g) {
                        my $semiPos = pos $_;
                        my $jreLibPath = substr($_, $equalPos, $semiPos - $equalPos - 1);
                        my @libs = split /$XPSep/, $jreLibPath;
                        foreach $lib (@libs) {
                            if (($lib !~ /NSES_JRE/)) {
                                $#nativeLibs++;
                                @nativeLibs[scalar(@nativeLibs) - 1] = $lib;
                            }
                        }
                     }
                }
            } elsif ($_ =~ /^NSES_JDK_RUNTIME_CLASSPATH/ ) {
####
#  if the user has added anything to the RUNTIME CLASSPATH then we 
#  need to add it to the classpathprefix attribute.
####
                if($_ =~ m/\=/g){
                    my $equalPos = pos $_;
                    if($_ =~ m/\;/g) {
                        my $semiPos = pos $_;
                        my $jdkCPPath = substr($_, $equalPos, $semiPos - $equalPos - 1);
                        my @cpaths = split /$CPSep/, $jdkCPPath;
                        foreach $cpath (@cpaths) {
                            if ($cpath !~ /NSES/) {
                                $#serverCPprefix++;
                                @serverCPprefix[scalar(@serverCPprefix) - 1] = $cpath;
                            }
                        }
                    }
                }
            }
        }
        close(STARTJVM);
    }

    if ($useDefault == 0) {
         print "Info: The configured JDK is a valid version, $jreVersion, it will be used in the new server configuration.<BR>\n";
    } else {
        print "Info: The JDK version in the old server configuration is not 1.4 or greater, the JDK configured for Web Server 6.1 while installing will be used.<BR>\n"
    }


####
#  Now time to start the jvm12.conf parsing
#  Each of the lines in here will end up as an option in the 
#  server.xml, for the most part.  There are some special cases that
#  are really web container settings not JVM settings.
####

    open(JVM12, "<$oldHome/config/jvm12.conf") or die "<Font COLOR=RED>Error:</FONT> Cannot open the file $oldHome/https-admserv/config/jvm12.conf.\n<BR>";
    my @options;

    while (<JVM12>) {

    # Get rid the the new line character, it makes for ugly XML
        $_ =~ s/\n//;

        if ($_ =~ /^jvm.minHeapSize/) {
            my @temp = split/=/, $_;
               $minheap = @temp[1];
            if (@temp[1] <= "134217728" ) {
                $#options++;
                @options[scalar(@options) - 1] = "-Xms"."128m";
            } else {
                $#options++;
                @options[scalar(@options) - 1] = "-Xms".@temp[1];
            }

        } elsif ($_ =~ /^jvm.maxHeapSize/) {
            my @temp = split/=/, $_;
            if (@temp[1] <= "268435456" && @temp[1] >= $minheap) {
                $#options++;
                @options[scalar(@options) - 1] = "-Xmx"."256m";
            } elsif (@temp[1] <= $minheap)  {
                $#options++;
                @options[scalar(@options) - 1] = "-Xmx".$minheap;
            } elsif (@temp[1] >= "268435456")  {
                $#options++;
                @options[scalar(@options) - 1] = "-Xmx".@temp[1];
            }

        } elsif ($_ =~ /jvm.maxHeapSize/) {
            if ($minheap >= "268435456") {
                $#options++;
                @options[scalar(@options) - 1] = "-Xmx".$minheap;
            } else {
                $#options++;
                @options[scalar(@options) - 1] = "-Xmx"."256m";
            }

        } elsif ($_ =~ /^jvm.enableClassGC/) {
            my @temp = split /=/, $_;
            if (@temp[1] != "1") {
                $#options++;
                @options[scalar(@options) - 1] = "-Xdoclassgc";
            }
         } elsif ($_ =~ /^jvm.verboseMode/) {
            my @temp = split/=/, $_;
            if (@temp[1] == "gc") {
                $#options++;
                @options[scalar(@options) - 1] = "-verbose:gc";
           } elsif (@temp[1] == '1') {
                $#options++;
                @options[scalar(@options) - 1] = "-verbose";
           }
        } elsif ($_ =~ /^jvm.enableDebug/) {
           my @temp = split /=/, $_;
            if (@temp[1] == "1") {
                $debugAttrib = "true";
                if ($debugOptions) {
                    $debugOptions = $debugOptions . " -Xdebug";
                } else {
                    $debugOptions = "-Xdebug";
                }
            }
        } elsif ($_ =~ /^jvm.profiler/) {
            $profilerEnabled = "true";
        } elsif ($_ =~ /^OPTITDIR/) {
            @temp = split /=/, $_;
            $profilerClasspath = @temp[1];
            $profilerNative = @temp[1];
        } elsif ($_ =~ /^jvm.include.CLASSPATH/) {
            @temp = split /=/, $_;
            if (@temp[1] == "1") {
                $envClasspathIgnored = "false";
            }
        } elsif ($_ =~ /^jvm.trace/) {
            my @temp = split/=/, $_;
            if (@temp[1] == 5) {
                $loglevel = "fine";
            } elsif (@temp[1] == 6) {
                $loglevel = "finer";
            } elsif (@temp[1] == 7) {
                $loglevel = "finest";
            }
        } elsif ($_ =~ /^jvm.option/ ) {
            if($_ =~ m/\=/g){
                my $equalPos = pos $_;
                my $optionStr = substr($_, $equalPos);
                if ($optionStr =~ /^-Xrunjdwp/ || $optionStr =~ /^-Xdebug/) {
                    if ($debugOptions) {
                        $debugOptions = $debugOptions . " $optionStr";
                    } else {
                        $debugOptions = $optionStr;
                    }
                } else {
                    $#options++;
                    @options[scalar(@options) - 1] = $optionStr;
                }
    	}
        }elsif ($_ =~ /^jvm.classpath/ ) {
            if($_ =~ m/\=/g){
                my $equalPos = pos $_;
                my $jdkCPPath = substr($_, $equalPos);
                my @cpaths = split /$CPSep/, $jdkCPPath;
                foreach $cpath (@cpaths) {
                    if(($cpath !~ /SDKBeans10.jar/i) && ($cpath !~ /SDKBeans.jar/i) && ($cpath !~ /Bugbase.jar/i) && ($cpath !~ /Calljsac.jar/i)) {
                        $#serverCPprefix++;
                        @serverCPprefix[scalar(@serverCPprefix) - 1] = $cpath;
                    }
                }
            }
        }
    }

    close(JVM12);
    my $admServXML = XMLin("$newRoot/https-admserv/config/server.xml", forcearray=>1, keyattr=>[]);
    $javaHome = ${admServXML}->{JAVA}->[0]->{javahome};
    if(!($javaHome))  {
        $javaHome = "\"$newRoot/bin/https/jdk\"";
      }
    print SXML "  <JAVA javahome=";
    if ($jrePath && $useDefault == 0) {
        print SXML "\"$jrePath\"";
    } else {
        print SXML "\"$javaHome\"";
    }
    print SXML " serverclasspath=\"$newRoot/bin/https/jar/webserv-rt.jar$CPSep\${java.home}/lib/tools.jar$CPSep$newRoot/bin/https/jar/webserv-ext.jar$CPSep$newRoot/bin/https/jar/webserv-jstl.jar$CPSep$newRoot/bin/https/jar/ktsearch.jar\" classpathsuffix=\"";
    my $printColon = 0;
    foreach (@serverCPprefix) {
        if ($printColon == 1) {
            print SXML "$CPSep";
        } else {
            $printColon = 1;
        }
        print SXML "$_";
    }
    print SXML "\" envclasspathignored=\"$envClasspathIgnored\" debug=\"$debugAttrib\" debugoptions=\"$debugOptions\" dynamicreloadinterval=\"2\">\n";
    print SXML <<"TO_HERE";
        <JVMOPTIONS>-Dorg.xml.sax.parser=org.xml.sax.helpers.XMLReaderAdapter</JVMOPTIONS>
        <JVMOPTIONS>-Dorg.xml.sax.driver=org.apache.crimson.parser.XMLReaderImpl</JVMOPTIONS>
        <JVMOPTIONS>-Djava.security.policy=$newHome/config/server.policy</JVMOPTIONS>
        <JVMOPTIONS>-Djava.security.auth.login.config=$newHome/config/login.conf</JVMOPTIONS>
        <JVMOPTIONS>-Djava.util.logging.manager=com.iplanet.ias.server.logging.ServerLogManager</JVMOPTIONS>
TO_HERE
    foreach (@options) {
        print SXML "    <JVMOPTIONS>$_</JVMOPTIONS>\n";
    }
print SXML <<"TO_HERE";
    <SECURITY defaultrealm=\"file\" anonymousrole=\"ANYONE\" audit=\"false\">
      <AUTHREALM name=\"file\" classname=\"com.iplanet.ias.security.auth.realm.file.FileRealm\">
        <PROPERTY name=\"file\" value=\"$newHome/config/keyfile\"/>
        <PROPERTY name=\"jaas-context\" value=\"fileRealm\"/>
      </AUTHREALM>
      <AUTHREALM name=\"ldap\" classname=\"com.iplanet.ias.security.auth.realm.ldap.LDAPRealm\">
        <PROPERTY name=\"directory\" value=\"ldap://localhost:389\"/>
        <PROPERTY name=\"base-dn\" value=\"o=isp\"/>
        <PROPERTY name=\"jaas-context\" value=\"ldapRealm\"/>
      </AUTHREALM>
      <AUTHREALM name=\"certificate\" classname=\"com.iplanet.ias.security.auth.realm.certificate.CertificateRealm\"/>
TO_HERE

    foreach (@auth_dbs) {
        print SXML << "TO_HERE";
      <AUTHREALM name=\"$_\" classname=\"com.sun.enterprise.security.auth.realm.webcore.NativeRealm\">
          <PROPERTY name=\"auth-db\" value=\"$_\" />
       </AUTHREALM>
TO_HERE
    }

    print SXML << "TO_HERE";
    </SECURITY>
    <RESOURCES/>
  </JAVA>
TO_HERE
     $error_log = $magnus->value('ErrorLog');
     if ($error_log) {
         if ($error_log =~ m/SYSLOG/) {
            if (! $isNT) {
            print SXML "<LOG usesyslog=\"true\" loglevel=\"info\" />\n";
            }
         } else {
                 $old_errorlog = $error_log;
                 $set_errorflag = 0;
                 if ($isNT) {
                     if ($error_log =~ m/$oldHome/i) {
                         $error_log =~ s/$oldHome/$newHome/i;
                         $set_errorflag = 1;
                     }
                 } else {
                     if ($error_log =~ m/$oldHome/) {
                         $error_log =~ s/$oldHome/$newHome/;
                         $set_errorflag = 1;
                     }
                 }
                 if ($set_errorflag) {
                     BinUtil::copyFile("$old_errorlog", "$error_log");
                     chmod(0644,"$error_log");
                     if (!$isNT) {
                            chown((getpwnam($cgiVars{'user'}))[2, 3], "$error_log");
                       }

                 } else {
                   if (!$isNT) {
                         chown((getpwnam($cgiVars{'user'}))[2, 3], "$error_log");
                       }
                 }
                 print SXML "<LOG file=\"$error_log\" loglevel=\"info\" />\n";
               }
       } else {
                 print SXML "<LOG file=\"$newHome/logs/errors\" loglevel=\"info\" />\n";
       } 

}

sub  migrateWebApps
{
    if ($webAppMsg == 0) {
        print "Info: There seem to be Web Applications for the virtual servers.  There may be some setting that do not migrate correctly, please check the indicated file for exceptions.\n";
        $webAppMsg = 1;
    }
    # check if webAppFile is a relative or absolute path
    # If its relative then append oldHome/config to it.
    # else if its absolute path use the file name as it is.

    if ($isNT) {
        $tmpwebAppFile = substr($webAppFile, 1, length($webAppFile)-1);
        if ($tmpwebAppFile !~ /^:/) {
            # the filename does not have absolute path
            $webAppFile = "$oldHome/config/$webAppFile";
        }
    } else {
            if ($webAppFile !~ /^\//) {
            # the filename does not have absolute path
              $webAppFile = "$oldHome/config/$webAppFile";
            }
     }

     # Extract only the filename from the path to create webAppFile.log

       if( $webAppFile =~ /^(.*)\/(.*)$/ ) {
           $webAppPath = $1;
           $logFileName = $2;
       }

    print "    Virtual Server: $vs_id -> WebApp exception file: $newHome/logs/$logFileName.log\n";
    open (WAF, "<$webAppFile") or die "<FONT COLOR=RED>Error: Cannot open Web Applications file $webAppFile</FONT> <br>\n";
    open (WAL, ">$newHome/logs/$logFileName.log") or die "<FONT COLOR=RED>Error: Cannont open Web Applications log file $newHome/logs/$logFileName.log></FONT> <br>\n";
    my $waConfig = XMLin("$webAppFile", forcearray=>1, keyattr=>[]);
    if ($isNT) {
        $SWXML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE vs  PUBLIC \"-//Sun Microsystems, Inc.; //DTD Sun Specific Web
 Applications Deployment Descriptor//EN\" \"file:///$newRoot/bin/https/dtds/sun-web-app_2_3-1.dtd\">";
    } else {
        $SWXML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE vs  PUBLIC \"-//Sun Microsystems, Inc.; //DTD Sun Specific Web
 Applications Deployment Descriptor//EN\" \"file://$newRoot/bin/https/dtds/sun-web-app_2_3-1.dtd\">";
    }

# ----  XML::simple does not handle strings with - in them so they must be set
# ----  into variables.
    my $wa = "web-app";
    my $an = "auth-native";
    my $cl = "class-loader";
    my $rli = "reload-interval";
    my $pa = "parameter-encoding";
    my $fhf = "form-hint-field";
    my $fls = "form-login-session";
    my $jsp = "jsp-servlet";
    my $sm = "session-manager";
    my $iParam = "init-parameter";
    my $pm = "param-name";
    my $pv = "param-value";
    my @vs_userDB = ();



# ----- Element = auth-native --------
    for ($w = 0; ${waConfig}->{$wa}->[$w]; $w++) {
        if (${waConfig}->{$wa}->[$w]->{$an} || ${waConfig}->{$an}) {
            my $add = 1;
            my $vsadd = 1;
            my $authDB;
            if (${waConfig}->{$wa}->[$w]->{$an}->[0]->{authdb}) {
                $authDB = ${waConfig}->{$wa}->[$w]->{$an}->[0]->{authdb};
            } else {
                $authDB = {waConfig}->{$an}->[0]->{authdb};
            }
            foreach $line (@auth_dbs) {
                if ($line == $authDB) {
                    $add = 0;
                    last $line;
                }
            }
            foreach $line (@vs_userDB) {
                if ($line == $authDB) {
                   $vsadd = 0;
                   last $line;
	        }
            }
            print WAL <<"TO_HERE";
A new AuthRealm $authDB has been added. It may be necessary to add the following to the web.xml for the web applicatation at $uri in virtual server $vs_id.
        <login-config> 
            <realm-name>$authDB</realm-name>
       </login-config>

TO_HERE
            if ($add == 1) {
                $#auth_dbs++;
                @auth_dbs[scalar(@auth_dbs) - 1] = $authDB;
            }
            if ($vsadd == 1 && $authDB && $authDB ne $userdb_id ) {
                print SXML "        <USERDB id=\"$authDB\" database=\"$authDB\"/>\n";
            }
        }
    }

# ----  ---- Work through each web app in the virtual server ---- ----
# ---- Check each setting in both the web app and the VS ----
# ---- Web app setting take persidence -------

    for ($w = 0; ${waConfig}->{$wa}->[$w]; $w++) {
        my $uri = ${waConfig}->{$wa}->[$w]->{uri};
        my $path = ${waConfig}->{$wa}->[$w]->{dir};
        my $enabled = ${waConfig}->{$wa}->[$w]->{enabled};
        my $haveSWXml = 0;
        my $reload;
        if (!$enabled) {
            $enabled = "true";
        }
        print SXML "                <WEBAPP uri=\"$uri\" path=\"$path\" enabled=\"$enabled\"/>\n";
        print WAL "\n\n -----  MIGRATING Web Application $uri ----- \n\n";


# ---- Element = class-loader --------
        if (${waConfig}->{$wa}->[$w]->{$cl} || ${waConfig}->{$cl}) {
            if (!$haveSWXml) {
                open (SWXml, ">$path/WEB-INF/sun-web.xml");
                print SWXml $SWXML_HEADER;
                $haveSWXml = 1;
            }
            print SWXml "    <class-loader ";
            my $clClasspath;
            if ($clClasspath = ${waConfig}->{$wa}->[$w]->{$cl}->[0]->{classpath}) {
            } else {
                $clClasspath = ${waConfig}->{$cl}->[0]->{classpath};
            }
            if ($clClasspath) {
                print SWXml "extra-class-path=\/$clClasspath\" ";
            }
            my $clDelegate;
            if ($clDelegate = ${waConfig}->{$wa}->[$w]->{$cl}->[0]->{delegate}) {
            } else {
                $clDelegate = ${waConfig}->{$cl}->[0]->{delegate};
            }
            if ($clDelegate) {
                print SWXml "delegate=\"$clDelegate\" ";
            }
            print SWXml "/>\n";
            if ($reload = ${waConfig}->{$wa}->[$w]->{$cl}->[0]->{$rli}) {
            } else {
                $reload = ${waConfig}->{$cl}->[0]->{$rli};
            }
        }

# ---- Element = jsp-servlet ----
        if (${waConfig}->{$wa}->[$w]->{$jsp} || ${waConfig}->{$jsp} || $reload) {
            my %init_param = ();
            if ($reload) {
                $init_param{reload-interval} = $reload;
            }
            my @paramList = ("keepgenerated", "largefile", "scratchdir", "mappedfile", "ieClassId");
            if (${waConfig}->{$wa}->[$w]->{$jsp}) {
                for ($q = 0; ${waConfig}->{$wa}->[$w]->{$jsp}->[0]->{$iParam}->[$q]; $q++) {
                    foreach $param (@paramList) {
                        if (${waConfig}->{$wa}->[$w]->{$jsp}->[0]->{$iParam}->[$q]->{paramName} == $param) {
                            $init_param{$param} = ${waConfig}->{$wa}->[$w]->{$jsp}->[0]->{$iParam}->[$q]->{$paramValue};
                        }
                    }
                }
            } else {
                for ($q = 0; ${waConfig}->{$jsp}->[0]->{$iParam}->[$q]; $q++) {
                    foreach $param (@paramList) {
		        if (${waConfig}->{$jsp}->[0]->{$iParam}->[$q]->{paramName} == $param) {
                                $init_param{$param} = ${waConfig}->{$jsp}->[0]->{$iParam}->[$q]->{$paramValue};
                        }
                    }
                }
            }
            if (%init_param) {
                if (!$haveSWXml) {
                    open (SWXml, ">$path/WEB-INF/sun-web.xml");
                    print SWXml $SWXML_HEADER;
                    $haveSWXml = 1;
		}
                print SWXml "    <jsp-config>\n";
                while (($key, $value) = each %init_param) {
                    print SWXml "        <property name=\"$key\" value=\"$value\" />\n";
                }
                print SWXml "    </jsp-config>\n";
	    }
        }

# ---- Element = filter ----
        if (${waConfig}->{$wa}->[$w]->{filter}) {
            print WAL << "TO_HERE";

This Web Application is using filters.  These are now supported through the web.xml as per Servlet 2.3 specification.  You will need to manual copy the filter and filter-mapping elements from $webAppFile to $dir/WEB-INF/web.xml.


TO_HERE

        }

# ---- Element = parameter-encoding
        if (${waConfig}->{$wa}->[$w]->{$pa} || ${waConfig}->{$pa}) {
            my $paramEnc;
            my $hint;
            if (${waConfig}->{$wa}->[$w]->{$pa}) {
                $paramEnc = ${waConfig}->{$wa}->[$w]->{$pa}->[0]->{enc};
                $hint = ${waConfig}->{$wa}->[$w]->{$pa}->[0]->{$fhf};
            } else {
                $paramEnc = ${waConfig}->{$pa}->[0]->{enc};
                $hint = ${waConfig}->{$pa}->[0]->{$fhf};
            }
            if ($paramEnc == "auto" || $paramEnc == "none") {
                $paramEnc = 0;
            }
            if ($hint = "none") {
                $hint = 0;
            }
            if ($hint || $paramEnc) {
                if (!$haveSWXml) {
                    open (SWXml, ">$path/WEB-INF/sun-web.xml");
                    print SWXml $SWXML_HEADER;
                    $haveSWXml = 1;
                }
                print SWXml "    <$pa ";
                if ($hint) {
                    print SWXml "$fhf=\"$hint\" ";
                }
                if ($paraEnc) {
                    print SWXml "default-locale=\"$paramEnc\" ";
                }
                print SWXml "/>";
            }
        }

# ---- Element tempdir ----
        if (${waConfig}->{$wa}->[$w]->{tempdir} || ${waConfig}->{tempdir}) {
            if (!$haveSWXml) {
                open (SWXml, ">$path/WEB-INF/sun-web.xml");
                print SWXml $SWXML_HEADER;
                $haveSWXml = 1;
            }
            my $tmpDir;
            if ($tmpDir = ${waConfig}->{$wa}->[$w]->{tempdir}) {
            } else {
               $tmpDir = ${waConfig}->{tempdir};
            }
            print SWXml "    <property name=\"tempdir\" value=\"$tmpDir\"/>";
        }

# ---- Element form-login-session ----
        if (${waConfig}->{$wa}->[$w]->{$fls} || ${wConfig}->{$fls}) {
            if (!$haveSWXml) {
                open (SWXml, ">$path/WEB-INF/sun-web.xml");
                print SWXml $SWXML_HEADER;
                $haveSWXml = 1;
            }
            print SWXml "    <property name=\"sso-enabled\" value=\"true\"\>\n";
            my $flsTimeout;
            if ($flsTimeout = ${waConfig}->{$wa}->[$w]->{$fls}->{timeOut}) {
            } else {
                $flsTimeout = ${waConfig}->{$fls}->{timeOut};
            }
            print SWXml"    <property name=\"sso-max-inactive-seconds\" value=\"$flsTimeout\" />\n";
        }

# ---- Element session-manager ----
        if (${waConfig}->{$wa}->[$w]->{$sm} || ${waConfig}->{$sm}) {
            if (!$haveSWXml) {
                open (SWXml, ">$path/WEB-INF/sun-web.xml");
                print SWXml $SWXML_HEADER;
                $haveSWXml = 1;
            }
            print SWXml "    <session-config>\n        <session-manager>\n            <manager-properties>\n";
            my $className;
            if ($className = ${waConfig}->{$wa}->[$w]->{$sm}->{class}) {
            } else {
                $className = ${waConfig}->{$sm}->[0]->{class};
            }
            print SWXml "                <property name=\"class\" value=\"$className\" />\n";
            if (${waConfig}->{$wa}->[$w]->{$sm}) {
                    for ($q=0; ${waConfig}->{$wa}->[$w]->{$sm}->{$iParam}->[$q]; $q++) {
                        my $paramName = ${waConfig}->{$wa}->[$w]->$sm->{$iParam}->[$q]->{$pm}->[0];
                        my $paramValue = ${waConfig}->{$wa}->[$w]->$sm->{$iParam}->[$q]->{$pv}->[0];
                        my $descr = ${waConfig}->{$wa}->[$w]->$sm->{$iParam}->[$q]->{description}->[0];
                        print SWXml "                <property name=\"$paramName\" value=\"$paramValue\" ";
                        if ($descr) {
                            print SWXml ">/n            <description>\n                $descr\n            </description>\n            </property>/n";
                        } else {
                            print SWXml "/>";
                        }
                }
            } else {
                for ($q=0; ${waConfig}->{$sm}->[0]->{$iParam}->[$q]; $q++) {
                    my $paramName = ${waConfig}->{$sm}->[0]->{$iParam}->[$q]->{$pm}->[0];
                    my $paramValue = ${waConfig}->{$sm}->[0]->{$iParam}->[$q]->{$pv}->[0];
                    my $descr = ${waConfig}->{$sm}->[0]->{$iParam}->[$q]->{description}->[0];
                    print SWXml "                <property name=\"$paramName\" value=\"$paramValue\" ";
                    if ($descr) {
                        print SWXml ">/n            <description>\n                $descr\n            </description>\n            </property>/n";
                    } else {
                        print SWXml "/>";
                    }
                }
            }
        }
        close SWXml;
    }
    close(WAF);
    close(WAL);
    if(!$isNT) {
       chown((getpwnam($cgiVars{'user'}))[2, 3], "$newHome/logs/$logFileName.log");
    }

}
