#!./perl -w

# Install LivePayment
# Copyright (c) 1996, Netscape Communications Corp
# Author: Fred Cox
# $Id: lp-setup,v 1.43 1996/09/25 18:09:26 aprateek Exp $


$| = 1;

$login = getpwuid($<);

$random = getppid();

$product = "Netscape LivePayment";
$prod = "livepayment";

# Uninstall 
if ( $#ARGV == 0 ) {
	$arg = shift (@ARGV);
	if ( $arg eq "-d" ) {
		print "\nAre you sure you want to remove LivePayment ? [no] ";
		chomp( $result = <STDIN> );
		if ( $result !~ /^\s*yes\s*$/i ) {
			exit(0);
		}
		if ( $login ne "root" ) {
			print "\nYou have to be root to uninstall LivePayment\nsu to root and then uninstall\n";
			exit(1);
		}

		print "\n";
		$serverRoot = &promptDir( '/usr/ns-home', 0,
					'Where is the Netsite server root?' )
		unless defined( $serverRoot ) && -d $serverRoot;
		
		$port = &getServerInfo( $serverRoot );
		
		&closeServers($serverRoot, $port);

		print "Uninstalling LivePayment ....";

		system("rm -rf $serverRoot/admserv/livepayment-$port");
		print "...";
		system("rm -rf $serverRoot/livepayment-$port");
		print "...";
		system("rm -rf $serverRoot/bin/livepayment");
		print "...";
		system("rm -rf $serverRoot/https-$port/system.ini");
		print ".";
		system("cp $serverRoot/https-$port/config/livewire.conf /tmp/lp-setup.$random");
		print ".";
		system("grep -v LP /tmp/lp-setup.$random > $serverRoot/https-$port/config/livewire.conf");
		print ".";
		system("cp $serverRoot/admserv/servers.lst /tmp/lp-setup.$random");
		print ".";
		system("grep -v livepayment /tmp/lp-setup.$random > $serverRoot/admserv/servers.lst");
		print ".";
		print "...\n";

	} elsif ( $arg eq "-v")  {
		print "Netscape LivePayment 1.0.0 for Sun Solaris 2.4 and 2.5\n";
	} else  {
		&defaultMessage();
	}

	exit(0);
} elsif ( $#ARGV > 0 ) {
	&defaultMessage();
	exit(0);
} else {
	if ( $login ne "root" ) {
		print "\nYou have to be root to install LivePayment\nsu to root and then begin installation\n\n";
		exit(1);
	}

	umask( 022 );

	$sharedObj = "libccp.so";
	$sharedObjVer = "libccp.so.2";
	
	&verifyLicense( );
	
	print "\n";
	
	$serverRoot = &promptDir( '/usr/ns-home', 0,
			  	'Where is the Netsite server root?' )
    	unless defined( $serverRoot ) && -d $serverRoot;
	
	$port = &getServerInfo( $serverRoot );
	
	#check the uid of the user
	($uid, $gid) = &getId( "$serverRoot/admserv/https-$port/magnus.conf" );
	if ( $uid < 0 ) {
		print "Please, configure the server to run as a user \n   that has a valid user ID \n    before proceeding to install LivePayment\n\n";
		print "Installation Aborted !!\n";
		exit(0);
	}

	&closeServers($serverRoot, $port);
	
	&checkServerVersion( $serverRoot );
	&checkLiveWire( $serverRoot, $port );

	($uid, $gid) = &getId( "$serverRoot/admserv/ns-admin.conf" );
	$auid = $uid;
	$agid = $gid;

	$crRoot = "$serverRoot/$prod-$port";
	unless ( -d $crRoot ) {
		mkdir( $crRoot, 0755 ) || warn "Couldn't create $crRoot: $!\n";
	}
	chown( $uid, $gid , $crRoot ) || warn "\n **** Unable to change owner-ship: \n ****Probably installing on a mounted file system \n\n";
	
	$lpBin = "$serverRoot/bin/$prod";
	unless ( -d $lpBin ) {
		mkdir( $lpBin, 0755 ) || warn "Couldn't create $lpBin: $!\n";
	}
	chown($uid, $gid, $lpBin );
	
	print "Untarring the $product distribution\n\n";
	print "This may take several minutes.....\n";
	chomp( $srcDir = `pwd` );
	
	chdir( $crRoot ) || die "Can't cd to $crRoot: $!\n";
	system( 'tar', 'xf', "$srcDir/lp.tar" ) && die "Problem extracting tar file\n";
	system( "chmod -R 0755 $crRoot" ); 
	system( "chown -R $uid $crRoot 2> /dev/null" ); 
	system( "chgrp -R $gid $crRoot" );
	
	&moveTree( "$crRoot", 'admin', "$lpBin" );
	
	&hookToAdmin( "$lpBin", "$serverRoot/admserv" ); 
	
	&moveTree( 'bin', "$sharedObjVer", "$serverRoot/bin/https" );
	
	&addLink( "$serverRoot/bin/https/$sharedObjVer", 
	  	"$serverRoot/bin/https/$sharedObj" );
	
	chdir( $srcDir ) || die "Can't cd back $srcDir: $!\n";
	
	&xlatePaths( "$crRoot/samples", 1, 'LiveWireLibraryPath',
	     	"$serverRoot/bin/https/$sharedObj" );
	
	&addLWconf( "$serverRoot/https-$port/config/livewire.conf" );
	
	&xlatePaths( "$crRoot/config/system.ini", 1, 'LivePaymentInstallPath', 
             	$crRoot );
	
	&addLink( "$crRoot/config", "$lpBin/admin/config" );
	
	&addLink( "$crRoot/config/system.ini",
	  	"$serverRoot/https-$port/config/system.ini" );
	
	&addLink( "$serverRoot/bin/https/ns-httpd.so", "$lpBin/ns-httpd.so" );
	
	&addLink( "$serverRoot/bin/https/admin/bin/sec-key", 
	  	"$lpBin/admin/bin/sec-key");
	
	&addLink( "$serverRoot/bin/https/admin/bin/tutor", 
	  	"$lpBin/admin/bin/tutor");
	
	&addLink( "$serverRoot/bin/https/admin/icons/content1.gif", 
	  	"$lpBin/admin/icons/content1.gif");
	
	&addLink( "$serverRoot/bin/https/admin/icons/index1.gif", 
	  	"$lpBin/admin/icons/index1.gif");
	
	&addLink( "$serverRoot/bin/https/admin/icons/forward1.gif", 
	  	"$lpBin/admin/icons/forward1.gif");
	
	&addLink( "$serverRoot/bin/https/admin/icons/back1.gif", 
	  	"$lpBin/admin/icons/back1.gif");
	
	&addLink( "$serverRoot/bin/https/admin/icons/exit1.gif", 
	  	"$lpBin/admin/icons/exit1.gif");

	#move the docs

	&copyTree( "$srcDir", 'docs', "$crRoot" );
	system("cp $srcDir/README $crRoot");
	chown($uid, $gid, "$crRoot/README");
	
	#make links for LPAdmin
	&addLink(  "$crRoot/samples/LPStart/merchant_lib" ,
		 	"$crRoot/samples/LPAdmin/merchant_lib" );
	
	&addLink(  "$crRoot/samples/LPStart/readonly_lib" ,
		 	"$crRoot/samples/LPAdmin/readonly_lib" );
	
	&addLink(  "$crRoot/samples/LPStart/ifx" ,
		 	"$crRoot/samples/LPAdmin/ifx" );
	
	&addLink(  "$crRoot/samples/LPStart/images" ,
		 	"$crRoot/samples/LPAdmin/images" );
	
	&addLink(  "$crRoot/samples/LPStart/ora" ,
		 	"$crRoot/samples/LPAdmin/ora" );
	
	#change permissions to magnus uid and gid for specific files
	($uid, $gid) = &getId( "$serverRoot/admserv/https-$port/magnus.conf" );
	
	
	chmod(0755,"$crRoot/log");
	chown($uid, $gid, "$crRoot/log");
	chmod( 0700, "$crRoot/config");
	chown($uid, $gid, "$crRoot/config");
	chmod( 0755, "$crRoot/bin");
	chown($uid, $gid, "$crRoot/bin/ccpd");
	chown($uid, $gid, "$crRoot/bin/cpcmd");
	system("chmod 6555 $crRoot/bin/ccpd");
	system("chmod 6555 $crRoot/bin/cpcmd");
	system("chmod -R 444 $crRoot/samples/LPStart/readonly_lib 2> /dev/null");
	system("chmod 755 $crRoot/samples/LPStart/readonly_lib 2> /dev/null");
	
	$ENV{'PATH'} = "$ENV{'PATH'}:$lwPath";
	&buildWeb();
	chown($auid, $agid, "$crRoot/samples/LPStart/LPStart.web");
	chown($auid, $agid, "$crRoot/samples/LPStart/LPAdmin.web");
	chown($auid, $agid, "$crRoot/samples/LPStart/LPAuthOnly.web");

	
	&moreFile( 'loopback.txt' );
} ### end of main if statement
	
