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

use TokyoCabinet;
use Data::Dump qw(dump);
use autodie qw(:all);

open(my $passwd, '<', '/var/lib/extrausers/passwd');
while(<$passwd>) {
	chomp;
	my ($login,undef,$uid,$gid,undef,$dir,undef) = split(/:/,$_,7);
	warn "# $login $uid $gid $dir";

	my $md5 = "$dir/.md5";
	if ( ! -e $md5 ) {
		warn "ERROR: $login doesn't have $md5: $!";
		next;
	}

	my %h;
	tie %h, "TokyoCabinet::HDB", $md5 || die "ERROR $md5: $!";

	while( my($k,$v) = each(%h) ) {
		#print "$v  $k\n";
		if ( -e "$dir/$k" ) {
			system 'attr', '-s', 'md5', '-V', $v, "$dir/$k";
		} else {
			warn "SKIP $dir/$k\n";
		}
	}

	untie %h;

	unlink $md5;
}
