#!/usr/bin/perl

=head1 NAME

octo_tool - Octopussy tool to do simple tasks

=head1 DESCRIPTION

octo_tool backup - Backups Octopussy configuration
octo_tool cache_clear - Clears Cache (msgid_stats or taxonomy_stats)
octo_tool message_copy - Copies Message from a Service to another Service
octo_tool message_move - Moves Message from a Service to another Service
octo_tool service_clone - Clones a Service
octo_tool table_clone - Clones a Table

=head1 SYNOPSIS

octo_tool <task> [options]

octo_tool backup <filename>

octo_tool cache_clear msgid_stats|taxonomy_stats

octo_tool message_copy <msgid_src> <msg_dst>

octo_tool message_move <msgid_src> <msg_dst>

octo_tool service_clone <servicename> <cloned_servicename>

octo_tool table_clone <tablename> <cloned_tablename>

=head1 OPTIONS

=over 4

=item B<-h,--help>

Prints this help.

=item B<-v,--version>

Prints version.

=back

=cut

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;
use POSIX qw(strftime);

use Octopussy::Cache;
use Octopussy::Configuration;
use Octopussy::Service;
use Octopussy::Table;

my $VERSION = '0.6';

my %option = ();
my %task = (
	backup => \&Backup,
	cache_clear => \&Cache_Clear,
	message_copy => \&Message_Copy,
	message_move => \&Message_Move,
	service_clone => \&Service_Clone,
	table_clone => \&Table_Clone,
	);

my $status = GetOptions(
	'h|help|?'	=> \$option{help},
	'v|version' => sub { printf "octo_tool $VERSION\n"; exit; }
	);

=head1 FUNCTIONS

=head2 Usage($msg)

Prints Script Usage

=cut

sub Usage
{
	my $msg = shift;

	if (defined $msg)
	{
		pod2usage(-verbose => 99, -sections => [ qw(SYNOPSIS OPTIONS) ], 
			-message => "\n$msg\n");
	}
	else
	{
		pod2usage(-verbose => 99, -sections => [ qw(SYNOPSIS OPTIONS) ]);
	}
}

=head2 Backup($filename)

Backups Octopussy configuration

=cut

sub Backup
{
	my $filename = shift;

	my $timestamp   = strftime("%Y%m%d%H%M%S", localtime);
	$filename .= ($filename !~ /\.tgz$/ ? "_${timestamp}.tgz" : '');
	Octopussy::Configuration::Backup($filename);
}

=head2 Cache_Clear($cache_name)

Clears Cache 'msgid_stats' or 'taxonomy_stats'

=cut

sub Cache_Clear
{
	my $cache_name = shift;

	Usage('[ERROR] Invalid number of args.')    if (!defined $cache_name);

	if ($cache_name eq 'msgid_stats')
	{
		Octopussy::Cache::Clear_MsgID_Stats();
	}
	elsif ($cache_name eq 'taxonomy_stats')
	{
		Octopussy::Cache::Clear_Taxonomy_Stats();
	}
}

=head2 Message_Copy($msgid_src, $msgid_dst)

Copies Message 'msgid_src' to Message 'msgid_dst'

=cut

sub Message_Copy
{
	my ($msgid_src, $msgid_dst) = @_;

	Usage('[ERROR] Invalid number of args.')    if (!defined $msgid_dst);

	Octopussy::Service::Copy_Message($msgid_src, $msgid_dst);
}

=head2 Message_Move($msgid_src, $msgid_dst)

Moves Message 'msgid_src' to Message 'msgid_dst'

=cut

sub Message_Move
{
    my ($msgid_src, $msgid_dst) = @_;

    Usage('[ERROR] Invalid number of args.')    if (!defined $msgid_dst);

    my $nb_errors = Octopussy::Service::Copy_Message($msgid_src, $msgid_dst);
	if ($nb_errors == 0)
	{
		my ($serv_src) = $msgid_src =~ /^(.+):.+$/;
		Octopussy::Service::Remove_Message($serv_src, $msgid_src);
	}
}

=head2 Service_Clone($service_orig, $service_clone)

Clones Service '$service_orig' in '$service_clone'

=cut

sub Service_Clone
{
	my ($service_orig, $service_clone) = @_;

	Usage('[ERROR] Invalid number of args.')	if (!defined $service_clone);

	my $service_orig_filename = Octopussy::Service::Filename($service_orig);
	my $service_clone_filename = Octopussy::Service::Filename($service_clone);
	Usage("[ERROR] Service '$service_orig' doesn't exist !")
		if (!-f $service_orig_filename);
	Usage("[ERROR] Service '$service_clone' already exists !")
        if ((defined $service_clone_filename) && (-f $service_clone_filename));

	Octopussy::Service::Clone($service_orig, $service_clone);
}

=head2 Table_Clone($table_orig, $table_clone)

Clones Table '$table_orig' in '$table_clone'

=cut

sub Table_Clone
{
	my ($table_orig, $table_clone) = @_;

    Usage('[ERROR] Invalid number of args.')    if (!defined $table_clone);

    my $table_orig_filename = Octopussy::Table::Filename($table_orig);
    my $table_clone_filename = Octopussy::Table::Filename($table_clone);
    Usage("[ERROR] Table '$table_orig' doesn't exist !")
        if (!-f $table_orig_filename);
    Usage("[ERROR] Table '$table_clone' already exists !")
        if ((defined $table_clone_filename) && (-f $table_clone_filename));
	Octopussy::Table::Clone($table_orig, $table_clone);
}


Usage()	if ($option{help});
Usage('[ERROR] No task specified.')	if (@ARGV < 1);

my $t = $ARGV[0];
my @args = @ARGV;
shift @args;

$task{$t}(@args)	if (defined $task{$t}); 
