# -*- 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                xapian-core
version             1.4.27
categories          devel
maintainers         {ryandesign @ryandesign} openmaintainer
license             GPL-2+

description         Xapian search library
long_description    Xapian is a highly adaptable toolkit which allows \
                    developers to easily add advanced indexing and search \
                    facilities to their own applications. It supports the \
                    Probabilistic Information Retrieval model and also \
                    supports a rich set of boolean query operators.

homepage            https://xapian.org
master_sites        https://oligarchy.co.uk/xapian/${version}/
dist_subdir         xapian
use_xz              yes

compiler.cxx_standard \
                    2011

if {${subport} eq ${name}} {
    revision                    0

    # version.h does not get generated properly when there are multiple -arch flags
    PortGroup                   muniversal 1.0

    checksums                   rmd160  344e11ede45691dd6f76a35690838acbd6ad216b \
                                sha256  bcbc99cfbf16080119c2571fc296794f539bd542ca3926f17c2999600830ab61 \
                                size    3246624

    # TODO: Fix xapian-config to not require the .la file
    # /opt/local/bin/xapian-config --ltlibs --cxxflags
    # /opt/local/bin/xapian-config: Can't find libxapian.la to link against.
    destroot.delete_la_files    no

    depends_lib                 port:zlib

    post-build {
        if {[variant_exists universal] && [variant_isset universal]} {
            foreach arch ${universal_archs_to_use} {
                # xapian-config records build directory, which interferes with universal merge
                reinplace "s|${worksrcpath}-${arch}|${worksrcpath}|g" ${worksrcpath}-${arch}/xapian-config
            }
        }
    }

    livecheck.type              regex
    livecheck.url               ${homepage}/download
    livecheck.regex             ${name}-(\[0-9\]+\\.\[0-9\]*\[02468\](\\.\[0-9\]+)*)[quotemeta ${extract.suffix}]
} else {
    livecheck.type              none
}

subport xapian-omega {
    revision                    0
    checksums                   rmd160  bb9f9f8e497ac97fa506804114794799215de625 \
                                sha256  1d193b3285ec150557257b049ee1d8c94c40bcde906ce0ba7f1a38a1a9a5a5c1 \
                                size    583960

    description                 web application using the Xapian library
    long_description            Xapian Omega is an application built on the \
                                Xapian library, consisting of indexers and a \
                                CGI search frontend.

    homepage                    ${homepage}/docs/omega/overview.html

    distname                    xapian-omega-${version}

    depends_lib                 port:libiconv \
                                port:libmagic \
                                port:pcre2 \
                                port:xapian-core \
                                port:zlib

    configure.args              --with-iconv
}

if {${subport} ni [list ${name} xapian-omega]} {
    checksums                   rmd160  beec2bc2b651c54e97c2649db30c1c0240aedc72 \
                                sha256  ba3b5e10809e579acd11bd165779ce3fd29a8904ea37968ef5b57ad97c3618ba \
                                size    1116236

    homepage                    ${homepage}/docs/bindings/

    distname                    xapian-bindings-${version}

    depends_lib                 port:xapian-core

    configure.args              --without-csharp \
                                --without-java \
                                --without-lua \
                                --without-perl \
                                --without-python \
                                --without-python3 \
                                --without-php \
                                --without-ruby \
                                --without-tcl
}

# Java
subport xapian-bindings-java {
    PortGroup                   java 1.0

    revision                    0

    description                 Xapian bindings for Java
    long_description            {*}${description}

    # Required java version
    java.version                6+
    # JDK port to install if required java not found
    java.fallback               openjdk11

    configure.args-replace      --without-java --with-java

    # jni destination violates the mtree layout
    destroot.violate_mtree  yes

    # https://trac.xapian.org/ticket/774
    post-destroot {
        file mkdir ${destroot}/Library/Java/Extensions
        move ${destroot}/${worksrcpath}/java/built/libxapian_jni.jnilib \
            ${destroot}/Library/Java/Extensions/libxapian_jni.jnilib
    }
}

# Perl
foreach v {5.28 5.30 5.32 5.34 5.36 5.38} {
    subport xapian-bindings-perl${v} "
        revision                0

        description             Xapian bindings for Perl ${v}
        long_description        \{*\}\${description}

        depends_lib-append      port:perl${v}

        configure.env-append    PERL=${prefix}/bin/perl${v}
        configure.args-replace  --without-perl --with-perl

        post-destroot {
            move ${destroot}${prefix}/share/doc/xapian-bindings/perl ${destroot}${prefix}/share/doc/xapian-bindings/perl${v}
        }
    "
}

# PHP
foreach v {8.0 8.1 8.2 8.3} {
    set v_no_dot [string map {. {}} ${v}]
    set php php${v_no_dot}
    subport ${php}-xapian "
        revision                0

        description             Xapian bindings for PHP ${v}
        long_description        \{*\}\${description}

        depends_lib-append      port:${php}

        configure.env-append    PHP_CONFIG=${prefix}/bin/php-config${v_no_dot}
        configure.args-replace  --without-php --with-php

        post-destroot {
            move ${destroot}${prefix}/share/doc/xapian-bindings/php ${destroot}${prefix}/share/doc/xapian-bindings/${php}
            xinstall -m 0755 -d ${destroot}${prefix}/var/db/${php}
            set fp \[open ${destroot}${prefix}/var/db/${php}/xapian.ini w\]
            puts \${fp} {; Do not edit this file; it is automatically generated by MacPorts. Any changes}
            puts \${fp} {; you make will be lost if you upgrade, uninstall or deactivate ${subport}.}
            puts \${fp} \{; To configure ${php}, edit ${prefix}/etc/${php}/php.ini.\}
            puts \${fp} {extension=xapian.so}
            close \${fp}
        }
    "
}

# Python
foreach v {3.8 3.9 3.10 3.11 3.12 3.13} {
    set v_no_dot [string map {. {}} ${v}]
    if {[vercmp ${v} 3] >= 0} {
        set suffix 3
    } else {
        set suffix {}
    }
    subport xapian-bindings-python${v_no_dot} "
        revision                0

        description             Xapian bindings for Python ${v}
        long_description        \{*\}\${description}

        depends_build-append    port:py${v_no_dot}-sphinx

        depends_lib-append      port:python${v_no_dot}

        configure.env-append    PYTHON${suffix}=${prefix}/bin/python${v}
        configure.args-replace  --without-python${suffix} --with-python${suffix}

        post-destroot {
            move ${destroot}${prefix}/share/doc/xapian-bindings/python${suffix} ${destroot}${prefix}/share/doc/xapian-bindings/python${v_no_dot}
        }
    "
}

# Ruby
foreach v {2.7 3.0 3.1 3.2 3.3 3.4} {
    set v_no_dot [string map {. {}} ${v}]
    subport xapian-bindings-ruby${v_no_dot} "
        revision                0

        description             Xapian bindings for Ruby ${v}
        long_description        \{*\}\${description}

        depends_lib-append      port:ruby${v_no_dot}

        configure.env-append    RUBY=${prefix}/bin/ruby${v}
        configure.args-replace  --without-ruby --with-ruby

        post-destroot {
            move ${destroot}${prefix}/share/doc/xapian-bindings/ruby ${destroot}${prefix}/share/doc/xapian-bindings/ruby${v_no_dot}
        }
    "
}
