# -*- 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            squid
version         7.6
revision        0
categories      net
license         GPL-2+
maintainers     {jmr @jmroot} {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description     Advanced proxy caching server for http, https, ftp
long_description \
                Squid is a high-performance proxy caching server \
                for web clients, supporting FTP and HTTP data objects. \
                Squid keeps metadata and especially hot objects cached in RAM, \
                caches DNS lookups, supports non-blocking DNS lookups \
                and implements negative caching of failed requests. \
                Squid 7.x is the latest release series, and needs \
                a C++17 compiler to build.

homepage        http://www.squid-cache.org
master_sites    https://github.com/squid-cache/squid/releases/download/SQUID_[string map {. _} ${version}]/

use_xz          yes
checksums       rmd160  2c04c11732aa4beaa83938b6a63eb36d0c247124 \
                sha256  852178fdc37c5b0786a934fc990c7d2fffc82acf19b2284be209b96431d25992 \
                size    2436896

patchfiles      patch-compat_types.h.diff \
                patch-basic_pam_auth.cc.diff \
                kerberos_ldap_group_Makefile.in.diff \
                ltdl-lib.patch

# https://github.com/macos-powerpc/powerpc-ports/issues/140
patchfiles-append   patch-O_DSYNC.diff

platform darwin {
    if {${os.major} == 10} {
        # ticket #37102, /usr/include/rpcsvc/yp_prot.h tries to redefine bool
        patchfiles-append   snowleopard-bool.diff
    }
    if {${os.major} == 11} {
        # https://bugs.llvm.org/show_bug.cgi?id=10817
        patchfiles-append   getline-workaround.patch
    }
    # Do not break this.
    # FIXME: see what is needed for libc++ on PowerPC.
    if {${configure.cxx_stdlib} eq "libc++" && (${os.major} > 10 && ${os.major} <= 16)} {
        # bad_optional_access
        depends_lib-append  port:macports-libcxx
        configure.cppflags-prepend  -nostdinc++ -isystem${prefix}/include/libcxx/v1
        configure.ldflags-prepend   -L${prefix}/lib/libcxx
    }
}

depends_build-append \
                path:bin/pkg-config:pkgconfig
depends_lib-append \
                port:libtool \
                port:zlib

conflicts       squid2 squid3

compiler.cxx_standard   2017

options basic_auth
default basic_auth {DB NCSA POP3 RADIUS fake getpwnam}
options digest_auth
default digest_auth {file}
options acl_helpers
default acl_helpers {delayer file_userip unix_group}

# https://github.com/macos-powerpc/powerpc-ports/issues/140
if {${os.platform} ne "darwin" || ${os.major} > 9} {
    basic_auth-append PAM
}

configure.args  --datadir=${prefix}/share/squid \
                --disable-arch-native \
                --disable-strict-error-checking \
                --enable-auth \
                --enable-auth-basic=[join ${basic_auth} ,] \
                --enable-auth-digest=[join ${digest_auth} ,] \
                --enable-auth-negotiate="wrapper" \
                --enable-auth-ntlm \
                --enable-delay-pools \
                --enable-external-acl-helpers=[join ${acl_helpers} ,] \
                --enable-follow-x-forwarded-for \
                --enable-log-daemon-helpers \
                --enable-removal-policies \
                --enable-storeio=ufs,aufs,diskd,rock \
                --enable-zph-qos \
                --libexecdir=${prefix}/libexec/squid \
                --localstatedir=${prefix}/var/squid \
                --mandir=${prefix}/share/man \
                --sysconfdir=${prefix}/etc/squid \
                --with-default-user=squid \
                --with-ltdl-include=${prefix}/include \
                --with-ltdl-lib=${prefix}/lib \
                --with-pidfile=${prefix}/var/run/squid/squid.pid \
                --with-swapdir=${prefix}/var/squid/cache \
                --without-cppunit \
                --without-gnutls \
                --without-gss \
                --without-heimdal-krb5 \
                --without-ldap \
                --without-mit-krb5 \
                --without-nettle \
                --without-openssl \
                --without-sasl \
                --without-tdb \
                --without-xml2

platform darwin {
    # Doesn't work and just generates failure messages in the log
    configure.args-append   --disable-eui
}

post-configure {
    if {[variant_exists universal] && [variant_isset universal]} {
        system -W ${worksrcpath} "ed - include/autoconf.h < ${filespath}/include_autoconf.h.ed && touch include/stamp-h1"
    }
}

startupitem.create      yes
startupitem.name        Squid
startupitem.netchange   yes
startupitem.executable  ${prefix}/sbin/squid -s --foreground

add_users       squid group=squid home=${prefix}/var/squid

notes "Before starting $name for the first time, create the cache directories\
with:
    ${prefix}/sbin/squid -s -z --foreground"

post-destroot {
    xinstall -o squid -g squid -m 755 -d \
        ${destroot}${prefix}/var/run/squid ${destroot}${prefix}/var/squid \
        ${destroot}${prefix}/var/squid/cache ${destroot}${prefix}/var/squid/logs
    file delete -force  ${destroot}${prefix}/etc/squid/squid.conf \
                        ${destroot}${prefix}/etc/squid/mime.conf \
                        ${destroot}${prefix}/etc/squid/errorpage.css
}
destroot.keepdirs   ${destroot}${prefix}/var/run/squid \
                    ${destroot}${prefix}/var/squid/cache \
                    ${destroot}${prefix}/var/squid/logs

post-activate {
    # Make sure initial conf files are present and setup correctly
    foreach f {squid.conf mime.conf errorpage.css} {
        if {![file exists ${prefix}/etc/squid/${f}]} {
            file copy ${prefix}/etc/squid/${f}.default \
                ${prefix}/etc/squid/${f}
        }
    }
}

variant gnutls conflicts openssl description "Enable SSL/TLS support using GnuTLS (experimental)" {
    depends_lib-append      path:lib/pkgconfig/gnutls.pc:gnutls
    configure.args-delete   --without-gnutls
}

variant ipfw_transparent description "Enable transparent proxy support using IPFW" {
    configure.args-append   --enable-ipfw-transparent
}

variant kerberos description "Enable MIT kerberos support" {
    depends_lib-append      port:kerberos5
    configure.args-delete   --without-mit-krb5
    configure.args-replace  --enable-auth-negotiate="wrapper" --enable-auth-negotiate
    acl_helpers-append      kerberos_sid_group
}

variant ldap description "Enable LDAP support" {
    depends_lib-append      path:lib/libldap.dylib:openldap
    configure.args-delete   --without-ldap
    basic_auth-append       LDAP
    digest_auth-append      eDirectory LDAP
    acl_helpers-append      LDAP_group eDirectory_userip
}

variant openssl conflicts gnutls description "Enable SSL/TLS support using OpenSSL" {
    depends_lib-append      path:lib/libssl.dylib:openssl
    configure.args-replace  --without-openssl --with-openssl=${prefix}
}

variant sasl description "Enable SASL support" {
    depends_lib-append      port:cyrus-sasl2
    configure.args-delete   --without-sasl
    basic_auth-append       SASL
}

variant smb description "Enable SMB support" {
    depends_lib-append      port:samba4
    configure.args-delete   --without-tdb
    basic_auth-append       SMB
    acl_helpers-append      wbinfo_group
}

variant ssl_crtd requires openssl description "Enable use of external certificate generator (for SSL-Bump)" {
    configure.args-append   --enable-ssl-crtd
}

if {![variant_isset gnutls]} {
    default_variants        +openssl
}

livecheck.type  regex
livecheck.url   https://api.github.com/repos/squid-cache/squid/releases/latest
livecheck.regex {"name":\s+"v([0-9.]+)",}
