#!/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 $distromapdir="$ENV{'HOME'}/.cache/distromap/$repo/$branch";

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

foreach my $type (qw!sourcename provides devel-libs headers pkg-config!) {
    `mkdir -p "$distromapdir/$type"`;
}

open SQLITE, "| sqlite3 '$ENV{'REPOCOP_TEST_DBDIR'}/rpm.db'" || die "can't run sqlite3: $!";
print SQLITE ".mode tabs
.output $distromapdir/sourcename/sourcename.txt
SELECT rpm.name, srcrpm.name FROM rpm JOIN srcrpm ON rpm.sourceid = srcrpm.pkgid;
.output $distromapdir/provides/provides.txt
select DISTINCT providename, rpm.name FROM rpm_provides JOIN rpm ON rpm_provides.pkgid = rpm.pkgid ORDER BY PROVIDENAME;
.output $distromapdir/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 $distromapdir/headers/headers.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/include/*.*';
.output $distromapdir/pkg-config/pkg-config.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib6?4?/pkgconfig/*.pc';
";
close (SQLITE);

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

#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-heroku> 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