#############
# Subroutines
#############

sub buildWeb {
    @cr=("LPAuthOnly", "LPStart", "LPAdmin" );
    foreach ( @cr ) {
        print "Compiling sample application $_\n";
        chdir( "$crRoot/samples/$_" ) || 
            die "Can't cd to $crRoot/samples/$_: $!\n";
        `./build`;
        print "\n";
    }
    chdir( $srcDir ) || die "Can't cd back $srcDir: $!\n";
}

sub promptDir {
    my	$default = shift;
    my	$mkdir = shift;
    my	$prompt = shift;
    my	$result;

    do {
	print "$prompt [$default] ";
	chomp( $result = <STDIN> );
	$result = $default if $result =~ /^\s*$/;
	unless ( -d $result ) {
	    if ( $mkdir ) {
		mkdir( $result, 0755 ) || warn "Couldn't create $result: $!\n";
	    } else {
		warn "Directory $result does not exist\n";
	    }
	}
    } until -d $result;
    return $result;
}

sub moreFile {
    my	$file = shift;
    my	$throwaway;

    open( FILE, $file ) ||
	die "Can't find the license file, $file: $!\n";
    while ( <FILE> ) {
	unless ( $. % 23 ) {
	    print "Press <return> for next page";
	    $throwaway = <STDIN>;
	}
	print;
    }
    close( FILE );
}

