#! /usr/bin/perl -wnp
##
## This script implements support for Build Profiles.
use strict;

my @profiles;

BEGIN {
    sub profile_concerned {
        my $profile = shift;

        eval 'require Dpkg::BuildProfiles' or return 0;
        return 0 if (!defined (&Dpkg::BuildProfiles::parse_build_profiles));

        my $profile_is_concerned = 0;
        my @restrictions = Dpkg::BuildProfiles::parse_build_profiles ($profile);
        if (@restrictions) {
            $profile_is_concerned = Dpkg::BuildProfiles::evaluate_restriction_formula
                (\@restrictions, \@profiles);
        }

        return $profile_is_concerned;
    }
}

if ($ENV{'DEB_BUILD_PROFILES'} || /<([^>]*)>/) {
    if (!$ENV{'DEB_BUILD_PROFILES'}) {
        @profiles = [];
    } else {
        @profiles = split /\s+/, $ENV{'DEB_BUILD_PROFILES'};
    }

    my $line_concerned = 0;
    if (!/<([^>]*)>/) {
        $line_concerned = 1;
    }
    while (/<([^>]*)>/) {
        $line_concerned |= profile_concerned ($1, $_);
        s/<([^>]*)>//;
    }

    s/(^\s+|\s+$)//;
    $_ .= "\n";

    if (!$line_concerned) {
        $_ = "";
    }
}
