#!/usr/bin/perl -w
#
# Script to be called from debian/rules to setup all the debian specifc
# required files
# Christoph Lameter, <clameter@debian.org> October 10, 1996
#
# All the parameters are documentation files to be installed.
# (but doc files can also be listed in debian/docs)
#
# This has been gutted and extensively rewritten to function as a debhelper
# command by Joey Hess. And then completly rewritten in perl.

# Need to stay compatable with debstd, so force use of level 1.
$ENV{DH_COMAPT}=1;

# Pre-parse command line before we load Dh_lib, becuase we use a
# different style of arguments.
@argv=();
foreach (@ARGV) {
	if ($_ eq '-p') {
		$ds{PERMS}=1;
	}
	elsif ($_ eq '-u') {
		$ds{UNDOC}=1;
	}
	elsif ($_ eq '-s') {
		$ds{SUMS}=1;
	}
	elsif ($_ eq '-m') {
		$ds{NOAUTOMAN}=1;
	}
	elsif ($_ eq '-c') {
		$ds{NOCOMPRESS}=1;
	}
	else {
		push @argv,$_;
	}
}
@ARGV=@argv;

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

# Tolerate old style debstd invocations
if ($ARGV[0] && $dh{FIRSTPACKAGE} eq $ARGV[0]) {
	shift;
}

# debinit handles the installation of an init.d script
sub debinit { my ($script, $filename, $package, @params)=@_;
	@initparams=();
	$norestart='';
	open (IN,$filename) || warn("$filename: $!");
	while (<IN>) {
		if (/^FLAGS=(.*)/) {
			push @initparams, $1;
		}
		if (/NO_RESTART_ON_UPGRADE/) {
			$norestart='--no-restart-on-upgrade';
		}
	}
	close IN;
	$initparams='';
	if (@initparams) {
		$initparams="--update-rcd-params='".join(" ",@initparams)."'";
	}

	doit("dh_installinit",$norestart,"-p$package",$initparams,"--init-script=$script",@params);
}

# Do package specific things for a package.
sub do_package { my ($package, $tmp, $prefix)=@_;
	# Deal with scripts in etc directories
	if (-d "$prefix/rc.boot") {
		warning("file $prefix/rc.boot was ignored.");
	}

	# etc files that could need some tweaking
	foreach $f ('services','inittab','crontab','protocols','profile',
		'shells','rpc','syslog.conf','conf.modules','modules',
		'aliases','diversions','inetd.conf','X11/Xresources',
		'X11/config','X11/window-managers','X11/xinit','purge') {
		if ( -f "$prefix$f") {
			warning("file $prefix$f was ignored.");
		}
	}

	if (-f "${prefix}init.d") {
		debinit($package,"${prefix}init.d",$package,"");
	}

	# The case of a daemon without the final d
	if (-f "${prefix}init") {
		$p=$package;
		if ($p=~s/d$//) {
			debinit($p,"${prefix}init",$package,"--remove-d");
		}
	}

	if (-f "${prefix}info") {
		warning("debhelper does not yet support info files, so ${prefix}info was ignored.");
	}

	# Set up undocumented man page symlinks.
	if (defined($ds{UNDOC}) && $ds{UNDOC}) {
		open (FIND,"find $tmp -type f -perm +111 2>/dev/null |") || warning("find: $!");
		while (<FIND>) {
			chomp;
			($binpath, $binname)=m:$tmp/(.*)/(.*):;
	
			# Check if manpages exist
			$section='';
			if ($binpath eq 'sbin' || $binpath eq 'usr/sbin') {
				$section=8;
			}
			elsif ($binpath eq 'usr/X11R6/bin') {
				$section='1x';
			}
			elsif ($binpath eq 'bin' || $binpath eq 'usr/bin') {
				$section=1;
			}
			elsif ($binpath eq 'usr/games') {
				$section=6;
			}	
			if ($section && `find $tmp/usr/share/man $tmp/usr/X11R6/man -name "$binname.*" 2>/dev/null` eq '') {
				doit("dh_undocumented","-p$package","$binname.$section");
			}
		}
		close FIND;
	}
}

# Special case of changelog
$changelogfile='';
if ($ARGV[0] && $ARGV[0]=~m/change|news|history/) {
	$changelogfile=shift;
}

doit("dh_installdirs"); # here just to make the debian/tmp, etc directories.
doit("dh_installdocs",@ARGV);
doit("dh_installexamples");
if ($changelogfile) {
	doit("dh_installchangelogs",$changelogfile);
}
else {
	doit("dh_installchangelogs");
}
doit("dh_installmenu");
doit("dh_installcron");

# Manpage scan
if (! $ds{NOAUTOMAN}) {
	doit("dh_installmanpages","-p$dh{FIRSTPACKAGE}");
}

# Per-package stuff:
foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
	if ($PACKAGE eq $dh{FIRSTPACKAGE}) {
		if (-f "debian/clean") {
			warning("file debian/clean ignored.");
		}
		do_package($PACKAGE,"debian/tmp","debian/");
	}
	else {
		do_package($PACKAGE,"debian/$PACKAGE","debian/$PACKAGE.");
		if ( -x "debian/$PACKAGE.prebuild") {
			warning("file debian/$PACKAGE.prebuild ignored.");
		}
	}
}

doit("dh_movefiles");
doit("dh_strip");

if (! $ds{NOCOMPRESS}) {
	doit("dh_compress");
}

doit("dh_fixperms");
doit("dh_suidregister");
doit("dh_shlibdeps");
doit("dh_gencontrol");
doit("dh_makeshlibs");

# Check to see if the install scripts have #DEBHELPER# in them, if not,
# warn.
@filelist=();
foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
	foreach $file (qw{postinst postrm preinst prerm}) {
		$f=pkgfile($PACKAGE,$file);
		if ($f) {
			open (IN,$f);
			$found=undef;
			while (<IN>) {
				if (/#DEBHELPER#/) {
					$found=1;
					last;
				}
			}
			if (! $found) {
				push @filelist, $f;
			}
		}
	}
}
if (@filelist) {
	warning("The following scripts do not contain \"#DEBHELPER#\" in them,");
	warning("and so debhelper will not automatically add commands to them:");
	warning(join(" ",@filelist));
}

doit("dh_installdeb");

if (! $ds{SUMS}) {
	doit("dh_md5sums");
}

# This causes the main binary package to be built, which
# real debstd does not do. Shouldn't be a problem though,
# if that package gets built twice.
doit("dh_builddeb");
