#!/usr/bin/perl -w

=head1 NAME

dh_di_numbers - install numbered scripts into package build directories

=cut

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

=head1 SYNOPSIS

B<dh_di_numbers> [S<B<debhelper options>>] [S<I<dir [...] dest>>]

=head1 DESCRIPTION

dh_di_numbers is a debhelper program that installs directories of numbered
scripts into package build directories.

Within each directory, there should be a C<_numbers> file in which each line
contains at least two space-separated fields. The first field in each line
is a two-digit number, which will be prefixed to the entry name to provide
ordering within the directory; the second is the entry name, which must
correspond to a file or directory alongside the C<_numbers> file; and the
third field, if present, is another two-digit number which identifies
another entry with the same entry name to which this entry should be a
symlink.

Any additional directory names specified as parameters will be installed in
the package build directory of the first package dh_di_numbers is told to
act on. By default, this is the first binary package in debian/control, but
if you use -p, -i, or -a flags, it will be the first package specified by
those flags.

=head1 FILES

=over 4

=item debian/I<package>.di-numbers

List the directories to install into each package and the directory they
should be installed to. The format is a set of lines, where each line lists
a directory or directories to install, and at the end of the line tells the
directory they should be installed in. The name of the directories to
install should be given relative to the current directory, while the
installation directory is given relative to the package build directory. You
may use wildcards in the names of the files to install (in v3 mode and
above).

=back

=head1 OPTIONS

=over 4

=item I<dir [...] dest>

Lists directories to install and where to install them to. The files will be
installed into the first package dh_di_numbers acts on.

=back

=cut

sub install_dir {
	my $tmp = shift;
	my $dir = shift;
	my $dest = shift;
	$dest = "$tmp/$dest/" . basename($dir);
	doit("install", "-d", $dest);

	my %numbers;
	my %links;
	local (*NUMBERS, $_);
	if (open NUMBERS, "$dir/_numbers") {
		while (<NUMBERS>) {
			my @fields = split;
			if (@fields < 2) {
				next;
			} elsif (@fields == 2) {
				push @{$numbers{$fields[1]}}, $fields[0];
			} else {
				push @{$links{$fields[1]}}, [@fields[0, 2]];
			}
		}
	}
	close NUMBERS;

	foreach my $file (glob("$dir/*")) {
		my $base = basename($file);
		next if $base eq "_numbers";
		if (exists $numbers{$base}) {
			for my $number (@{$numbers{$base}}) {
				doit("cp", "-r", $file,
				     "$dest/$number$base");
			}
		} else {
			doit("cp", "-r", $file, "$dest/$base");
		}
		if (exists $links{$base}) {
			for my $linkpair (@{$links{$base}}) {
				my ($link, $target) = @$linkpair;
				doit("ln", "-s", "$target$base",
				     "$dest/$link$base");
			}
		}
	}
}

init();

# PROMISE: DH NOOP WITHOUT di-numbers

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp = tmpdir($package);
	my $file = pkgfile($package,"di-numbers");

	my @di_numbers;
	if ($file) {
		@di_numbers = filedoublearray($file, '.');
	}

	if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
		push @di_numbers, [@ARGV];
	}

	foreach my $set (@di_numbers) {
		if (@$set <= 1) {
			error("$file or parameters lists a directory without a destination.");
		}
		my $dest = pop @$set;

		foreach my $src (@$set) {
			if (-d $src) {
				install_dir($tmp, $src, $dest);
			}
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of dh-di.

=head1 AUTHOR

Colin Watson <cjwatson@debian.org>

=cut
