#!/usr/bin/perl

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2001-2006 by moleSoftware GmbH - http://www.molesoftware.com
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp-dsk-quota 2505 2010-01-06 20:30:53Z nuxwin $
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "VHCS - Virtual Hosting Control System".
#
# The Initial Developer of the Original Code is moleSoftware GmbH.
# Portions created by Initial Developer are Copyright (C) 2001-2006
# by moleSoftware GmbH. All Rights Reserved.
# Portions created by the ispCP Team are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#

use FindBin;
use lib "$FindBin::Bin/..";
require 'ispcp_common_code.pl';

use strict;

use warnings;

sub dsk_quota_start_up {

	my ($rs) = (undef, undef);

	push_el(\@main::el, 'dsk_quota_start_up()', 'Starting...');

	# Let's clear Execution Logs, if any.

	if (-e $main::ispcp_dsk_quota_el) {

		$rs = del_file($main::ispcp_dsk_quota_el);

		return $rs if ($rs != 0);

	}

	# config check;

	$rs = get_conf();

	return $rs if ($rs != 0);

	push_el(\@main::el, 'dsk_quota_start_up()', 'Ending...');

	return 0;

}


sub dsk_quota_shut_down {

	my $rs = undef;

	push_el(\@main::el, 'dsk_quota_shut_down()', 'Starting...');

	push_el(\@main::el, 'dsk_quota_shut_down()', 'Ending...');

	return 0;

}

sub dsk_quota_engine {

	my ($rs, $ref, $rdata) = (undef, undef, undef);

	push_el(\@main::el, 'dsk_quota_engine()', 'Starting...');

	my $sql = "SELECT domain_id, domain_name FROM domain WHERE domain_status = 'ok';";

	($rs, $ref) = doSQL($sql);

	return $rs if ($rs != 0);

	my $cmd_du = $main::cfg{'CMD_DU'};

	foreach (@$ref) {

		my $disk_limit  = @$_[2];

		my $domain_name = @$_[1];

		my $domain_id   = @$_[0];

		my $size = 0;

		# BSD/NUX Command
		if ($main::cfg{'ROOT_GROUP'} eq "wheel") {
			# @todo Why '-I "*.tar.bz2" -I "*.tar.lzma"' and not '-I "*.bz2" -I "*.lzma"', too?
			my $s = `$cmd_du -I "*.gz" -I "*.tar.bz2" -I "*.tar.lzma" -d 0 $main::cfg{APACHE_WWW_DIR}/$domain_name`;

			$s =~ /^(\d+)/; $size += $1*1024;

			$s = `$cmd_du -d 0 $main::cfg{MTA_VIRTUAL_MAIL_DIR}/$domain_name`;

			$s =~ /^(\d+)/; $size += $1*1024;

		} else {

			my $s = `$cmd_du --exclude="backups/$domain_name-*.lzma" --exclude="backups/$domain_name-*.bz2" --exclude="backups/$domain_name-*.gz" --exclude="backups/*.sql.lzma" --exclude="backups/*.sql.bz2" --exclude="backups/*.sql.gz" --exclude="logs/*.log" -s -B1 $main::cfg{APACHE_WWW_DIR}/$domain_name`;

			$s =~ /^(\d+)/; $size += $1;

			$s = `$cmd_du -s -B1 $main::cfg{MTA_VIRTUAL_MAIL_DIR}/$domain_name`;

			$s =~ /^(\d+)/; $size += $1;
		}

		if ($size > 0) {

			$sql = "UPDATE domain SET domain_disk_usage='$size' WHERE domain_id='$domain_id'";

			($rs, $rdata) = doSQL($sql);

			return $rs if ($rs != 0);

			$sql = "SELECT COUNT(name) AS cnt FROM quotatallies WHERE name = '$domain_name'";

			($rs, $rdata) = doSQL($sql);

			return $rs if ($rs != 0);

			$rdata = @$rdata[0];

			my $quota_ent = @$rdata[0];

			if ($quota_ent > 0) {

				$sql = "UPDATE quotatallies SET bytes_in_used = '$size' WHERE name = '$domain_name'";

				($rs, $rdata) = doSQL($sql);

				return $rs if ($rs != 0);

			}

		}
	}

	push_el(\@main::el, 'dsk_quota_engine()', 'Ending...');

	return 0;

}


