# A debhelper build system class for handling Eclipse bundles projects.
#
# Copyright: © 2018 Emmanuel Bourg
# License: Apache-2.0

package Debian::Debhelper::Buildsystem::eclipse_bundles;

use strict;
use warnings;
use parent qw(Debian::Debhelper::Buildsystem);
use Debian::Debhelper::Dh_Lib qw(%dh addsubstvar);

sub DESCRIPTION {
	"Eclipse bundles"
}

sub check_auto_buildable {
	return 0;
}

sub new {
	my $class=shift;
	my $this=$class->SUPER::new(@_);
	$this->enforce_in_source_building();

	# Load the list of bundles to build from debian/bundles
	my $bundles = `grep -v '#' debian/bundles | xargs`;
	chomp $bundles;
	@{$this->{bundles}} = split(/ /, $bundles);

	if (-d "$this->{bundles}[0]") {
		$this->{bundledir} = ".";
	} elsif (-d "bundles/$this->{bundles}[0]") {
		$this->{bundledir} = "bundles";
	} else {
		die "Couldn't locate the base directory of the bundles";
	}

	return $this;
}

sub build {
	my $this=shift;
	my $d_ant_prop = $this->get_sourcepath('debian/ant.properties');
	my @args;
	if ( -f $d_ant_prop ) {
		push(@args, '-propertyfile', $d_ant_prop);
	}

	push(@args, "-Dbasedir", ".");
	push(@args, "-Dbundledir", $this->{bundledir});
	push(@args, "-f", "debian/build.xml");
	push(@args, @{$this->{bundles}});

	# Set the username to improve the reproducibility
	push(@args, "-Duser.name", "debian");

	$this->doit_in_sourcedir("ant", @args, @_);
}

sub install {
	my $this=shift;

	my $file = "debian/bundles.properties";
	open(my $data, '<', $file) or die "Could not open '$file' $!\n";
	while (my $line = <$data>) {
		chomp $line;
		my @fields = split(/\s/ , $line);

		# Install the Maven artifacts
		$this->doit_in_builddir("mh_installpom", "-p$fields[3]", "--no-parent", "-e$fields[1]", "$this->{bundledir}/$fields[0]/target/pom.xml");
		$this->doit_in_builddir("mh_installjar", "-p$fields[3]", "--java-lib", "-e$fields[1]", "--usj-name=$fields[2]", "$this->{bundledir}/$fields[0]/target/pom.xml", "$this->{bundledir}/$fields[0]/target/$fields[0].jar");

		# Specify the bundle dependencies in the substvars file
		addsubstvar($fields[3], "bundle:Depends", $fields[4]);
	}
}

sub clean {
	my $this=shift;

	$this->doit_in_builddir("rm", "-Rf", "debian/bundles.properties");
	for my $bundle (@{$this->{bundles}}) {
		$this->doit_in_builddir("rm", "-Rf", "$this->{bundledir}/$bundle/target");
	}

	$this->doit_in_builddir("mh_clean");
}

1
