#!/usr/bin/env perl
########################################################################
# Locate the nearest etc/<program name>.conf walking up from the
# program's directory.  The name comes from $0, so each symlink to the
# program can have its own config.  See POD.
########################################################################

use v5.40;

use File::Basename  qw( basename );

use FindBin::Bin    qw( $Bin_p );

# use evaluates its arguments at compile time: $prog has to be set
# in a BEGIN block, a plain assignment would run too late.

my $prog;

BEGIN
{
    $prog   = basename $0;
}

use FindBin::Files
(
    "file=etc/$prog.conf"
  , qw( scalar export=prog_conf nofail )
);

$prog_conf
or die "No etc/$prog.conf in any directory above '$Bin_p'.\n";

say "Config: $prog_conf";

__END__

=head1 NAME

find_prog_conf - locate the nearest etc/<program name>.conf

=head1 SYNOPSIS

    find_prog_conf

=head1 DESCRIPTION

    BEGIN { $prog = basename $0 }

    use FindBin::Files
    (
        "file=etc/$prog.conf"
      , qw( scalar export=prog_conf nofail )
    );

The arguments to C<use> are evaluated at compile time, so the file
name can come from C<$0>.  C<$prog> is set in a C<BEGIN> block: a plain
assignment would not run until after the C<use>.  C<export=prog_conf>
is needed because the default export name would vary with the
program's name.

=head2 One program, many configs

C<$0> is the path the program was run as, so C<basename $0> is the name
of a symlink, not the file it points to.  With

    bin/report
    bin/report-daily  -> report
    bin/report-weekly -> report
    etc/report.conf
    etc/report-daily.conf
    etc/report-weekly.conf

each name reads its own config.  The search still starts from
C<$Bin_p>, the directory holding the real program; pass C<noabspath>
to start from the directory holding the symlink (see
F<example/symlink_paths>).

=head1 SEE ALSO

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