#!/usr/bin/perl -w

use warnings;
use strict;
use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );

# local classes
use czartool::Config;
use czartool::CzarDb;

my $czarDbName = undef;
my $begin = undef;
my $end = undef;
my $interval = undef;
my $verbose = undef;
my $optimize = undef;
my $config = new czartool::Config();

GetOptions (
        "dbname|d=s" => \$czarDbName,
        "interval|i=s" => \$interval,
        "begin|b=s" => \$begin,
        "end|e=s" => \$end,
        "optimize|o" => \$optimize,
        "verbose|v" => \$verbose,
        );

print "\n*******************************************************************************\n";
print "* \n";
my $quit = 0;

if (@ARGV) {
    $quit=1;
    print "* UNKNKOWN: option                          @ARGV\n";
}
if (!$begin) {
    $quit=1;
    print "* REQUIRED: choose a begin date                -b <datetime>\n";
}
if (!$end) {
    if($begin) {$end=$begin;} else {$end="NULL";}
    print "* OPTIONAL: choose an end date date            -e <datetime>                   (default=$end)\n";
}
if (!$czarDbName) {
    $czarDbName = "czardb";
    print "* OPTIONAL: choose czar Db name                -d <name>                       (default=$czarDbName\n";
}
if (!$interval) {
    print "* OPTIONAL: choose time interval               -i <'30 MINUTE'|'1 hour'|etc>   (default=".$config->getCzarCleanupInterval().")\n";
}
if (!$optimize) {
    $optimize = 0;
    print "* OPTIONAL: optimize database after cleanup    -o                              (default=$optimize)\n";
}
print "*\n*******************************************************************************\n";

if ($quit) { exit; }

my $czarDb = $config->getCzarDbInstance();

my $labels = undef;


$czarDb->cleanupDateRange($begin, $end, $config->getCzarCleanupInterval());
if ($optimize) {$czarDb->optimize();}