sub verifyLicense {
    system("clear");
    print "\n";
    print "                Netscape Communications Corporation\n"; 
    print "                 Netscape LivePayment Installation\n"; 
    print "------------------------------------------------------------------\n\n\n"; 
    print "  BY INSTALLING THIS SOFTWARE YOU ARE CONSENTING TO BE BOUND BY\n";
    print "  AND ARE BECOMING A PARTY TO THE AGREEMENT FOUND IN THE\n";
    print "  LICENSE.TXT FILE. IF YOU DO NOT AGREE TO ALL OF THE TERMS\n";
    print "  OF THIS AGREEMENT, PLEASE DO NOT INSTALL OR USE THIS SOFTWARE.\n\n";
    print "Do you agree to abide with this license agreement (Type \"yes\" to continue)? ";
    $throwaway = <STDIN>;
    die "You did not type YES, please delete this distribution.\n"
	unless $throwaway =~ /^\s*yes\s*$/i;
}

# Copy the tree over, using tar, then remove it
sub moveTree {
    my	$srcDir = shift;
    my	$src = shift;
    my	$destDir = shift;

    system( "cd $srcDir ; tar cf - $src | ( cd $destDir ; tar xopBf - )" ) &&
	die "Couldn't tar $srcDir/$src to $destDir: $!\n";
	system( "chmod -R 0755 $destDir" ); 
	system( "chown -R $uid $destDir 2> /dev/null" ); 
	system( "chgrp -R $gid $destDir" );
        system( "rm -rf $srcDir/$src" ) && warn "Couldn't delete $srcDir/dir after copying: $!\n";
}

# Copy a file over using tar
sub copyFile {
    my	$src = shift;
    my	$destDir = shift;

    system( "tar cf - $src | ( cd $destDir ; tar xopBf - )" ) &&
	die "Couldn't tar $src to $destDir: $!\n";
}

