#!/usr/bin/perl -w

=head1 NAME

dh_installchangelogs - install changelogs into package build directories

=cut

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

=head1 SYNOPSIS

B<dh_installchangelogs> [S<I<debhelper options>>] [B<-k>] [B<-X>I<item>] [I<upstream>]

=head1 DESCRIPTION

B<dh_installchangelogs> is a debhelper program that is responsible for
installing changelogs into package build directories.

An upstream F<changelog> file may be specified as an option. If none is
specified, it looks for files with names that seem likely to be changelogs.
(In compatibility level 7 and above.)

If there is an upstream F<changelog> file, it will be be installed as
F<usr/share/doc/package/changelog> in the package build directory. If the
changelog is a F<html> file (determined by file extension), it will be
installed as F<usr/share/doc/package/changelog.html> instead, and will be
converted to plain text with B<html2text> to generate
F<usr/share/doc/package/changelog>.

=head1 FILES

=over 4

=item F<debian/changelog>

=item F<debian/NEWS>

=item debian/I<package>.changelog

=item debian/I<package>.NEWS

Automatically installed into usr/share/doc/I<package>/
in the package build directory.

Use the package specific name if I<package> needs a different
F<NEWS> or F<changelog> file.

The F<changelog> file is installed with a name of changelog
for native packages, and F<changelog.Debian> for non-native packages.
The F<NEWS> file is always installed with a name of F<NEWS.Debian>.

=back

=head1 OPTIONS

=over 4

=item B<-k>, B<--keep>

Keep the original name of the upstream changelog. This will be accomplished
by installing the upstream changelog as F<changelog>, and making a symlink from
that to the original name of the F<changelog> file. This can be useful if the
upstream changelog has an unusual name, or if other documentation in the
package refers to the F<changelog> file.

=item B<-X>I<item>, B<--exclude=>I<item>

Exclude upstream F<changelog> files that contain I<item> anywhere in their
filename from being installed.

=item I<upstream>

Install this file as the upstream changelog.

=back

=cut

# Install a changelog into a package.
# For binNMUs the first changelog entry is written into an extra file to keep
# the packages coinstallable.
sub install_changelog
{
	my ($package, $input_fn, $changelog_name)=@_;

	my $arch=package_arch($package);
	my $tmp=tmpdir($package);
	my $output_fn="$tmp/usr/share/doc/$package/$changelog_name";
	my $mask=umask 0022;
	my @stat=stat $input_fn or error("could not stat $input_fn: $!");

	open my $input, "<", $input_fn
		or error("could not read $input_fn: $!");
	open my $output, ">", $output_fn
		or error("could not open $output_fn for writing: $!");

	my $line=<$input>;
	if ($line =~ /\A\S.*;.*\bbinary-only=yes/) {
		my $output_fn_binary="$output_fn.$arch";
		open my $output_binary, ">", $output_fn_binary
			or error("could not open $output_fn_binary for writing: $!");
		do {
			print {$output_binary} $line
				or error("Could not write to $output_fn_binary: $!");
		} while(defined($line=<$input>) && $line !~ /\A\S/);
		close $output_binary or error("Couldn't close $output_fn_binary: $!");
		utime $stat[8], $stat[9], $output_fn_binary;
	}

	do {
		print {$output} $line
			or error("Could not write to $output_fn: $!");
	} while(defined($line=<$input>));

	close $input or error("Couldn't close $input_fn: $!");
	close $output or error("Couldn't close $output_fn: $!");
	utime $stat[8], $stat[9], $output_fn;

	umask $mask;
}

init();

my $upstream=shift;
my $changelog_name="changelog.Debian";
if (! defined $upstream) {
	if (! isnative($dh{MAINPACKAGE}) && !compat(6)) {
		foreach my $dir (qw{. doc docs}) {
			my @files=sort glob("$dir/*");
			foreach my $name (qw{changelog changes changelog.txt changes.txt history history.txt}) {
				my @matches=grep {
					lc basename($_) eq $name && -s $_ && ! excludefile($_)
				} @files;
				if (@matches) {
					$upstream=shift @matches;
					last;
				}
			}
		}
	}
	if (isnative($dh{MAINPACKAGE})) {
		$changelog_name='changelog';
	}
}
my $news_name="NEWS.Debian";

foreach my $package (@{$dh{DOPACKAGES}}) {
	next if is_udeb($package);
	
	my $tmp=tmpdir($package);
	my $changelog=pkgfile($package,"changelog");
	my $news=pkgfile($package,"NEWS");

	if (!$changelog) {
		$changelog="debian/changelog";
	}
	if (!$news) {
		$news="debian/NEWS";
	}

	if (! -e $changelog) {
		error("could not find changelog $changelog");
	}

	# If it is a symlink to a documentation directory from the same
	# source package, then don't do anything. Think multi-binary
	# packages that depend on each other and want to link doc dirs.
	if (-l "$tmp/usr/share/doc/$package") {
		my $linkval=readlink("$tmp/usr/share/doc/$package");
		my %allpackages=map { $_ => 1 } getpackages();
		if ($allpackages{basename($linkval)}) {
			next;
		}
		# Even if the target doesn't seem to be a doc dir from the
		# same source package, don't do anything if it's a dangling
		# symlink.
		next unless -d "$tmp/usr/share/doc/$package";
	}

	if (! -d "$tmp/usr/share/doc/$package") {
		doit("install","-d","$tmp/usr/share/doc/$package");
	}
	install_changelog($package, $changelog, $changelog_name);
	if (-e $news) {
		doit("install","-o",0,"-g",0,"-p","-m644",$news,
			"$tmp/usr/share/doc/$package/$news_name");
	}

	if (defined $upstream) {
		my $link_to;
		if ($upstream=~m/\.html?$/i) {
			# HTML changelog
			doit("install","-o",0,"-g",0,"-p","-m644",
				$upstream,"$tmp/usr/share/doc/$package/changelog.html");
			doit("html2text","-nobs","-o","$tmp/usr/share/doc/$package/changelog",$upstream);
			$link_to='changelog.html';
		}
		else {
			doit("install","-o",0,"-g",0,"-p","-m644",
				$upstream,"$tmp/usr/share/doc/$package/changelog");
			$link_to='changelog';
		}
		if ($dh{K_FLAG}) {
			# Install symlink to original name of the upstream changelog file.
			# Use basename in case original file was in a subdirectory or something.
			doit("ln","-sf",$link_to,"$tmp/usr/share/doc/$package/".basename($upstream));
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut
