#!/usr/bin/perl -w

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

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, "-|", "/sbin/runlevel");
(undef, $runlevel) = split(' ',<RUNLEVEL>);
close RUNLEVEL;


if ($runlevel ne '7' || $event eq 'bootstrap-console-save')
{
    my $MYSQL = new FileHandle;
    $MYSQL->open(">/usr/libexec/mysqld "
		. "--bootstrap --skip-grant-tables --basedir=/usr "
		. "--datedir=/var/lib/mysql"
		);
    open(SQL, "<$file");
    while (<SQL>)
    {
	print $MYSQL;
    }
    close SQL;
    close $MYSQL;
}
else
{
    system("/usr/bin/mysql < $file");
}
exit(0);
