#!/bin/perl
#
#  This file is part of WebDyne.
#
#  This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
#
#  This is free software; you can redistribute it and/or modify it under
#  the same terms as the Perl 5 programming language system itself.
#
#  Full license text is available at:
#
#  <http://dev.perl.org/licenses/>
#



#  Init Apache config files
#
use strict qw(vars);
use vars   qw($VERSION);
use warnings;


#  External modules
#
use Cwd    qw(realpath);
use WebDyne::Util;
use Getopt::Long;
use Pod::Usage;
use FindBin qw($RealBin $Script);
use Data::Dumper;


#  Local customisation
#
local $Data::Dumper::Indent=1;
local $Data::Dumper::Sortkeys=1;


#  Version info
#
$VERSION='3.005';


#  Get command line options, add them to environment so picked up
#  by WebDyne::Install::Apache::Constant module when loaded
#
my %opt;
GetOptions(
    \%opt,
    my @opt=(
    'help|?',
    'man',
    'apache_uname|apache-uname|uname=s',
    'apache_gname|apache-gname|gname=s',
    'httpd_bin|httpd-bin|httpd=s',
    'apachectl_bin|apachectl-bin|apachectl=s',
    'apxs_bin|apxs-bin|apxs=s',
    'dir_apache_conf|dir-apache-conf|apache_conf|apache-conf|conf=s',
    'dir_apache_modules|dir-apache-modules|apache_modules|apache-modules|modules=s',
    'file_mod_perl_lib|file-mod-perl-lib|mod_perl_lib|mod-perl-lib|mod_perl|mod-perl=s',
    'mp2',
    'webdyne_cache_dn|webdyne-cache-dn|webdyne_cache|webdyne-cache|cache_dn|cache-dn|cache|dir_webdyne_cache|dir-webdyne-cache=s',
    'silent',
    'setcontext',
    'uninstall',
    'text|print',
    'dry_run|dry-run',
    'dump_opt|dump-opt|opt',
    'dump_config|dump-config|config',
    'version'
    )
) || pod2usage(2);
pod2usage(-verbose => 99, -sections => 'SYNOPSIS|OPTIONS', -exitval => 1)
    if $opt{'help'};
pod2usage(-verbose => 2) if $opt{'man'};
$opt{'version'} && do {print "$Script version: $VERSION\n"; exit 0};


#  Dump options for debugging
#
die Dumper(\%opt) if $opt{'dump_opt'};

## no critic (Variables::RequireLocalizedPunctuationVars)
map {$ENV{uc($_)}=$opt{$_}} keys %opt;
## use critic

#  By default the WebDyne::Install::Apache::Constant module will issue a
#  warning if something not found, but will continue. In this case we want
#  to consider any warning fatal.
#
local $SIG{'__WARN__'}=sub {
    my $warn=shift();
    my %warn=(
        APACHE_UNAME      => '--uname',
        APACHE_GNAME      => '--gname',
        HTTPD_BIN         => '--httpd_bin',
        DIR_APACHE_CONF   => '--dir_apache_conf',
        FILE_MOD_PERL_LIB => '--mod_perl_lib',
    );

    #  Translate warning messages from Contants module about setting env vars into
    #  messages about setting command line options. Crude, but will do for now.
    #
    while (my ($env, $param)=each %warn) {
        last if $warn=~s/(.*)$env.*/$1$param option./;
    }
    pod2usage(-verbose => 99, -sections => 'Options', -message => "ERROR: " . ucfirst($warn))
};
require WebDyne::Install::Apache;
if ($opt{'dump_config'}) {
    no warnings qw(once);
    die Dumper(\%WebDyne::Install::Apache::Constant::Constant);
}


#  Get location prefile, call install/uninstall routine
#
my $prefix_dn=realpath(File::Spec->rel2abs(File::Spec->updir(), $RealBin));
debug("about to call install/uninstall with prefix_dn: $prefix_dn, RealBin: $RealBin, opt: %s", Dumper(\%opt));
my $rc;
if ($opt{'uninstall'}) {
    $rc=${WebDyne::Install::Apache->uninstall($prefix_dn, $RealBin, \%opt) || die errstr()} || 0;
}
else {
    $rc=${WebDyne::Install::Apache->install($prefix_dn, $RealBin, \%opt) || die errstr()} || 0;
}
exit $rc;

