#!/usr/bin/perl -n
# This is specifically for removing redundant files across my old backups.
# Its input it the output of "finddup".  It splits the groups into different
# categories w.r.t. a "base" directory.

use IO::File;

BEGIN {
	@ARGV or die "syntax: removable base-dir\n  e.g.:  removable ./sam\n";
	$base = shift;
	$base =~ s,/$,,;
	$one_in_base_file = IO::File->new("one_in_base", "w");
	$many_in_base_file = IO::File->new("many_in_base", "w");
	$none_in_base_file = IO::File->new("none_in_base", "w");
}

chomp;
if ($_ eq "") {
	# got a group
	# check for a file in $base (e.g. ./sam)
	@in_base = (); @out_base = ();
	for (@group) {
		if (m,^$base/,o) {
			push @in_base, $_;
		} else {
			push @out_base, $_;
		}
	}
	@group = (@in_base, @out_base);
	if (@in_base == 1) {
		# if there are files in , output the group to the "remove" file
		$out = $one_in_base_file;
	} elsif (@in_base) {
		$out = $many_in_base_file;
	} else {
		$out = $none_in_base_file;
	}
	for (@group) {
		print $out $_, "\n";
	}
	print $out "\n";
	# if there is not, output to the other file
	@group = ();
} else {
	push @group, $_
}
