#!/bin/perl
#
#  This file is part of WebDyne.
#
#  This software is copyright (c) 2025 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 Cwd    qw(realpath);
use WebDyne::Util;
use Getopt::Long;
use Pod::Usage;
use FindBin qw($RealBin $Script);
use Data::Dumper;
$Data::Dumper::Indent=1;


#  Version info
#
$VERSION='2.019';


#  Get command line options, add them to environment so picked up
#  by WebDyne::Install::Apache::Constant module when loaded
#
my %opt;
GetOptions(
    \%opt,
    (
        'help|?',
        'man',
        'apache_uname|uname=s',
        'apache_gname|gname=s',
        'httpd_bin|httpd=s',
        'dir_apache_conf|apache_conf|conf=s',
        'dir_apache_modules|apache_modules|modules=s',
        'file_mod_perl_lib|mod_perl_lib|mod_perl=s',
        'mp2',
        'webdyne_cache_dn|webdyne_cache|cache_dn|cache|dir_webdyne_cache=s',
        'silent',
        'setcontext',
        'uninstall',
        'text|print',
        '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};
map {$ENV{uc($_)}=$opt{$_}} keys %opt;


#  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;


#  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));
if ($opt{'uninstall'}) {
    exit ${WebDyne::Install::Apache->uninstall($prefix_dn, $RealBin, \%opt) || die errstr()}
}
else {
    exit ${WebDyne::Install::Apache->install($prefix_dn, $RealBin, \%opt) || die errstr()}
}

__END__

# Documentation in Markdown. Convert to POD using markpod from 
#
# https://github.com/aspeer/pl-markpod.git 

=begin markdown

# NAME

wdapacheinit - Install or uninstall WebDyne Apache configuration files

# SYNOPSIS

`wdapacheinit [--option]`

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

# 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 permissions on the cache directory.

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 | --uname`
  Specify the Apache user name.

- `--apache_gname | --gname`
  Specify the Apache group name.

- `--httpd_bin | --httpd`
  Specify the path to the httpd binary.

- `--dir_apache_conf | --apache_conf | --conf`
  Specify the directory for Apache configuration files.

- `--dir_apache_modules | --apache_modules | --modules`
  Specify the directory for Apache modules.

- `--file_mod_perl_lib | --mod_perl_lib | --mod_perl`
  Specify the path to the mod_perl library.

- `--mp2`
  Use mod_perl 2.

- `--webdyne_cache_dn | --webdyne_cache | --cache_dn | --cache | --dir_webdyne_cache`
  Specify the directory for WebDyne cache.

- `--silent`
  Run the script in silent mode.

- `--setcontext`
  Set the context for the script.

- `--uninstall`
  Uninstall the WebDyne Apache configuration.

- `--text | --print`
  Print the configuration.

- `--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`

# 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 renderings
of WebDyne pages. 

The second component is of configuration variables that can be set to change behaviour of the WebdDyne system, 

# AUTHOR

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

# LICENSE and COPYRIGHT

This file is part of WebDyne.

This software is copyright (c) 2025 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 NAME

wdapacheinit - Install or uninstall WebDyne Apache configuration files


=head1 SYNOPSIS

C<wdapacheinit [--option]>

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


=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 permissions on the cache directory.

NOTE: Apache must be restarted after running this script !


=head1 OPTIONS

=over

=item -

C<--help | -?>
  Display a brief help message and exit.



=item -

C<--man>
  Display the full manual.



=item -

C<--apache_uname | --uname>
  Specify the Apache user name.



=item -

C<--apache_gname | --gname>
  Specify the Apache group name.



=item -

C<--httpd_bin | --httpd>
  Specify the path to the httpd binary.



=item -

C<--dir_apache_conf | --apache_conf | --conf>
  Specify the directory for Apache configuration files.



=item -

C<--dir_apache_modules | --apache_modules | --modules>
  Specify the directory for Apache modules.



=item -

C<--file_mod_perl_lib | --mod_perl_lib | --mod_perl>
  Specify the path to the mod_perl library.



=item -

C<--mp2>
  Use mod_perl 2.



=item -

C<--webdyne_cache_dn | --webdyne_cache | --cache_dn | --cache | --dir_webdyne_cache>
  Specify the directory for WebDyne cache.



=item -

C<--silent>
  Run the script in silent mode.



=item -

C<--setcontext>
  Set the context for the script.



=item -

C<--uninstall>
  Uninstall the WebDyne Apache configuration.



=item -

C<--text | --print>
  Print the configuration.



=item -

C<--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>


=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 renderings
of WebDyne pages. 

The second component is of configuration variables that can be set to change behaviour of the WebdDyne system, 


=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) 2025 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
