#!/usr/bin/perl -w

use strict;
use Test::Repocop::Options;
use Test::Repocop::TestCache;

$Repocop::arg::reportlevel='skip';
my $distro='altlinux';
my $branch='sisyphus';
my $component='classic';
&Test::Repocop::Options::get_common_options(
    "distribution=s"  => \$distro,
    "branch=s"  => \$branch,
    "component=s"  => \$component,
);
$branch=lc($branch);

&Test::Repocop::Options::die_if_nothing_to_report();
&Test::Repocop::Common::mkdir_test_environment('');
&Test::Repocop::TestMtimeDB::initialize();

my $distrodbdir="$ENV{'HOME'}/.cache/distrodb/$distro/$branch";
my $db_version=5;
my $db_compat_version=5;

`rm -rf $distrodbdir`;
`mkdir -p $distrodbdir/groups-allowed`;
my $repocop_statedir=$ENV{'REPOCOP_STATEDIR'};
die "Oops! REPOCOP_STATEDIR is not initialized" unless defined $repocop_statedir;
if (-f "$repocop_statedir/distromap/groups_allowed/GROUPS") {
    `cp -a "$repocop_statedir/distromap/groups_allowed/GROUPS" $distrodbdir/groups-allowed/GROUPS.tsv`;
}

my @compactify=qw!bin devel-libs gir headers path perl pkg-config provides python2 python3 srcname2binnames!;
# no need to compactify sourcename
foreach my $type (@compactify, qw!sourcename!) {
    `mkdir -p "$distrodbdir/$type"`;
}

open SQLITE, "| sqlite3 '$ENV{'REPOCOP_TEST_DBDIR'}/rpm.db'" || die "can't run sqlite3: $!";
print SQLITE ".mode tabs
.output $distrodbdir/sourcename/${component}.txt
SELECT rpm.name, srcrpm.name FROM rpm JOIN srcrpm ON rpm.sourceid = srcrpm.pkgid;
.output $distrodbdir/srcname2binnames/${component}.txt
SELECT srcrpm.name, rpm.name FROM rpm JOIN srcrpm ON rpm.sourceid = srcrpm.pkgid;
.output $distrodbdir/provides/${component}.txt
select DISTINCT providename, rpm.name FROM rpm_provides JOIN rpm ON rpm_provides.pkgid = rpm.pkgid ORDER BY PROVIDENAME;
.output $distrodbdir/devel-libs/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/lib*.so' or filename glob '/lib64/lib*.so';
.output $distrodbdir/headers/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/include/*.*';
.output $distrodbdir/pkg-config/${component}-lib.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/pkgconfig/*.pc';
.output $distrodbdir/pkg-config/${component}-share.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/share/pkgconfig/*.pc';
.output $distrodbdir/bin/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/bin/*' or filename glob '/usr/sbin/*' or filename glob '/bin/*' or filename glob '/sbin/*';
.output $distrodbdir/gir/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/share/gir-1.0/*.gir';
.output $distrodbdir/perl/${component}.txt
select providename, name from rpm_provides join rpm on rpm_provides.pkgid=rpm.pkgid where providename glob 'perl(*)';
.output $distrodbdir/python2/${component}-rp.txt
select providename, name from rpm_provides join rpm on rpm_provides.pkgid=rpm.pkgid where providename glob 'python2.*(*)';
.output $distrodbdir/python3/${component}-rp.txt
select providename, name from rpm_provides join rpm on rpm_provides.pkgid=rpm.pkgid where providename glob 'python3.*(*)';
.output $distrodbdir/python2/${component}-py.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python2.*/site-packages/*.py' or filename glob '/usr/lib/python2.*/site-packages/*.py';
.output $distrodbdir/python3/${component}-py.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python3.*/site-packages/*.py' or filename glob '/usr/lib/python3.*/site-packages/*.py';
.output $distrodbdir/python2/${component}-so.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python2.*/site-packages/*.so';
.output $distrodbdir/python3/${component}-so.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python3.*/site-packages/*.so';
";
close (SQLITE);

`sed -e 's,^/usr,,;s,^/sbin/,,;s,^/bin/,,' $distrodbdir/bin/${component}.txt > $distrodbdir/path/${component}.txt`;
`sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^lib,,;s,\.so\t,\t,' $distrodbdir/devel-libs/${component}.txt`;
`sed -i -e 's,^/usr/include/,,' $distrodbdir/headers/${component}.txt`;
`sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^/lib/,,;s,^/share/,,;s,^pkgconfig/,,;s,\.pc\t,\t,' $distrodbdir/pkg-config/*.txt`;
`sed -i -e 's,^/usr/share/gir-1.0/,,;s,\.gir\t,\t,' $distrodbdir/gir/${component}.txt`;
`sed -i -e '/^debug64\(/d' $distrodbdir/provides/${component}.txt`;
`sed -i -e 's,^python2\.[0-9]*(,,;s,)\t,.py\t,' $distrodbdir/python2/${component}-rp.txt`;
`sed -i -e 's,^python3\.[0-9]*(,,;s,)\t,.py\t,' $distrodbdir/python3/${component}-rp.txt`;
`awk '{sub(/\./,"/",\$1)}' $distrodbdir/python2/${component}-rp.txt > $distrodbdir/python2/${component}-pf.txt`;
`awk '{sub(/\./,"/",\$1)}' $distrodbdir/python3/${component}-rp.txt > $distrodbdir/python3/${component}-pf.txt`;
`rm -f $distrodbdir/python2/${component}-rp.txt`;
`rm -f $distrodbdir/python3/${component}-rp.txt`;
`sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python2.*/site-packages/,,' $distrodbdir/python2/${component}-{so,py}.txt`;
`sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python3.*/site-packages/,,' $distrodbdir/python3/${component}-{so,py}.txt`;
`cat $distrodbdir/python2/${component}-??.txt | sort -u > $distrodbdir/python2/${component}.txt`;
`cat $distrodbdir/python3/${component}-??.txt | sort -u > $distrodbdir/python3/${component}.txt`;
`rm -f $distrodbdir/python2/${component}-??.txt`;
`rm -f $distrodbdir/python3/${component}-??.txt`;

