#!/usr/bin/perl

########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

BEGIN {
	use FindBin;
  use lib "$FindBin::Bin/subs";
  use lib "$FindBin::Bin/admin/subs";

	use lib "../subs";
	use lib "./admin/subs";
}

use File::Copy;
use File::Path;

use lib_module_common;

use strict;

my ($confixx_dir, $file);

if ( $installDir && -d $installDir ){
	$confixx_dir = $installDir;
} else {
	$0 = $^X unless ($^X =~ m%(^|[/\\])(perl)|(perl.exe)$%i);
	my ($base) = $0 =~ m%^(.*)[/\\]%;
	$base ||= ".";
	$confixx_dir = $base;
}

my ( $perms, $uid, $gid, $return, $dir, $size, $dst );
foreach $file (qw /Base64 DBI Dumper Expat Fcntl Glob IO POSIX Pg ProcessTable 
									 Quota ReadKey Socket SSLeay Storable Zlib mysql re/ ) {
  if(-f "$confixx_dir/$file.so"){
		$dst = "/tmp/$file.so";
    if (-f $dst ) {

			( $perms, $uid, $gid ) = (stat( $dst ))[2,4,5];
			$perms &= 07777;

      if( ($perms == 0400) &&
							($uid == 0) &&
							($gid == 0) ) {
				$return = system( "diff -q $confixx_dir/$file.so $dst" ); 
				&copy_file( $file ) if $return;

			} else {
        &copy_file( $file );
      }
    }
    else{
      &copy_file( $file );
    }
  }
	$dst = "/tmp/$file.bs";
  if ( -f  $dst ) {
    ($perms,$uid, $gid,$size) = (stat( $dst))[2,4,5,7];
		$perms &= 07777;
 
    unless( ( $perms == 0400) &&
						( $uid == 0) &&
						( $gid == 0) &&
						( $size == 0)
					) {
      &touch_file( $file );
    }
  }  else {
    &touch_file( $file );
  }
}

foreach $dir ("/tmp/auto", "/tmp/5.6.1", "/tmp/i686-linux") {
  if(-d $dir ) {
    rmtree( $dir );
  }  
  print "Creating directory: $dir ...\n";
  mkdir( $dir, 0000 );
  chmod( 0000, $dir );
  chown( 0, 0, $dir );
}


sub copy_file{
  my $file = shift;
	my $dst = "/tmp/$file.so";
  if( -e $dst ) {
    unlink( $dst );
  }
  print "Copying file: $confixx_dir/$file.so -> $dst ...\n";
  copy("$confixx_dir/$file.so", $dst );
  chmod( 0400, $dst );
  chown( 0, 0, $dst );
}


sub touch_file{
  my $file = shift;
	my $dst = "/tmp/$file.bs";
  if( -e $dst ) {
    unlink( $dst );
  }
  print "Creating file: $dst ...\n";
  if ( open( FILE, '>', $dst ) ) {
		close( FILE );
	}
  
  chmod( 0400, $dst );
  chown( 0, 0, $dst );
}
