# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0

name                nginx-devel
set propername      nginx
version             1.27.5
revision            0
categories          www mail
license             BSD
maintainers         {mps @Schamschula} openmaintainer

conflicts           nginx

description         High-performance HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 \
                    proxy server
long_description    Nginx ("engine x") is an HTTP and reverse proxy server, a mail proxy \
                    server, and a generic TCP/UDP proxy server.

homepage            https://nginx.org/
master_sites        https://nginx.org/download:nginx \
                    https://github.com/simpl/ngx_devel_kit/archive/:devel_kit_module \
                    https://github.com/leev/ngx_http_geoip2_module/archive/:geoip2_module \
                    https://github.com/openresty/headers-more-nginx-module/archive/:h_more_module \
                    https://github.com/openresty/lua-nginx-module/archive/:lua_module \
                    https://codeload.github.com/arut/nginx-rtmp-module/tar.gz/:rtmp_module

distfiles           ${propername}-${version}${extract.suffix}:nginx
checksums           ${propername}-${version}${extract.suffix} \
                    rmd160  92e79b7237175eca0c3db62bdf327cc9b7c41415 \
                    sha256  e96acebb9c2a6db8a000c3dd1b32ecba1b810f0cd586232d4d921e376674dd0e \
                    size    1279891

depends_lib         port:pcre \
                    port:zlib

worksrcdir          ${propername}-${version}

patchfiles          patch-auto__install.diff \
                    patch-conf__nginx.conf.diff

set nginx_share     ${prefix}/share/${propername}
set nginx_examples  ${nginx_share}/examples
set nginx_confdir   ${prefix}/etc/${propername}
set nginx_logdir    ${prefix}/var/log/${propername}
set nginx_moduledir ${prefix}/etc/${propername}/modules
set nginx_rundir    ${prefix}/var/run/${propername}
set nginx_pidfile   ${nginx_rundir}/${propername}.pid
set nginx_all_confs {fastcgi.conf fastcgi_params mime.types nginx.conf scgi_params uwsgi_params}
set auto_activate_confs {nginx.conf mime.types fastcgi.conf}

# for aforementioned re-defs of portfile-scoped vars in variant routines
proc nginx_set_portfile_var {var_name new_value} {
    upvar   $var_name   portfile_var
    set portfile_var   $new_value
}

# portfile-scoped vars for variant routines to re-def and use in port-stages (post-extract, pre-patch, etc)
set nginx_lua_mod_dirname      ""

configure.args-append \
                    --with-cc-opt=\"${configure.cppflags} ${configure.cflags}\" \
                    --with-ld-opt=\"${configure.ldflags}\" \
                    --conf-path=${nginx_confdir}/${propername}.conf \
                    --error-log-path=${nginx_logdir}/error.log \
                    --http-log-path=${nginx_logdir}/access.log \
                    --pid-path=${nginx_pidfile} \
                    --lock-path=${nginx_rundir}/${propername}.lock \
                    --http-client-body-temp-path=${nginx_rundir}/client_body_temp \
                    --http-proxy-temp-path=${nginx_rundir}/proxy_temp \
                    --http-fastcgi-temp-path=${nginx_rundir}/fastcgi_temp \
                    --http-uwsgi-temp-path=${nginx_rundir}/uwsgi_temp \
                    --with-compat

# pcre2 breaks the lua module (https://trac.macports.org/ticket/65150)
configure.args-append \
                    --without-pcre2

# remove --disable-dependency-tracking
configure.universal_args-delete   --disable-dependency-tracking

build.target        build

if {${subport} eq ${name}} {
    destroot.keepdirs   ${destroot}${nginx_logdir} \
                        ${destroot}${nginx_rundir}

    post-destroot {
        xinstall -d -m 755 ${destroot}${nginx_share}
        xinstall -d -m 755 ${destroot}${nginx_examples}

        foreach conf ${nginx_all_confs} {
            set conf_path "${destroot}${nginx_confdir}/${conf}"
            if {[file exists ${conf_path}]} {
                file delete ${conf_path}
            }
            if {[file exists ${conf_path}.default]} {
                move ${conf_path}.default ${destroot}${nginx_examples}
            }
        }

        file rename ${destroot}${prefix}/html ${destroot}${nginx_share}

        # Install the manpage
        set man_path "${destroot}${prefix}/share/man/man8"
        xinstall -d -m 0755 ${man_path}
        xinstall    -m 0644 ${worksrcpath}/man/${propername}.8 ${man_path}
        reinplace -q "s|/var/run/mynginx.pid|${nginx_pidfile}/|g" ${man_path}/${propername}.8
    }

    post-activate {
        foreach conf ${auto_activate_confs} {
            if {![file exists ${nginx_confdir}/${conf}]} {
                xinstall -m 644 ${nginx_examples}/${conf}.default ${nginx_confdir}/${conf}
            }
        }
    }

    startupitem.create      yes
    startupitem.pidfile     auto ${nginx_pidfile}
    startupitem.executable  ${prefix}/sbin/nginx -g "daemon off;"

    notes "\
    A set of sample configuration files has been installed in ${nginx_examples}.\n\n\
    Additionally, the files [join ${auto_activate_confs} ", "] have been copied to ${nginx_confdir} if they didn't exist yet.\n\
    Adjust these files to your needs before starting nginx."

    default_variants +devel_kit +flv +http2 +mp4 +secure_link +ssl +stream
} else {
    depends_lib             port:${name}

    build.target            modules

    notes "\
    Add load_module etc/nginx/modules/<MODULE-NAME>.so; to ${nginx_confdir}/nginx.conf"
}

