#!/usr/bin/perl -w 

# This is ptxArchiver.pl, a program to archive records from one MySQL table to
# an another table,a file or simply purged.
#
# This program is copyright 2009 frederic descamps <lefred@inuits.be>.
# Feedback and improvements are welcome.
#
# This program requires mk-archiver from maatkit <http://www.maatkit.org/>
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# This program 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, version 2; OR the Perl Artistic License.  On UNIX and similar
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
# licenses.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA.


use Getopt::Std;
use Config::General;
use FindBin qw( $Bin $Script $RealBin $RealScript );
use lib qw( $Bin/modules /usr/share/ptxarchiver/modules );
use strict;

use ptxArchiverVars;

my $version="0.3";
my $destination;
my $ignore;

my %options=();
getopts("hvt:w:",\%options);
# like the shell getopt, "d:" means d takes an argument
if ($options{h}) {
   print "ptxArchiver.pl - Archiver script for Fleetworks table
sintax: ptxtArchiver.pl [-h] [-p] -t <table name> -w <where_statement>
     -h : this help
     -v : display the version number
     -t : define the table to archive
     -w : define the where statement   ex: -w \"end < unix_timestamp('2008-05-22')*1000\"\n";
   exit 0;
}
if ($options{v}) {
  print "ptxArchiver.pl - version $version\n";
  exit 0;
}
if (!$options{t}) {
  print "Error a table must be defined !\n";
  exit 2;
}
if (!$options{w}) {
  print "Error a where statement must be defined !\n";
  exit 3;
}

my $table       = $options{t};
my $whereclause = $options{w};

if (!find($table)) {
  print "Error the table [$table] cannot be found in the configuration file !\n";
  exit 4;
}

print "command started : \n" if $debug;
if ($purge) {
    $destination = "--purge -P 100";
}
elsif ($fileout) {
    $destination = "--file ". $filename ."_". $table . "_" . timestampStr() ." --port 100";
}
else {
    $destination = "--dest h=$dest_dbhost,P=3306,D=$dest_dbname,t=".$table."_archived,u=$dest_dbuser,p=$dest_dbpass";
}
if ($ignorereplica) { $ignore = ",b=1"; }
else { $ignore =""; }


print "perl -I $modulesPath $archiver --source h=$src_dbhost,P=3306,D=$src_dbname,t=$table,u=$src_dbuser,p=$src_dbpass,m=archiver$ignore $destination --where \"$whereclause\"" if $debug; 
system("perl -I $modulesPath $archiver --source h=$src_dbhost,P=3306,D=$src_dbname,t=$table,u=$src_dbuser,p=$src_dbpass,m=archiver$ignore $destination --where \"$whereclause\" ");

#print Dumper find($table);
