#!/usr/bin/perl

=head1 NAME

dh_auto_install - automatically runs make install or similar

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;
use Debian::Debhelper::Dh_Buildsystems;
use File::Spec;
use Cwd;

our $VERSION = DH_BUILTIN_VERSION;

=head1 SYNOPSIS

B<dh_auto_install> [S<I<build system options>>] [S<I<debhelper options>>] [S<B<--> I<params>>]

=head1 DESCRIPTION

B<dh_auto_install> is a debhelper program that tries to automatically install
built files. It does so by running the appropriate command for the build
system it detects the package uses. For example, if there's a F<Makefile> and
it contains a B<install> target, then this is done by running B<make> (or B<MAKE>,
if the environment variable is set). If there is a F<setup.py> or F<Build.PL>,
it is used. Note that the Ant build system does not support installation,
so B<dh_auto_install> will not install files built using Ant.

Unless B<--destdir> option is specified, the files are installed into
debian/I<package>/ if there is only one binary package. In the multiple binary
package case, the files are instead installed into F<debian/tmp/> (or
F<< debian/tmp-I<label> >> with B<--buildlabel>), and should be moved from
there to the appropriate package build directory using L<dh_install(1)>.

B<DESTDIR> is used to tell make where to install the files. 
If the Makefile was generated by MakeMaker from a F<Makefile.PL>, it will
automatically set B<PREFIX=/usr> too, since such Makefiles need that.

This is intended to work for about 90% of packages. If it doesn't work, or
tries to use the wrong install target, you're encouraged to skip using
B<dh_auto_install> at all, and just run make install manually.

=head1 OPTIONS

See L<debhelper(7)/B<BUILD SYSTEM OPTIONS>> for a list of common build
system selection and control options.

=over 4

=item B<--buildlabel=>I<label>

Set the label for this command.  If all packages related to the chosen label is
skipped, the command will skip the build.

If omitted, the command behaves like B<--buildlabel> was set to I<default>.  The
I<default> buildlabel uses F<debian/tmp> as default destination directory.

=item B<--destdir=>I<directory>

Install files into the specified I<directory>. If this option is not specified,
destination directory is determined automatically as described in the
L</B<DESCRIPTION>> section.

Other tools B<debhelper> tools that support the B<--sourcedir> option will
default to using the specified directory for packages related to the selected
buildlabel.

=item B<--> I<params>

Pass I<params> to the program that is run, after the parameters that
B<dh_auto_install> usually passes.

=back

=cut

my ($destdir, $orig_dest_dir);

buildsystems_init(options => {
	"destdir=s" => \$destdir,
});

# If destdir is not specified, determine it automatically
if (!$destdir) {
	my @allpackages=getpackages();
	if (@allpackages > 1) {
		$destdir="debian/tmp";
		$destdir="debian/tmp-$dh{BUILDLABEL}" if $dh{BUILDLABEL} ne 'default';
	}
	else {
		$destdir=tmpdir($dh{MAINPACKAGE});
	}
} else {
	$orig_dest_dir = $destdir;
}
$destdir = File::Spec->rel2abs($destdir, cwd());

if (compat(10)) {
	# Ensure that all debian/<pkg> directories exist
	install_dir(map { tmpdir($_) } @{$dh{DOPACKAGES}});
} else {
	install_dir($destdir);
}

buildsystems_do("install", $destdir);

my $label_dir = "debian/.debhelper/buildlabels/$dh{BUILDLABEL}";
if (defined($orig_dest_dir)) {
	# only set this, if we are not using the default
	install_dir($label_dir);
	open(my $fd, '>', "${label_dir}/install-dir") or error("open(${label_dir}/install-dir): $!");
	print {$fd} "$orig_dest_dir\n";
	close($fd) or error("close(${label_dir}/install-dir): $!");
} else {
	rm_files("${label_dir}/install-dir");
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut
