# -*- 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
PortGroup             conflicts_build 1.0
PortGroup             github 1.0
PortGroup             legacysupport 1.1

github.setup          varnishcache varnish-cache 7.6.0 varnish-
github.tarball_from   archive
name                  varnish
epoch                 20110709
revision              0
categories            www
maintainers           {wohner.eu:normen @Gminfly} {@catap korins.ky:kirill} openmaintainer
license               BSD

description           Varnish is a state-of-the-art, high-performance HTTP accelerator
long_description      Varnish was written from the ground up to be a high \
                      performance caching reverse proxy.

homepage              https://varnish-cache.org/

checksums             rmd160  a7a8cc1c9819f60d23997b243c901cebbfa0370b \
                      sha256  7565fe0311043b58a2e86191d85ea6c015c12169b5f021333fb31939c0984928 \
                      size    2088151

set python_branch     3.12
set python_version    [string map {. {}} ${python_branch}]

depends_build         path:bin/pkg-config:pkgconfig \
                      port:py${python_version}-docutils \
                      port:py${python_version}-sphinx

depends_lib           port:pcre2

# https://github.com/varnishcache/varnish-cache/issues/4192
conflicts_build       umem

use_autoreconf        yes
autoreconf.args       -fi

# needs MAP_ANONYMOUS
legacysupport.newest_darwin_requires_legacy 14

configure.args-append --disable-silent-rules \
                      --with-rst2man=${prefix}/bin/rst2man-${python_branch} \
                      --with-rst2html=${prefix}/bin/rst2html-${python_branch} \
                      --with-sphinx-build=${prefix}/bin/sphinx-build-${python_branch} \
                      PYTHON=${prefix}/bin/python${python_branch}

platform darwin powerpc {
    # cc1: warnings being treated as errors
    patchfiles-append patch-no-werror.diff
}

startupitem.create    yes
startupitem.pidfile   auto "${prefix}/var/run/${name}/${name}.pid"
startupitem.start     "${prefix}/share/${name}/varnish.init start"
startupitem.stop      "${prefix}/share/${name}/varnish.init stop"
startupitem.restart   "${prefix}/share/${name}/varnish.init restart"

post-destroot {
    # create dir
    xinstall -d -m 755 ${destroot}${prefix}/etc/${name}

    # copy files
    xinstall -m 644 ${filespath}/varnish.conf.in ${destroot}${prefix}/etc/${name}/varnish.conf.default
    xinstall -m 755 ${filespath}/varnish.init.in ${destroot}${prefix}/share/${name}/${name}.init
    xinstall -m 755 ${filespath}/varnish-vcl-reload.in ${destroot}${prefix}/sbin/varnish-vcl-reload

    # replace @PREFIX@ to ${prefix}
    reinplace "s|@PREFIX@|${prefix}|g" \
        ${destroot}${prefix}/etc/${name}/varnish.conf.default \
        ${destroot}${prefix}/share/${name}/${name}.init \
        ${destroot}${prefix}/sbin/varnish-vcl-reload

    copy ${destroot}${prefix}/share/doc/${name}/example.vcl ${destroot}${prefix}/etc/${name}/default.vcl.default
}

post-activate {
    if {![file exists ${prefix}/etc/${name}/default.vcl]} {
        file copy ${prefix}/etc/${name}/default.vcl.default \
            ${prefix}/etc/${name}/default.vcl
    }
    if {![file exists ${prefix}/etc/${name}/varnish.conf]} {
        file copy ${prefix}/etc/${name}/varnish.conf.default \
            ${prefix}/etc/${name}/varnish.conf
    }

    # dirs nedded to run varnish
    xinstall -d -m 755 -o nobody -g nobody ${prefix}/var/${name}
    xinstall -d -m 755 -o nobody -g nobody ${prefix}/var/run/${name}
}