subport ${name}-geoip2 {
    set geoipext_filename   ngx_http_geoip2_module
    set geoipext_version    3.4
    revision                1
    set geoipext_distname   ${geoipext_filename}-${geoipext_version}
    distfiles-append        ${geoipext_version}.tar.gz:geoip2_module
    checksums-append        ${geoipext_version}.tar.gz \
                            rmd160  d6a4115102b8aa13205431d91fe46d60b0762dc8 \
                            sha256  ad72fc23348d715a330994984531fab9b3606e160483236737f9a4a6957d9452 \
                            size    8877

    configure.args-append   --add-dynamic-module=${workpath}/${geoipext_distname}
    depends_lib-append      port:libmaxminddb
    
    destroot {
        xinstall -d -m 0755 ${destroot}${nginx_moduledir}
        move ${worksrcpath}/objs/ngx_http_geoip2_module.so ${destroot}${nginx_moduledir}
    }
}

subport ${name}-headers_more {
    set ngx_h_more_filename     headers-more-nginx-module
    set ngx_h_more_version      0.38
    revision                    0
    set ngx_h_more_distname     ${ngx_h_more_filename}-${ngx_h_more_version}
    distfiles-append            v${ngx_h_more_version}.tar.gz:h_more_module
    checksums-append            v${ngx_h_more_version}.tar.gz \
                                rmd160  5f799b8825924db61e82a49d4bedc6652f45d869 \
                                sha256  febf7271c0c3de69adbd02c1e98ee43e91a60eeb6b27abfb77b5b206fda5215a \
                                size    29472

    configure.args-append       --add-dynamic-module=${workpath}/${ngx_h_more_distname}

    destroot {
        xinstall -d -m 0755 ${destroot}${nginx_moduledir}
        move ${worksrcpath}/objs/ngx_http_headers_more_filter_module.so ${destroot}${nginx_moduledir}
    }
}

subport ${name}-lua {
    depends_lib-append      port:luajit-openresty
    set ngx_lua_filename    lua-nginx-module
    set ngx_lua_version     0.10.28
    revision                0
    set ngx_lua_distname    ${ngx_lua_filename}-${ngx_lua_version}
    distfiles-append        v${ngx_lua_version}.tar.gz:lua_module
    checksums-append        v${ngx_lua_version}.tar.gz \
                            rmd160  dfae2eb6c36bf153703cccc0b17fb2cb09750adf \
                            sha256  634827d54de6216cb0502d14f76610788b3a3e33160e91d5578d6db0d9a34a20 \
                            size    869217

    platforms {darwin >= 11}

    configure.cflags        -I${prefix}/include/luajit-2.1/ ${configure.cflags}
    configure.env           LUAJIT_INC=${prefix}/include/luajit-2.1/
    configure.env           LUAJIT_LIB=${prefix}/lib
    configure.args-append   --add-dynamic-module=${workpath}/${ngx_lua_distname}

    nginx_set_portfile_var  nginx_lua_mod_dirname    "${ngx_lua_distname}"

    patchfiles-append       patch-lua_config.diff

    # patch routine balks on file names with "../", but rel-paths in patchfiles start from $worksrcpath
    pre-patch {
        ln -s   "${workpath}/${nginx_lua_mod_dirname}" "${worksrcpath}/ngx_lua_distname.tmp"
    }
    post-patch {
        file delete   "${worksrcpath}/ngx_lua_distname.tmp"
    }

    destroot {
        xinstall -d -m 0755 ${destroot}${nginx_moduledir}
        move ${worksrcpath}/objs/ngx_http_lua_module.so ${destroot}${nginx_moduledir}
    }
}

subport ${name}-rtmp {
    set rtmp_filename       nginx-rtmp-module
    set rtmp_version        1.2.2
    revision                1
    set rtmp_distname       ${rtmp_filename}-${rtmp_version}
    distfiles-append        v${rtmp_version}:rtmp_module
    checksums-append        v${rtmp_version} \
                            rmd160  a19f42e4f15214fd11ac8f334468dc400b86b5bd \
                            sha256  07f19b7bffec5e357bb8820c63e5281debd45f5a2e6d46b1636d9202c3e09d78 \
                            size    519934

    configure.args-append   --add-dynamic-module=${workpath}/${rtmp_distname}

    nginx_set_portfile_var  nginx_rtmp_mod_dirname    "${rtmp_distname}"

    destroot {
        xinstall -d -m 0755 ${destroot}${nginx_moduledir}
        move ${worksrcpath}/objs/ngx_rtmp_module.so ${destroot}${nginx_moduledir}
    }
}

