#!perl
# Import server settings.
# Called by import60.
# This script is used for both 4.1/6.0 -6.1 migration.

#All the files and directories created by the script are forced to have permissions by chmod(), so that the migration can happen between different user domains.

# to get an unique temp dir name
sub tempdirhash {
    my ($str) = @_;
    my $hash = 0;

    foreach $chr (unpack("C*", $str)) {
        $hash = $hash ^ ($hash << 5) ^ $chr;
    }
    sprintf("%08x", $hash);
}

sub copyObjconf60to61($);

# Some The following variables need to be removed for 6.1 as they may not be reqd.

BEGIN {
    $rootDir = $ENV{'NETSITE_ROOT'};
    $vPort="";
    $vAddress = "";
    $vAcceptLanguage="off";
    $vSecurity="";
    $vConf41="notfound";
    $vConfUrl41="urlnotfound";
    $OsvCount=0;
    $hvsCount=0;
    $OhvCount=0; # not being used
    $aOhvCount=0; # count h/w vs in obj.conf with only address in NameTrans
    $bOhvCount=0; # count h/w vs in obj.conf with address, port in NameTrans
    $conGroupid=1;
    $ClassCount=0;
    @vDocroot=();
    @vhDocroot=(); # stores docroot from virtual.conf
    @vhDocrootAddr=(); # stores docroot from obj.conf with "address" alone
    @vhDocrootPort=(); # stores docroot from obj.conf with " address, port"
    @vlSocket=();
    @vlSocketAddr=(); # stores address from obj.conf  with address in NameTrans
    @vlSocketPort=(); # stores address from obj.conf with address,port
    @vlPort = (); # stores port from obj.conf conf address,port in NameTrans
    @urlHosts=(); # stores simple SVS entries
    @urlHostsRoot=(); # stores SVS document root
    @urlClass=(); # stores complex SVS entries
    $url_vconf=0;
    $docRoot=(); # the main doc root for context.properties
    $flag=0;
    $nextVScount=0;
    $shortHost="";
    %warned = ();
    $initialized = 0;
    %roots=(); # maintains the hashtable w.r.t obj.conf parsed vaules-used by writeDocRoot()
# SSL migration parameters
        $CERTDefaultNickname="Server-Cert";
        $SSL2="on";
        $SSL3="on";
        $SSLClientAuth="off";
        $ssl3Ciphers="";
        $ssl2Ciphers="";
}
use Cgi;
use BinUtil;
use Magnus;
use XML::Simple;

$| = 1;
$isNT = -d '\\';

if ($isNT) {
    $libExt = ".dll";
    $libPrefix = "";
    $libLocation = "bin";
    $os = "WINNT";
    chomp($hostName = `hostname`);
 # hostname is made lowercase on windows just for consistency with other certs.
    $hostName = lc($hostName);
} else {
    $libPrefix = "lib";
    $libLocation = "lib";
    chomp($os = `uname -s`);
    chomp($hostName = `uname -n`);
    if ($os =~ /HP-UX/) {
        $libExt = ".sl";
    } else {
        if (!$os) {
            print "<FONT COLOR=\"red\">Warning:</FONT> Cannot run <CODE>uname</CODE>.  Assuming this is <B>not</B> an HP machine.\n";
        }
        $libExt = ".so";
    }
}
%pkgList = BinUtil::getPkgList($ENV{'NETSITE_ROOT'});

$rootDir = $ENV{'NETSITE_ROOT'};
$binDir = "$rootDir/bin/https";
$libDir = "$binDir/$libLocation"; 
$libDir =~ s/\\/\//g if $isNT;

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

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

$newRoot=$rootDir;
$oldsname="$cgiVars{'server'}";
my $enableJava = $cgiVars{'enableJava'};
$oldlibDir = "$oldRoot/bin/https/$libLocation";

if($isNT) {
    # create NT-specific entries
    print "Creating Registry Keys and Service...\n";
    system("$rootDir/bin/https/bin/ntregister -r$rootDir -i$cgiVars{'name'} > nul");
} else {
    # create Unix-specific scripts
    $ldlibrarypath = "\${SERVER_ROOT}/bin/\${PRODUCT_NAME}/lib:";
    if ($os =~ /AIX/) {
        # AIX requires /usr/lib and /lib to be in the path
        $ldlibrarypath .= "/usr/lib:/lib:";
    }
    $ldlibrarypath .= "\${LD_LIBRARY_PATH}";

# The following takes the new start/stop/reconfig scripts of 6.1 install
# directory and places in the config directory with the right paths.

    $binSubDir = "bin/https/bin";
    $libSubDir = "bin/https/lib";
#    ($shortInstance)=split(/\./, $sname);
    $src = "$newRoot/https-admserv/start";
    open(SRC, "<$src") or die "<FONT COLOR=\"red\">[Error]</FONT> Cannot read $src: $!\n";
    while (<SRC>)
    {
        ($name,$value) = split(/\=/, $_);
        $hash{$name} = $value;
        $javaRunTime = $hash{'JAVA_RUNTIME_LIBPATH'};
    }
    close (SRC);

   BinUtil::makeScript("SERVER_ROOT=$rootDir", "INSTANCE_ROOT=$rootDir",
                       "INSTANCE_NAME=$sname", "NAME=https", 
                       "PRODUCT_BIN=webservd-wdog",
                       "LD_LIBRARY_PATH=$ldlibrarypath","BIN_SUBDIR=$binSubDir",
                       "LIB_SUBDIR=$libSubDir",
                       "JAVA_RUNTIME_LIBPATH=$javaRunTime",
                        "$rootDir/bin/https/install/misc/start",
                        "$newHome/start");
    BinUtil::makeScript("INSTANCE_DIR=$rootDir/$sname",
                        "$rootDir/bin/https/install/misc/stop",
                        "$newHome/stop");
    BinUtil::makeScript("INSTANCE_DIR=$rootDir/$sname",
                        "$rootDir/bin/https/install/misc/restart",
                        "$newHome/restart");
    BinUtil::makeScript("WSENV_PATH=$rootDir/bin/https/bin",
                        "WS_INSTANCENAME=$sname",
                        "$rootDir/bin/https/install/misc/reconfig.template",
                        "$newHome/reconfig");
    BinUtil::makeScript("SERVER_ROOT=$rootDir", "BIN_SUBDIR=$binSubDir",
                        "INSTANCE_DIR=$rootDir/$sname",
                        "$rootDir/bin/https/install/misc/rotate",
                        "$newHome/rotate");
    chmod(0755,"$newHome/start");
    chmod(0755,"$newHome/stop");
    chmod(0755,"$newHome/restart");
    chmod(0755,"$newHome/reconfig");
    chmod(0755,"$newHome/rotate");
}

#create directories
mkdir("$newHome/logs", 0755);
chmod(0755, "$newHome/logs");
mkdir("$newHome/config", 0755);
chmod(0755, "$newHome/config");
mkdir("$newHome/conf_bk", 0755);
chmod(0755, "$newHome/conf_bk");

mkdir("$newHome/ClassCache", 0755);
mkdir("$newHome/SessionData", 0755);
chmod(0755, "$newHome/ClassCache", "$newHome/SessionData");

# copy ACL files
print "Overwriting dbswitch.conf ... \n";
BinUtil::copyFile("$cgiVars{'dir'}/userdb/dbswitch.conf",
                   "$rootDir/userdb/dbswitch.conf");
print "Assimilating ACL Files...\n";
BinUtil::copyFile("$cgiVars{'dir'}/httpacl/generated.$cgiVars{'server'}.acl",
                  "$rootDir/httpacl/generated.${sname}.acl");
BinUtil::copyFile("$cgiVars{'dir'}/httpacl/genwork.$cgiVars{'server'}.acl",
                  "$rootDir/httpacl/genwork.${sname}.acl");

if (!$isNT) {
    # change owner of acl files
    chown((getpwnam($cgiVars{'user'}))[2, 3],
          "$rootDir/httpacl/genwork.${sname}.acl",
          "$rootDir/httpacl/generated.${sname}.acl");
}

chmod(0644,"$newHome/conf_bk/backups.conf");

# handle magnus.conf
print "Assimilating <CODE>magnus.conf</CODE>...\n";

#BinUtil::copyFile("$oldHome/config/magnus.conf", "$newHome/config/magnus.conf");
# check if version is 4.x. if so call copyMconfig41() and convert 41 to 60
# magnus.conf and save this as magnus.conf.tmp. pass this file to copyMconfig61
# to convert 60 to 61 magnus.conf

if ($cgiVars{'version'} eq "4.1" ) {
   getESDocRoot("$oldHome/config/obj.conf");
   copyMconfig41to60();

   # strip out obsolete directives
   my %obsoleteDirectives =(
                         'Certfile' =>1,
                         'Keyfile'  =>1
                        );
   removeObsoleteDirectives("$newHome/config/magnus.conf.tmp", \%obsoleteDirectives);

# translate paths
BinUtil::fixPaths("$newHome/config/magnus.conf.tmp",
                  2,
                  $oldHome, $cgiVars{'dir'},
                  $newHome, $rootDir,
                  '/logs/', '/httpacl/', '/alias/');
$magnusfile = "magnus.conf.tmp";
}

$oldMagnus = new Magnus("$oldHome/config/magnus.conf");


