#!/usr/bin/perl
# setup_tables: setup MYSQL/PGSQL side of things for blootbot.
# written by the xk.
###

$bot_config_dir	= '/etc/blootbot';

require "$bot_config_dir/directories";
my $bot_base_dir = $bot_state_dir;

require "$bot_src_dir/core.pl";
require "$bot_src_dir/logger.pl";
require "$bot_src_dir/modules.pl";
require "$bot_src_dir/Misc.pl";
require "$bot_src_dir/interface.pl";

# read param stuff from blootbot.config.
&loadConfig("$bot_config_dir/blootbot.config");
&loadDBModules();
my $dbname = $param{'DBName'};
my $query;

if ($dbname eq "") {
  print "error: appears that the config file was not loaded properly.\n";
  exit 1;
}

if ($param{'DBType'} =~ /mysql/i) {
    use DBI;

    # OK, can I find a mysql server running?
#    {
#      my $rc = system("mysqlshow --host=$param{SQLHost} >/dev/null 2>&1") >> 8;
#      die "Couldn't connect to SQL server" if $rc;
#    }
    # Well, we have a mysql server. Let's take a crack at connecting normally,
    #  maybe the work is already done?
    &status("Trying to see if we have already done this stuff...");
    if (&openDB($param{DBName}, $param{SQLUser}, $param{SQLPass}, 1)) {
      &closeDB();
      &status("I was able to connect to the database, stopping processing now");
      print "It appears that you already have a database set up.\n";
      print "(This script is not capable of fixing a broken database)\n";
      print "If you need to recreate the database, either delete the existing one,\n";
      print " or dpkg-reconfigure blootbot and give a new database name\n";
      exit 0;
    }
    &status("Could not connect to database $param{DBName}, continuing");

    print "Please enter user information for root access to the mysql database.\n";
    print "The username is probably 'root', the password you should have set\n";
    print "yourself. If you haven't set one yet, leave it blank, and read\n";
    print "/usr/share/doc/mysql-server/README.Debian\n";
    # username.
    print "Username: ";
    chop (my $adminuser = <STDIN>);

    # passwd.
    system "stty -echo";
    print "Password: ";
    chop(my $adminpass = <STDIN>);
    print "\n";
    system "stty echo";

    if ($adminuser eq "") {
	&ERROR("error: adminuser is blank.");
	exit 1;
    }

    &openDB("mysql", $adminuser, $adminpass);

    my $database_exists = 0;
    foreach $database (&dbRawReturn("SHOW DATABASES")) {
	$database_exists++ if $database eq $param{DBName};
    }
    if ($database_exists) {
	&status("Database '$param{DBName}' already exists. Continuing...");
    } else {
	&status("Creating db ...");
	&dbRaw("create(database)", "CREATE DATABASE $param{DBName}");
    }

    &status("--- Adding user $param{'SQLUser'} privileges on database $param{DBName}...");

    $query = "GRANT ALTER, CREATE, DELETE, DROP, INSERT, SELECT, UPDATE " .
      "ON $param{DBName}.* TO $param{SQLUser} IDENTIFIED BY '$param{SQLPass}'";
    &dbRaw("create(user)", $query);

    &status("--- Adding user $param{'SQLUser'}\@localhost privileges on database $param{DBName}...");
    $query = "GRANT ALTER, CREATE, DELETE, DROP, INSERT, SELECT, UPDATE " .
      "ON $param{DBName}.* TO $param{SQLUser}\@localhost IDENTIFIED BY '$param{SQLPass}'";

    &dbRaw("create(user)", $query);

    # flush.
    &status("Flushing privileges...");
    $query = "FLUSH PRIVILEGES";
    &dbRaw("mysql(flush)", $query);
}

&status("Done.");

&closeDB();
