#!/usr/bin/perl

=head1 NAME

dh_cruft - install cruft files into package build directories

=cut

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

our $VERSION = DH_BUILTIN_VERSION;

=head1 SYNOPSIS

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

=head1 DESCRIPTION

B<dh_cruft> is a debhelper program that is responsible for installing
information files used by cruft(-ng) into package build directories.

=head1 FILES

=over 4

=item debian/I<package>.cruft

Installed into usr/lib/cruft/filters-unex/I<package> in the package
build directory. This file is used to register runtime files
as belonging to this package

=item debian/I<package>.purge

Appended to usr/lib/cruft/filters-unex/I<package> in the package
build directory. This file is used to register runtime files
as belonging to this package.

Also, a snippet is appended to F<postrm>
with the needed I<rm -rf> commands
to remove these files on I<purge> phase.

=back

=cut

init();

# PROMISE: DH NOOP WITHOUT cruft purge cli-options()

foreach my $package (@{$dh{DOPACKAGES}}) {
	next if is_udeb($package);

	my $tmp = tmpdir($package);
	my $cruft_dir = "$tmp/usr/lib/cruft/filters-unex/";
	my $cruft = pkgfile($package, "cruft");

	my @purgelines = ();
	my $purge = pkgfile($package, "purge");
	my @lines;
	if ($purge ne '') {
		open (IN, $purge);
		@lines = <IN>;
		close IN;
		foreach my $line (@lines)
		{
			chomp $line;
			push @purgelines, "    rm -rf ${line}" if $line =~ /^\//;
		}
		autoscript($package, "postrm", "postrm-cruft", { 'PURGE' => @purgelines});

		if ($cruft eq '') {
			install_dir($cruft_dir);
			install_dh_config_file($purge, "$cruft_dir/$package");
		}
	}

	if ($cruft ne '') {
		install_dir($cruft_dir);
		install_dh_config_file($cruft, "$cruft_dir/$package");
		if (@lines) {
			open(APPEND, ">>", "$cruft_dir/$package");
			foreach my $line (@lines)
			{
				print APPEND $line . "\n";
			}
			close APPEND;
		}
	}
}

=head1 SEE ALSO

L<debhelper(1)>

This program is a part of debhelper.

L<cruft(1)>

=head1 AUTHOR

Alexandre Detistes <alexandre.detiste@gmail.com>

=cut