sub mysqldb_quota_by_mysql_engine {

	my ($rs, $ref, $rdata, $size, $databases) = (undef, undef, undef, undef, undef);

	push_el(\@main::el, 'mysqldb_quota_by_mysql_engine()', 'Starting...');

	my $sql = "SHOW DATABASES;";

	($rs, $databases) = doSQL($sql);

	$sql = "SELECT domain_id, domain_name, domain_disk_usage FROM domain WHERE domain_status = 'ok';";

	($rs, $ref) = doSQL($sql);

	return $rs if ($rs != 0);

	my $domain_ids = $ref;

	foreach (@$domain_ids) {

		my $disk_usage  = @$_[2];
		my $domain_name = @$_[1];
		my $domain_id   = @$_[0];
		my $sql = "SELECT sqld_id, sqld_name FROM sql_database WHERE domain_id = '$domain_id';";

		($rs, $ref) = doSQL($sql);

		my $sqldb_ids = $ref;

		$size = 0;

		foreach (@$sqldb_ids) {

			my $sqldb_name   = @$_[1];
			my $sqldb_id     = @$_[0];
			my $sql = "show table status from `$sqldb_name`;";

			if(grep{$$_[0] eq $sqldb_name} @$databases) {

				($rs, $ref) = doSQL($sql);
				my ($data_length, $index_length) = (0, 0);

				foreach(@$ref) {
					# Replace unknown values by 0
					$data_length  = (defined(@$_[6]) && @$_[6] =~ /^\d+$/) ? @$_[6] : 0;
					$index_length = (defined(@$_[8]) && @$_[8] =~ /^\d+$/) ? @$_[8] : 0;

					$size += $data_length + $index_length;
				}
			} else {

				send_error_mail('mysqldb_quota_by_mysql_engine()', "Database '$sqldb_name' exists only in table 'sql_database'!!!");

			}
		}

		if ($size > 0) {

			$size += $disk_usage;

			$sql = "UPDATE domain SET domain_disk_usage='$size' WHERE domain_id='$domain_id'";

			($rs, $rdata) = doSQL($sql);
			return $rs if ($rs != 0);

			$sql = "SELECT COUNT(name) AS cnt FROM quotatallies WHERE name = '$domain_name'";

			($rs, $rdata) = doSQL($sql);
			return $rs if ($rs != 0);

			$rdata = @$rdata[0];

			my $quota_ent = @$rdata[0];

			if ($quota_ent > 0) {

				$sql = "UPDATE quotatallies SET bytes_in_used = '$size' WHERE name = '$domain_name'";

				($rs, $rdata) = doSQL($sql);
				return $rs if ($rs != 0);

			}

		}
	}

	push_el(\@main::el, 'mysqldb_quota_by_mysql_engine()', 'Ending...');

	return 0;

}

sub mysqldb_quota_by_dir_engine {

	my ($rs, $ref, $rdata, $size) = (undef, undef, undef, undef);

	push_el(\@main::el, 'mysqldb_quota_by_dir_engine()', 'Starting...');

	my $sql = "SELECT domain_id, domain_name, domain_disk_usage FROM domain WHERE domain_status = 'ok';";

	($rs, $ref) = doSQL($sql);

	return $rs if ($rs != 0);

	my $cmd_du = $main::cfg{'CMD_DU'};

	my $domain_ids = $ref;

	foreach (@$domain_ids) {

		my $disk_usage  = @$_[2];

		my $domain_name = @$_[1];

		my $domain_id   = @$_[0];

		my $sql = "SELECT sqld_id, sqld_name FROM sql_database WHERE domain_id = '$domain_id';";

		($rs, $ref) = doSQL($sql);

		my $sqldb_ids = $ref;

		$size = 0;

		foreach (@$sqldb_ids) {

			my $sqldb_name   = @$_[1];

			my $sqldb_id     = @$_[0];

			# BSD/NUX Command
			if ($main::cfg{'ROOT_GROUP'} eq "wheel") {

				$size = `$cmd_du -I "*.frm" -I "db.opt" -d 0 $main::cfg{DATABASE_DIR}/$sqldb_name/* | tail -1`;

				$size =~ /^(\d+)/; $size = $1;

			} else {

				$size = `$cmd_du -csb --exclude=*.frm --exclude=db.opt $main::cfg{DATABASE_DIR}/$sqldb_name/* | tail -1`;

				$size =~ /^(\d+)/; $size = $1;

			}

		}


		if ($size > 0) {

			$size += $disk_usage;

			$sql = "UPDATE domain SET domain_disk_usage='$size' WHERE domain_id='$domain_id'";

			($rs, $rdata) = doSQL($sql);
			return $rs if ($rs != 0);

			$sql = "SELECT COUNT(name) AS cnt FROM quotatallies WHERE name = '$domain_name'";

			($rs, $rdata) = doSQL($sql);
			return $rs if ($rs != 0);

			$rdata = @$rdata[0];

			my $quota_ent = @$rdata[0];

			if ($quota_ent > 0) {

				$sql = "UPDATE quotatallies SET bytes_in_used = '$size' WHERE name = '$domain_name'";

				($rs, $rdata) = doSQL($sql);
				return $rs if ($rs != 0);

			}

		}
	}

	push_el(\@main::el, 'mysqldb_quota_by_dir_engine()', 'Ending...');

	return 0;

}

my $rs = undef;


$rs = dsk_quota_start_up();

if ($rs != 0) {

	dump_el(\@main::el, $main::ispcp_dsk_quota_el);

	dsk_quota_shut_down();
	exit 1;

}


$rs = dsk_quota_engine();

if ($rs != 0) {

	dump_el(\@main::el, $main::ispcp_dsk_quota_el);

	dsk_quota_shut_down();
	exit 1;

}

$rs = mysqldb_quota_by_mysql_engine();

if ($rs != 0) {

	dump_el(\@main::el, $main::ispcp_dsk_quota_el);

	dsk_quota_shut_down();
	exit 1;

}


$rs = dsk_quota_shut_down();

if ($rs != 0) {

	dump_el(\@main::el, $main::ispcp_dsk_quota_el);
	exit 1;

}

exit 0;

