#!/usr/bin/perl -w
#
use DBI;
use strict;
#require ("/etc/mail/mimedefang-filter-greylist");
my $mysql_dbh = DBI->connect("DBI:mysql:database=greylist;host=localhost",
                         "greylist", "YOURPASSWORD",
                         {'RaiseError' => 1, AutoCommit => 0 });

my $DBFilename = "/var/spool/MIMEDefang/greylist.db";

my $sqlite_dbh = DBI->connect("dbi:SQLite:dbname=$DBFilename","","",
        { RaiseError => 1, AutoCommit => 0 });

my $insert_sql = qq{ INSERT INTO greylist_data(id, ip, sender, recipient, created, modified, reset, accepted, count)
                VALUES (?,?,?,?,?,?,?,?,?) };
my $insert_sth = $mysql_dbh->prepare($insert_sql);

my $select_sql = qq{ SELECT * FROM greylist_data };
my $select_sth = $sqlite_dbh->prepare($select_sql);
$select_sth->execute();

my $count = 0;
while (my $ref = $select_sth->fetchrow_hashref()) {
	$insert_sth->execute(undef,$ref->{'ip'}, $ref->{'sender'}, $ref->{'recipient'}, $ref->{'created'}, $ref->{'modified'}, $ref->{'reset'}, $ref->{'accepted'}, $ref->{'count'});
	$count++;
}
$select_sth->finish();
$insert_sth->finish();

$mysql_dbh->commit();

$mysql_dbh->disconnect();
$sqlite_dbh->disconnect();

print("Converted $count records.\n") if ($count);
