#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.


package Vhffs::Robots::Mysql;

require 5.004;
use strict;
use utf8;
use POSIX;
use locale;
use Locale::gettext;
use Vhffs::Services::Mysql;
use Vhffs::Constants;
use Vhffs::Functions;


sub mysql_admin_db_connect
{
	use DBI;
	my $vhffs = shift;
	my $mysqlconfig = $vhffs->get_config->get_service('mysql');
	my $dbuser = $mysqlconfig->{'username'};
	my $dbpass = $mysqlconfig->{'password'};
	my $dbhost = $mysqlconfig->{'host'};

	return DBI->connect( 'DBI:mysql:dbname=mysql;host='.$dbhost , $dbuser , $dbpass );
}


sub delete_db
{
	my $main = shift;
	my $db = shift;
	return unless defined $db;

	my $dbi = mysql_admin_db_connect( $main );
	return unless $dbi;

	my $query;
	$db->add_history( 'Ok, robots takes the destruction of this database' );

	$query = 'DELETE FROM `user` WHERE User = \''.$db->get_dbname.'\'';
	$dbi->do( $query ) or $db->add_history('Error while grant access to the database');

	$query = 'DELETE FROM `db` WHERE User = \''.$db->get_dbname.'\'';
	$dbi->do( $query ) or $db->add_history('Error while grant access to the database');

	$query = 'DELETE FROM `tables_priv` WHERE User = \''.$db->get_dbname.'\'';
	$dbi->do( $query ) or $db->add_history('Error while grant access to the database');

	$query = 'DELETE FROM `columns_priv` WHERE User = \''.$db->get_dbname.'\'';
	$dbi->do( $query ) or $db->add_history('Error while grant access to the database');

	$query = 'DROP DATABASE `'.$db->get_dbname.'`';
	$dbi->do( $query ) or $db->add_history('Error while deleting the database');

	$dbi->do( 'FLUSH PRIVILEGES' );
	$dbi->disconnect;

	$db->add_history( 'Robots finished the destruction' );
	$db->delete;
}



sub create_db
{       
	my $main = shift;
	my $db = shift;
	return unless defined $db;

	my $dbi = mysql_admin_db_connect( $main );
	return unless $dbi;

	my $query;
	$db->add_history( 'Ok, robots takes the creation of this database' );

	# create the database
	$query = 'CREATE DATABASE `'.$db->get_dbname.'`';
	$dbi->do( $query ) or $db->add_history('Error while creating the database');

	# grant privileges to user
	$query = 'GRANT ALL ON '.$db->get_dbname.'.* TO '.$db->get_dbname.' IDENTIFIED BY ?';
	$dbi->do( $query, undef, $db->get_dbpassword ) or $db->add_history( 'An error occured while granting privileges to an admin user' );

	$dbi->do( 'FLUSH PRIVILEGES' );
	$dbi->disconnect;

	$db->add_history( 'Robots finished the creation' );
	$db->set_status( Vhffs::Constants::ACTIVATED );
	$db->commit;
	$db->blank_password;
}



sub update_db
{
	my $main = shift;
	my $db = shift;
	return unless defined $db;

	my $dbi = mysql_admin_db_connect( $main );
	return unless $dbi;

	my $query;
	$db->add_history( 'Ok, will change password for this database' );

	$query = 'UPDATE user SET PASSWORD=PASSWORD(?) WHERE user = ?';
	$dbi->do( $query, undef, $db->get_dbpassword, $db->get_dbname) or $db->add_history('Error while grant access to the database');

	$dbi->do( 'FLUSH PRIVILEGES' );
	$dbi->disconnect;

	$db->add_history( 'Robots changed the password for this database' );
	$db->set_status( Vhffs::Constants::ACTIVATED );
	$db->commit;
	$db->blank_password;
}

1;
