package CloudStore::dedup;
use warnings;
use strict;
use autodie;

use CloudStore::JSON;
use File::Path qw(make_path);
use Data::Dump qw(dump);

sub path_md5 {
	my ( $path, $md5 ) = @_;

	my $pool = 'md5'; # FIXME sharding?
	mkdir $pool unless -e $pool;

	if ( -e "$pool/$md5" ) {
		warn "dedup hit $md5 $path\n";
		my $dedup = $path . '.dedup';
		rename $path, $dedup;
		link "$pool/$md5", $path;
		unlink $dedup;
	} else {
		link $path, "$pool/$md5";
	}
}

sub path_remove {
	my $path = shift;

	my $deleted = CloudStore::JSON::file_data( $path );

	my $md5_path = "md5/$deleted->{md5}";

	my ( undef, undef, undef, $nlink ) = stat $md5_path;

	warn "path_remove $path $nlink ",dump $deleted;
	open(my $fh, '>>', 'var/dedup.log');
	print $fh join(' ', $md5_path, $nlink, @_), $/;
	close $fh;
}

my $empty_md5 = " " x 32;

sub data {
	my $data = shift;

	my $blob = "users/$data->{login}/blob";
	my $path = "$blob/$data->{file}";

	return if -d $path;

	if ( $data->{file} =~ /^(.+\/)?md5sum$/ ) {
		my $dir = $1;
		my $imported = 0;
		warn "$path import\n";
		open(my $md5sum, '<', $path);
		while(<$md5sum>) {
			chomp;
			my ( $md5, $file ) = split(/\s+/,$_,2);
			if ( ! -e "md5/$md5" ) {
				warn "MISSING $md5 $file\n";
				next;
			}
			my $new = "users/$data->{login}/blob/$dir$file";
			if ( ! -e $new ) {
				# create path from md5sum file
				my $dir = $1 if $new =~ m{^(.+)/[^/]+$};
				make_path $dir unless -d $dir;
				$imported += link "md5/$md5", $new;
			} else {
				path_md5 $new => $md5;
			}
		}
		print "INFO: $path imported $imported files\n";
	}

	if ( $data->{md5} ne $empty_md5 ) {
		path_md5 $path => $data->{md5};
	} else {
		path_remove $path, $data->{timestamp};
	}
}

1;
