#!/usr/bin/perl

# Copyright (C) 2000 NEC Corporation
# All Rights Reserved.

# RCS: $Id: wbmcedquota,v 1.1 2006/12/22 05:33:58 shodai Exp $

##010418 v4.0.1 饹б  S.Oku
##021026 v5.0 edquota ѹб oyama

require "/opt/nec/wbmc/lib/wbmc.pl";
$file = $ARGV[0];

$home = $ENV{'WBMC_QUOTA_HOME'};
$mail = $ENV{'WBMC_QUOTA_MAIL'};
my($i);

#####################################################################
# /home -> $homedir
&readFile("/etc/default/useradd", "useradd", *lines);
for ($i = 0; $i <= $#lines; $i++) {
	if ($lines[$i] =~ /^HOME=(.+)/) {
		$homedir = $1;
		$homedir =~ s/\s+$//;
	}
}

### 饹б 010417 
#INITCONF.INI WorkModeMountPointȤäƤ
&readFile("/etc/opt/nec/wbmc/INITCONF.INI", "INITCONF.INI", *lines);
$workmode_find = "";
$mountpoint_find = "";
for ($i = 0; $i <= $#lines; $i++) {
	if ($lines[$i] =~ /WorkMode\s*=\s*MailCluster/) {
		$workmode_find = 1;
	}
	elsif ($lines[$i] =~ /MountPoint\s*=\s*(.*)/) {
		$mountpoint_find = 1;
	}
	# ե륪з֤λ$homedirMountPointͤ
	if($workmode_find && $mountpoint_find) {
		$homedir = $1;
                last;
	}
}
## ޤ

$homefs = &getQuotaFS("$homedir");

$mailfs = &getQuotaFS("/var/spool/mail");

open(IN, $file);
@line = <IN>;
close(IN);

for ($i = 0; $i <= $#line; $i++) {
#ѹб 20021026 oyama
 $line[$i] =~ s/^\s*//;
  $line[$i] =~ s/(\s)+/$1/g;
  @ent = split(/ /, $line[$i]);
  if ($ent[0] eq $homefs) {
    $line[$i] =~ /\s*$homefs\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
    $line[$i] = "$homefs $ent[1] $ent[2] $home $ent[4] $ent[5] $ent[6]";
    $home_quota = $3;
  }
#    @ent = split(/:/, $line[$i]);
#    if ($ent[0] eq $homefs) {
#        $line[$i] =~ /^$homefs: blocks in use: (\d+), limits \(soft = (\d+), hard = (\d+)\)$/;
#        $line[$i] = "$homefs: blocks in use: $1, limits (soft = $2, hard = $home)\n";
#	$home_quota = $3;
#    }

    if ($ent[0] eq $mailfs) {
#        $line[$i] =~ /^$mailfs: blocks in use: (\d+), limits \(soft = (\d+), hard = (\d+)\)$/;
#        $line[$i] = "$mailfs: blocks in use: $1, limits (soft = $2, hard = $mail)\n";
        $line[$i] =~ /\s*$mailfs\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
        $line[$i] = "$mailfs $ent[1] $ent[2] $mail $ent[4] $ent[5] $ent[6]";
	$mail_quota = $3;
    }
}

if ($home eq "" && $mail eq "") {
	print "home=$home_quota\n";
	print "mail=$mail_quota\n";
	exit(1);
}

open(OUT, ">$file");
print OUT @line;
close(OUT);
exit(1);

sub getQuotaFS
{
    my($dir) = @_;
    my($fs);

    open(IN, "/bin/df $dir 2>&1 |");
    <IN>;
    $fs = (split(/\s+/, <IN>))[0];
    close(IN);

    return ($fs);
}

