#!/usr/bin/perl -w

# we've got a file filled with lines OLD_INODE FILENAME
# and a second file with lines NEW_INODE FILENAME

# the filenames are the same

# output OLD_INODE NEW_INODE  and maybe FILENAME

# usage: inode_merge.pl old_inodes new_indoes

# read in the old inodes, chopping off any leading whitespace. store in a hash
# keyed on filename then as the new inode-filename mapping is read, you can
# look up the old inode from the file name and there you go:  old inode, new
# inode, file name

if ( $#ARGV != 1 ) {
	die "usage: $0 old_inode_map new_inode_map\n";
}

open(OLD_INODES, $ARGV[0]);

while (<OLD_INODES> ) {
	$_ =~ s/^\s+//;
	($inode, $filename) = split(/ /);
	$old_inodes{$filename} = $inode;
}

close OLD_INODES;
open(NEW_INODES, $ARGV[1]);

while (<NEW_INODES>) {
	$_ =~ s/^\s+//;
	($inode, $filename) = split(/ /);
	if ( ! exists $old_inodes{$filename} ) {
		warn "no old inode: $inode $filename";
		# XXX: contiune the loop?  print out something?
	} else {
		printf ("%d %d %s", $old_inodes{$filename}, $inode, $filename);
		# clean out the hash so we can see later if any old files 
		# have no new inode
		delete $old_inodes{$filename};
	}
}

# chatty, what with all the 'no new inode' lines, but we hope not to have it
foreach $filename ( sort keys %old_inodes ) {
	warn "no new inode: $old_inodes{$filename} $filename";
}
