#!/usr/bin/perl -w

use strict;
use DBI;
use RPM qw/evrcmp/;
use RPM::Constants qw/:rpmsense/;

my $separator="\t";

#use Test::Repocop::Options;

my $dbfile=$ARGV[0];
my $sqlfile=$ARGV[1];

unless ($dbfile && $sqlfile) {
    print "usage: TODO \n";
    exit 1;
}

my $TODO='
     -init file
       -echo  Print commands before execution.
       -[no]header
       -column
       -html  Query results will be output as simple HTML tables.
       -line  Query results will be displayed with one value  per  line,  rows
       -list  Query results will  be  displayed  with  the  separator  (|,  by
       -separator separator
       -nullvalue string
       -version
       -help  Show help on options and exit.
';

my $dsn = "dbi:SQLite:dbname=$dbfile";
my $dbh = DBI->connect($dsn,"","") || die "can't connect to sqlite\n";

$dbh->func('rpm_compare_op_evr_e_v_r',5,\&rpm_compare_op_evr_e_v_r, 'create_function');
$dbh->func('rpm_compare_op_evr_evr',3,\&rpm_compare_op_evr_evr, 'create_function');
$dbh->func('evrcmp',2,\&evrcmp, 'create_function');

open(SQL,'<',$sqlfile) || die "can't open $sqlfile: $!";

while (<SQL>) {
    next if /^\s*(?:--.*)?$/;
    next if /^\s*\.mode/;
    #print STDERR "echo:$_";
    my $sth = $dbh->prepare($_);
    $sth->execute();
    while (my @row = $sth->fetchrow_array ) {
	print join($separator,@row),"\n";
    }
}

close(SQL) || die "can't close $sqlfile: $!";

sub rpm_compare_op_evr_evr {
    my ($rpmopflag,$evr_given,$evr_real)=@_;
    return 1 unless $rpmopflag and $evr_given;
    return 0 unless $evr_real;
    # skip epoch if both does not use it
    if ($evr_given!~/^\d+:/ || $evr_real!~/^\d+:/) {
	    $evr_given=~s/^\d+://;
	    $evr_real=~s/^\d+://;
    }
    # hack: to detect version vs version-release comparison
    if ($rpmopflag & RPMSENSE_SENSEMASK & RPMSENSE_EQUAL) {
	#if ($evr_given!~/^.+-(?:alt)?.+/ || $evr_real!~/^.+-(?:alt)?.+/) {
	if ($evr_given!~/^.+-.+/ || $evr_real!~/^.+-.+/) {
	    $evr_given=~s/-.+$//;
	    $evr_real=~s/-.+$//;
	}
    }
    return __rpm_op_calculate($rpmopflag, evrcmp($evr_real,$evr_given));
}

sub rpm_compare_op_evr_e_v_r {
    my ($rpmopflag,$evr,$epoch,$version,$release)=@_;
    if ($evr=~/^\d+:/ || (defined $epoch && $epoch ne '')) {
	$epoch='0' if !defined $epoch || $epoch eq '';
	$evr='0:'.$evr if ($evr!~/^\d+:/);
	return __rpm_op_calculate($rpmopflag, evrcmp("$epoch:$version-$release",$evr));
    } else {
	return __rpm_op_calculate($rpmopflag, evrcmp("$version-$release",$evr));
    }
}


# cmpresult: 1 A>B 0 A=B -1 A<B
sub __rpm_op_calculate {
    my ($rpmopflag, $cmpresult)=@_;
    $rpmopflag&=RPMSENSE_SENSEMASK;
    return 1 if $rpmopflag& RPMSENSE_EQUAL && $cmpresult==0;
    return 1 if $rpmopflag& RPMSENSE_GREATER && $cmpresult==1;
    return 1 if $rpmopflag& RPMSENSE_LESS && $cmpresult==-1;
    return 0;
}


=head1	NAME

repocop-sqlite - repocop wrapper to sqlite that adds a few rpm-specific functions to sqlite.

=head1	SYNOPSIS

B<repocop-sqlite>
[B<-h|--help>]
[B<-v|--verbose>]
[I<DB>] [I<SQLFILE>]

=head1	DESCRIPTION

B<repocop-sqlite> 

=head1	OPTIONS

=over

=item	B<-h, --help>

Display this help and exit.

=item	B<-v, --verbose>, B<-q, --quiet>

Verbosity level. Multiple -v increase the verbosity level, -q sets it to 0.

=back

=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
