#!/usr/bin/perl

#  Copyright 2010 Neil Williams <codehelp@debian.org>
#
#  This package is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;
use File::Basename;
use vars qw/ @aptlist @bits $use_sudo $name $dryrun $tools $deps
 @dependencies $cmd $arch /;
 
system ("apt-get -y install realpath");

exit (0) if (not -x "/usr/share/pdebuild-cross/xapt");

system ("mkdir -p /var/lib/xapt");
system ("cp /usr/share/pdebuild-cross/xapt /usr/sbin/xapt");
my $ctrl = `find /tmp/buildd/ -path "*/debian/control"|head -n1`;
chomp ($ctrl);
my $dir = dirname ($ctrl);
chomp ($dir);
$dir = `realpath $dir/../`;
chomp ($dir);
print "Changing to '$dir/'\n";
chdir ("$dir/");
$name=`dpkg-parsechangelog|grep Source:`;
chomp ($name);
$name =~ s/Source: //;
my @xc=();
if (-f "debian/xcontrol")
{
	open (XC, "<debian/xcontrol");
	@xc=<XC>;
	close (XC);
}
# allow multi-lines
my $str = join ("", @xc);
$str =~ s/\n / /g;
$str =~ s/  / /g;
my @long = split ("\n", $str);
@dependencies=();
foreach my $line (@long)
{
	if ($line =~ /^Build-Depends-Tools: /)
	{
		$line =~ s/^Build-Depends-Tools: //;
		$tools = $line;
	}
	if ($line =~ /^Build-Depends: /)
	{
		$line =~ s/^Build-Depends: //;
		$deps = $line;
		$deps =~ s/ +/ /g;
	}
}
@dependencies=split(/, /, $deps) if (defined $deps);
@aptlist=();
foreach my $dep (@dependencies)
{
	$dep =~ s/^ //;
	@bits=split(/ /, $dep);
	push @aptlist, $bits[0];
}
$arch = `debconf-show dpkg-cross 2>/dev/null|cut -d: -f2`;
$arch = "armel" if ($arch eq '');
chomp ($arch);
print ("Installing build dependencies for '$name' to build for $arch.\n");
if (scalar @aptlist > 0) {
	$cmd = "xapt -a $arch ".join(" ", @aptlist);
	system ("$cmd");
}
