#!/scratchbox/tools/bin/perl -w

=head1 NAME

dh_installxfonts - register X fonts

=cut

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

=head1 SYNOPSIS

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

=head1 DESCRIPTION

dh_installxfonts is a debhelper program that is responsible for
registering X fonts, so their corresponding fonts.dir, fonts.alias,
and fonts.scale be rebuilt properly at install time.

Before calling this program, you should have installed any X fonts provided
by your package into the appropriate location in the package build
directory, and if you have fonts.alias or fonts.scale files, you should
install them into the correct location under etc/X11/fonts in your
package build directory.

Your package should depend on xfonts-utils so that the
update-fonts-* commands are available. (This program adds that dependency to
${misc:Depends}.)

This program automatically generates the postinst and postrm commands needed
to register X fonts. These commands are inserted into the maintainer
scripts by dh_installdeb. See L<dh_installdeb(1)> for an explanation of how
this works.

=head1 NOTES

See L<update-fonts-alias(8)>, L<update-fonts-scale(8)>, and
L<update-fonts-dir(8)> for more information about X font installation.

See Debian policy, section 11.8.5. for details about doing fonts the Debian
way.

=cut

init();

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

	# Find all font directories in the package build directory.
	my @fontdirs;
	foreach my $parentdir ("$tmp/usr/share/fonts/X11/") {
		opendir(DIR, $parentdir) || next;
		@fontdirs = grep { -d "$parentdir/$_" && !/^\./ } (readdir DIR);
		closedir DIR;
	}

	if (@fontdirs) {
		# Figure out what commands the postinst and postrm will need 
		# to call.
		my @cmds;
		my @cmds_postinst;
		my @cmds_postrm;
		foreach my $f (@fontdirs) {
			# This must come before update-fonts-dir.
			push @cmds, "update-fonts-scale $f"
				if -f "$tmp/etc/X11/fonts/$f/$package.scale";
			push @cmds, "update-fonts-dir --x11r7-layout $f";
			if (-f "$tmp/etc/X11/fonts/$f/$package.alias") {
				push @cmds_postinst, "update-fonts-alias --include /etc/X11/fonts/$f/$package.alias $f";
				push @cmds_postrm, "update-fonts-alias --exclude /etc/X11/fonts/$f/$package.alias $f";
				addsubstvar($package, "misc:Depends", "xfonts-utils (>= 1:7.5+2)");
			}
		}

		autoscript($package, "postinst", "postinst-xfonts",
			"s:#CMDS#:".join(";", @cmds, @cmds_postinst).":");
		autoscript($package, "postrm", "postrm-xfonts",
			"s:#CMDS#:".join(";", @cmds, @cmds_postrm).":");

		addsubstvar($package, "misc:Depends", "xfonts-utils");
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut
