#!/usr/bin/perl -w

use Config;
use Module::Build;

my $build = Module::Build->current;

my %config = (%Config, $build->notes);

my $output_file = shift;
my $input_file  = $output_file . ".in";

open my $input_fh, $input_file          or die "Can't open $input_file: $!";
open my $output_fh, ">$output_file"     or die "Can't open $output_file: $!";
print $output_fh <<"END";
/*
 * !!! DO NOT EDIT THIS FILE!!!
 * This file was generated from $input_file.
 * Edit that and rerun make to regenerate.
 */

END

while(<$input_fh>) {
    my $matched = s{%%(.*)%%}{$config{$1}}g;

    if( $matched and !defined $config{$1} and !length $config{$1} ) {
        warn "Your configuration is missing \$config{$1}.\n".
             "Please edit $output_file by hand to fix.\n"
    }

    print $output_fh $_;
}
