#!/usr/bin/perl
# Generate build deps line from comments in the control file and replace
# the current build deps line in the control file with it.

my $control;
if (-e "debian/control") {
	$control="debian/control";
}
elsif (-e "control") {
	$control="control";
}
else {
	die "cannot find control file";
}

my @builddeps;
my @lines;
open (IN, $control) || die "read $control: $!";
while (<IN>) {
	push @lines, $_;
	chomp;
	if (/^#\s+-\s+(.*)$/) {
		push @builddeps, $1;
	}
}
close IN;

my $builddeps=join(", ", @builddeps);
open (OUT, ">$control.tmp") || die "write $control.tmp: $!";
foreach (@lines) {
	s/^(Build-Depends:\s+)(.*)/$1$builddeps/;
	print OUT || die "print: $!";
}
close OUT || die "close: $!";
rename("$control.tmp", "$control")
	|| die "rename $control.tmp to $control: $!";
