#!/usr/bin/perl
use Env qw(PATH QTDIR LD_LIBRARY_PATH);

use strict;
use warnings;

my $ct=10;

system('clear');

open(H, '>', 'qt.bm')  or die "Can't open/create 'qt.bm' file !";;
print H "===============================================\n";
print H "================== QT BM ======================\n";
print H "===============================================\n\n";
print H "Some system info.\n";

print H "\$ uname -a\n";
print H `uname -a`;
print H "===============================================\n\n";

print H "\$ cat /proc/cpuinfo\n";
print H `cat /proc/cpuinfo`;
print H "===============================================\n\n";

print H "\$ cat /proc/meminfo\n";
print H `cat /proc/meminfo`;
print H "===============================================\n\n";

print H "\$ lspci -v\n";
print H `lspci -v`;
print H "===============================================\n\n";
close H;



open my $oldout, ">&STDOUT"     or die "Can't dup STDOUT: $!";
open my $olderr, ">&STDERR"     or die "Can't dup STDERR: $!";
open STDOUT, '>>', "qt.bm" or die "Can't redirect STDOUT: $!";
open STDERR, ">&STDOUT"     or die "Can't dup STDOUT: $!";
select STDERR; $| = 1;      # make unbuffered
select STDOUT; $| = 1;      # make unbuffered

print STDOUT "\n\n\$ gcc -v\n";
system('gcc -v');

open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!";
open STDERR, ">&", $olderr or die "Can't dup \$odlerr: $!";
open(H, '>>', 'qt.bm')  or die "Can't open/create 'qt.bm' file !";;
print H "===============================================\n\n";

print H "\$ ps ax\n";
print H `ps ax`;
print H "===============================================\n";
print H "===============================================\n";


print "Please wait...\nsvn co svn://anonsvn.kde.org/home/kde/trunk/qt-copy\n";
system('svn co svn://anonsvn.kde.org/home/kde/trunk/qt-copy');
unlink('qt++.patch.bz2');
system('wget http://omnidatagrup.ro/~taipan/qt++.patch.bz2');

my ($pwd)=split(/\n/,`pwd`);
my @times;

for (my $k=0;$k<$ct;$k++)
{
    print "rm -fr qt-exceptions\n";
    system 'rm -fr qt-exceptions';

    print "cp -a qt-copy qt-exceptions\n";
    system 'cp -a qt-copy qt-exceptions';

    chdir('qt-exceptions');
    system('bzcat ../qt++.patch.bz2 | patch -p1');
    system("./configure -qt-gif -exceptions -debug -fast --prefix=$pwd/qt-unstable-exceptions -confirm-license");
    my $t=time;
    system('make -j2'); # I use -j2 because I have 2 cpus
    $t=time-$t;
    print H "Qt with exceptions:compiling $k took $t seconds\n";
    push @times,$t;
    chdir($pwd);
}
my $tt;

$tt+=($_) foreach (@times);
$tt/=$ct;

print H "---------------------------------------------------------\n";
print H "Qt with exceptions:compiling average is $tt seconds\n";
print H "---------------------------------------------------------\n";

mkdir('qte-bin');
system('cp -a qt-exceptions/bin/* qte-bin');
chdir("$pwd/qte-bin");
my ($qtebinsize)=split(/\t/,`du -sb`);
print H "Qt with exceptions: size of bin (debug) $qtebinsize\n";
system('strip -s *');
my ($qtesbinsize)=split(/\t/,`du -sb`);
print H "Qt with exceptions: size of bin (strip) $qtesbinsize\n";
chdir($pwd);
system('rm -fr qte-bin');

mkdir('qte-lib');
system('cp -a qt-exceptions/lib/*.4.1.* qte-lib');
chdir("$pwd/qte-lib");
my ($qtelibsize)=split(/\t/,`du -sb`);
print H "Qt with exceptions: size of lib (debug) $qtelibsize\n";
system('strip -s *');
my ($qteslibsize)=split(/\t/,`du -sb`);
print H "Qt with exceptions: size of lib (strip) $qteslibsize\n";
chdir($pwd);
system('rm -fr qte-lib');


