#!/usr/bin/perl
use warnings;
use strict;

use Data::Dump qw(dump);

sub home_dir {
	my $login = shift;
	my ( undef, undef, $uid, $gid, undef, undef, $email, $dir, $shell ) = getpwnam $login;
	return $dir;
}

use Gearman::Worker;
my $worker = Gearman::Worker->new;
$worker->job_servers('127.0.0.1:4730');

die "missing ZSLICE from env" unless $ENV{ZSLICE};

sub zfs_get {
	my ($command,$job) = @_;
	my $work = $job->arg;
	chomp $work;


	my $zfs = sprintf 'zfs get -H -p -o value %s@%s %s', $command, $work, $ENV{ZSLICE};
	   $zfs = sprintf 'zfs userspace -H -p -o name,used,quota %s', $ENV{ZSLICE} if $command eq 'userspace';

	warn "# zfs_get $command [$work] -> $zfs\n";

	my $v = `$zfs`;

	warn "# $command $work = $v\n";

	return $v;
}

$worker->register_function( zfs_userused  => sub { zfs_get( 'userused'  => $_[0] ) } );

$worker->register_function( zfs_userquota => sub { zfs_get( 'userquota' => $_[0] ) } );

$worker->register_function( zfs_userspace => sub { zfs_get( 'userspace' => $_[0] ) } );

warn "$0 pid $$ waitng for jobs\n";
$worker->work while 1;

