#!/usr/bin/perl -w

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

$Repocop::arg::reportlevel='skip';
my $repo='altlinux';
my $branch='sisyphus';
&Test::Repocop::Options::get_common_options(
    "branch=s"  => \$branch,
);
$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/$repo/$branch";
my $db_version=1;

`rm -rf $distrodbdir`;
`mkdir -p $distrodbdir/groups-allowed`;
if (-f "$ENV{'REPOCOP_STATEDIR'}/distromap/groups-allowed/GROUPS") {
    `cp -a "$ENV{'REPOCOP_STATEDIR'}/distromap/groups-allowed/GROUPS $distrodbdir/groups-allowed/GROUPS.tsv`
}

foreach my $type (qw!sourcename provides devel-libs headers pkg-config path!) {
    `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/sourcename.txt
SELECT rpm.name, srcrpm.name FROM rpm JOIN srcrpm ON rpm.sourceid = srcrpm.pkgid;
.output $distrodbdir/srcname2binnames/srcname2binnames.txt
SELECT srcrpm.name, rpm.name FROM rpm JOIN srcrpm ON rpm.sourceid = srcrpm.pkgid;
.output $distrodbdir/provides/provides.txt
select DISTINCT providename, rpm.name FROM rpm_provides JOIN rpm ON rpm_provides.pkgid = rpm.pkgid ORDER BY PROVIDENAME;
.output $distrodbdir/devel-libs/devel-libs.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/headers.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/include/*.*';
.output $distrodbdir/pkg-config/pkg-config.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/pkg-config-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/path/path.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/*';
";
close (SQLITE);

`sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^lib,,;s,\.so\t,\t,' $distrodbdir/devel-libs/devel-libs.txt`;
`sed -i -e 's,^/usr/include/,,' $distrodbdir/headers/headers.txt`;
`sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^/lib/,,;s,^/share/,,;s,^pkgconfig/,,;s,\.pc\t,\t,' $distrodbdir/pkg-config/*.txt`;

&compactify("$distrodbdir/provides/provides.txt");
&compactify("$distrodbdir/devel-libs/devel-libs.txt");
&compactify("$distrodbdir/headers/headers.txt");
&compactify("$distrodbdir/pkg-config/pkg-config.txt");
&compactify("$distrodbdir/path/path.txt");
&compactify("$distrodbdir/srcname2binnames/srcname2binnames.txt");
`echo $db_version > $distrodbdir/.version`;

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;
	}
	push @$ref, $line[1];
    }
    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