__END__

=begin markdown

# wdapacheinit #

# NAME #

wdapacheinit - Install or uninstall WebDyne Apache configuration files

# SYNOPSIS

`wdapacheinit [--option]`

`wdapacheinit --cache /var/www/cache`

`wdapacheinit --print`

`wdapacheinit --uninstall`

# DESCRIPTION

`wdapacheinit` will install or uninstall the WebDyne Apache configuration files. 
It will create the necessary configuration files and directories for Apache to serve WebDyne pages. 
The script will also attempt to set the correct ownership and permissions on the cache directory.

Command-line options are copied into uppercase environment variables before the Apache discovery constants are loaded, so supplied options override the detected values.

NOTE: Apache must be restarted after running this script !

# OPTIONS

* **--help**

    Display a brief help message and exit.

* **--man**

    Display the full manual.

* **--apache_uname=USER**

    Specify the Apache user name.

* **--apache_gname=GROUP**

    Specify the Apache group name.

* **--httpd_bin=PATH**

    Specify the path to the httpd binary.

* **--apachectl_bin=PATH**

    Specify the path to the apachectl or apache2ctl binary.

* **--apxs_bin=PATH**

    Specify the path to the apxs binary.

* **--dir_apache_conf=DIR**

    Specify the directory for Apache configuration files.

* **--dir_apache_modules=DIR**

    Specify the directory for Apache modules.

* **--file_mod_perl_lib=PATH**

    Specify the path to the mod_perl library.

* **--mp2**

    Use mod_perl 2.

* **--webdyne_cache_dn=DIR**

    Specify the directory for WebDyne cache.

* **--silent**

    Suppress installer status messages.

* **--setcontext**

    When SELinux is detected, change the context of module library files that would otherwise only generate a warning. Cache directory context handling is attempted independently when SELinux support is available.

* **--uninstall**

    Uninstall the WebDyne Apache configuration.

* **--text**

    Print the complete generated Apache/WebDyne configuration fragments and exit without writing files, creating directories, changing ownership, or enabling Apache configuration.

* **--dry_run**

    Report install or uninstall actions without writing, removing, linking, changing ownership, or changing SELinux context.

* **--dump_opt**

    Dump the processed option hash and exit.

* **--dump_config**

    Dump the resolved Apache/WebDyne installation configuration after discovery and exit.

* **--version**

    Display the script version and exit.


# EXAMPLES

The script will attempt to automatically detect the correct settings for your system, so in most cases you will not need to specify any options. However, you can override the defaults by specifying the options on the command line.

`wdapacheinit`

`wdapacheinit --cache /var/www/cache`

`wdapacheinit --print`

`wdapacheinit --uninstall`

# NOTES

Apache installation of WebDyne is split into two components. The first is the installation of the WebDyne Apache configuration files, which is done by this script. The second is the installation of the configuration files for the WebDyne framework to enable rendering of WebDyne pages.

The generated WebDyne Perl configuration contains variables that can be set to change the behaviour of the WebDyne system.

Where Apache uses a conf.d-style include directory, the generated WebDyne Apache include is written there and the main Apache configuration file is not changed. Where no such include directory is detected, the script updates the main Apache configuration between WebDyne delimiter markers.

During uninstall, cache cleanup is limited to files in the resolved WebDyne cache directory whose names match WebDyne's compiled cache pattern: a 32-character word name, optionally followed by `.html`. The cache directory itself is removed only if it is empty afterwards and is not the system temporary directory.

# AUTHOR

Andrew Speer <andrew.speer@isolutions.com.au>

# LICENSE and COPYRIGHT

This file is part of WebDyne.

This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

Full license text is available at:

<http://dev.perl.org/licenses/>

=end markdown


=head1 wdapacheinit


