# -*- 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
# $Id: Portfile 120309 2014-05-22 21:14:09Z cal@macports.org $

PortSystem          1.0

name                nginx
version             1.6.0
categories          www mail
platforms           darwin
license             BSD
maintainers         cal openmaintainer

description         High-performance HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
long_description    \
    Nginx ("engine x") is a high-performance HTTP(S) server and reverse proxy, \
    as well as an IMAP/POP3 proxy server. Nginx was written by Igor Sysoev for \
    Rambler.ru, Russia's second-most visited website, where it has been running \
    in production for over two and a half years. Igor has released the source \
    code under a BSD-like license. Although still in beta, Nginx is known for \
    its stability, rich feature set, simple configuration, and low resource \
    consumption.

homepage            http://nginx.org/
master_sites        http://nginx.org/download:nginx \
                    http://www.grid.net.ru/nginx/download/:upload_module \
                    googlecode:mod-zip:zip_module \
                    http://people.freebsd.org/~osa/:redis_module \
                    https://github.com/downloads/masterzen/nginx-upload-progress-module:uploadprogress_module \
                    https://github.com/arut/nginx-dav-ext-module/archive/:davext_module \
                    https://github.com/arut/nginx-rtmp-module/archive/:rtmp_module

distfiles           ${name}-${version}${extract.suffix}:nginx
checksums           ${name}-${version}${extract.suffix} \
                    rmd160  a19cd6d7bdb642049901a4e184a69e434753c021 \
                    sha256  943ad757a1c3e8b3df2d5c4ddacc508861922e36fa10ea6f8e3a348fc9abfc1a

depends_lib         port:pcre \
                    port:zlib

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

set nginx_confdir   ${prefix}/etc/${name}
set nginx_logdir    ${prefix}/var/log/${name}
set nginx_rundir    ${prefix}/var/run/${name}
set nginx_pidfile   ${nginx_rundir}/${name}.pid
set nginx_all_confs {fastcgi.conf fastcgi_params mime.types nginx.conf scgi_params uwsgi_params}

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

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


configure.args-append \
                    --with-cc-opt=\"${configure.cppflags} ${configure.cflags}\" \
                    --with-ld-opt=\"${configure.ldflags}\" \
                    --conf-path=${nginx_confdir}/${name}.conf \
                    --error-log-path=${nginx_logdir}/error.log \
                    --http-log-path=${nginx_logdir}/access.log \
                    --pid-path=${nginx_pidfile} \
                    --lock-path=${nginx_rundir}/${name}.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-ipv6

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

build.target        build
destroot.keepdirs   ${destroot}${nginx_logdir} \
                    ${destroot}${nginx_rundir}

post-destroot {
    foreach conf ${nginx_all_confs} {
        set conf_path "${destroot}${nginx_confdir}/${conf}"
        if {[file exists ${conf_path}]} {
            file delete ${conf_path}
        }
    }
    file mkdir ${destroot}${prefix}/share/nginx
    file rename ${destroot}${prefix}/html ${destroot}${prefix}/share/nginx
}

startupitem.create      yes
startupitem.pidfile     auto ${nginx_pidfile}
startupitem.executable  ${prefix}/sbin/nginx

notes                   \
    "A sample nginx configuration file has been installed in
    ${nginx_confdir}/nginx.conf.default.\nCopy the configuration to
    ${nginx_confdir}/nginx.conf\nand adjust it to your needs before starting\
    nginx."

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 davext description {Add WebDAV (extended) support to server} {
    set davext_filename     nginx-dav-ext-module
    set davext_version      0.0.3
    set davext_distname     ${davext_filename}-${davext_version}
    distfiles-append        v${davext_version}.tar.gz:davext_module
    checksums-append        v${davext_version}.tar.gz \
                            rmd160  0d407e687bd59e81c2c9805ee8b39b8b85d43d08 \
                            sha256  d428a0236c933779cb40ac8c91afb19d5c25a376dc3caab825bfd543e1ee530d
    configure.args-append   --with-http_dav_module --add-module=${workpath}/${davext_distname}
}

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 flv description {Add FLV (Flash Video) streaming support to server} {
    configure.args-append   --with-http_flv_module
}

variant geoip description {Enable Ngx http GeoIP module (http://wiki.nginx.org/HttpGeoIPModule)} {
    configure.args-append   --with-http_geoip_module
    depends_lib-append      port:libgeoip
}

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

    # 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 'google-perftools' port did not install a libprofiler library (libprofiler.dylib or\
                libprofiler.a) on your version of OS X, but the +google_perftools variant will not work\
                without this library. Disable the +google_perftools variant to continue installing ${name}"
            error "Required libprofiler library missing from google-perftools port."
        }
    }
}

