#!/usr/bin/perl -w

use strict;
use Test::Repocop::Options;
use Test::Repocop::Common;

$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('');

my $distrodbdir="$ENV{'HOME'}/.cache/distrodb/$distro/$branch";
my $db_version=10;
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 fonts gir headers headers-rebased path perl php.raw pkg-config plugins provides python2 python3 python2.raw python3.raw pyegg srcname2binnames static-libs texmf cmake!;
# no need to compactify sourcename,..etc
foreach my $type (@compactify, qw!sourcename unique_data buildreqs-subst!) {
    `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/plugins/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/*.so' and not filename glob '/usr/lib64/lib*.so';
.output $distrodbdir/static-libs/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/lib*.a' or filename glob '/lib64/lib*.a';
.output $distrodbdir/headers/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/include/*.*' and filemode & 16384 = 0;
.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/*' or filename glob '/usr/games/*' or filename glob '/usr/lib/kde3/bin/*' or filename glob '/usr/lib/kde4/bin/*' or filename glob '/usr/lib/kf5/bin/*' or filename glob '/usr/lib64/qt3/bin/*' or filename glob '/usr/lib64/qt4/bin/*' or filename glob '/usr/share/qt5/bin/*';
.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.raw/${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.raw/${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.raw/${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.raw/${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';
.output $distrodbdir/python2/${component}-sc.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python2*/*.so';
.output $distrodbdir/python2/${component}-pc.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python2*/*.py';
.output $distrodbdir/python3/${component}-sc.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python3*/*.so';
.output $distrodbdir/python3/${component}-pc.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/python3*/*.py';
.output $distrodbdir/pyegg/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '*.egg-info' and not filename glob '/usr/share/doc/*' and not rpm.sourceid glob 'python-2.*';
.output $distrodbdir/php.raw/${component}-so.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/lib64/php/*/extensions/*.so';
.output $distrodbdir/php.raw/${component}-mo.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/share/php*/modules/*' or filename glob '/usr/share/php/pear/*';
.output $distrodbdir/unique_data/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid  where filemode & 16384 = 0 and filename glob '/usr/share/*' GROUP BY filename HAVING COUNT(filename)=1;
.output $distrodbdir/texmf/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where rpm.sourceid not glob 'tetex-2.0-alt*' and filemode & 16384 = 0 and (filename glob '/usr/share/texmf/*' or filename glob '/usr/share/texmf-texlive/*' or filename glob '/usr/share/texlive/texmf-dist/*');
.output $distrodbdir/cmake/${component}.txt
select LOWER(filename), name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filemode & 61440 = 16384 and (filename glob '/usr/lib64/cmake/*' or filename glob '/usr/lib/cmake/*' or filename glob '/usr/share/cmake/*');
.output $distrodbdir/fonts/${component}.txt
select filename, name from rpm_files join rpm on rpm_files.pkgid=rpm.pkgid where filename glob '/usr/share/fonts/*' and (filename glob '*.ttf' or filename glob '*.ttc' or filename glob '*.ttx' or filename glob '*.otf' or filename glob '*.afm' or filename glob '*.pfb' or filename glob '*.pcf' or filename glob '*.pcf.bz2' or filename glob '*.pcf.gz' or filename glob '*.pcf.xz');
.output /dev/null
attach database '$ENV{REPOCOP_DISTROTEST_DBDIR}/buildreqs-subst.db' as brs;
-- create temporary table tmp001 (realname TEXT, substname TEXT);
-- INSERT INTO tmp001 SELECT realname, substname FROM buildreq_subst WHERE realname <> substname;
.output $distrodbdir/buildreqs-subst/${component}.txt
SELECT realname, substname FROM buildreq_subst WHERE realname <> substname;
";
close (SQLITE);

## extcall ${distrodbdir} ${component}
system ('repocop-report-helper-distrodb-preprocess',$distrodbdir, $component)==0 or warn "WARNING: repocop-report-helper-distromap-db-preprocess ${distrodbdir} ${component} failed";

&rebase_1dep("$distrodbdir/headers/$component.txt","$distrodbdir/headers-rebased/$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;
}

sub rebase_1dep {
    my ($infile,$outfile)=@_;
    open (my $in,'<',$infile) || die "$!";
    open (my $out,'>',$outfile) || die "$!";
    while (<$in>) {
	my ($header)=split(/\t/,$_,-1);
	next if $header=~/^\./;
	my $rebased_header=$header;
	$rebased_header=~s!^[^/]*/!!;
	$rebased_header=~s!^[\d\.]*/!!;
	print $out "$rebased_header\t$header\n";
    }
    close($in) || die "$!";
    close($out) || die "$!";
}

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

=head1	NAME

repocop-report-distromap-db - a tool that creates repocop report as a distrodb 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-2016 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

