#!/usr/bin/perl -w

use strict;
use warnings;
use Test::Repocop::Options;
use Test::Repocop::TestDB;
use Test::Repocop::Metadata;
use DBI;
use DBD::SQLite;

$Repocop::arg::reportlevel='skip';
my $distro='altlinux';
my $branch='sisyphus';
my $component='classic';
my $dbfile;
my $repocop_patch_url=$branch eq 't6' ? "http://autoports.altlinux.org/pub/repocop" :
    "http://repocop.altlinux.org/pub/repocop";

&Test::Repocop::Options::get_common_options(
    'patchurl=s' => \$repocop_patch_url,
    "distribution=s"  => \$distro,
    "b|branch=s"  => \$branch,
    "component=s"  => \$component,
    "out=s"  => \$dbfile,
);
&Test::Repocop::Options::die_if_nothing_to_report();
#my $branch_id=1;
#$branch_id=4 if $branch and $branch eq 't6';
my ($dbUser,$dbPass);
our $dsn = "DBI:SQLite:dbname=$dbfile";
if (-e $dbfile) {
    warn "$dbfile already exists - removing\n";
    unlink $dbfile;
}
our $dbh = DBI->connect($dsn,$dbUser,$dbPass) || die "can't connect to SQLite\n";
$dbh->do("PRAGMA synchronous=OFF");
$dbh->do("CREATE TABLE repocop_metadata (
distribution TEXT,
branch TEXT,
component TEXT
)");
$dbh->do("INSERT INTO repocop_metadata (distribution,branch,component) VALUES ('$distro','$branch','$component')");
$dbh->do("CREATE TABLE repocop (
 name TEXT,
 version TEXT,
 release TEXT,
 arch TEXT,
 srcname TEXT,
 srcversion TEXT,
 srcrel TEXT,
 testname TEXT,
 status TEXT,
 message TEXT
)");
my $report_sth=$dbh->prepare('INSERT INTO repocop (name, version, release, arch, srcname, srcversion, srcrel, testname, status, message) VALUES (?,?,?,?,?,?,?,?,?,?)');
$dbh->do("CREATE TABLE repocop_patches (
 name TEXT,
 version TEXT,
 release TEXT,
 url TEXT
)");
my $patch_sth=$dbh->prepare('INSERT INTO repocop_patches (name, version, release, url) VALUES (?,?,?,?)');
$dbh->begin_work;
my $aclmap;
#$aclmap=Test::Repocop::ALTLinuxACL->new($Repocop::arg::aclfile) if $Repocop::arg::aclfile;
my $metadata=Test::Repocop::Metadata->new();
my $testdb=Test::Repocop::TestDB->new();
my $cache=$testdb->get_pkg_test_status_result_iterator();
while (my ($rpm,$test,$status,$result)=$cache->iterate4_filtered()) {
    # linearize -no need
    # $result=~s/\n/; /gs;
    # semi - linearize as we write directly into a database.
    $result=~s/\n/;\n/gs;
    my $srpm=$metadata->sourceid($rpm);
    my $packager=$metadata->nick($rpm);
    my $name=$metadata->name($rpm);
    my $version=$metadata->version($rpm);
    my $release=$metadata->release($rpm);
    my $arch=$metadata->arch($rpm);
    my $srcname=$metadata->name($srpm);
    my $srcversion=$metadata->version($srpm);
    my $srcrelease=$metadata->release($srpm);
    $report_sth->execute($name,$version,$release,$arch,$srcname,$srcversion,$srcrelease,$test,$status,$result);
    if ($srpm eq $rpm) {
	my $patchid=$srpm;
	$patchid=~s/\.src$//;
	my $suffix='/reports/diff/by-srpm/'.$patchid.'.diff';
	if (-e $repocop_cachedir.$suffix) {
	    my $url=$repocop_patch_url.$suffix;
	    $patch_sth->execute($name,$version,$release,$url);
	}
    }
}
$dbh->commit;

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

=head1	NAME

repocop-report-prometeus2 - a tool that creates repocop reports for sisyphus.heroku.com.

=head1	SYNOPSIS

see repocop-report-txt

=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

see repocop-report-txt

=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-2012 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

