#!/usr/bin/perl -w
#
# Reads debian/docs, installs all files listed there into /usr/doc/$PACKAGE
# Also installs the debian/copyright and debian/README.debian and debian/TODO

BEGIN { push @INC, "debian", "/usr/lib/debhelper" }
use Dh_Lib;
init();

foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
	$TMP=tmpdir($PACKAGE);
	$file=pkgfile($PACKAGE,"docs");

	if ( ! -d "$TMP/usr/doc/$PACKAGE") {
		doit("install","-d","$TMP/usr/doc/$PACKAGE");
	}

	undef @docs;

	if ($file) {
		@docs=filearray($file);
	}

	if (($PACKAGE eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
		push @docs, @ARGV;
	}

	if (@docs) {
		doit("cp","-a",@docs,"$TMP/usr/doc/$PACKAGE/");
	}

	# .Debian is correct, according to policy, but I'm easy.
	$readme_debian=pkgfile($PACKAGE,'README.Debian');
	if (! $readme_debian) {
		$readme_debian=pkgfile($PACKAGE,'README.debian');
	}
	if ($readme_debian) {
		doit("install","-m","644","-p","$readme_debian","$TMP/usr/doc/$PACKAGE/README.Debian");
	}

	$todo=pkgfile($PACKAGE,'TODO');
	if ($todo) {
		if (isnative($PACKAGE)) {
			doit("install","-m","644","-p",$todo,"$TMP/usr/doc/$PACKAGE/TODO");
		}
		else {
			doit("install","-m","644","-p",$todo,"$TMP/usr/doc/$PACKAGE/TODO.Debian");
		}
	}

	# Support debian/package.copyright, but if not present, fall back
	# on debian/copyright for all packages, not just the main binary
	# package.
	$copyright=pkgfile($PACKAGE,'copyright');
	if (! $copyright && -e "debian/copyright") {
		$copyright="debian/copyright";
	}
	if ($copyright) {
			doit("install","-m","644","-p",$copyright,"$TMP/usr/doc/$PACKAGE/copyright");
	}
}