variant gzip_static description {Avoids compressing the same file each time it is requested} {
    configure.args-append   --with-http_gzip_static_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.16
    configure.args-append   --with-http_perl_module \
                            --with-perl=${prefix}/bin/perl5.16
}

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      port:openssl
}

variant spdy requires ssl description {Add SPDY support to the server} {
    configure.args-append   --with-http_spdy_module
}

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 upload description {Enable Valery Kholodkov's upload module (http://grid.net.ru/nginx/upload.en.html)} {
    set upload_filename     nginx_upload_module
    set upload_version      2.2.0
    set upload_distname     ${upload_filename}-${upload_version}
    distfiles-append        ${upload_distname}.tar.gz:upload_module
    checksums-append        ${upload_distname}.tar.gz \
                            rmd160  5734af837be3fe8ec444a7e5e7f6707118594098 \
                            sha256  b1c26abe0427180602e257627b4ed21848c93cc20cefc33af084983767d65805
    configure.args-append   --add-module=${workpath}/${upload_distname}

    # v2.2.0 release references members of ngx_http_request_body_t that were renamed in later nginx versions.
    # Patch nginx_upload_module.c with fixed version from https://github.com/TimothyKlim/nginx-upload-module.
    patchfiles-append       patch-nginx_upload_module.tmp-ngx_http_upload_module.c.diff


    nginx_set_portfile_var   nginx_upload_mod_dirname    "${upload_distname}"

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

variant upload_progress description {Enable Brice Figureau's upload progress module (https://github.com/masterzen/nginx-upload-progress-module)} {
    set uploadprg_filename  nginx_uploadprogress_module
    set uploadprg_version   0.9.0
    set uploadprg_distname  ${uploadprg_filename}-${uploadprg_version}
    distfiles-append        ${uploadprg_distname}.tar.gz:uploadprogress_module
    checksums-append        ${uploadprg_distname}.tar.gz \
                            rmd160  10ff91748a06afd81abb9fdb203f164ea369463a \
                            sha256  3fb903dab595cf6656fa0fc5743a48daffbba2f6b5c554836be630800eaad4e2
    configure.args-append   --add-module=${workpath}/${uploadprg_distname}

    nginx_set_portfile_var  nginx_upload_progress_mod_dirname "${uploadprg_distname}"
    post-extract {
        move [glob -directory ${workpath} masterzen-nginx-upload-progress-module*] \
            "${workpath}/${nginx_upload_progress_mod_dirname}"
    }
}

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

    depends_lib-append      port:libxslt
}

variant zip description {Enable Ngx zip download module \
        (http://code.google.com/p/mod-zip/)} {

    set zip_distname        mod_zip-1.1.6
    distfiles-append        ${zip_distname}.tar.gz:zip_module
    checksums-append        ${zip_distname}.tar.gz \
                            rmd160  17a465d87b8e1ba71bdd6ad40e20d21cc7dcd434 \
                            sha256  d30546b4be8308c721df0240ebc38c474c6b577381a110321eded6dbed5104b1
    configure.args-append   --add-module=${workpath}/${zip_distname}
}

variant redis description {Enable Ngx HTTP Redis module} {
    set redis_filename      ngx_http_redis
    set redis_version       0.3.6
    set redis_distname      ${redis_filename}-${redis_version}
    distfiles-append        ${redis_distname}.tar.gz:redis_module
    checksums-append        ${redis_distname}.tar.gz \
                            rmd160  0d1de19ce1ae8b9b7fcd9693908403e613f5391d \
                            sha256  9e5d8c0f1317a6910710c94d27f9aca4968c48ee1de271e5034525a1e9b6c70e
    configure.args-append   --add-module=${workpath}/${redis_distname}
}

variant rtmp description {Enable Ngx RTMP module (https://github.com/arut/nginx-rtmp-module)} {
    set rtmp_filename       nginx-rtmp-module
    set rtmp_version        1.1.4
    set rtmp_distname       ${rtmp_filename}-${rtmp_version}
    distfiles-append        v${rtmp_version}.tar.gz:rtmp_module
    checksums-append        v${rtmp_version}.tar.gz \
                            rmd160  c6338c64bab5c1c4aa10f908439fb1a5caa86ffc \
                            sha256  076efef9698076fa23622e093cfb3959e4d14fcd6e9dcdf0b8e6d9154c720dc9
    configure.args-append   --add-module=${workpath}/${rtmp_distname}
}

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