#!/usr/bin/perl

# Outputs missing mail_params.h lines for the proxy_read_maps default
# value.
#
# First, get the proxy_read_maps default value from postconf command
# output. This gives us a list of parameter names that are already
# present in the proxy_read_maps default value.

$command = "bin/postconf -dh proxy_read_maps | tr ' ' '\12'";
open(PROXY_READ_MAPS, "$command|")
	|| die "can't execute $command: !$\n";
while (<PROXY_READ_MAPS>) {
    chomp;
    next unless /^\$(.+)$/;
    $proxy_read_maps{$1} = 1;
}
close(PROXY_READ_MAPS) || die "close $command: $!\n";

# Parse mail_params.h, to determine the VAR_XXX name for each main.cf
# parameter. Ignore parameter names composed from multiple strings,
# unless the parameter name is known to be of interest. The code
# block after this one will discover if we ignored too much.

$mail_params_h = "src/global/mail_params.h";
open(MAIL_PARAMS, "<$mail_params_h")
    || die "Open $mail_params_h";
while ($line = <MAIL_PARAMS>) {
    chomp;
    if ($line =~ /^#define\s+(VAR\S+)\s+"(\S+)"\s*(\/\*.*\*\/)?$/) {
	$mail_params{$2} = $1;
    } elsif ($line =~/^#define\s+(VAR\S+)\s+"address_verify_"\s+VAR_SND_DEF_XPORT_MAPS/) {
	$mail_params{"address_verify_sender_dependent_default_transport_maps"} = $1;
    } elsif ($line =~/^#define\s+(VAR\S+)\s+"sender_dependent_"\s+VAR_DEF_TRANSPORT\s+"_maps"/) {
	$mail_params{"sender_dependent_default_transport_maps"} = $1;
    }
}
close(MAIL_PARAMS) || die "close $mail_params_h: !$\n";
#
# Produce mail_params.h lines for all parameters that have names
# ending in _maps and that are not listed in proxy_read_maps. We get
# the full parameter name list from postconf command output. Abort
# if we discover that our mail_params.h parser missed something.

$command = "bin/postconf -H";
open(ALL_PARAM_NAMES, "$command|")
         || die "can't execute $command: !$\n";  
while ($param_name = <ALL_PARAM_NAMES>) { 
    chomp($param_name);
    next unless ($param_name =~ /_maps$/);
    next if ($param_name =~ /^(proxy_read|proxy_write)_maps$/);
    next if defined($proxy_read_maps{$param_name});
    die "unknown parameter: $param_name\n"
	unless defined($mail_params{$param_name});
    print "\t\t\t\t\" \$\" $mail_params{$param_name} \\\n";
}