foreach my $component (grep {-e $_} map {glob "$distrodbdir/$_/*.txt"} @compactify) {
    &compactify($component);
}
`echo $db_version > $distrodbdir/.version`;
`echo $db_compat_version > $distrodbdir/.compat`;

sub compactify {
    my ($file)=@_;
    my %KEY;
    open my $fh, "<", $file or die $!;
    while (<$fh>) {
	chomp;
	my @line=split(/\s+/);
	my $ref=$KEY{$line[0]};
	unless ($ref) {
	    $ref=[];
	    $KEY{$line[0]}=$ref;
	}
	my $val=$line[1];
	push @$ref, $val if not grep {$_ eq $val} @$ref;
    }
    close $fh;
    open $fh, '>', $file or die $!;
    map {print $fh "$_\t", join("\t",@{$KEY{$_}}),"\n"} sort {$a cmp $b} keys (%KEY);
    close $fh;
}

#print STDERR "done.\n" if $verbose;

=head1	NAME

repocop-report-distromap-db - a tool that creates repocop reports for distromap db.

=head1	SYNOPSIS

see repocop-report-distromap-db

=head1	DESCRIPTION

B<repocop-report-distromap-db> processes results of repocop unit tests, created with 
repocop-run command, stored in <cachedir> and creates results in txt form.
Presize subset of tests can be selected using B<--include>
and B<--exclude> options.

=head1	OPTIONS

--branch <branch name>

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	ACKNOWLEGEMENTS

To Alexey Torbin <at@altlinux.org>, whose qa-robot package
had a strong influence on repocop. 

=head1	COPYING

Copyright (c) 2008 Igor Vlasenko, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

=cut