=head1 NAME

wdapacheinit - Install or uninstall WebDyne Apache configuration files


=head1 SYNOPSIS

C<wdapacheinit [--option]>

C<wdapacheinit --cache /var/www/cache>

C<wdapacheinit --print>

C<wdapacheinit --uninstall>


=head1 DESCRIPTION

C<wdapacheinit> will install or uninstall the WebDyne Apache configuration files. 
It will create the necessary configuration files and directories for Apache to serve WebDyne pages. 
The script will also attempt to set the correct ownership and permissions on the cache directory.

Command-line options are copied into uppercase environment variables before the Apache discovery constants are loaded, so supplied options override the detected values.

NOTE: Apache must be restarted after running this script !


=head1 OPTIONS

=over

=item *

B<--help>

Display a brief help message and exit.



=item *

B<--man>

Display the full manual.



=item *

B<--apache_uname=USER>

Specify the Apache user name.



=item *

B<--apache_gname=GROUP>

Specify the Apache group name.



=item *

B<--httpd_bin=PATH>

Specify the path to the httpd binary.



=item *

B<--apachectl_bin=PATH>

Specify the path to the apachectl or apache2ctl binary.



=item *

B<--apxs_bin=PATH>

Specify the path to the apxs binary.



=item *

B<--dir_apache_conf=DIR>

Specify the directory for Apache configuration files.



=item *

B<--dir_apache_modules=DIR>

Specify the directory for Apache modules.



=item *

B<--file_mod_perl_lib=PATH>

Specify the path to the mod_perl library.



=item *

B<--mp2>

Use mod_perl 2.



=item *

B<--webdyne_cache_dn=DIR>

Specify the directory for WebDyne cache.



=item *

B<--silent>

Suppress installer status messages.



=item *

B<--setcontext>

When SELinux is detected, change the context of module library files that would otherwise only generate a warning. Cache directory context handling is attempted independently when SELinux support is available.



=item *

B<--uninstall>

Uninstall the WebDyne Apache configuration.



=item *

B<--text>

Print the complete generated Apache/WebDyne configuration fragments and exit without writing files, creating directories, changing ownership, or enabling Apache configuration.



=item *

B<--dry_run>

Report install or uninstall actions without writing, removing, linking, changing ownership, or changing SELinux context.



=item *

B<--dump_opt>

Dump the processed option hash and exit.



=item *

B<--dump_config>

Dump the resolved Apache/WebDyne installation configuration after discovery and exit.



=item *

B<--version>

Display the script version and exit.



=back


=head1 EXAMPLES

The script will attempt to automatically detect the correct settings for your system, so in most cases you will not need to specify any options. However, you can override the defaults by specifying the options on the command line.

C<wdapacheinit>

C<wdapacheinit --cache /var/www/cache>

C<wdapacheinit --print>

C<wdapacheinit --uninstall>


=head1 NOTES

Apache installation of WebDyne is split into two components. The first is the installation of the WebDyne Apache configuration files, which is done by this script. The second is the installation of the configuration files for the WebDyne framework to enable rendering of WebDyne pages.

The generated WebDyne Perl configuration contains variables that can be set to change the behaviour of the WebDyne system.

Where Apache uses a conf.d-style include directory, the generated WebDyne Apache include is written there and the main Apache configuration file is not changed. Where no such include directory is detected, the script updates the main Apache configuration between WebDyne delimiter markers.

During uninstall, cache cleanup is limited to files in the resolved WebDyne cache directory whose names match WebDyne's compiled cache pattern: a 32-character word name, optionally followed by C<.html>. The cache directory itself is removed only if it is empty afterwards and is not the system temporary directory.


=head1 AUTHOR

Andrew Speer L<mailto:andrew.speer@isolutions.com.au>


=head1 LICENSE and COPYRIGHT

This file is part of WebDyne.

This software is copyright (c) 2026 by Andrew Speer L<mailto:andrew.speer@isolutions.com.au>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

Full license text is available at:

L<http://dev.perl.org/licenses/>

=cut