#recursively translate paths from old to new, starting at $what
sub xlatePaths {
    my	$what = shift;
    my	$numXlations = shift;
    my	@oldPaths = splice( @_, 0, $numXlations );
    my	@newPaths = splice( @_, 0, $numXlations );
    my	@data = ();
    my	@files;
    my	$file;
    my	$i;

    if ( -l $what ) {	# Not doing anything for these, now QQQQ
	# warn "Found a symbolic link, which which I am doing nothing\n";
    } elsif ( -d $what ) {
	opendir( DIR, $what ) || die "Can't read directory $what: $!\n";
	@files = readdir( DIR );
	closedir( DIR );
	foreach $file ( @files ) {
	    next if $file =~ /^\.\.?$/;	# skip . and ..
	    &xlatePaths( "$what/$file", $numXlations, @oldPaths, @newPaths );
	}
    } elsif ( -f $what ) {
	my	$permissions;
	my	$junk;

	unless ( -w _ ) {
	    ( $junk, $junk, $permissions ) = stat( _ );
	    chmod( 0755, $what );
	}
	open( WHAT, "+<$what" ) || die "Can't open $what\n";
	while ( <WHAT> ) {
	    for ( $i = 0 ; $i < $numXlations ; ++$i ) {
		s/$oldPaths[$i]/$newPaths[$i]/g;
	    }
	    push( @data, $_ );
	}
	seek( WHAT, 0, 0 );
	truncate( WHAT, 0 );
	print WHAT @data;
	close( WHAT ) || die "Can't write to $what: $!\n";
	chmod( $permissions, $what ) if defined( $permissions );
    }
}

sub checkServerVersion {
    my	$serverRoot = shift;
    my  $httpDir;
    my  $curDir;

    chomp( $curDir = `pwd` );

    $httpDir="$serverRoot/bin/https";
    chdir ( $httpDir ) || die "Can't cd to $httpDir: $!\n";

    `./ns-httpd -v | grep "Netscape-Enterprise/2.0a"` || 
	die "Failed to check server version\n";
 
    if ( $? ) {
       die "Incorrect server version, please install Enterprise server version 2.0 before installing $product\n";
    }

    chdir( $curDir ) || die "Can't cd back $curDir: $!\n";
}

sub checkLiveWire {
    my	$serverRoot = shift;
    my	$port = shift;

    unless ( -e "$serverRoot/https-$port/config/livewire.conf" ) {
	die "LiveWire has not been activated, please activate LiveWire before installing $product\n";
    }
    if ( -e "$serverRoot/bin/https/lwcomp" ) {
        $lwPath = "$serverRoot/bin/https";
    } elsif ( -e "$serverRoot/LiveWire/bin/lwcomp" ) {
        $lwPath = "$serverRoot/LiveWire/bin"; 
    } elsif ( -e "$serverRoot/livewire/bin/lwcomp" ) {
        $lwPath = "$serverRoot/livewire/bin"; 
    } else {
	die "The LiveWire compiler does not exist, please install the LiveWire compiler before installing $product\n";
    }
}

