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

use Data::Dump qw(dump);

use lib 'lib';
use CloudStore::Couchbase;

my $login = $ARGV[0] || die "usage: $0 login\n";

my $store = CloudStore::Couchbase->new;

my $offset = 0;
my $limit  = 100;

while(1) {
	my $r = $store->couchdb(
		'http://10.60.0.244:5984/files/_design/files/_view/login_usage?reduce=false&connection_timeout=60000&limit=%d&skip=%d&start_key="%s"&end_key="%s"', $limit, $offset, $login, $login
	) || die;

	last if $#{ $r->{rows} } < 1;

	foreach my $row ( @{ $r->{rows} } ) {
		my ( $login, $file ) = split(/:/,$row->{id},2);
		my $path = "users/$login/blob/$file";
		if ( -e $path ) {
			warn "+ $path\n";
		} else {
			$store->remove_file({
				login => $login,
				file  => $file,
				size  => $row->{value},
			});
			warn "- $path\n";
		}
	}

	$offset += $limit;

}

$store->usage_init( $login );

print dump( $store->user_get( $login ) );

