#!/usr/bin/perl
#
# Generate the substvars for every package with *.metapackages control files.
# The input file should be a list of packages, with blank lines and lines
# starting with # ignored.  This will create a substvar for that package with
# the name metapackage:Depends.

use 5.010;
use autodie;
use strict;
use warnings;

# Find all of the control files.
my @files = glob('debian/*.metapackage');

# Iterate through each control file.
for my $file (@files) {
    my $package = $file;
    $package =~ s{ \A debian/ (.*) \.metapackage \z }{$1}xms;

    # Get the list of packages.
    my @packages;
    open(my $in, '<', $file);
  LINE:
    for my $line (<$in>) {
        $line =~ s{ \A \s+ }{}xms;
        $line =~ s{ \s+ \z }{}xms;
        next LINE if ($line eq q{});
        next LINE if ($line =~ m{ \A \# }xms);
        push(@packages, $line);
    }
    close($in);

    # Print out the substvar for that package.
    open(my $out, '>>', "debian/$package.substvars");
    say {$out} 'metapackage:Depends=', join(q{, }, @packages);
    close($out);
}
exit 0;