####################################################################
## Make some pertinent tests
####################################################################

my @timesq;

for (my $k=0;$k<$ct;$k++)
{
    print "rm -fr qt-no-exceptions\n";
    system 'rm -fr qt-no-exceptions';

    print "cp -a qt-copy qt-no-exceptions\n";
    system 'cp -a qt-copy qt-no-exceptions';

    chdir('qt-no-exceptions');
    system("./configure -qt-gif -no-exceptions -debug -fast --prefix=$pwd/qt-unstable-no-exceptions -confirm-license");
    my $t=time;
    system('make -j2'); # I use -j2 because I have 2 cpus
    $t=time-$t;
    print H "Qt with no-exceptions:compiling $k took $t seconds\n";
    push @timesq,$t;
    chdir($pwd);
}
my $ttq;

$ttq+=($_) foreach (@timesq);
$ttq/=$ct;

print H "---------------------------------------------------------\n";
print H "Qt with no-exceptions:compiling average is $ttq seconds\n";
print H "---------------------------------------------------------\n";

mkdir('qtne-bin');
system('cp -a qt-no-exceptions/bin/* qtne-bin');
chdir("$pwd/qtne-bin");
my ($qtnebinsize)=split(/\t/,`du -sb`);
print H "Qt with no-exceptions: size of bin (debug) $qtnebinsize\n";
system('strip -s *');
my ($qtnesbinsize)=split(/\t/,`du -sb`);
print H "Qt with no-exceptions: size of bin (strip) $qtnesbinsize\n";
chdir($pwd);
system('rm -fr qtne-bin');

mkdir('qtne-lib');
system('cp -a qt-no-exceptions/lib/*.4.1.* qtne-lib');
chdir("$pwd/qtne-lib");
my ($qtnelibsize)=split(/\t/,`du -sb`);
print H "Qt with no-exceptions: size of lib (debug) $qtnelibsize\n";
system('strip -s *');
my ($qtneslibsize)=split(/\t/,`du -sb`);
print H "Qt with no-exceptions: size of lib (strip) $qtneslibsize\n";
chdir($pwd);
system('rm -fr qtne-lib');

print H "---------------------------------------------------------\n\n";
print H "---------------------------------------------------------\n";
print H "-           QT compile conclusions                      -\n";
print H "-   QT with exceptions      QT with no-exceptions       -\n";
print H "---------------------------------------------------------\n";
my $pro=(int((($tt-$ttq)*100/$ttq)*100+.5))/100;
my $sub=$tt-$ttq;
print H "Compile time (seconds):$tt\t\t$ttq\t\t$sub($pro%)\n";
$pro=(int((($qtebinsize-$qtnebinsize)*100/$qtnebinsize)*100+.5))/100;
$sub=$qtebinsize-$qtnebinsize;
print H "bin debug size (bytes):$qtebinsize\t\t$qtnebinsize\t$sub($pro%)\n";
$pro=(int((($qtesbinsize-$qtnesbinsize)*100/$qtnesbinsize)*100+.5))/100;
$sub=$qtesbinsize-$qtnesbinsize;
print H "bin strip size (bytes):$qtesbinsize\t\t$qtnesbinsize\t\t$sub($pro%)\n";
$pro=(int((($qtelibsize-$qtnelibsize)*100/$qtnelibsize)*100+.5))/100;
$sub=$qtelibsize-$qtnelibsize;
print H "lib debug size (bytes):$qtelibsize\t$qtnelibsize\t$sub($pro%)\n";
$pro=(int((($qteslibsize-$qtneslibsize)*100/$qtneslibsize)*100+.5))/100;
$sub=$qteslibsize-$qtneslibsize;
print H "lib strip size (bytes):$qteslibsize\t\t$qtneslibsize\t$sub($pro%)\n";

close H;