sub getServerInfo {
    my	$serverRoot = shift;
    my	$adminPort;
    my	$host;
    my	@ports;
    my	$port;
    my	$user;

    open( CONF, "$serverRoot/admserv/ns-admin.conf" ) ||
	die "Can't find admin server config info for $serverRoot\n";
    while ( <CONF> ) {
	chomp( $_ );
	if ( /^\s*Port\s+/i ) {	# found the admin server port
	    $adminPort = $';
	} elsif ( /^\s*ServerName\s+/i ) {
	    $host = $';
	} elsif ( /^\s*User\s+/i ) {
	    $user = $';
	}
    }
    close( CONF );
    opendir( SERVER, $serverRoot ) || die "Can't read $serverRoot: $!\n";
    while ( $_ = readdir( SERVER ) ) {
	if ( /^https-/ ) {	# found a server
	    push( @ports, $' );
	}
    }
    if ( scalar( @ports ) == 1 ) {	# ideal situation
	$port = $ports[0];
    } elsif ( ! scalar( @ports ) ) {
	die "There don't seem to be any Enterprise Servers installed in $serverRoot\n";
    } else {	# too many servers, gotta choose one
	do {
	    print "You have installed these servers: @ports, which do you want ?";
	    chomp( $port = <STDIN> );
	} until -d "$serverRoot/https-$port";
    }
    return  $port;
}

sub addLWconf {
    my	$file = shift;
    my	$foundIt1 = 0;
    my	$foundIt2 = 0;
    my	$foundIt3 = 0;
    my  $app1 = "LPAuthOnly";
    my  $app2 = "LPStart";
    my  $app3 = "LPAdmin";

    open( FILE, $file ) || die "Can't read $file: $!\n";
    while ( <FILE> ) {
        if ( /^LPAuthOnly\b/ ) {
           $foundIt1 = 1;
	}
        if ( /^LPStart\b/ ) {
           $foundIt2 = 1;
	}
        if ( /^LPAdmin\b/ ) {
           $foundIt3 = 1;
	}
    }
    close( FILE );
    unless ( $foundIt1 ) {
	open( FILE, ">>$file" ) || die "Can't append to $file: $!\n";
	print FILE "$app1 uri=/$app1 object=$crRoot/samples/$app1/$app1.web start=start.html home=home.html client-mode=client-cookie library=$serverRoot/bin/https/$sharedObj maxdbconnect=0\n";
	close( FILE );
    }
    unless ( $foundIt2 ) {
	open( FILE, ">>$file" ) || die "Can't append to $file: $!\n";
	print FILE "$app2 uri=/$app2 object=$crRoot/samples/$app2/$app2.web start=start.html home=home.html client-mode=client-cookie library=$serverRoot/bin/https/$sharedObj maxdbconnect=1\n";
	close( FILE );
    }
    unless ( $foundIt3 ) {
	open( FILE, ">>$file" ) || die "Can't append to $file: $!\n";
	print FILE "$app3 uri=/$app3 object=$crRoot/samples/$app3/$app3.web start=start.html home=home.html client-mode=client-cookie library=$serverRoot/bin/https/$sharedObj maxdbconnect=1\n";
	close( FILE );
    }
}

sub hookToAdmin
{
    my	$crBin = shift;
    my	$admserv = shift;
    my  $servFile = "$admserv/servers.lst";
    my  $admLP = "$admserv/$prod-$port";
    my  $magnusLP = "$admLP/magnus.conf";
    my  $magnusHT = "$admserv/https-$port/magnus.conf";
    my  $found = 0;

    unless ( -d $admLP ) {
	mkdir( $admLP, 0755 ) || warn "Couldn't create $admLP: $!\n";
    }
    chown($uid, $gid, $admLP );

    #
    # get ServerName
    #
    open (FILE, $magnusHT) || die "Can't read $magnusHT: $!\n";
    while ( <FILE> ) 
    {
       if ( $found == 0 )
       {
           if ( /^\s*ServerName\s*(.*)/ ) 
           {
               $sername = $1;
               $found = 1;
           }
       }
    }
    close (FILE);

    #
    # create $serverRoot/admserv/$prod-$port/magnus.conf
    #
    if ( -e "$magnusLP" )
    {
        unlink( "$magnusLP" );
    }
    open( FILE, ">$magnusLP" ) || die "Can't create $magnusLP: $!\n";
    chown($uid, $gid, $magnusLP );
    print FILE "#ServerRoot $crRoot\n";
    print FILE "#HttpsRoot $serverRoot/https-$port\n";
    print FILE "ServerName $sername\n";
    close (FILE);

    open( FILE, $servFile ) || die "Can't read $servFile: $!\n";
    while ( <FILE> ) {
        if ( /^livepayment\b/ ) {
           $foundIt = 1;
	}
    }
    close( FILE );
    unless ( $foundIt ) {
	open( FILE, ">>$servFile" ) || die "Can't append to $servFile: $!\n";
	print FILE "$prod: $product\n";
	close( FILE );
    }
}

sub addLink
{
    my	$src = shift;
    my	$dst = shift;

    if ( -l "$dst" ) {
      unlink( "$dst" );
    }

    symlink( "$src", "$dst" ) ||
        die "Couldn't make symbolic link $dst -> $src: $!\n";
    system( "chmod -R 0755 $dst" ) && warn "Unable to change owner of $dst : $!\n"; 
    chown( $uid, $gid , $dst );
}

sub closeServers
{ 
    my $serverRoot = shift;
    my $port = shift;

    if ( -e "$serverRoot/admserv/pid" ) {
	ATTEMPT:{
		print "\nWARNING: The administration server and the HTTP server are running!\n";
		print "\nStop them now? [y/n]: ";
		chomp( $result = <STDIN> );
		if ( $result =~/[Yy].*/ ) {
		 if ( -e "$serverRoot/admserv/pid" ) {
		   system("kill `cat $serverRoot/admserv/pid`") && warn "Unable to stop the servers.\n";
                  }
		   if ( -e "$serverRoot/https-$port/logs/pid" ) {
		      system("kill `cat $serverRoot/https-$port/logs/pid`") && warn "Unable to stop the servers.\n";
		   } 
		   if ( -e "$serverRoot/livepayment-$port/bin/ccpd.pid" ) {
		      system("kill `cat $serverRoot/livepayemnt-$port/bin/ccpd.pid`") && warn "Unable to stop the card processor.\n";
		   } 
		   
		} else {
		  print "\nExit now? [y/n]: ";
		  chomp( $result = <STDIN> );
		  if ( "$result" =~/[Yy].*/ ) {
		     exit(1);
		  } 
		  print "\nDo you want to proceed without stopping the servers? [y/n]: ";
		  chomp( $result = <STDIN> );
		  if ( $result =~/[yY].*/ ) {
		     return;
                  } else {
		      redo ATTEMPT;
		  }
               }
	}
   }
}



sub getId
{
	my  $confFile = shift;
	my $found = 0;
	#
	# get ServerName
	#
	open (FILE, $confFile) || die "Can't read $confFile: $!\n";
	while ( <FILE> ) 
	{
	    if ( $found == 0 )
	    {
		if ( /^\s*User\s*(.*)/ ) 
		{
		    $owner = $1;
		    $found = 1;
	       }
	   }
	}
	@info = getpwnam($owner);
	return ($info[2], $info[3]);
}

sub defaultMessage
{
	print " Usage:\n";
	print "   lp-setup       /to install\n";
	print "   lp-setup -d    /to uninstall\n";
	print "   lp-setup -h    /for help message\n";
	print "   lp-setup -v    /for version info\n";
}



sub applyConfStyle
{
	my $root = shift;
	my $port = shift;

	$obj_conf = "$root/https-$port/config/obj.conf";
	$bckup_dir =  "$root/admserv/https-$port";
	$admin_conf = "$bckup_dir/admin.conf";
	$nsadmin_conf = "$root/admserv/ns-admin.conf";

	open( FILE, $nsadmin_conf) ||
		die "Can't find the ,$file, unable to change the style: $!\n";
	while ( <FILE> ) {
		chomp( $_ );
		if ( /^\s*Backups\s+/i ) {	#found the Backups entry
	    		$max = $';
		}
	}
	close( FILE );

	$found = "false";
	open( FILE, $admin_conf ) ||
		die "Can't find the ,$file, unable to change the style: $!\n";
	$i =0;
	while ( <FILE> ) {
		$string[$i] = $_;
		$i = $i+1;
	}
	close (FILE);

	if ( $i != 6 ) {
		print "Unable to change config style\n";
		return;
	}

	@maxnums = split(/:/,$string[4],4);
	$num1 = $maxnums[1];
	$num2 = $maxnums[2];
	$num1 = int($num1) +1;
	$num2 = int($num2) +1;

	if ( $num1 != $num2 ){
		print "Unable to change the config type\n";
		return;
	}

	open( FILE, "$bckup_dir/obj.conf" ) ||
		die "Can't find the ,$file, unable to change the style: $!\n";
	$found = "false";
	$found1 = "false";
	while ( <FILE> ) {
		chomp( $_ );
		if ( /path=/ ) {	#found the Backups entry
			@path_entry = split(/\*/,$');
			foreach ( @path_entry ) {
				if ( /LPAdmin/ ) {
					$found = "true";
				} elsif ( /appmgr/ || /dbadmin/ ) {
					$found1 = "true";
				} 
			}
		}
	}
	close( FILE );

	if ( $found1 eq "true" ) {
	  print "Access to LPAdmin will require administration server password\n";
	  open( FILE, "$bckup_dir/obj.conf" ) ||
		die "Can't find the ,$bckup_dir/obj.conf, unable to change the style: $!\n";
	  unlink $obj_conf;
	  open( OBJ, ">$obj_conf" ) ||
		die "Can't find the ,$obj_conf, unable to change the style: $!\n";
	  $foundit = "false";
	  while ( <FILE> ) {
		select( OBJ);
		$string = $_;
		if ( $string =~ /.*Object name.*LiveWire.*/ ){
			$foundit = "true";
		}
		if ( $string =~ /.*LiveWire Administration.*/ && $foundit eq "true" ){
	  		$string ="PathCheck realm=\"LiveWire Administration\" fn=\"require-auth\" path=\"(*LPAdmin*|*appmgr*|*dbadmin*)\" auth-type=\"basic\"";
			print "$string\n";
		} else {
			print "$_";
		}
	  }
	  close( FILE);
	  close( OBJ);

	} else {
		print "Access to LPAdmin, dbadmin, and appmgr will now require administration \nserver password\n";

	open( FILE, "$bckup_dir/obj.conf" ) ||
		die "Can't find the ,$bckup_dir/obj.conf, unable to change the style: $!\n";
	unlink $obj_conf;
	open( OBJ, ">$obj_conf" ) ||
		die "Can't find the ,$obj_conf, unable to change the style: $!\n";
	while ( <FILE> ) {
		select( OBJ);
		$string = $_;
		print "$_";
		if ( $string =~ /.*Object name.*LiveWire.*/ ){
	  		$string ="AuthTrans fn=\"basic-auth\" userdb=\"$root/admserv/admpw\" userfn=\"simple-userdb\" auth-type=\"basic\"";
			print "$string\n";
	  		$string ="PathCheck realm=\"LiveWire Administration\" fn=\"require-auth\" path=\"(*LPAdmin*|*appmgr*|*dbadmin*)\" auth-type=\"basic\"";
			print "$string\n";
		}
	}
	close( FILE);
	close( OBJ);
      }
      select(STDOUT);

	system("cp $obj_conf $bckup_dir/obj.conf.v$num1");
	system("cp $bckup_dir/obj.conf.v$num1 $bckup_dir/obj.conf");
	system("cp $bckup_dir/magnus.conf $bckup_dir/magnus.conf.v$num1");

	if ( num1 ge $max ) {
		$min = int($num1) - int($max);
		unlink "$bckup_dir/magnus.conf.v$min";
		unlink "$bckup_dir/obj.conf.v$min";
	}

	system("cp $obj_conf $bckup_dir/obj.conf");

	system("sed s/'^.\:.*'/0:$num1:$num2/g $admin_conf > /tmp/admin.$random");
	system("cp /tmp/admin.$random $admin_conf");

	#modfiy the adm.conf so that server can't find out that the file
	# has been modified
	$time =  (stat($obj_conf))[9];
		
	  open( FILE, "$admin_conf" ) ||
		die "Can't find the ,$admin.conf, unable to change the style: $!\n";
	  $i =0;
	  while ( <FILE> ) {
		$string[$i] = $_;
		$i = $i+1;
	  }

	  close( FILE);

	  @mtimes = split(/:/,$string[5],5);
	  $mtimes[1]=$time;
	  $string[5]=join(':',@mtimes);

	  unlink $admin_conf;
	  open( FILE, ">$admin_conf" ) ||
		die "Can't find the ,$admin.conf, unable to change the style: $!\n";
	  select( FILE);
	  foreach ( @string ) {
		print $_;
	  }
	  close(FILE);
	  select(STDOUT);
}

# Copy the tree over, using tar,
sub copyTree {
    my	$srcDir = shift;
    my	$src = shift;
    my	$destDir = shift;

    system( "cd $srcDir ; tar cf - $src | ( cd $destDir ; tar xopBf - )" ) &&
	die "Couldn't tar $srcDir/$src to $destDir: $!\n";
	system( "chmod -R 0755 $destDir" ); 
	system( "chown -R $uid $destDir 2> /dev/null" ); 
	system( "chgrp -R $gid $destDir" );
}

