#!/usr/bin/env perl

use strict;
use warnings;

my %kv = ();

for(@ARGV) {
  if(!m#^\s*([^=\s]+)\s*=\s*(.*?)\s*$#) {
    die "$0: Bad parameter '$_': Not a key=value pair\n"
  }
  $kv{$1} = $2;
}

my $invar = 0;

LOOP: while(my $line = <STDIN>) {
  if(1==$invar) {
    if($line=~m/^(\s+)(\S+)(\s*:\s*)(\S+)(.*)/) {
      my ($pre1, $key, $pre2, $var, $post) = ($1, $2, $3, $4, $5);
      if('#dyn#' eq $var) {
        if(not exists $kv{$key}) {
          die "$0: No value given for key '$key' in the parameters but it is defined in input!\n"
        }
        my $v = $kv{$key};
        delete $kv{$key};
        print "$pre1$key$pre2$v$post\n";
        next LOOP;
      }
    } elsif($line=~m/^[^#\t ]/) {
      $invar = 2
    }
  }
  if(0==$invar and $line=~m#^\s*variables\s*:\s*$#) {
    $invar = 1;
  }
  print $line;
}

my @rem = sort keys %kv;

die "$0: Variables occur in parameter but not in input: @rem!\n" if @rem;

