#!/usr/local/cpanel/3rdparty/bin/perl

# Return errors if Perl experiences problems.
use strict;
use warnings;

use IO::Select;
use JSON::XS;
use GT::DB::Mysql::ProxysqlSync;
use Data::Dumper;

# Use cPanel's error logging module.
use Cpanel::Logger;

# Instantiate the cPanel logging object.
my $logger = Cpanel::Logger->new();

if ( (@ARGV) && ( $ARGV[0] eq '--describe' ) ) {
    print encode_json( describe() );
    exit 1;
}
else {
    # Get decoded input.
    my $input = get_passed_data();

    # Declare return variables and set their values.
    my ( $result, $message ) = ProxySQLSync($input);
    $logger->info("$message");

    # Return the return variables and exit.
    print "$result $message";
}

# Embed hook attributes alongside the action code.
sub describe {
    my $hooks = [
        {
            'category'      => 'Cpanel',
            'event'         => 'Api1::Mysql::adduser',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        },
        {
            'category'      => 'Cpanel',
            'event'         => 'Api1::Mysql::deluser',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        },
        {
            'category'      => 'Cpanel',
            'event'         => 'Api2::MysqlFE::createdbuser',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        },
        {
            'category'      => 'Cpanel',
            'event'         => 'Api2::MysqlFE::deletedbuser',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        },
        {
            'category'      => 'Cpanel',
            'event'         => 'Api2::MysqlFE::changedbuserpassword',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        },
        {
            'category'      => 'Cpanel',
            'event'         => 'UAPI::Mysql::create_user',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        },
        {
            'category'      => 'Cpanel',
            'event'         => 'UAPI::Mysql::delete_user',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        },
        {
            'category'      => 'Cpanel',
            'event'         => 'UAPI::Mysql::set_password',
            'stage'         => 'post',
            'hook'          => '/usr/local/cpanel/3rdparty/bin/proxysql_sync',
            'exectype'      => 'script',
            'escalateprivs' => 1,
        }
    ];
    return $hooks;
}

# Process data from STDIN.
sub get_passed_data {

    # Declare input variables.
    my $raw_data   = '';
    my $input_data = {};

    # Set up an input object.
    my $selects = IO::Select->new();

    # Get input from STDIN.
    $selects->add( \*STDIN );

    # Process the raw output, and JSON-decode.
    if ( $selects->can_read(.1) ) {
        while (<STDIN>) {
            $raw_data .= $_;
        }
        $input_data = decode_json($raw_data);
    }

    # Return the output.
    return $input_data;
}

sub ProxySQLSync {

    # Get the data that the system passes to the hook.
    my ($input) = @_;
    my ( $context, $data, $result ) = ( $input->{context}, $input->{data}, undef );
    my @keys = qw{username dbuser name user};

    foreach my $key (@keys) {
        if ( ( $data->{args} ) && ( $data->{args}->{$key} ) ) {
            $logger->info("Calling db_mysql_proxysql_sync for $key $data->{args}->{$key}");
            $result = db_mysql_proxysql_sync( { user => $data->{args}->{$key} } );
            last;
        }
    }
    return ( 1, encode_json($result) );
}
1;