variant auth_request description {Add client authorization based on the result of a subrequest} {
    configure.args-append   --with-http_auth_request_module
}

variant addition description {Append text to pages} {
    configure.args-append   --with-http_addition_module
}

variant dav description {Add WebDAV support to server} {
    configure.args-append   --with-http_dav_module
}

variant debug description {Enable debug mode} {
    configure.args-append   --with-debug
}

variant degradation description {Allow to return 204 or 444 code for some locations on low memory condition} {
    configure.args-append   --with-http_degradation_module
}

variant devel_kit description {Enable ngx_devel_kit module (https://github.com/simpl/ngx_devel_kit)} {
    set devel_kit_filename  ngx_devel_kit
    set devel_kit_version   0.3.4
    set devel_kit_distname  ${devel_kit_filename}-${devel_kit_version}
    distfiles-append        v${devel_kit_version}.tar.gz:devel_kit_module
    checksums-append        v${devel_kit_version}.tar.gz \
                            rmd160  ac8524f30b6c5d9b17fb954331face80486c3ed5 \
                            sha256  14a28063294f645d457b1eb10e3c23bbba44398f1c5f021421b58b6f8ab31662 \
                            size    66474

    configure.args-append     --add-module=${workpath}/${devel_kit_distname}
}

variant flv description {Add FLV (Flash Video) streaming support to server} {
    configure.args-append   --with-http_flv_module
}

variant gperftools description {Enable Google Performance Tools profiling for workers} {
    configure.args-append   --with-google_perftools_module
    depends_lib-append      port:gperftools

    # If 'google_perftools' port doesn't find headers needed for the profiler lib, it just quietly skips it.
    # Check if "-lprofiler" will fail -- if so, pre-empt configure-stage error with a more useful error msg.
    pre-configure {
        if {![file exists "${prefix}/lib/libprofiler.dylib"] && ![file exists "${prefix}/lib/libprofiler.a"]} {
            ui_error "\
                The 'gperftools' port did not install a libprofiler library (libprofiler.dylib or\
                libprofiler.a) on your version of macOS, but the +google_perftools variant will not work\
                without this library. Disable the +google_perftools variant to continue installing ${propername}"
            error "Required libprofiler library missing from gperftools port."
        }
    }
}

variant gzip_static description {Avoids compressing the same file each time it is requested} {
    configure.args-append   --with-http_gzip_static_module
}

variant http2 requires ssl description {Add HTTP/2 support to the server} {
    configure.args-append   --with-http_v2_module
}

variant image_filter description {Transform images with libgd} {
    configure.args-append   --with-http_image_filter_module
    depends_lib-append      port:gd2
}

variant mail description {Add IMAP4/POP3 mail proxy support} {
    configure.args-append   --with-mail
    if {[variant_isset ssl]} {
        configure.args-append   --with-mail_ssl_module
    }
}

variant mp4 description {Enables mp4 streaming with seeking ability} {
    configure.args-append   --with-http_mp4_module
}

variant perl5 description {Add perl support to the server directly within nginx and call perl via SSI} {
    depends_lib-append      port:perl5.30
    configure.args-append   --with-http_perl_module \
                            --with-perl=${prefix}/bin/perl5.30
}

variant random_index description {Randomize directory indexes} {
    configure.args-append   --with-http_random_index_module
}

variant realip description {For using nginx as backend} {
    configure.args-append   --with-http_realip_module
}

variant secure_link description {Protect pages with a secret key} {
    configure.args-append   --with-http_secure_link_module
}

variant ssl description {Add SSL (HTTPS) support to the server, and also to the mail proxy if that is enabled} {
    configure.args-append   --with-http_ssl_module
    depends_lib-append      path:lib/libssl.dylib:openssl
}

variant status description {Add /nginx_status support to the server} {
    configure.args-append   --with-http_stub_status_module
}

variant substitution description {Replace text in pages} {
    configure.args-append   --with-http_sub_module
}

variant stream description {Enable ngx_stream_core_module for generic TCP proxying and load balancing (install with +ssl to enable ngx_stream_ssl_module)} {
    configure.args-append   --with-stream
    if {[variant_isset ssl]} {
        configure.args-append \
            --with-stream_ssl_module --with-stream_ssl_preread_module
    }
}

variant threads description {Add threads support (http://nginx.org/en/docs/ngx_core_module.html#thread_pool)} {
    configure.args-append   --with-threads
}

variant xslt description {Post-process pages with XSLT} {
    configure.args-append   --with-http_xslt_module

    depends_lib-append      port:libxslt
}

livecheck.type      regexm
livecheck.url       ${homepage}
livecheck.regex     {nginx-(\d+\.\d+\.\d+)</a>\s+mainline}
