#!/local/bin/perl5

use Quota;

print "Enter path to get quota for (NFS possible; default '.'): ";
chop($path = <STDIN>);
$path = "." unless $path =~ /\S/;
$dev = Quota::getqcarg($path) || die "$path: $!\n";
print "Using device/argument $dev\n";

##
##  Check if quotas present on this filesystem
##

if($dev =~ m#^[^/]+:#) {
  print "Is a remote file system\n";
}
elsif(Quota::sync($dev) && ($! != 1)) {
  die "Quota::sync: ".Quota::strerr."\n";
}
else {
  print "Quotas are present on this filesystem (sync ok)\n";
}

##
##  call with one argument (uid defaults to getuid()
##

($bc,$bs,$bh,$bt,$fc,$fs,$fh,$ft) = Quota::query($dev);
if(defined($bc)) {
  print "Your quota is $bc ($bs/$bh/$bt) $fc ($fs/$fh/$ft)\n\n";
}
else {
  warn "Quota::query: ",Quota::strerr,"\n\n";
}

##
##  call with two arguments
##

{
  print "Enter a uid to get quota for: ";
  chop($uid = <STDIN>);
  unless($uid =~ /^\d{1,5}$/) {
    print "You have to enter a numerical uid in range 0..65535 here.\n";
    redo;
  }
}

($bc,$bs,$bh,$bt,$fc,$fs,$fh,$ft) = Quota::query($dev, $uid);
if(defined($bc)) {
  print "Quota for $uid is $bc ($bs/$bh/$bt) $fc ($fs/$fh/$ft)\n\n";
}
else {
  warn Quota::strerr,"\n\n";
}

##
##  get quotas via RPC
##

if($dev =~ m#^/#) {
  print "Query localhost via RPC.\n";

  ($bc,$bs,$bh,$bt,$fc,$fs,$fh,$ft) = Quota::rpcquery('localhost', $path);
  if(defined($bc)) {
    print "Your Quota is $bc ($bs/$bh/$bt) $fc ($fs/$fh/$ft)\n\n";
  }
  else {
    warn Quota::strerr,"\n\n";
  }
  print "Query localhost via RPC for $uid.\n";

  ($bc,$bs,$bh,$bt,$fc,$fs,$fh,$ft) = Quota::rpcquery('localhost', $path, $uid);
  if(defined($bc)) {
    print "Quota for $uid is $bc ($bs/$bh/$bt) $fc ($fs/$fh/$ft)\n\n";
  }
  else {
    warn Quota::strerr,"\n\n";
  }

}
else {
  print "Skipping RPC query - already done above.\n\n";
}

##
##  set quota block & file limits for user
##

print "Enter path to set quota (empty to skip): ";
chop($path = <STDIN>);

if($path =~ /\S/) {
  print "New quota limits bs,bh,fs,fh for $uid (empty to abort): ";
  chop($in = <STDIN>);
  if($in =~ /\S/) {
    $dev = Quota::getqcarg($path) || die "$path: $!\n";
    unless(Quota::setqlim($dev, $uid, split(/\s*,\s*/, $in), 1)) {
      print "Quota set for $uid\n";
    }
    else {
      warn Quota::strerr,"\n";
    }
  }
}

##
##  Force immediate update on disk
##

if($dev !~ m#^[^/]+:#) {
  Quota::sync($dev) && ($! != 1) && die "Quota::sync: ".Quota::strerr."\n";
}

