#!/usr/bin/perl -w

use strict;
use esmith::config;
use esmith::db;
use esmith::util;

my %conf;
tie %conf, 'esmith::config';

my $event = $ARGV[0];
my $file = $ARGV[1];

#---------------------------------------------------------------------------
# Check the runlevel, if we're in runlevel 7, and we're being called from
# bootstrap-console-save, then MySQL can't be running, so use bootstrap mode.
# Otherwise, just use mysql to do a straight import.
#---------------------------------------------------------------------------

my $runlevel;

open (RUNLEVEL, "-|", "/usr/bin/systemctl get-default");
(undef, $runlevel) = split(' ',<RUNLEVEL>);
close RUNLEVEL;


if ( ($runlevel ne 'multi-user.target' &&  $runlevel ne "sme-server.target") || $event eq 'bootstrap-console-save')
{
    my $pid = open(KID, "|-");
    if (defined $pid)
    {
	if ($pid)
	{
	    open(SQL, "<$file");
	    print KID foreach (<SQL>);
	    close SQL;
	    close(KID);
	    waitpid $pid,0;	
	}
	else
	{
	    # Find our mysqld binary
	    my $mysqld = "/opt/rh/rh-mariadb103/root/usr/libexec/mysqld";
	    if (-f "/opt/rh/rh-mariadb103/root/usr/sbin/mysqld") {
		$mysqld = "/opt/rh/rh-mariadb103/root/usr/sbin/mysqld";
	    }
	    # Hard-code user, since it is set in mysqld_safe currently.
	    # See http://bugs.mysql.com/2163
	    exec("/usr/bin/scl enable rh-mariadb103 -- $mysqld", qw(--bootstrap --user=mysql --skip-grant-tables));
	}
    }
    else
    {
	warn "Couldn't fork: $!";
    }
}
else
{
    system("/usr/bin/scl enable rh-mariadb103 -- /opt/rh/rh-mariadb103/root/usr/bin/mysql < $file");
}
exit(0);
