#! /usr/bin/perl
#
# dpkg-prof2purp.pl - Convert profile syntax to purpose override syntax.
#
# Copyright © 2012 Patrick "P. J." McDermott <pjm@nac.net>
#
# This program is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This 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 Getopt::Std;
use Dpkg::Deps;

sub print_usage
{
	my ($fh) = @_;

	$fh = *STDOUT if not defined($fh);

	printf($fh 'Usage: %s [-v] <field>' . "\n", $0);
}

sub HELP_MESSAGE
{
	my ($fh) = @_;

	$fh = *STDOUT if not defined($fh);

	print_usage($fh);
	print $fh <<EOF
Convert profile syntax to purpose override syntax.

<field> is a complete control field, for example:
    Build-Depends: huge (>= 1.0) <!stage1 !embedded>, tiny [!armel !armhf]

Options:
    -v              print verbose informational messages
    -h, --help      display this help and exit
    -V, --version   display version information and exit
EOF
}

sub VERSION_MESSAGE
{
	my ($fh) = @_;

	$fh = *STDOUT if not defined($fh);

	print $fh <<EOF
dpkg-prof2purp.pl 0.2.0

This is free software; see the GNU General Public License version 2 or
later for copying conditions. There is NO warranty.
EOF
}

my %opts;
my $field;
my $field_name;
my $field_value;
my @profiles;
my $deps_full;

$Getopt::Std::STANDARD_HELP_VERSION = 1;
getopts('vhV', \%opts);

if (defined($opts{h})) {
	HELP_MESSAGE();
	exit(0);
}
if (defined($opts{V})) {
	VERSION_MESSAGE();
	exit(0);
}

($field) = @ARGV;

if (not defined($field)) {
	print_usage(*STDERR);
	exit(1);
}

# Parse control field into name and value.
if (not $field =~ /^([A-Za-z0-9-]+):\s*(.*)$/) {
	printf(STDERR "Invalid control field: %s\n", $field);
	exit(1);
} else {
	$field_name = $1;
	$field_value = $2;
}

$deps_full = deps_parse($field_value);

# Collect profiles.
foreach my $dep_and ($deps_full->get_deps()) {
	foreach my $dep_or ($dep_and->get_deps()) {
		next if not defined($dep_or->{profiles});
		foreach my $prof (@{$dep_or->{profiles}}) {
			next if not $prof;
			my $normal_prof = $prof;
			$normal_prof =~ s/^!//;
			push(@profiles, $normal_prof)
				if not grep($_ eq $normal_prof, @profiles);
		}
	}
}

if (defined($opts{v})) {
	# Print original Build-Depends field.
	printf("Control with profiles:\n  %s: %s\n",
		$field_name, $deps_full->output());
	# Print supported profiles.
	printf("Supported profiles: %s\n", join(', ', @profiles));
}


# Add default empty profile.
@profiles = ('', @profiles);

# Print Build-Depends fields with purpose overrides.
print("Control with purpose overrides:\n") if defined($opts{v});
foreach my $prof (@profiles) {
	printf("%s%s%s: %s\n",
		(defined($opts{v}) ? '  ' : ''),
		$field_name,
		($prof ? '[' . $prof . ']' : ''),
		deps_parse($field_value, reduce_profile => 1, build_profile => $prof));
}