# handle 4.1 - 6.0 obj.conf
if($cgiVars{'version'} eq "4.1") {
copyObjconf41to60();
fixObjects("$newHome/config/obj.conf.tmp");
BinUtil::fixPaths("$newHome/config/obj.conf.tmp",
                   2,
                  $oldHome, $cgiVars{'dir'},
                  $newHome, $rootDir,
                  '/logs/' );
writeDocRoot("$newHome/config/obj.conf.tmp");
}

#create 6.0 server.xml from 4.x data
if($cgiVars{'version'} eq "4.1") {
    if($vConf41 eq "notfound" && $vConfUrl41 eq "urlnotfound")
    {
            createDefaultSdotXml();
    }
    else
    {
            closeSXMLtag();
    }
}

copyMconfig60to61();
# translate paths
BinUtil::fixPaths("$newHome/config/magnus.conf",
                  2,
                  $oldHome, $cgiVars{'dir'},
                  $newHome, $rootDir,
                  '/logs/', '/httpacl/', '/alias/');


$magnus = new Magnus("$newHome/config/magnus.conf");
($shortHost)=split(/\./, $hostName);

print "Assimilating <CODE>Keys and Certificates</CODE>...\n";
if ( -f "$oldRoot/alias/$oldsname-$hostName-cert7.db" ) {
    BinUtil::copyFile("$oldRoot/alias/$oldsname-$hostName-cert7.db", "$newRoot/alias/$sname-$hostName-cert7.db");
    BinUtil::copyFile("$oldRoot/alias/$oldsname-$hostName-key3.db", "$newRoot/alias/$sname-$hostName-key3.db");
} elsif ( -f "$oldRoot/alias/$oldsname-$shortHost-cert7.db" ) {
    BinUtil::copyFile("$oldRoot/alias/$oldsname-$shortHost-cert7.db", "$newRoot/alias/$sname-$shortHost-cert7.db");
    BinUtil::copyFile("$oldRoot/alias/$oldsname-$shortHost-key3.db", "$newRoot/alias/$sname-$shortHost-key3.db");
}
if (-e "$oldRoot/alias/secmod.db") {

    print "Overwriting secmod.db ... \n";
    BinUtil::copyFile("$oldRoot/alias/secmod.db", "$newRoot/alias/secmod.db");

    # Set LD_LIBRARY_PATH For Solaris and Linux \n";
    $ld_libpath = "$rootDir/bin/https/lib:/usr/lib/mps/secv1:/usr/lib/mps:/usr/lib/mps/sasl2:/opt/sun/private/lib:/opt/sun/private/share/lib:/opt/sun/lib";
    $ENV{'LD_LIBRARY_PATH'} = $ld_libpath;

    # Set LIBPATH for AIX.\n";
    $libpath = "$rootDir/bin/https/lib";
    $ENV{'LIBPATH'} = $libpath;

    # Set SHLIB_PATH for HP-UX\n";
    $shlib_path = "$rootDir/bin/https/lib";
    $ENV{'SHLIB_PATH'} = $shlib_path;

    $modutil = "$rootDir/bin/https/admin/bin/modutil";

    if ($isNT) {
	$atemp = $ENV{'PATH'};
	$atemp = "$atemp;$rootDir/bin/https/bin";
	$ENV{'PATH'}=$atemp;
	system("$modutil -dbdir $newRoot/alias -delete \"Root Certs\" -force");
	system("$modutil -dbdir $newRoot/alias -add \"Root Certs\" -libfile \"nssckbi$libExt\" -force");
    }else {
	system("$modutil -dbdir $newRoot/alias -delete 'Root Certs' -force");
	system("$modutil -dbdir $newRoot/alias -add 'Root Certs' -libfile 'libnssckbi$libExt' -force");
    }
}

$serverid = $magnus->value('ServerID');
if ($serverid) {
    $magnus->set('ServerID', $cgiVars{'name'});   
}

# Following will change the User, TempDir values in 6.1 magnus.conf
if (!$isNT) {
    # deal with any user changes
    $user = $magnus->value('User');
    if ($cgiVars{'user'} and $cgiVars{'user'} ne $user) {
        $magnus->set('User', $cgiVars{'user'});
    }
    chown((getpwnam($cgiVars{'user'}))[2, 3], "$newHome/logs");
    # edit the TempDir directive
      $magnus->set('TempDir', "/tmp/$sname" . "-" . tempdirhash($newHome));
    $pidlog = $magnus->value('PidLog');
    $pidlog = "$newHome/logs/pid";
    $magnus->set('PidLog',$pidlog);
}

# The following will be set only for 6.0 as 4.1 did not have server.xml
$oldmagnus = new Magnus("$oldHome/config/magnus.conf");
$serverfilename = $oldmagnus->value('ServerConfigurationFile');
 if ($serverfilename) {
   if ($isNT) {
        $tmpserverfilename = substr($serverfilename, 1, length($serverfilename)-1);
        if ($tmpserverfilename !~ /^:/) {
            # the filename does not have absolute path 
            $serverfilename = "$oldHome/config/$serverfilename";
        }
   } else {
            if ($serverfilename !~ /^\//) {
            # the filename does not have absolute path 
              $serverfilename = "$oldHome/config/$serverfilename";
            }
     }
   }
   else {
       $serverfilename = "$oldHome/config/server.xml";
   }

# this is to write all the magnus-set into magnus.conf
$magnus->flush();

# handle  6.0 - 6.1 obj.conf
# Here check if version is 4.x and call objconf for 6.0 - 6.1 only once.
# if version is 6.0 this is called in a loop as many entries of obj.conf
# found in server.xml for virtual servers.

if ($cgiVars{'version'} eq "4.1" ) {
     print "Assimilating <CODE>obj.conf</CODE>...\n";
     copyObjconf60to61("obj.conf.tmp");
    BinUtil::backupFile($newHome, $sname, "obj.conf");
    chmod(0644,"$newHome/conf_bk/obj.conf");
}
else {
     $serverXml_obj = XMLin("$serverfilename", forcearray=>1, keyattr=>[]);
    if(${serverXml_obj}->{VSCLASS}) {
        for($a=0; ${serverXml_obj}->{VSCLASS}->[$a]; $a++) {
            my $objFile = ${serverXml_obj}->{VSCLASS}->[$a]->{objectfile};
            print "Assimilating <CODE>$objFile</CODE>...\n";
            copyObjconf60to61($objFile);
	    BinUtil::backupFile($newHome, $sname, "$objFile");
            chmod(0644,"$newHome/conf_bk/$objFile");
            if(!$isNT) {
                chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/$objFile" ,"$newHome/conf_bk/$objFile", "$newHome/conf_bk/$objFile.1");
            }
        }
    }
}

# backup magnus.conf
BinUtil::backupFile($newHome, $sname, "magnus.conf");
chmod(0644,"$newHome/conf_bk/magnus.conf");


# copy new files for 6.1 from install directory
BinUtil::copyFile("$rootDir/bin/https/install/misc/login.conf", "$newHome/config/login.conf");
BinUtil::copyFile("$rootDir/bin/https/install/misc/keyfile", "$newHome/config/keyfile");
BinUtil::copyFile("$rootDir/bin/https/install/misc/server.policy", "$newHome/config/server.policy");
BinUtil::copyFile("$rootDir/bin/https/install/misc/default-web.xml", "$newHome/config/default-web.xml");

chmod(0644,"$newHome/config/login.conf");
chmod(0644,"$newHome/config/keyfile");
chmod(0644,"$newHome/config/server.policy");
chmod(0644,"$newHome/config/default-web.xml");

BinUtil::backupFile($newHome, $sname, "login.conf");
chmod(0644,"$newHome/conf_bk/login.conf");
BinUtil::backupFile($newHome, $sname, "keyfile");
chmod(0644,"$newHome/conf_bk/keyfile");
BinUtil::backupFile($newHome, $sname, "server.policy");
chmod(0644,"$newHome/conf_bk/server.policy");
BinUtil::backupFile($newHome, $sname, "default-web.xml");
chmod(0644,"$newHome/conf_bk/default-web.xml");

# copy .clfilter files
# web-apps and web.conf filter are not required in 6.1

BinUtil::copyFile("$oldHome/config/magnus.conf.clfilter", "$newHome/config/magnus.conf.clfilter");
BinUtil::copyFile("$oldHome/config/obj.conf.clfilter", "$newHome/config/obj.conf.clfilter");
# Create a default server.xml.clfilter in case of 4.1-6.1 migration
if ($cgiVars{'version'} eq "4.1" ) {
        open(SXML,">>$newHome/config/server.xml.clfilter") or die "server.xml.clfilter can not be created\n";
print SXML <<__UP_TO_THIS_POINT_;
#
# Filter specification for receiving cluster file transfer
#
FILTER=filterobj %OLDFILE% %NEWFILE%
__UP_TO_THIS_POINT_
} else {
BinUtil::copyFile("$oldHome/config/server.xml.clfilter", "$newHome/config/server.xml.clfilter");
}
chmod(0644,"$newHome/config/server.xml.clfilter");
chmod(0644,"$newHome/config/magnus.conf.clfilter");
chmod(0644,"$newHome/config/obj.conf.clfilter");

#certmap.conf and cron.conf
print "<FONT COLOR=\"red\">Warning:</FONT> certmap.conf and ns-cron.conf are not migrated. <BR>";

#access logs are not migrated. provide warning .
print "<FONT COLOR=\"red\">Warning:</FONT> old access logs are not migrated. 6.1 will have new access logs.<BR>";

print "Errors file only under old-instance/logs/errors is migrated to new-instance/logs/errors.\n";

