#!/usr/bin/perl

use Quota;

#print "---------------------------------
#Setting quotas for all domains
#---------------------------------\n";

## Set quota for group and user
## ($user, $newlimit)
sub set_quota {
	my($user, $newlimit) = @_;

	#print "Domain: " . $user . ", Limit: " . $newlimit . " MB\n";

	#Get current quota ------------------------------------------------------------------------
	$dev = Quota::getqcarg('/');
	$uid = getpwnam($user);
	$gid = getgrnam($user);
	($bc,$bs,$bh,$bt, $ic,$is,$ih,$it) = Quota::query($dev, $gid, 1);
	#print "Current limits:\n";
	#print "Current usage: " . $bc . "\n";
	#print "Soft limit: " . $bs . "\n";
	#print "Hard limit: " . $bh . "\n";
	#print "Block timelimit: " . $bt . "\n";
	#print "Inode current: " . $ic . "\n";
	#print "Inode soft: " . $is . "\n";
	#print "Inode hard: " . $ih . "\n";
	#print "Inode timelimit: " . $it . "\n";

	#Set new quota ------------------------------------------------------------------------
	$newbs = $newlimit * 1024;
	$newbh = $newlimit * 1024;
	$tlo = 0; # Set time limits to not started
	#print "New limits:\n";
	#print "Soft limit: " . $newbs . "\n";
	#print "Hard limit: " . $newbh . "\n";

	$currentUsage = sprintf("%.0f", $bc/1024);
	$oldQuota = sprintf("%.0f", $bs/1024);

	#Check that current usage is lower than newlimit, else mail admin about the problem
	if($bc >= $newbh) {
		print "ERROR: Quota - Not set. Account $user is over limit. New limit: $newlimit MB, Current usage: $currentUsage MB, Current quota: $oldQuota MB\n";
	} elsif($bs != $newbs || $bh != $newbh) {
		# Only set quota for uid and gid >= 1000
		if($uid >= 1000 && $gid >= 1000) {
			Quota::setqlim($dev, $uid, $newbs,$newbh, $is,$ih, $tlo, 0); #Set for user
			Quota::setqlim($dev, $gid, $newbs,$newbh, $is,$ih, $tlo, 1); #Set for group
			print "INFO: Quota - Set. New limit: $newlimit MB, User: $user, Current usage: $currentUsage MB, Old quota: $oldQuota MB\n";
		} else {
			print "ERROR: Quota - Not set. uid ($uid) and/or gid ($gid) is wrong\n";
		}
	} else {
		#print "INFO: Quota - Not set. Already correct quota for $user\n";
	}
}

##	Simple Email Function
##	($to, $from, $subject, $message)
sub sendEmail {
	my ($to, $from, $subject, $message) = @_;
	my $sendmail = '/usr/lib/sendmail';
 	open(MAIL, "|$sendmail -oi -t");
 	print MAIL "From: $from\n";
 	print MAIL "To: $to\n";
 	print MAIL "Subject: $subject\n\n";
 	print MAIL "$message\n";
 	close(MAIL);
} 

$dirname = "/etc/webmin/virtual-server/domains";

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {
	$file = $dirname . "/" . $file;

	if(-f $file) {
		#print $file . "\n";


		open(FILE, $file);
		while(<FILE>) {
			chomp;	
			($key, $value) = split("=");
			#print $key . "=" . $value . "\n";
			if($key eq "dom") {
				$dom = $value;
			} elsif($key eq "ugroup") {
				$ugroup = $value;
			} elsif($key eq "quota") {
				$quota = $value;
			}
		}
		close(FILE);

		#print "dom: " . $dom . " ugroup: " . $ugroup . " quota: " . $quota . "\n";

		if($dom eq $ugroup && $quota > 0) {
			$new_quota = $quota/1024;
			set_quota($ugroup, $new_quota);
		} else {
			## Don't set quota for subdomains
			#print "INFO: Subdomain: $dom to: $ugroup\n";
		}
		#print "------------\n";
	}
}
closedir(DIR);