#!/usr/bin/env/perl

# deduplcate repeated words from .[ch] files

my $word = '\b[A-Z]?[a-z]{2,}\b';

for my $filename (@ARGV) {
    my $oldline = '';
    my @output = ();

    if ($filename eq '-') {
	open($FILE, '<&STDIN');
    } else {
	open($FILE, '<', "$filename") ||
	    die "$P: $filename: open failed - $!\n";
    }

    while (<$FILE>) {
	my $newline = $_;
	while ($newline =~ /\b($word) (?=($word))/g) {

	    $first = $1;
	    $second = $2;

	    if ($first =~ /(?:struct|union|enum)/) {
		pos($newline) += length($first) + length($second) + 1;
		next;
	    }

	    next if ($first ne $second);
	    next if ($first eq 'long');

	    $newline =~ s/\b$first $second\b/$first/;
	}

	# if it's a repeated word on consecutive lines in a comment block
	if ($oldline =~ /($word)\s*$/) {
	    my $last_word = $1;
	    $newline =~ s/(^\s*\*\s*)$last_word /$1/;
	}

	push (@output, $newline);

	$oldline = $newline;
    }

    close($FILE);

    if ($filename eq '-') {
	open($FILE, '<&STDOUT');
    } else {
	open($FILE, '>', "$filename") ||
	    die "$P: $filename: open failed - $!\n";
    }
    foreach my $line (@output) {
	print $FILE $line;
    }

    close ($FILE);
}