# copy mime types
print "Assimilating <CODE>mime.types</CODE>...\n";
if ($cgiVars{'version'} eq "4.1" ) {
            BinUtil::copyFile("$oldHome/config/mime.types", "$newHome/config/mime.types");
            chmod(0644,"$newHome/config/mime.types");
            BinUtil::backupFile($newHome, $sname, "mime.types");
            chmod(0644,"$newHome/conf_bk/mime.types");
            if(!$isNT) {
                chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/mime.types" ,"$newHome/conf_bk/mime.types", "$newHome/conf_bk/mime.types.1");
            }
} else {
    if(${serverXml_obj}->{MIME}) {
        for($i=0; ${serverXml_obj}->{MIME}->[$i]; $i++) {
            $mime_file = ${serverXml_obj}->{MIME}->[$i]->{file};
            BinUtil::copyFile("$oldHome/config/$mime_file", "$newHome/config/$mime_file");
            chmod(0644,"$newHome/config/$mime_file");
            BinUtil::backupFile($newHome, $sname, "$mime_file");
            chmod(0644,"$newHome/conf_bk/$mime_file");
            if(!$isNT) {
                chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/$mime_file","$newHome/conf_bk/$mime_file", "$newHome/conf_bk/$mime_file.1");
            }
        }
    }
}
print "&nbsp;&nbsp;See <CODE>$binDir/install/misc/mime.types</CODE> for any new MIME types.\n";

# copy all the ACL files at instance level for 6.0-6.1

if ($cgiVars{'version'} eq "6.0") {
    if(${serverXml_obj}->{ACLFILE}) {
        for($i=0; ${serverXml_obj}->{ACLFILE}->[$i]; $i++) {
              $acl_file = ${serverXml_obj}->{ACLFILE}->[$i]->{file};
              if ($isNT) {
                 if ($acl_file =~ /$oldRoot\/httpacl\//i) {
                    $new_acl_file = $acl_file;
                    $new_acl_file =~ s/$oldRoot/$newRoot/i;
                    BinUtil::copyFile("$acl_file", "$new_acl_file");
                    chmod(0644,"$new_acl_file");
                 }
              } else {
                 if ($acl_file =~ /$oldRoot\/httpacl\//) {
                    $new_acl_file = $acl_file;
                    $new_acl_file =~ s/$oldRoot/$newRoot/;
                    BinUtil::copyFile("$acl_file", "$new_acl_file");
                    chmod(0644,"$new_acl_file");
                    # change owner of acl files
                    chown((getpwnam($cgiVars{'user'}))[2, 3],
                           "$new_acl_file");
                 }
              }
         }
     }
}
# copy snmp.conf
if (-e "$oldHome/config/snmp.conf") {
        print "Assimilating <CODE>snmp.conf</CODE>...\n";
        open(OLDSNMP,"<$oldHome/config/snmp.conf");
        open(NEWSNMP,">$newHome/config/snmp.conf");
        while( $line = <OLDSNMP>)
        {
                if ($line =~ m/MASTERHOST/) {
                    $line = "MASTERHOST $cgiVars{'name'}\n";
                }
                print NEWSNMP $line;
        }
close(OLDSNMP);
close(NEWSNMP);

chmod(0644,"$newHome/config/snmp.conf");
BinUtil::backupFile($newHome, $sname, "snmp.conf");
chmod(0644,"$newHome/conf_bk/snmp.conf");
if(!$isNT) {
   chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/snmp.conf", "$newHome/conf_bk/snmp.conf", "$newHome/conf_bk/snmp.conf.1");
}
}

#copy nsfc.conf
if (-e "$oldHome/config/nsfc.conf") {
            print "Assimilating <CODE>nsfc.conf</CODE>...\n";
            BinUtil::copyFile("$oldHome/config/nsfc.conf", "$newHome/config/nsfc.conf");
            chmod(0644,"$newHome/config/nsfc.conf");
            BinUtil::backupFile($newHome, $sname, "nsfc.conf");
            chmod(0644,"$newHome/conf_bk/nsfc.conf");
            if(!$isNT) {
               chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/nsfc.conf", "$newHome/conf_bk/nsfc.conf", "$newHome/conf_bk/nsfc.conf.1");
            }
}

exit;

