#!/usr/bin/perl

$|=1;

use strict;

our $config={
	 'sites'=>[ 
		   ["dbi:Pg:dbname=frida", "frida", "" ],
		   ["dbi:Pg:dbname=frida;host=frida.ljudmila.org", "frida", "ride" ]
		 ],
	 'mirrors'=>{
		     'waypoint'=>{
			     'key'=>'id',
			    },
		     'magellan'=>{
			     'key'=>'id',
			    },
		     'satstat'=>{
			     'key'=>'id',
			    },
		     'satellite'=>{
			     'key'=>'satstat',
			    },
		    }
	 };

use Data::Dumper;
#print Dumper($config);

use WDBI;

sub wdbi_sync {
  my ($dbi1,$dbi2,$table,$pkey)=@_;
	
  my $view1= my $view2=$table;

#  print "DBI1: ";
#  print Dumper($dbi1);
#  print "DBI2: ";
#  print Dumper($dbi2);

  my $dbh1=DBI->connect(@$dbi1);
  my $dbh2=DBI->connect(@$dbi2);

  if($dbh1 && $dbh2) {

    $WDBI::db=$dbh1;

    my $sth1=$dbh1->prepare("SELECT max($pkey) FROM $view1");
    if(!$sth1) { die $dbh1->errstr; }
    my $sth2=$dbh2->prepare("SELECT max($pkey) FROM $view2");
    if(!$sth2) { die $dbh2->errstr; }
    
    $sth1->execute();
    $sth2->execute();
    
    my $res1=$sth1->fetchrow_arrayref;
    my $res2=$sth2->fetchrow_arrayref;

    my $maxid1=$res1->[0]+0;
    my $maxid2=$res2->[0]+0;

    if ($maxid1<$maxid2) {
	log_error("Oops. Target has less data than source. You might want to reverse them...");
	die;
    } elsif ($maxid1==$maxid2) {
	print "...was up-to-date.\n"; 
	next;
    } 
	
    $sth1=$dbh1->prepare("SELECT * FROM $view1 WHERE $pkey > $maxid2 ORDER BY $pkey");
    $sth1->execute();

    while($res1=$sth1->fetchrow_hashref) {
      my $q= makeinsert($view2,$res1);
     
#      print $q;
      my $sth=$dbh2->prepare($q);
      my $res=$sth->execute();
     
#      print Dumper($res);
       print '.'; 
    }

  } else {
    die "No database connection.".$dbh1->errstr;
  }
  
}

sub makeinsert {
  my ($table,$row) = @_;

  my $keys=join(',',keys(%$row));
  my $values=join(',',map { sqlq($_) } values(%$row));

  return 
"INSERT INTO $table
($keys)
VALUES
($values) "
;
}

#print Dumper($config);

for my $mirror ( keys(%{$config->{'mirrors'}}) ) {
  print "Syncing $mirror...";

  wdbi_sync(
	    $config->{'sites'}->[0],
	    $config->{'sites'}->[1],
	    $mirror,
	    $config->{'mirrors'}->{$mirror}->{'key'},
	   );
}