# $Id$

use strict;
use warnings;
use English qw(-no_match_vars);

use Sympa::ConfDef;
use Sympa::Constants;
use Sympa::ListDef;

my %overrides;
while (my ($pname, $pitem) = each %Sympa::ListDef::pinfo) {
    %overrides = (%overrides, _get_overrides($pname, $pitem, [$pname]));
}

my $parameters = '';

foreach my $param (@Sympa::ConfDef::params) {
    if (exists $param->{name}) {
        my $name = $param->{name};
        my $default =
              (exists $param->{default_s}) ? $param->{default_s}
            : (exists $param->{default})   ? $param->{default}
            :                                undef;
        my $query = $param->{gettext_id}
            if exists $param->{gettext_id};
        my $advice = $param->{gettext_comment}
            if exists $param->{gettext_comment};
        my $example = $param->{sample}
            if exists $param->{sample};
        my $optional = 1
            if exists $param->{optional} and $param->{optional};
        my $vhost = 1
            if exists $param->{vhost} and $param->{vhost};
        my $multiple = 1
            if exists $param->{multiple} and $param->{multiple};
        my $scenario = $name
            if exists $param->{scenario} and $param->{scenario};

        if ($name eq 'color_0') {
            $parameters .= "=head4 C<color_0>, ..., C<color_15>\n\n";
        } elsif ($name =~ /\Acolor_/) {
            next;
        } elsif ($name eq 'dark_color') {
            $parameters .=
                "=head4 C<dark_color>, C<light_color>, C<text_color>, C<bg_color>, C<error_color>, C<selected_color>, C<shaded_color>\n\n";
        } elsif ($name =~ /_color\z/) {
            next;
        } elsif ($name eq 'main_menu_custom_button_1_title') {
            $parameters .=
                "=head4 C<main_menu_custom_button_1_title>, ... C<main_menu_custom_button_3_title>, C<main_menu_custom_button_1_url>, ... C<main_menu_custom_button_3_url>, C<main_menu_custom_button_1_target>, ... C<main_menu_custom_button_3_target>\n\n";
        } elsif ($name =~ /\Amain_menu_custom_button_/) {
            next;
        } else {
            $parameters .= sprintf "=head4 C<%s>\n\n", $param->{name};
        }
        $parameters .= sprintf "%s\n\n", _escape_pod($query)
            if defined $query;

        $parameters .= "=over\n\n";

        $parameters .= "=item Default:\n\n";
        if ($name =~ /\Acolor_/ or $name =~ /_color\z/) {
            $parameters .= "See description on web interface.\n\n";
        } elsif (defined $default) {
            if (exists $param->{gettext_unit}) {
                $parameters .= sprintf "C<%s> (%s)\n\n",
                    _escape_pod($default), $param->{gettext_unit};
            } else {
                $parameters .= sprintf "C<%s>\n\n", _escape_pod($default);
            }
        } elsif ($optional) {
            if (exists $param->{gettext_unit}) {
                $parameters .= sprintf "None (%s).\n\n",
                    $param->{gettext_unit};
            } else {
                $parameters .= "None.\n\n";
            }
        } else {
            $parameters .= "None, I<mandatory>.\n\n";
        }

        $parameters .= "=item Overrides:\n\n";
        $parameters .= "Virtual domain\n\n"
            if defined $vhost;
        if ($overrides{$name}) {
            if ($overrides{$name} eq $name) {
                $parameters .= "List\n\n";
            } else {
                $parameters .= sprintf "List (C<%s>)\n\n", $overrides{$name};
            }
        }
        $parameters .= "None.\n\n"
            unless defined $vhost or $overrides{$name};

        $parameters .= "=back\n\n";

        $parameters .=
            sprintf "Value of this parameter is name of C<%s> scenario.\n\n",
            $name
            if defined $scenario;
        $parameters .= sprintf "%s\n\n",
            join("\n\n", split "\n", _escape_pod($advice))
            if defined $advice;
        if ($name eq 'main_menu_custom_button_1_title') {
            $parameters .= "Example:\n\n";
            $parameters .= "  main_menu_custom_button_1_title FAQ\n";
            $parameters .=
                "  main_menu_custom_button_1_url http://www.renater.fr/faq/universalistes/index\n";
            $parameters .= "  main_menu_custom_button_1_target Help\n\n";
        } else {
            $parameters .= sprintf "Example:\n\n  %s %s\n\n", $name, $example
                if defined $example;
        }
    } else {
        $parameters .= sprintf "=head2 %s\n\n",
            _escape_pod($param->{gettext_id});
        $parameters .= sprintf "%s\n\n",
            join("\n\n", split "\n", _escape_pod($param->{gettext_comment}))
            if exists $param->{gettext_comment};
    }
}

printf do { local $RS; <DATA> }, Sympa::Constants::CONFIG(), $parameters,
    Sympa::Constants::CONFIG();

exit 0;

sub _get_overrides {
    my $pname  = shift;
    my $pitem  = shift;
    my $pnames = shift;

    return () if $pitem->{obsolete};

    my %ov;
    if (exists $pitem->{format} and ref $pitem->{format} eq 'HASH') {
        %ov = map {
            _get_overrides($_, $pitem->{format}->{$_}, [@$pnames, $_]);
        } keys %{$pitem->{format}};
    } elsif (exists $pitem->{default}
        and ref $pitem->{default} eq 'HASH'
        and exists $pitem->{default}->{conf}) {
        %ov = ($pitem->{default}->{conf} => join('.', @$pnames));
    } else {
        %ov = ();
    }

    return %ov;
}

sub _escape_pod {
    my $string = shift;
    $string =~ s/([&<>])/
        'E<' . {'&' => 'amp', '<' => 'lt', '>' => 'gt'}->{$1} . '>'
    /eg;
    return $string;
}

__END__

=encoding utf-8

=head1 NAME

sympa.conf, robot.conf - Configuration file for default site and robot

=head1 DESCRIPTION

F<%s> is main configuration file of Sympa.
Several parameters defined in this file may be overridden by F<robot.conf>
configuration file for each virtual domain, or by F<config> configuration file
for each mailing list.

Format of F<sympa.conf> and F<robot.conf> is as following:

=over

=item *

Lines beginning with C<#> and containing only spaces are ignored.

=item *

Each line has the form "I<parameter> I<value>".
I<value> may contain spaces but may not contain newlines.

=back

=head1 PARAMETERS

Below is entire list of configuration parameters.
"Default" is built-in default value if any.
"Overrides" lists contexts (with parameter name) which can override
settings in site-wide context (F<sympa.conf>): Virtual domain (F<robot.conf>)
and/or List (F<config>).

%s

=head1 FILES

=over

=item F<%s>

Sympa main configuration file.

=item F<$SYSCONFDIR/E<lt>robot nameE<gt>/robot.conf>

Configuration specific to each virtual domain.

=item F<$EXPLDIR/E<lt>list nameE<gt>/config>
or F<$EXPLDIR/E<lt>robot nameE<gt>/E<lt>list nameE<gt>/config>

Configuration specific to each list.

=back

=head1 SEE ALSO

I<Sympa Administration Manual>.
L<https://sympa-community.github.io/manual/>.

=cut