#  copies obj.conf  and changes function names to current versions
sub copyObjconf60to61($)
{
        my $objFile = shift;
        if ( $cgiVars{'version'} eq "6.0") {
           open(OLDOCONF,"<$oldHome/config/$objFile") or die "$objFile can not be copied\n";
           open(NEWOCONF,">$newHome/config/$objFile") or die "$objFile can not be created\n";
           chmod(0644, "$newHome/config/$objFile");
        }
        elsif ( $cgiVars{'version'} eq "4.1" ) {
           $tmpobjFile = $objFile;
           open(OLDOCONF,"<$newHome/config/$tmpobjFile") or die "$tmpobjFile can not be opened\n";
           open(NEWOCONF,">$newHome/config/obj.conf") or die "obj.conf can not be created\n";
           chmod(0644, "$newHome/config/obj.conf");
# The following is done to print obj.conf instead of obj.conf.tmp
           $objFile = "obj.conf";
           }


        my $firstLog=1;
        my $deleteObject=0;

        while(<OLDOCONF>)
        {
                if($deleteObject == 1 || $_ =~ /Object name=\"jsp092\"/ || $_ =~ /Object name=\"search\"/ || $_ =~ /Object name=\"ServletByExt\"/)
                {
                    if($_ =~ /\/Object/)
                    {
                        $deleteObject=0;
                        next;
                    }
                    $deleteObject=1;
                    next;
                }

                if ($_ =~ /NSServletNameTrans/)
                {
                    $_ =~ s/NSServletNameTrans/ntrans-j2ee/;
                    $_ =~ s/name=\"servlet\"/name=\"j2ee\"/;
                    if($enableJava !~ /off/) {
                        print NEWOCONF $_;
		      }
                    next;
                }
                elsif ( $_ =~ /pfx2dir/ && ($_ =~ /name=\"servlet\"/) )
                {
                    print "<FONT COLOR=\"red\">Warning:</FONT> Servlet directories are not supported in Version 6.1, the line $_ is being removed from $objFile. <BR>\n";
                    next;
                }
                elsif ( $_ =~ /pfx2dir/ && ($_ =~ /name=\"ServletByExt\"/) )
                {
                    print "<FONT COLOR=\"red\">Warning:</FONT> Servlet by extension is not supported in Version 6.1, the line $_ is being removed from $objFile.
 <BR>\n";
                    next;
                }
                elsif ( $_ =~ /pfx2dir/ && ($_ =~ /name=\"jsp092\"/) )
                {
                    print "<FONT COLOR=\"red\">Warning:</FONT> JSP version 0.92 is not supported in Version 6.1, the line $_ is being removed from $objFile.  <BR>\n";

                    next;
                }
                elsif ( $_ =~ /pfx2dir/ && ($_ =~ /from=\"\/search-ui\"/) )
                {
                    print "<FONT COLOR=\"red\">Warning:</FONT> Search has been changed in Version 6.1, the line $_ is being removed from $objFile.  Please refer to documentation on how to configure the new Search subsystem.  <BR>\n";

                    next;
                }
                elsif ( $_ =~/es-search-nametrans/ )
                {
                    print "<FONT COLOR=\"red\">Warning:</FONT> Search has been changed in Version 6.1, the line $_ is being removed from $objFile.  Please refer to documentation on how to configure the new Search subsystem.  <BR>\n";
                    next;
                }
                elsif ( ($_ =~ /Object/) && ($_ =~ /name=\"servlet\"/) )
                {
                    $_ =~ s/servlet/j2ee/;
                    print NEWOCONF $_;
                    next;
                }
                elsif($_ =~ /NSServletService/)
                {
                    $_ =~ s/NSServletService/service-j2ee/;
                    if ($enableJava !~ /off/) {
                        print NEWOCONF $_;
		      }
                    next;
                }
                elsif( ($_ =~ AddLog) && ($firstLog == 1) && ($enableJava !~ /f/) )
                {
                    my $outLine = "Error fn=\"error-j2ee\"\n";
                    print NEWOCONF $outLine;
                    $firstLog = 0;
                    print NEWOCONF $_;
                    next;
                }
                else
                {
                    print NEWOCONF $_;
                }
                if ($_ =~ /$oldRoot/){
                    if($cgiVars{'version'} eq "6.0") {
                        print "<FONT COLOR=\"red\">Warning:</FONT> $objFile contains a reference to the Old Server installation directory. <BR>\n";
                        $_ =~ s/</&lt/; 
                        $_ =~ s/>/&gt/;
                        print "$_ <BR>\n";
                    }
		  } elsif ($isNT)
                  {
                      if (lc($_) =~ /\L$oldRoot/){
                          if($cgiVars{'version'} eq "6.0") {
                              print "<FONT COLOR=\"red\">Warning:</FONT> $objFile cont
ains a reference to the Old Server installation directory. <BR>\n";
                        $_ =~ s/</&lt/; 
                        $_ =~ s/>/&gt/;
                              print "$_ <BR>\n";
                          }
                      }
                   }
        }
        close(OLDOCONF);
        close(NEWOCONF);

}

#converts 6.0 magnus.conf to 6.1 magnus.conf
sub copyMconfig60to61
{
       @strings = ("ConfigFile", "Shtml", "ares3", "dirmon4", "freebl_hybrid_3", "freebl_pure32_3", "getprop", "icudata", "icui18n", "icuuc", "j2eeplugin", "ld ap50", "ldappr50", "libdbm", "libsi18n", "ns-httpd40", "nsfc", "nspr4", "nsprwrap", "nss3", "nssckbi", "nstime", "nstp", "plc4", "plds4", "smime3", "softokn3","ssl3", "ssldap50", "support", "xerces-c" );

    if ($cgiVars{'version'} eq "4.1") {
        open(OLDMCONF,"<$newHome/config/magnus.conf.tmp") or die "magnus.conf.tmp can not be opened\n";
    } else {
        open(OLDMCONF,"<$oldHome/config/magnus.conf") or die "magnus.conf can not be opened\n";
  }

        open(NEWMCONF,">$newHome/config/magnus.conf") or die "magnus.conf can not be created \n";
        chmod(0644,"$newHome/config/magnus.conf");

        while( $line = <OLDMCONF>)
        {
               if ($line =~ /^\s*ExtraPath\s+/) {
                   if ($isNT) {
                   $line =~ s/$oldRoot/$newRoot/i;
                   $line =~ s/\\/\//g ;
                   $line =~ s/\$\{NSES_JRE_RUNTIME_LIBPATH\}//;
                   }
               }
               elsif($line =~ /\s*ServerRoot\s+/)
                {
                    chomp($line);
                     next;
                }
                elsif($line =~ /^\s*ServerConfigurationFile\s+/)
                {
                    chomp($line);
                     next;
                }
                elsif($line =~ /^\s*MtaHost\s+/)
                {
                    chomp($line);
                     next;
                }
                elsif($line =~ m/NSServletEarlyInit/)
                {
                    chomp($line);
                     next;
                }
                elsif($line =~ m/NSServletLateInit/)
                {
                    chomp($line);
                     next;
                }
                elsif($line =~ m/NSServletPlugin/)
                {
                    chomp($line);
                     next;
                }
                elsif($line =~ m/es-search-init/)
                {
                    chomp($line);
                     next;
                }
                elsif($line =~ m/mime-types/)
                {
                    chomp($line);
                     next;
                }
                $i = 0;
                foreach (@strings)
                {
                    if ($line =~ m/$libPrefix$strings[$i]$libExt/)
                    {
                        if ($isNT) {
                        $line =~ s/$oldlibDir/$libDir/i;
                        last;
                        } else {
                        $line =~ s/$oldlibDir/$libDir/;
                        last;
                        }
                    }
                    $i++;
                }

                if ($line =~ m/plugins/) {
                   if ($line =~ m/htaccess/) {
                      if ($isNT) {
                      $line =~ s/$oldRoot\/plugins\/htaccess\/bin/$newRoot\/plugins\/htaccess/i;
                      $line =~ s/\\/\//g ;
                      } else {
                      $line =~ s/$oldRoot\/plugins\/htaccess\/bin/$newRoot\/plugins\/htaccess/;
                      }
                   }
                   if ($line =~ m/loadbal/) {
                      if ($isNT) {
                      $line =~ s/$oldRoot\/plugins\/loadbal\/bin/$newRoot\/plugins\/loadbal/i;
                      $line =~ s/\\/\//g ;
                     } else {
                      $line =~ s/$oldRoot\/plugins\/loadbal\/bin/$newRoot\/plugins\/loadbal/;
                     }
                   }
                }
                print NEWMCONF $line;
        }
       if($enableJava !~ /off/) {
          print NEWMCONF "Init fn=\"load-modules\" shlib=\"$libDir\/$libPrefix"."j2eeplugin$libExt\" shlib_flags=\"\(global|now\)\" \n";
        }
        close(OLDMCONF);
        close(NEWMCONF);

}

# Since Init directives are now handled in magnus.conf we have to parse those
# a little differently
sub magnusInit {
    $_ = shift;
    $OLDOCONFHANDLE = shift;
    

    if (/Init/) {

        $holdInit = $_;
        $marker = tell($OLDOCONFHANDLE);
        while(<$OLDOCONFHANDLE>){
            if($_ =~ /^(\s+)|(\t+)/){
               chomp($holdInit);
               $holdInit = $holdInit.$_;
               $marker = tell($OLDOCONFHANDLE);
            }
            else{
               $_ = $holdInit;
               seek($OLDOCONFHANDLE, $marker, 0);
               last;
            }

        }

        if($isNT){
           $_ =~ s/shlib=\"$oldRoot/shlib=\"$newRoot/i if /shlib=\"$oldRoot/i;
        }
        else{
           $_ =~ s/shlib=\"$oldRoot/shlib=\"$newRoot/ if /shlib=\"$oldRoot/;
        }

            if($isNT){
               $_ =~ s/$oldHome/$newHome/gi if /fn=\"es-search-init\"/;
               $_ =~ s/$oldRoot/$newRoot/gi if /fn=\"es-search-init\"/;
            }
            else{
               $_ =~ s/$oldHome/$newHome/g if /fn=\"es-search-init\"/;
               $_ =~ s/$oldRoot/$newRoot/g if /fn=\"es-search-init\"/;
            }


        if (!$initialized) {
            # new Init directives to add
                push(@data, "Init fn=\"load-modules\" shlib=\"$binDir/");
                push(@data, "$libLocation/$libPrefix"."NSServletPlugin$libExt\" ");
                push(@data, "funcs=\"NSServletEarlyInit,NSServletLateInit,NSServletNameTrans,NSServletService\" shlib_flags=\"(global|now)\"\n");
                push(@data, "Init fn=\"NSServletEarlyInit\" EarlyInit=yes\n");
                push(@data, "Init fn=\"NSServletLateInit\"  LateInit=yes\n");
            $initialized = 1;
        } else {
            if (/fn=\"?load-modules\"?/) {
                # obsolete Init fn="load-modules" directives to discard
                if (/SJavaBootInit/ || /java-init/ || /java-run/ ||
                    /java-find-applet/ || /java-run-applet/ ||
                    /ns_agent/ || /agent_name_trans/ ||
                    /rdm-init/ || /rdm-type/ || /rdm-service/ || /rdm-log/) {
                    return 0;
                }
                # on AIX, library suffixes have changed
                if ($os =~ /AIX/) {
                    # change name from _shr.a to .so
                    $_ =~ s/_shr\.a/\.so/;
                }
                # Init directives to modify
                if (/livewireInit/) {
                    # remove ssjs Init directive
                    print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> Server Side JavaScript is no longer supported.\n";
                    return 0;
                }
                
                if (/CM_Init/) {
                    # remove webpub Init directives
                    if (!$warned{'webpub'}) {
                        print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> Web Publishing is no longer supported.\n";
                        $warned{'webpub'} = 1;
                    }
                    return 0;
                }

            } elsif (/fn=\"?SJavaBootInit\"?/ ||
                     /fn=\"?java-init\"?/ || /fn=\"?java-run\"?/ ||
                     /fn=\"?java-find-applet\"?/ || /fn=\"?java-run-applet\"?/ ||
                     /fn=\"?ns_agentInit\"?/ || /fn=\"?rdm-init\"?/) {
                # obsolete Init directives to discard
                return 0;
            } elsif (/fn=\"?CM_Init\"?/) {
                # remove webpub Init directives if not installed
                return 0;
            } elsif (/fn=\"?livewireInit\"?/) {
                return 0;
            } # fn=livewireInit
#            if (!$pkgList{'search'} && (/es-search/)) {
#                # remove search Init directives if not installed
#                if (!$warned{'search'}) {
#                    print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> Search support has not been installed.\n";
#                    $warned{'search'} = 1;
#                }
#                return 0;
#            }  #es-search-init    
        } # initialized
    } # Init
    return 1;
}

sub removeObsoleteDirectives {
    my $file = shift;
    my $obsolete = shift;
    my @data = ();
    my $directive;

    open(FILE, $file) or
        die "<FONT COLOR=\"red\">[Error]</FONT> Cannot read $file: $!\n";
    while (<FILE>) {
        ($directive) = split(/\s/, $_);
        push(@data, $_) unless exists($obsolete->{"$directive"});
    }
    close(FILE);
    open(FILE, "> $file") or
        die "<FONT COLOR=\"red\">[Error]</FONT> Cannot edit $file: $!\n";
    print FILE @data;
    close(FILE);
}

#copies magnus.conf and creates server.xml from obj.conf or virtual.conf
sub copyMconfig41to60
{
        open(OLDMCONF,"<$oldHome/config/magnus.conf") or die "magnus.conf can not be copied\n";
        open(NEWMCONF,">$newHome/config/magnus.conf.tmp") or die "magnus.conf can not be created \n";
        chmod(0644,"$newHome/config/magnus.conf.tmp");
        while(<OLDMCONF>)
        {
                if($_ !~ /^\s*Port\s+/)
                {
                        if($_ =~ /^\s*SSL2\s+/)
                        {
                                chomp($_);
                                ($ignore,$SSL2)=split(/\s+/,$_);
                                $SSL2 =~ s/\s+//g;
                                next;
                        }
                        elsif($_ =~ /^\s*Address\s+/)  
                         {
                             chomp($_);
                             ($ignore,$vAddress)=split(/\s/,$_);
                             next;
                         }
                        elsif($_ =~ /^\s*LoadObjects\s+/)
                        {
                                chomp($_);
                                next;
                        }
                        elsif($_ =~ /^\s*RootObject\s+/)
                        {
                                chomp($_);
                                next;
                        }
                        elsif($_ =~ /^\s*ACLFile\s+/)
                        {
                                chomp($_);
                                next;
                        }
                        elsif($_ =~ /^\s*CGIWaitPid\s+/)
                        {
                                chomp($_);
                                next;

                        }
                        elsif($_ =~ /^\s*SSL3\s+/)
                        {
                                chomp($_);
                                ($ignore,$SSL3)=split(/\s+/,$_);
                                $SSL3 =~ s/\s+//g;
                                next;
                        }
                        elsif($_ =~ /^\s*SSLClientAuth\s+/)
                        {
                                chomp($_);
                                ($ignore,$SSLClientAuth)=split(/\s+/,$_);
                                $SSLClientAuth =~ s/\s+//g;
                                next;
                        }
                        elsif($_ =~ /^\s*CERTDefaultNickname\s+/)
                        {
                                chomp($_);
                                ($ignore,$CERTDefaultNickname)=split(/\s+/,$_);

                                $SSLClientAuth =~ s/\s+//g;
                                next;
                        }
                        elsif($_ =~ /^\s*CERTDefaultNickname\s+/)
                        {
                                chomp($_);
                                ($ignore,$CERTDefaultNickname)=split(/\s+/,$_);

                                $CERTDefaultNickname =~ s/\s+//g;
                                next;
                        }
                        elsif($_ =~ /^\s*SSL3Ciphers\s+/)
                        {
                                chomp($_);
                                ($ignore,$ssl3Ciphers)=split(/\s+/,$_);
                                $ssl3Ciphers =~ s/\s+//g;
                                next;
                        }
                        elsif($_ =~ /^\s*Ciphers\s+/)
                        {
                                chomp($_);
                                ($ignore,$ssl2Ciphers)=split(/\s+/,$_);
                                $ssl2Ciphers =~ s/\s+//g;
                                next;
                        }
                        elsif($_ =~ /^\s*Security\s+/)
                        {
                                chomp($_);
                                ($ignore,$vSecurity)=split(/\s+/,$_);
                                print NEWMCONF "$_\n";
                                next;
                }
                elsif($_ =~ /^\s*AcceptLanguage\s+/)
                      {
                       chomp($_);
                       ($ignore,$vAcceptLanguage)=split(/\s+/,$_);
                       next;
                      }
                elsif($_ =~ /^\s*VirtualServerFile\s+/)
                {
                         $mymagnus = new Magnus("$oldHome/config/magnus.conf");
                         $virtualfile = $mymagnus->value('VirtualServerFile');
                         if ($isNT) {
                            $tmpvirtualfile = substr($virtualfile, 1, length($virtualfile)-1);
                            if ($tmpvirtualfile !~ /^:/) {
                                # the filename does not have absolute path 
                             $virtualfile = "$oldHome/config/$virtualfile";
                            }
                         }
                         else {
                             if ($virtualfile !~ /^\//) {
                             # the filename does not have absolute path 
                                 $virtualfile = "$oldHome/config/$virtualfile";
                             }
                        }

                         createSdotXml_Vconf($virtualfile);
                        $vConf41="vconfigfound";
                        next;
                }

                print NEWMCONF $_;
         }
                else
                {
                        chomp($_);
                        ($ignore,$vPort)=split(/\s/,$_);
                }
        }
        close(OLDMCONF);
        close(NEWMCONF);

}

#copies obj.conf and add init directive to magnus.conf
sub copyObjconf41to60
{
        open(OLDOCONF,"<$oldHome/config/obj.conf") or die "obj.conf can not be copied\n";
        open(NEWOCONF,">$newHome/config/obj.conf.tmp") or die "obj.conf can not be created\n";
        chmod(0644, "$newHome/config/obj.conf.tmp");
        open(NEWMCONF,">>$newHome/config/magnus.conf.tmp") or die "magnus.conf can not be created successfully\n";
        chmod(0644, "$newHome/config/magnus.conf.tmp");
        print NEWMCONF "\n"."\n";
	$OsvCount=0;
	$OhvCount=0;
	$ClassCount=0;
	my $newconf = 0;
	my @data = ();
	my $whileFlag=1;
	my $trail=();
	while(<OLDOCONF>)
	{
                if($_ =~ /^\s*<Client\s+/)
		{
			$_ =~ /urlhost=[^>]+/;
			($ignore,$urlHosts[$OsvCount])=split(/=/,$&);
			$urlHosts[$OsvCount] =~ s/"//g;
			if ($urlHosts[$OsvCount] =~ /\*/) {
               			print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> Wildcards not allowed in Software Virtual Servers, removing.\n";
			}
			$urlHosts[$OsvCount++] =~ s/\*//g;
			$vConfUrl41="urlfound";
			while(<OLDOCONF>)
			{
		            if (/fn=\"home-page\"\s+(.*)/)
		       	    {
                  	        ($ignore,$urlHostsRoot[$OsvCount-1])=split(/=/,$1);
			        $urlHostsRoot[$OsvCount-1] =~ s/"//g;
                               if ( $urlHostsRoot[$OsvCount-1] =~ /\//) {
                                $whileFlag = 1;
				while($whileFlag !=0)                # to eliminate the homepage being a docroot in migrated instance
                                { 
	                            $trail = substr($urlHostsRoot[$OsvCount-1], length($urlHostsRoot[$OsvCount-1])-1, length($urlHostsRoot[$OsvCount-1]));
                                    if ( ($isNT) )  # taking care of NT slashes 
                                    {
                                        if ( ($trail ne "\\") && ($trail ne "/") )
                                        {
                                            chop($urlHostsRoot[$OsvCount-1]);
                                        }
                                        else
                                        {
                                            $whileFlag = 0;
                                            
                                        }
                                    }                                                                                   
                                    elsif ( $trail ne "/" )  # looping uptil normal slashes in UNIX
                                    { 
            	                        chop($urlHostsRoot[$OsvCount-1]);
    		                    }
 	                            else 
                                    {
 		                        $whileFlag = 0; # break the shackles
 	                            }
                                }   
                                if( length($urlHostsRoot[$OsvCount-1]) != 1)
                                {
                                    chop($urlHostsRoot[$OsvCount-1]); # cut the final slash
                    		    print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> Software Virtual Server root set to $urlHostsRoot[$OsvCount-1]\n";
                                }
                                if ($urlHostsRoot[$OsvCount-1] !~ /^\//) {
                                   #  partial path with a slash
                                   $urlHostsRoot[$OsvCount-1] = "$docRoot/$urlHostsRoot[$OsvCount-1]";
                                } 
                              } else {
                                 $urlHostsRoot[$OsvCount-1] = "$docRoot";
                               }
			    }
             		    elsif ($_ =~ /root=[^\s]+/)
			    {
			        ($ignore,$urlHostsRoot[$OsvCount-1])=split(/=/,$&);
			        $urlHostsRoot[$OsvCount-1] =~ s/"//g;
			    }
			    elsif($_ =~ /<\/Client>/)
			    {
					if ($newconf) {
					    SVSObjConf($ClassCount++, @data);
					    $newconf=0;
					    @data = ();
					}
					last;
				}
				else {
					$urlClass[$OsvCount-1] = 1;
					$newconf = 1;
					push(@data, $_);
				}
			    }
			next;
		    }
		    if( ($_ =~ /^\s*NameTrans\s+/) && ($_ !~/port[^\s]+/) && ($_ =~/address[^\s]+/) )
		    {
			if($vConf41 eq "vconfigfound")
			{
# virtual.conf file is found. only one of this set is allowed. either vconf or
# address/port in obj.conf
                            next;
                        }
			else
			{
				$vConfUrl41="urlfound";
			}
			 if($_ =~ /address=[^\s]+/)
                         {
                              ($ignore,$vlSocketAddr[$aOhvCount])=split(/=/,$&);
                              $vlSocketAddr[$aOhvCount] =~ s/"//g;
                         }
                        if($_ =~ /root=[^\s]+/)
                        {
                             ($ignore,$vhDocrootAddr[$aOhvCount])=split(/=/,$&);
                             $vhDocrootAddr[$aOhvCount] =~ s/"//g;
                        }
			 $aOhvCount++;
			 next;
		    }
                    elsif( ($_ =~ /^\s*NameTrans\s+/) && ($_ =~/port[^\s]+/) && ($_ =~ /address[^\s]+/) )
                    {
                        if($vConf41 eq "vconfigfound") {
# virtual.conf file is found. only one of this set is allowed. either vconf or
# address/port in obj.conf
                            next;
                        }
                        else {
                                $vConfUrl41="urlfound";
                        }
                    if($_ =~ /address=[^\s]+/) {    
                      ($ignore,$vlSocketPort[$bOhvCount])=split(/=/,$&);
                      $vlSocketPort[$bOhvCount] =~ s/"//g;
                    }
                    if( $_ =~/port[^\s]+/)
                    {
                       ($ignore,$vlPort[$bOhvCount])=split(/=/,$&);
                       $vlPort[$bOhvCount] =~ s/"//g;
                    }
                   if($_ =~ /root=[^\s]+/)
                   {
                     ($ignore,$vhDocrootPort[$bOhvCount])=split(/=/,$&);
                     $vhDocrootPort[$bOhvCount] =~ s/"//g;
                   }
                         $bOhvCount++;
                         next;
                   }
		elsif($_ !~ /^\s*Init\s+/) 
		{
                    if(/Service fn=\"send-cgi\"/)
		    {
	                $_="Service fn=send-cgi type=\"magnus-internal\/cgi\" user=\"\$user\" group=\"\$group\" chroot=\"\$chroot\" dir=\"\$dir\" nice=\"\$nice\"\n" ;
		    }
                    print NEWOCONF $_;
                }
		else
		{
		        if($isNT){
			   $_ =~ s/shlib=\"$oldRoot/shlib=\"$newRoot/i if /shlib=\"$oldRoot/i;
                        }
                        else{
			   $_ =~ s/shlib=\"$oldRoot/shlib=\"$newRoot/ if /shlib=\"$oldRoot/;
                        }

                            if($isNT){
			       $_ =~ s/$oldHome/$newHome/gi if /fn=\"es-search-init\"/;
			       $_ =~ s/$oldRoot/$newRoot/gi if /fn=\"es-search-init\"/;
                            }
                            else{
			       $_ =~ s/$oldHome/$newHome/g if /fn=\"es-search-init\"/;
			       $_ =~ s/$oldRoot/$newRoot/g if /fn=\"es-search-init\"/;
                            }

			if (magnusInit($_, OLDOCONF)) {
                            print NEWMCONF $_;
			}
		}
	}
	if($vConfUrl41 eq "urlfound")
	{
		createSdotXmlfile("objConf");
	}

        close(OLDOCONF);
        close(NEWOCONF);
        close(NEWMCONF);
}

#called from copyMconfig to create server.xml from virtual.conf
sub createSdotXml_Vconf
{
        my $virtualFile = shift;
        open(VCONF,"<$virtualFile") or die "$virtualFile cannot be opened\n";
        $hvCount=0;
        while(<VCONF>)
        {
                chomp($_);
                ($vlSocket[$hvCount],$vhDocroot[$hvCount])=split(/\s/,$_);
                $hvCount++;
        }
        close(VCONF);
        createSdotXmlfile("magConf");
}


sub createSdotXmlfile
{

        open(SXML,">>$newHome/config/server.xml.tmp") or die "server.xml.tmp can not be created\n";

        chmod(0644,"$newHome/config/server.xml.tmp");
       
if ($vAddress eq "")
{
    $vAddress ="0.0.0.0";
}
if($url_vconf == 0)
{
print SXML <<__UP_TO_THIS_POINT_;
<?xml version="1.0" encoding="UTF-8"?>

<!-- declare any variables to be used in the "server.xml" file in the ATTLIST below -->
<!DOCTYPE SERVER SYSTEM "server.dtd" [
 <!ATTLIST VARS
    docroot CDATA #IMPLIED
    adminusers CDATA #IMPLIED
    webapps_file CDATA #IMPLIED
    webapps_enable CDATA #IMPLIED
    accesslog CDATA #IMPLIED
    user CDATA #IMPLIED
    group CDATA #IMPLIED
    chroot CDATA #IMPLIED
    dir CDATA #IMPLIED
    nice CDATA #IMPLIED
 >
]>

<SERVER legacyls="ls0" >
   <LS id="ls0" ip="$vAddress" port="$vPort" security="$vSecurity" acceptorthreads="1" >
   <CONNECTIONGROUP id="group_ls0" matchingip="default" servername="$hostName" defaultvs="https-$hostName">
__UP_TO_THIS_POINT_
if($vSecurity eq "on")
{
    print SXML "        <SSLPARAMS servercertnickname=\"$CERTDefaultNickname\" ssl2=\"$SSL2\" ssl2ciphers=\"$ssl2Ciphers\" ssl3=\"$SSL3\" ssl3tlsciphers=\"$ssl3Ciphers\" tls=\"on\" clientauth=\"$SSLClientAuth\" tlsrollback=\"on\"/>\n";
}
print SXML <<__UP_TO_THIS_POINT_;
   </CONNECTIONGROUP>
__UP_TO_THIS_POINT_
$url_vconf=1;
}
if($_[0] eq "magConf")
{
	for($i=0,$j=1;$i<$hvCount;$i++,$j++)
	{
                print SXML "    <CONNECTIONGROUP id=\"group_ls$j\" matchingip=\"$vlSocket[$i]\" servername=\"$hostName\" defaultvs=\"vs$j\">\n";
	        if($vSecurity eq "on")
	        {
		print SXML "        <SSLPARAMS servercertnickname=\"$CERTDefaultNickname\" ssl2=\"$SSL2\" ssl2ciphers=\"$ssl2Ciphers\" ssl3=\"$SSL3\" ssl3tlsciphers=\"$ssl3Ciphers\" tls=\"on\" clientauth=\"$SSLClientAuth\" tlsrollback=\"on\"/>\n";
	        }
                print SXML "    </CONNECTIONGROUP>\n";
	}
	print SXML "   </LS>\n";
	print SXML "   <MIME id=\"mime1\" file=\"mime.types\" />\n";
	print SXML "   <ACLFILE id=\"acl1\" file=\"$rootDir/httpacl/generated.${sname}.acl\"/>\n";
	print SXML "   <VSCLASS id=\"defaultclass\" objectfile=\"obj.conf\" rootobject=\"default\" acceptlanguage=\"$vAcceptLanguage\" >\n";
	print SXML "   <VARS docroot=\"$docRoot\" user=\"\" group=\"\" chroot=\"\" dir=\"\" nice=\"\" />\n";
	print SXML "      <VS id=\"https-$hostName\" connections=\"group_ls0\" mime=\"mime1\" urlhosts=\"$hostName\" aclids=\"acl1\">\n";
	print SXML "        <USERDB id=\"default\" database=\"default\"/>\n";
	print SXML "      </VS>\n";

	for($i=1;$i<=$hvCount;$i++)
	{
		print SXML "      <VS id=\"vs$i\" connections=\"group_ls$i\" mime=\"mime1\"  urlhosts=\"$hostName\" aclids=\"acl1\">\n";
		print SXML "        <VARS docroot=\"$vhDocroot[$i-1]\"/>\n";
		print SXML "        <USERDB id=\"default\" database=\"default\"/>\n";
		print SXML "      </VS>\n";
	}
$hvsCount=$i;
$flag=1;
}
elsif($_[0] eq "objConf")
{
	if($aOhvCount!=0 )
	{
# you are inside an if which will print only <CG> for default <LS> as
# there is only address and no port.
# 4.1 obj.conf should have all "address" alone first and then the lines with
# "port" should appear for ease of migration.
			for($i=0,$j=1;$i<$aOhvCount;$i++,$j++)
			{
                                print SXML "    <CONNECTIONGROUP id=\"group_ls$j\" matchingip=\"$vlSocketAddr[$i]\" servername=\"$hostName\" defaultvs=\"vs$j\">\n";
	                        if($vSecurity eq "on")
	                        {
		                    print SXML "        <SSLPARAMS servercertnickname=\"$CERTDefaultNickname\" ssl2=\"$SSL2\" ssl2ciphers=\"$ssl2Ciphers\" ssl3=\"$SSL3\" ssl3tlsciphers=\"$ssl3Ciphers\" tls=\"on\" clientauth=\"$SSLClientAuth\" tlsrollback=\"on\"/>\n";
	                        }
                                print SXML "    </CONNECTIONGROUP>\n";
			}
	                print SXML "   </LS>\n";
                        $conGroupid = $j;
         }
         if($bOhvCount != 0)
         {
           if ($aOhvCount == 0)
           {
               print SXML "    </LS>\n";
           }
           for($i=0,$j=$conGroupid;$i<$bOhvCount;$i++,$j++)
           {
                                print SXML "    <LS id=\"ls$j\" ip=\"$vlSocketPort[$i]\" port=\"$vlPort[$i]\"  acceptorthreads=\"1\" security=\"$vSecurity\">\n";
                                print SXML "    <CONNECTIONGROUP id=\"group_ls$j\" matchingip=\"$vlSocketPort[$i]\" servername=\"$hostName\" defaultvs=\"vs$j\">\n";
                                if($vSecurity eq "on")
                                {
                                    print SXML "        <SSLPARAMS servercertnickname=\"$CERTDefaultNickname\" ssl2=\"$SSL2\" ssl2ciphers=\"$ssl2Ciphers\" ssl3=\"$SSL3\" ssl3tlsciphers=\"$ssl3Ciphers\" tls=\"on\" clientauth=\"$SSLClientAuth\" tlsrollback=\"on\"/>\n";
                                }
                                print SXML "    </CONNECTIONGROUP>\n";
                                print SXML "    </LS>\n";
            }
         }
         if ($flag == 0) {
                  if ($vConf41 eq "notfound"  && $aOhvCount == 0 && $bOhvCount == 0 ) {
# h/w virtual servers not found in virtual.conf & obj.conf.only Client tags found.
	                print SXML "   </LS>\n";
      }
	                print SXML "   <MIME id=\"mime1\" file=\"mime.types\" />\n";
			print SXML "   <ACLFILE id=\"acl1\" file=\"$rootDir/httpacl/generated.${sname}.acl\"/>\n";
	                print SXML "   <VSCLASS id=\"defaultclass\" objectfile=\"obj.conf\" rootobject=\"default\" acceptlanguage=\"$vAcceptLanguage\" >\n";
	                print SXML "   <VARS docroot=\"$docRoot\" user=\"\" group=\"\" chroot=\"\" dir=\"\" nice=\"\" />\n";
	                print SXML "      <VS id=\"https-$hostName\" connections=\"group_ls0\" mime=\"mime1\" urlhosts=\"$hostName\" aclids=\"acl1\">\n";
	                print SXML "        <USERDB id=\"default\" database=\"default\"/>\n";
		        print SXML "      </VS>\n";
         }

# Following prints H/W virtual servers from obj.conf with address alone  in
# NameTrans.

           for($i=1;$i<=$aOhvCount;$i++)
           {
                                print SXML "      <VS id=\"vs$i\" connections=\"group_ls$i\" mime=\"mime1\"  urlhosts=\"$hostName\" aclids=\"acl1\">\n";
                                print SXML "        <VARS docroot=\"$vhDocrootAddr[$i-1]\"/>\n";
                                print SXML "        <USERDB id=\"default\" database=\"default\"/>\n";
                                print SXML "      </VS>\n";
            }
                        $nextVScount=$i;

# Following prints S/W virtual servers from obj.conf with both address and
# port in NameTrans.
                        for($i=1,$j=$nextVScount;$j<$bOhvCount+$nextVScount;$i++, $j++)
                        {
                                print SXML "      <VS id=\"vs$j\" connections=\"group_ls$j\" mime=\"mime1\"  urlhosts=\"$hostName\" aclids=\"acl1\">\n";
                                print SXML "        <VARS docroot=\"$vhDocrootPort[$i-1]\"/>\n";
                                print SXML "        <USERDB id=\"default\" database=\"default\"/>\n";
                                print SXML "      </VS>\n";
                        }

                        $nextVScount=$j;
                        if ($flag == 1) {
                        $nextVScount=$hvsCount;
                        }
                         for($j=0,$i=$nextVScount;$j<$OsvCount;$i++,$j++)
                         {
                            if (!$urlClass[$j]) {
                print SXML "      <VS id=\"vs$i\" connections=\"group_ls0\" mime =\"mime1\" urlhosts=\"$urlHosts[$j]\" aclids=\"acl1\">\n";
                                if ($urlHostsRoot[$j]) {
                        print SXML "        <VARS docroot=\"$urlHostsRoot[$j]\"/>\n";
                                }
                print SXML "        <USERDB id=\"default\" database=\"default\"/>\n";
                print SXML "      </VS>\n";
                             }
                        }
            
	# Do Software Virtual Servers with their own obj.conf
	$k=0;
	for($j=0,$i=$nextVScount;$j<$OsvCount;$i++,$j++)
	{
	    if ($urlClass[$j]) {
		print SXML "   </VSCLASS>\n";
		print SXML "   <VSCLASS id=\"class$k\" objectfile=\"class$k.obj.conf\" rootobject=\"default\" acceptlanguage=\"$vAcceptLanguage\" >\n";
		if ($urlHostsRoot[$j]) {
			print SXML "   <VARS docroot=\"$urlHostsRoot[$j]\" user=\"\" group=\"\" chroot=\"\" dir=\"\" nice=\"\" />\n";
		}
		print SXML "      <VS id=\"vs$i\" connections=\"group_ls0\" mime=\"mime1\" urlhosts=\"$urlHosts[$j]\" aclids=\"acl1\">\n";
		print SXML "        <USERDB id=\"default\" database=\"default\"/>\n";
		print SXML "      </VS>\n";
		$k++;
	    }
	}
}
close(SXML);
}

# creates default server.xml

sub createDefaultSdotXml
{

        open(SXML,">$newHome/config/server.xml.tmp") or die "server.xml.tmp can not be created\n";

        chmod(0644,"$newHome/config/server.xml.tmp");

if ($vAddress eq "")
{
    $vAddress ="0.0.0.0";
}

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

<!-- declare any variables to be used in the "server.xml" file in the ATTLIST below -->
<!DOCTYPE SERVER SYSTEM "server.dtd" [
 <!ATTLIST VARS
    docroot CDATA #IMPLIED
    adminusers CDATA #IMPLIED
    webapps_file CDATA #IMPLIED
    webapps_enable CDATA #IMPLIED
    accesslog CDATA #IMPLIED
    user CDATA #IMPLIED
    group CDATA #IMPLIED
    chroot CDATA #IMPLIED
    dir CDATA #IMPLIED
    nice CDATA #IMPLIED
 >
]>

<SERVER legacyls="ls0" >
   <LS id="ls0" ip="$vAddress" port="$vPort" security="$vSecurity" acceptorthreads="1" >
     <CONNECTIONGROUP id="group_ls0" matchingip="default" servername="$hostName" defaultvs="https-$hostName" >
__UP_TO_THIS_POINT_
if($vSecurity eq "on")
{
print SXML "       <SSLPARAMS servercertnickname=\"$CERTDefaultNickname\" ssl2=\"$SSL2\" ssl2ciphers=\"$ssl2Ciphers\" ssl3=\"$SSL3\" ssl3tlsciphers=\"$ssl3Ciphers\" tls=\"on\" clientauth=\"$SSLClientAuth\" tlsrollback=\"on\"/>\n";
}
print SXML <<__UP_TO_THIS_POINT_;
     </CONNECTIONGROUP>
   </LS>
   <MIME id="mime1" file="mime.types" />
   <ACLFILE id="acl1" file="$rootDir/httpacl/generated.${sname}.acl"/>
   <VSCLASS id="defaultclass" objectfile="obj.conf" rootobject="default" acceptlanguage="$vAcceptLanguage" >
   <VARS docroot="$docRoot" user="" group="" chroot="" dir="" nice="" />
      <VS id="https-$hostName" connections="group_ls0" mime="mime1" urlhosts="$hostName"  aclids="acl1">
       <USERDB id="default" database="default"/>
      </VS>
   </VSCLASS>
</SERVER>
__UP_TO_THIS_POINT_

close(SXML);

}

sub closeSXMLtag
{
        print "Assimilating <CODE>server.xml</CODE>...\n";
        open(XML,">>$newHome/config/server.xml.tmp") or die "server.xml can not be created\n";
        print XML "   </VSCLASS>\n";
        print XML "</SERVER>\n";
        close(XML);
}


sub getESDocRoot {
    my $src = shift;
    my $address;
    my $ignore;
    my $tmp;
    my $esDocRoot; #to store the absolute docroot in the variable docroot
    my $objDocRootLine; # the line corresponsing to Name Trans document-root in obj.conf

    # get original document root
    open(SRC, $src) or die "<FONT COLOR=\"red\">[Error]</FONT> Cannot read $src:$!\n";
    while (<SRC>) {
        if (/document-root/) {
            /address=([\d\.\"]*)/;
            $address = $1 ? $1 : 'none';
            $esDocRoot = $_;
            if ($cgiVars{'docroot'} eq "new") {
                /^(.*root=)(\S*)(.*)$/;
                if($1 =~ /\"document-root\"/) {
                    $objDocRootLine = "NameTrans fn=document-root root=";
                   $_ = "$objDocRootLine\"\$docroot\"\n";
                 }
            }
            else{
            /^(.*root=)(\S*)(.*)$/;
                #$_ = "$1\"$rootDir/docs\" $3\n";
                if($1 =~ /\"document-root\"/) {
                    $objDocRootLine = "NameTrans fn=document-root root=";
                    $_ = "$objDocRootLine\"\$docroot\"\n";
                }
           }
           $roots{$address} = $_;
           }
    }
    close(SRC);
    # Find the main document root: since obj.conf is modified to contain only $docroot, the case to maintain the old docroot or new one is dealt in if-else staements
    if ($cgiVars{'docroot'} eq "new") {
        $docRoot="$rootDir/docs";
     }
    else
    {
        ($tmp = $esDocRoot) =~ /root=[^\s]+/;
        ($ignore, $docRoot) = split(/=/, $&);
        $docRoot =~ s/"//g;
    }
}


sub writeDocRoot { 
# To write docroot in the new obj.conf file;the rest of the
#operations regarding docroot are taken care in ESDocRoot
    my $dest = shift;
    my $address;
    my @data = ();

           # replace document root
    open(SRC, $dest) or die "<FONT COLOR=\"red\">[Error]</FONT> Cannot read $dest: $!\n";
    while ( <SRC> ) {
        if (/document-root/) {
            /address=([\d\.\"]*)/;
            $address = $1 ? $1 : 'none';
            push(@data, $roots{$address});
        } else {
            push(@data, $_);
        }
    }
    close(SRC);
    open(SRC, "> $dest") or
        die "<FONT COLOR=\"red\">[Error]</FONT> Cannot edit $dest: $!\n";
    print SRC @data;
    close(SRC);
}

# Create an obj.conf for a new VSCLASS. We need to merge in the default
# obj.conf with the additional directives within the Client tags in the
# migrating one.
sub SVSObjConf
{
	my ($ClassCount, @data) = @_;
	my $i = 0;
	my $addlog = 0;
	my $line = "";
	my $fname = "class$ClassCount.obj.conf";

	print "Creating class-specific obj.conf $fname...\n";
	open(CLASSOCONF,">$newHome/config/$fname") or die "$fname can not be created successfully.\n";
	
        chmod(0644,"$newHome/config/$fname");
	
print CLASSOCONF <<__UP_TO_THIS_POINT_;
<Object name=default>
NameTrans fn="ntrans-j2ee" name="j2ee"
__UP_TO_THIS_POINT_

# Do NameTrans
foreach $line (@data) {
	if ($line =~ /NameTrans/) {
		print CLASSOCONF $line;
	}
}

print CLASSOCONF <<__UP_TO_THIS_POINT_;
NameTrans fn=document-root root="\$docroot"
PathCheck fn=unix-uri-clean
PathCheck fn="check-acl" acl="default"
PathCheck fn=find-pathinfo
PathCheck fn=find-index index-names="index.html,home.html"
__UP_TO_THIS_POINT_

# Do PathCheck
foreach $line (@data) {
	if ($line =~ /PathCheck/) {
		print CLASSOCONF $line;
	}
}

print CLASSOCONF <<__UP_TO_THIS_POINT_;
ObjectType fn=type-by-extension
ObjectType fn=force-type type=text/plain
__UP_TO_THIS_POINT_

# Do ObjectType
foreach $line (@data) {
	if ($line =~ /ObjectType/) {
		print CLASSOCONF $line;
	}
}

# Do custom Services first here
foreach $line (@data) {
	if ($line =~ /Service/) {
		print CLASSOCONF $line;
	}
}

print CLASSOCONF <<__UP_TO_THIS_POINT_;
Service method=(GET|HEAD) type=magnus-internal/imagemap fn=imagemap
Service method=(GET|HEAD) type=magnus-internal/directory fn=index-common
Service method=(GET|HEAD) type=*~magnus-internal/* fn=send-file
__UP_TO_THIS_POINT_

$addlog=0;
# Do AddLog
foreach $line (@data) {
	if ($line =~ /AddLog/) {
		$addlog=1;
		print CLASSOCONF $line;
	}
}

if (!$addlog) {
print CLASSOCONF "AddLog fn=flex-log name=\"access\"\n"
}

# Do Error
foreach $line (@data) {
	if ($line =~ /Error/) {
		print CLASSOCONF $line;
	}
}

print CLASSOCONF <<__UP_TO_THIS_POINT_;
</Object>

<Object name=cgi>
ObjectType fn=force-type type=magnus-internal/cgi
Service fn=send-cgi user=\"\$user\" group=\"\$group\" chroot=\"\$chroot\" dir=\"\$dir\" nice=\"\$nice\"
</Object>

<Object name="j2ee">
Service fn="service-j2ee"
</Object>

<Object name="ServletByExt">
ObjectType fn=force-type type=magnus-internal/servlet
Service type="magnus-internal/servlet" fn="NSServletService"
</Object>

<Object name="es-internal">
PathCheck fn="check-acl" acl="es-internal"
</Object>
__UP_TO_THIS_POINT_

close(CLASSOCONF);

BinUtil::backupFile($newHome, $sname, $fname);
chmod(0644,"$newHome/conf_bk/$fname");
  if(!$isNT) {
     chown((getpwnam($cgiVars{'user'}))[2, 3],"$newHome/config/$fname" ,"$newHome/conf_bk/$fname", "$newHome/conf_bk/$fname.1");
   }
}


sub fixObjects {
    my $src = shift;
    my @data = ();
    my $gotServletDir = 0;
    my $gotShtml = 0;

    open(SRC, $src) or die "<FONT COLOR=\"red\">[Error]</FONT> Cannot read $src: $!\n";
    while (<SRC>) {
#	} elsif (/<.*Object/) {
	if (/<.*Object/) {
	    if (/name=\"?default\"?/) {
		# in default object
		push(@data, $_);
                $inNameTransStage = 0;
		$passedNameTransStage1 = 0;
		while (<SRC>) {
		    last if (/<\/Object>/);
		    # fix NameTrans directives:
		    if (/NameTrans/) {
			if (!$inNameTransStage) {
			    if (!/fn=\"?strip-params\"?/) {
				push(@data, "NameTrans fn=\"strip-params\"\n");
			    }
			    $inNameTransStage = 1;
			}
			if (!$passedNameTransStage1 && /fn=\"?pfx2dir\"?/) {
			    # just completed assigned name subsection in NameTrans
			    if (!$gotServletDir) {
				push(@data, "NameTrans fn=\"pfx2dir\" from=\"/servlet\" dir=\"$rootDir/docs/servlet\" name=\"ServletByExt\"\n");
			    }
			    push(@data, "NameTrans fn=\"pfx2dir\" from=\"/manual\" dir=\"$rootDir/manual/https\" name=\"es-internal\"\n");
			    $passedNameTransStage1 = 1;
			}
			if (/^(.*name=)\"?(servlets)\"?(.*)$/) {
				$gotServletDir = 1;
			} elsif (/fn=\"?livewireNameTrans\"?/) {
			    # remove the livewireNameTrans
			    next;
			} elsif (/fn=\"?java-find-applet\"?/) {
			    print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> Server Side Java Applets (HttpApplets) are no longer supported. Use Java Servlets and JSP instead.\n";
			    next;
			} elsif (/fn=\"?agent_name_trans\"?/) {
			    print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> Agents are no longer supported.\n";
			    next;
			} elsif (/$oldHome\/catalog/) {
			    print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> AutoCatalog is no longer supported.\n";
			    next;
			} elsif ( 
				 (/fn=\"?es-search-nametrans\"?/ || /from=\"?\/publisher\"?/ ||
				  /from=\"?\/search-ui\"?/ || /from=\"?\/webpub-ui\"?/)) {
			    next;
			} elsif (/fn=\"?pfx2dir\"?/) {
			    if (/from=\"?\/ns-icons\"?/ || /from=\"?\/ns-icons\"?/ || /from=\"?\/help\"?/) {
				chomp;
				$_ .= " name=\"es-internal\"\n";
			    }
			}
		    } elsif (/^Service/) {
			if (/fn=\"?shtml_send\"?/) {
			    $gotShtml = 1;
			} elsif (/fn=\"?rdm-service\"?/) {
			    # obsolete services to discard
			    next;
			} elsif ((/fn=\"?CM_StopRev\"?/ || /fn=\"?CM_StartRev\"?/ ||
				  /fn=\"?CM_GetPS\"?/ || /fn=\"?CM_GetAttrNames\"?/ ||
				  /fn=\"?CM_GetAttr\"?/ || /fn=\"?CM_SetAttr\"?/ || 
				  /fn=\"?CM_RevNum\"?/ || /fn=\"?CM_RevAdd\"?/ || 
				  /fn=\"?CM_RevLog\"?/ || /fn=\"?CM_RevLabel\"?/ || 
				  /fn=\"?CM_Unlock\"?/ || /fn=\"?CM_Lock\"?/ || 
				  /fn=\"?CM_Save\"?/ || /fn=\"?CM_Unedit\"?/ || 
				  /fn=\"?CM_Edit\"?/ || /fn=\"?CM_Copy\"?/ || 
				  /fn=\"?CM_Post\"?/ || /fn=\"?CM_MkDir\"?/ || 
				  /fn=\"?CM_Move\"?/ || /fn=\"?CM_Put\"?/ || 
				  /fn=\"?CM_Get\"?/ || /fn=\"?CM_Index\"?/ || 
				  /fn=\"?CM_Delete\"?/)) {
			    # remove webpub directives
			    next;
			}
		    } else {
			# miscellanous obsolete directives to discard
			if (/fn=\"?rdm-type\"?/ || /fn=\"?rdm-log\"?/) {
			    next;
			}
		    }
		    push (@data, $_);
		}
	    } elsif (/name=\"?server-applets\"?/ || /name=\"?servlets\"?/ ||
		     /name=\"?agents\"?/) {
		# old objects to throw out
		while (<SRC>) {
		    last if (/<\/Object>/);
		}
		next;
	    } elsif (/name=\"?ServerSideJS\"?/) {
# retaining tHe following code for ssjs as do not know the impact of if/else. 
# migration works fine for 4.1 with ssjs. this code was there in 4.1-6.0 also.
# since ssjs is not set it should take only the if.
		if (!$pkgList{'ssjs'}) {
		    # throw out ServerSideJS object if not installed
		    while (<SRC>) {
			last if (/<\/Object>/);
		    }
		    next;
		}
		else {
		    push (@data, $_);
		    # fix up the authentication information if present
		    while (<SRC>) {
			last if (/<\/Object>/);
			if (/AuthTrans/) {
			    s/admin-serv/https-admserv/g;
			}
			push (@data, $_);
		    }
		}
	    } elsif (
		     (/name=\"?search\"?/ || /ppath=\"?.*\/plugins\/content_mgr\/client\"?/)) {
		# throw out webpub objects if not installed
		while (<SRC>) {
		    last if (/<\/Object>/);
		}
		next;
	    }
	} elsif (/Service.*fn=\"?shtml_send\"?/) {
	    $gotShtml = 1;
	}
	push (@data, $_);
    }
    close(SRC);

    # now write it all out
    open(DEST, "> $src") or die "<FONT COLOR=\"red\">[Error]</FONT> Cannot edit $src: $!.\n";
    if ($gotShtml) {
	$initialized = 0;
	foreach $line (@data) {
	    if (!$initialized && $line =~ /Init/) {
		print DEST "Init funcs=\"shtml_init,shtml_send\" NativeThread=\"no\" ";
		print DEST "shlib=\"$libDir/$libPrefix"."Shtml$libExt\" fn=\"load-modules\"\n";
		print DEST "Init LateInit=\"yes\" fn=\"shtml_init\"\n";
		$initialized = 1;
	    }
	    print DEST $line;
	}
    } else {
	print DEST @data;
    }
    close(DEST);
#    if ($gotSSJS) {
#		if(-e , "$oldHome/config/jsa.conf")
#		{
#			BinUtil::copyFile("$oldHome/config/jsa.conf", "$newHome/config/jsa.conf");
#    		# backup jsa.conf
# 			BinUtil::backupFile($newHome, $sname, "jsa.conf");
#		}
#		else
#		{
#    		print "&nbsp;&nbsp;<FONT COLOR=\"red\">Warning:</FONT> jsa.conf file is not found in 4.x.\n";
#		}
#
#    }
}
