#!/usr/bin/perl -w

=head1 NAME

dh_autoreconf_clean - Clean all changes made by dh_autoreconf

=cut

use strict;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<dh_autoreconf_clean> [S<I<debhelper options>>]

=head1 DESCRIPTION

dh_autoreconf_clean removes all files which have been created or changed
during the autoreconf call executed by L<dh_autoreconf(1)>.

=cut

init();

if (! -r 'debian/autoreconf.before' || ! -r 'debian/autoreconf.after') {
    exit 0;
}

# Mapping of filename => md5sum.
my %file = ();
# An array of the names of the files which should be removed.
my @delete = ();

# Read the old files in
open(FILE, 'debian/autoreconf.before') or die($!);

while(<FILE>) {
    chomp($_);
    # The delimiter here is a comma
    my ($checksum, $filename) = split;
    # Put the key => value pair in the hash
    $file{$filename} = $checksum;
}
close(FILE);

# Read the new files
open(FILE, 'debian/autoreconf.after') or die($!);

while(<FILE>) {
    chomp($_);
    my ($checksum, $filename) = split;
    # Mark file for deletion if its new or if it has changed and only if it is
    # not excluded and not inside debian/.
    push @delete, $filename if (!defined($file{$filename}) ||
                                $file{$filename} ne $checksum);
}
close(FILE);

# Cleanup
doit("rm", "-f", "--", @delete) if @delete;
doit("rm", "-f", "debian/autoreconf.before", "debian/autoreconf.after");

=head1 SEE ALSO

L<debhelper(7)>, L<dh_autoreconf(1)>, L<dh-autoreconf(7)>

=head1 AUTHOR

Julian Andres Klode <jak@debian.org>

=cut
