#!/usr/bin/perl
use strict;
use warnings;


use EBox::Config;
use EBox::Logs::Consolidate;
use File::Slurp;

my ($action, $tableName, @otherParams) = @ARGV;
$tableName or 
    die 'tableName argument is empty';

my @timePeriods = @{ EBox::Logs::Consolidate->timePeriods()  };


if ($action eq 'add') {
    my ($sqlFile) = @otherParams;
    $sqlFile or
        die 'sqlFile argument is empty';

    _addTable($tableName, $sqlFile, @timePeriods);
}
elsif ($action eq 'drop') {
    _dropTable($tableName, @timePeriods);
}
else {
    die "Usage: $0 [add table file | drop table]"
}



sub _addTable
{
    my ($tableName, $sqlFile, @timePeriods) = @_;

    if (not -r $sqlFile) {
        die "$sqlFile either does not exist or is unreadeable";
    }

    my $dbName = EBox::Config::configkey("eboxlogs_dbname");
    my $dbUser = EBox::Config::configkey("eboxlogs_dbuser");

    my $fileCmds = File::Slurp::read_file($sqlFile);

    foreach my $timePeriod (@timePeriods) {
        my $fullName = $tableName . '_' . $timePeriod;
        
        my $sqlCmds = $fileCmds;
        
        $sqlCmds =~ s/$tableName/$fullName/g;
        

        my $tmpFile = EBox::Config::tmp() . "$fullName.sql";
        File::Slurp::write_file($tmpFile, $sqlCmds);

        my $createCmd = qq{ su postgres -c "psql -f $tmpFile $dbName" > /dev/null 2>&1};
        system $createCmd;

        my $grantCmd = qq{su postgres -c "psql -c 'GRANT SELECT, INSERT, UPDATE, DELETE ON $fullName TO $dbUser' $dbName" > /dev/null 2>&1};
        system $grantCmd;
    }

}



       
        

sub _dropTable
{
    my ($tableName, @timePeriods) = @_;

    my $dbName = EBox::Config::configkey("eboxlogs_dbname");
    
    foreach my $timePeriod (@timePeriods) {
        my $fullName = $tableName . '_' . $timePeriod;

        my $dropCmd = qq{su postgres -c "psql -c 'DROP TABLE $fullName' $dbName" > /dev/null 2>&1};
        system $dropCmd;
    }

 
}


1;
