#!/usr/bin/perl

=head1 NAME

dh_installdeb - install files into the DEBIAN directory

=cut

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

=head1 SYNOPSIS

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

=head1 DESCRIPTION

B<dh_installdeb> is a debhelper program that is responsible for installing
files into the F<DEBIAN> directories in package build directories with the
correct permissions.

=head1 FILES

=over 4

=item I<package>.postinst

=item I<package>.preinst

=item I<package>.postrm

=item I<package>.prerm

These maintainer scripts are installed into the F<DEBIAN> directory.

Inside the scripts, the token B<#DEBHELPER#> is replaced with
shell script snippets generated by other debhelper commands.

=item I<package>.triggers

=item I<package>.shlibs

These control files are installed into the F<DEBIAN> directory.

Note that I<package>.shlibs is only installed in compat level 9 and
earlier.  In compat 10, please use L<dh_makeshlibs(1)>.

=item I<package>.conffiles

This control file will be installed into the F<DEBIAN> directory.

In v3 compatibility mode and higher, all files in the F<etc/> directory in a
package will automatically be flagged as conffiles by this program, so
there is no need to list them manually here.

=item I<package>.maintscript

Lines in this file correspond to L<dpkg-maintscript-helper(1)> commands and
parameters.  Any shell metacharacters will be escaped, so arbitrary shell
code cannot be inserted here.  For example, a line such as C<mv_conffile
/etc/oldconffile /etc/newconffile> will insert maintainer script snippets
into all maintainer scripts sufficient to move that conffile.

=back

=cut

init();

# dpkg-maintscript-helper commands with their associated dpkg pre-dependency
# versions.
my %maintscript_predeps = (
	"rm_conffile" => "",
	"mv_conffile" => "",
	"symlink_to_dir" => "1.17.14",
	"dir_to_symlink" => "1.17.13",
);

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp=tmpdir($package);

	if (! -d "$tmp/DEBIAN") {
		install_dir("$tmp/DEBIAN");
	}

	if (is_udeb($package)) {
		# For udebs, only do the postinst, and no #DEBHELPER#.
		# Udebs also support menutest and isinstallable scripts.
		foreach my $script (qw{postinst menutest isinstallable}) {
			my $f=pkgfile($package,$script);
			if ($f) {
				install_prog($f, "$tmp/DEBIAN/$script");
			}
		}

		# stop here for udebs
		next;
	}
	
	my $maintscriptfile=pkgfile($package, "maintscript");
	if ($maintscriptfile) {
		foreach my $line (filedoublearray($maintscriptfile)) {
			my $cmd=$line->[0];
			error("unknown dpkg-maintscript-helper command: $cmd")
				unless exists $maintscript_predeps{$cmd};
			addsubstvar($package, "misc:Pre-Depends", "dpkg",
				    ">= $maintscript_predeps{$cmd}")
			    	if length $maintscript_predeps{$cmd};
			my $params=escape_shell(@$line);
			foreach my $script (qw{postinst preinst prerm postrm}) {
				autoscript($package, $script, "maintscript-helper",
					   "s!#PARAMS#!$params!g");
			}
		}
	}

	# Install debian scripts.
	foreach my $script (qw{postinst preinst prerm postrm}) {
		debhelper_script_subst($package, $script);
	}

	# Install non-executable files
	my @non_exec_files = (qw{conffiles});
	# In compat 10, we let dh_makeshlibs handle "shlibs".
	push(@non_exec_files, 'shlibs') if compat(9);
	foreach my $file (@non_exec_files) {
		my $f=pkgfile($package,$file);
		if ($f) {
			install_file($f, "$tmp/DEBIAN/$file");
		}
	}

	install_triggers($package, $tmp);

	# Automatic conffiles registration: If it is in /etc, it is a
	# conffile.
	if ( -d "$tmp/etc") {
		complex_doit("find $tmp/etc -type f -printf '/etc/%P\n' | LC_ALL=C sort >> $tmp/DEBIAN/conffiles");
		# Anything found?
		if (-z "$tmp/DEBIAN/conffiles") {
			doit("rm","-f","$tmp/DEBIAN/conffiles");
		}
		else {
			doit("chmod","0644","$tmp/DEBIAN/conffiles");
		}
	}
}

sub install_triggers {
	my ($package, $tmp) = @_;
	my $generated = generated_file($package, 'triggers', 0);
	my @sources = grep { -f $_ } (
		pkgfile($package, 'triggers'),
		$generated,
	);
	my $target = "$tmp/DEBIAN/triggers";
	return if not @sources;
	if (@sources > 1) {
		my $merged = "${generated}.merged";
		open(my $ofd, '>', $merged)
			or error("open ${target} failed: $!");
		for my $src (@sources) {
			open(my $ifd, '<', $src)
				or error("open ${src} failed: $!");
			print {$ofd} $_ while <$ifd>;
			close($ifd);
		}
		close($ofd)	or error("close ${merged} failed: $!");
		@sources = ($merged);
	}
	install_file($sources[0], $target);
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut

# Local Variables:
# indent-tabs-mode: t
# tab-width: 4
# cperl-indent-level: 4
# End:
