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

=head1 NAME

dh_movefiles - move files out of debian/tmp into subpackages

=cut

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

=head1 SYNOPSIS

B<dh_movefiles> [S<I<debhelper options>>] [B<--sourcedir=>I<dir>] [B<-X>I<item>] S<I<file ...>>]

=head1 DESCRIPTION

dh_movefiles is a debhelper program that is responsible for moving files
out of debian/tmp or some other directory and into other package build
directories. This may be useful if your package has a Makefile that installs
everything into debian/tmp, and you need to break that up into subpackages.

Note: dh_install is a much better program, and you are recommended to use
it instead of dh_movefiles.

=head1 FILES

=over 4

=item debian/I<package>.files

Lists the files to be moved into a package, separated by whitespace. The
filenames listed should be relative to debian/tmp/. You can also list
directory names, and the whole directory will be moved.

=back

=head1 OPTIONS

=over 4

=item B<--sourcedir=>I<dir>

Instead of moving files out of debian/tmp (the default), this option makes
it move files out of some other directory. Since the entire contents of
the sourcedir is moved, specifying something like --sourcedir=/ is very
unsafe, so to prevent mistakes, the sourcedir must be a relative filename;
it cannot begin with a `/'.

=item B<-Xitem>, B<--exclude=item>

Exclude files that contain "item" anywhere in their filename from
being installed.

=item I<file ...>

Lists files to move. The filenames listed should be relative to
debian/tmp/. You can also list directory names, and the whole directory will
be moved. It is an error to list files here unless you use -p, -i, or -a to
tell dh_movefiles which subpackage to put them in.

=back

=head1 NOTES

Note that files are always moved out of debian/tmp by default (even if you
have instructed debhelper to use a compatibility level higher than one,
which does not otherwise use debian/tmp for anything at all). The idea
behind this is that the package that is being built can be told to install
into debian/tmp, and then files can be moved by dh_movefiles from that
directory. Any files or directories that remain are ignored, and get
deleted by dh_clean later.

=cut

init(options => {
	"sourcedir=s" => \$dh{SOURCEDIR},	
});

my $ret=0;

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp=tmpdir($package);
	my $files=pkgfile($package,"files");

	my $sourcedir="debian/tmp";
	if ($dh{SOURCEDIR}) {
		if ($dh{SOURCEDIR}=~m:^/:) {
			error("The sourcedir must be a relative filename, not starting with `/'.");
		}
		$sourcedir=$dh{SOURCEDIR};
	}

	if (! -d $sourcedir) {
		error("$sourcedir does not exist.");
	}

	my @tomove;

        # debian/files has a different purpose, so ignore it.
	if ($files && $files ne "debian/files" ) {
		@tomove=filearray($files, $sourcedir);
	}
	
	if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
		push @tomove, @ARGV;
	}

	if (@tomove && $tmp eq $sourcedir) {
		error("I was asked to move files from $sourcedir to $sourcedir. Perhaps you should set DH_COMPAT=2?");
	}

	# Now we need to expand wildcards in @tomove.
	# This is only necessary in pre-v3 land -- as of v3, the
	# expension is automatically done by filearray().
	if (@tomove && compat(2)) {
		my @filelist=();
		foreach (@tomove) {
			push @filelist, glob("$sourcedir/$_");
		}
		@tomove=@filelist;
	}
	else {
		# However, filearray() does not add the sourcedir,
		# which we need.
		@tomove = map { "$sourcedir/$_" } @tomove;
	}

	if (@tomove) {
		if (! -d $tmp) {
			doit("install","-d",$tmp);
		}

		doit("rm","-f","debian/movelist");
		foreach (@tomove) {
			my $file=$_;
			if (! -e $file && ! -l $file && ! $dh{NO_ACT}) {
				$ret=1;
				warning("$file not found (supposed to put it in $package)");
			}
			else {
				$file=~s:^\Q$sourcedir\E/+::;
				my $cmd="(cd $sourcedir >/dev/null ; find $file ! -type d ";
				if ($dh{EXCLUDE_FIND}) {
					$cmd.="-a ! \\( $dh{EXCLUDE_FIND} \\) ";
				}
				$cmd.="-print || true) >> debian/movelist";
				complex_doit($cmd);
			}
		}
		my $pwd=`pwd`;
		chomp $pwd;
		complex_doit("(cd $sourcedir >/dev/null ; tar --create --files-from=$pwd/debian/movelist --file -) | (cd $tmp >/dev/null ;tar xpf -)");
		# --remove-files is not used above because tar then doesn't
		# preserve hard links
		complex_doit("(cd $sourcedir >/dev/null ; tr '\\n' '\\0' < $pwd/debian/movelist | xargs -0  -i rm -f '{}')");
		doit("rm","-f","debian/movelist");
	}
}

# If $ret is set, we wern't actually able to find some 
# files that were specified to be moved, and we should
# exit with the code in $ret. This program puts off 
# exiting with an error until all files have been tried
# to be moved, because this makes it easier for some 
# packages that arn't always sure exactly which files need
# to be moved.
exit $ret;

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut
