#!/usr/bin/env perl
########################################################################
# Merge every etc/app.conf walking up from the program's directory:
# read them farthest first so that nearer files override.  See POD.
########################################################################

use v5.40;

# @app_conf: every etc/app.conf found, nearest first.

use FindBin::Files  qw( file=etc/app.conf nofail );

my %config  = ();
my %source  = ();

for my $path ( reverse @app_conf )
{
    open my $fh, '<', $path
    or die "Cannot read '$path': $!\n";

    while( my $line = readline $fh )
    {
        # "name = value"; anything else, comments included, is skipped.

        my ( $name, $value )
        = $line =~ m{ ^ \s* ( \w+ ) \s* = \s* ( .*? ) \s* $ }x
        or next;

        $config{ $name }    = $value;
        $source{ $name }    = $path;
    }
}

say "$_ = $config{ $_ }\t($source{ $_ })"
for sort keys %config;

__END__

=head1 NAME

merge_app_conf - merge every etc/app.conf, nearer files override

=head1 SYNOPSIS

    merge_app_conf

=head1 DESCRIPTION

    use FindBin::Files qw( file=etc/app.conf nofail );

exports C<@app_conf>, each C<etc/app.conf> found from C<$Bin_p> up to
the root, nearest first.  Reading them in reverse applies the site
settings first, then the project's, then the package's:

    /work/etc/app.conf                  site:     db = prod, log = warn
    /work/projects/etc/app.conf         project:  db = test
    /work/projects/app/etc/app.conf     package:  log = debug

    db = test       (/work/projects/etc/app.conf)
    log = debug     (/work/projects/app/etc/app.conf)

Each value is printed with the file that set it.  With no files found
C<@app_conf> is empty and nothing is printed.

To layer a sandbox over the project through a directory symlink, see
F<example/layered_etc>; F<example/symlink_paths> shows what the
C<abspath> and C<noabspath> walks find.

=head1 SEE ALSO

L<FindBin::Files>, F<example/find_app_conf>, F<example/layered_etc>
