#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-apache
# BEFORE: apache24
. /etc/rc.freenas

generate_httpd_conf()
{

{
cat << __EOF__

# Generating apache general httpd.conf
# The absolutely necessary modules
LoadModule authn_file_module libexec/apache24/mod_authn_file.so
LoadModule authn_core_module libexec/apache24/mod_authn_core.so
LoadModule authz_user_module libexec/apache24/mod_authz_user.so
LoadModule authz_core_module libexec/apache24/mod_authz_core.so
LoadModule alias_module libexec/apache24/mod_alias.so
LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so
LoadModule mpm_itk_module libexec/apache24/mod_mpm_itk.so
LoadModule unixd_module libexec/apache24/mod_unixd.so
LoadModule auth_basic_module libexec/apache24/mod_auth_basic.so
LoadModule auth_digest_module libexec/apache24/mod_auth_digest.so
LoadModule setenvif_module libexec/apache24/mod_setenvif.so
LoadModule dav_module libexec/apache24/mod_dav.so
LoadModule dav_fs_module libexec/apache24/mod_dav_fs.so
LoadModule allowmethods_module libexec/apache24/mod_allowmethods.so
LoadModule ssl_module libexec/apache24/mod_ssl.so
LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so

# The still deciding whether or not to keep thse modules or not
LoadModule authz_host_module libexec/apache24/mod_authz_host.so
LoadModule authz_groupfile_module libexec/apache24/mod_authz_groupfile.so
LoadModule access_compat_module libexec/apache24/mod_access_compat.so
LoadModule reqtimeout_module libexec/apache24/mod_reqtimeout.so
LoadModule filter_module libexec/apache24/mod_filter.so
LoadModule mime_module libexec/apache24/mod_mime.so
LoadModule log_config_module libexec/apache24/mod_log_config.so
LoadModule env_module libexec/apache24/mod_env.so
LoadModule headers_module libexec/apache24/mod_headers.so
#LoadModule version_module libexec/apache24/mod_version.so
LoadModule status_module libexec/apache24/mod_status.so
LoadModule autoindex_module libexec/apache24/mod_autoindex.so
LoadModule dir_module libexec/apache24/mod_dir.so

# Third party modules
IncludeOptional etc/apache24/modules.d/[0-9][0-9][0-9]_*.conf
ServerName localhost

# Limiting the number of idle threads
# see: http://httpd.apache.org/docs/current/mod/prefork.html#MinSpareServers
<IfModule mpm_itk_module>
        StartServers 1
        MinSpareServers 1
</IfModule>

# I really do not know why mpm-prefork or mpm-event are needed
# to start apache24 successfully when I already have mpm itk
# (see: https://bugs.freenas.org/issues/14396)
# Bandaid fix, till I can (hopefully) think of a better fix
# Please fix if you can!
<IfModule mpm_prefork_module>
        StartServers 1
        MinSpareServers 1
</IfModule>

<IfModule unixd_module>
User www
Group www
</IfModule>

<IfModule dir_module>
    DirectoryIndex disabled
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "/var/log/httpd-error.log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "/var/log/httpd-access.log" common

</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/usr/local/www/apache24/cgi-bin/"
</IfModule>

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig etc/apache24/mime.types

    #
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    #
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    #AddHandler cgi-script .cgi

    # For type maps (negotiated resources):
    #AddHandler type-map var

    #
    # Filters allow you to process content before it is sent to the client.
    #
    # To parse .shtml files for server-side includes (SSI):
    # (You will also need to add "Includes" to the "Options" directive.)
    #
    #AddType text/html .shtml
    #AddOutputFilter INCLUDES .shtml
</IfModule>

# Secure (SSL/TLS) connections
#Include etc/apache24/extra/httpd-ssl.conf
#
# Note: The following must must be present to support
#       starting without SSL on platforms with no /dev/random equivalent
#       but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
SSLProtocol +TLSv1 +TLSv1.1 +TLSv1.2
</IfModule>

Include etc/apache24/Includes/*.conf
__EOF__
} > /etc/local/apache24/httpd.conf

}

generate_webdav_conf ()
{
    # First check if the webdav service is enabled or not.
    # We do this here as executing a django query just for this one value
    # would be expensive.
    if checkyesno apache24_enable; then
        # The service is enabled so call its python-based config generator
        /usr/local/bin/python /usr/local/libexec/nas/generate_webdav_conf.py
    else
        # webdav is disabled, we now write blanks to the webdav config files
        echo "" > /etc/local/apache24/Includes/webdav.conf
        echo "" > /etc/local/apache24/Includes/webdav-ssl.conf
        # Note: This (below) is a hack. We bind apache to some random port when webdav shares are absent
        # The purpose of this is to facilitate apache to run even in the absence of a secondary config
        # file. Else, it will try to bind to port 80 and fail as nginx is using it and thus spit out a
        # mesaage stating that unable to bind to port, cannot create logs.
        echo "Listen 8080" >> /etc/local/apache24/httpd.conf
    fi

}

ix_apache_start()
{
    generate_httpd_conf
    generate_webdav_conf
}

name="ix-apache"
start_cmd='ix_apache_start'
stop_cmd=':'
            
load_rc_config $name
run_rc_command "$1"
