#!/usr/bin/perl -w

use strict;
use DBI;

my $dbh;
my $sth_insert_files;

my $pkgkey=$ENV{'REPOCOP_PKG_KEY'};
my $pkgname=$ENV{'REPOCOP_PKG_NAME'};

&connect();

foreach my $filename(glob $ENV{'REPOCOP_PKG_ROOT'}."/usr/lib/rpm/*-files.req.list") {
    open my $fh, $filename or die $!;
    my @content = <$fh>;
    close $fh;
    foreach (@content){
	chomp;
	next if /^\s*#/ or /^\s*$/;
	/(\S+)\s+(\S+)\s*/;
	my ($dirpath,$dirowner)=($1,$2);
	$sth_insert_files->execute($pkgkey,$pkgname,$filename,$dirpath,$dirowner);
    }
}

&disconnect();

sub connect {
    my $dbfile=$ENV{'REPOCOP_TEST_DB'};
    die "database is not initialized properly!" unless $dbfile;
    $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","", {
	PrintError => 1,
	AutoCommit => 0,
			}) or die $dbh->errstr;
    $sth_insert_files=$dbh->prepare('INSERT INTO RPMBUILDFILESREQLIST VALUES(?,?,?,?,?)') or die $dbh->errstr;
}

sub disconnect {
    $dbh->commit;
    $sth_insert_files->finish;
    # hack around closing dbh with active statement handles bug
    local $SIG{'__WARN__'} = sub {};
    $dbh->disconnect;
}

