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

github.setup        gdraheim zziplib 0.13.74 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0
checksums           rmd160  152bcf39c6520b31fb52ead52893896b36526d4f \
                    sha256  310bed3c8341c2d688cf58fd3434bf6a63eb84b4fdc9e0326fbfdb39a52c4045 \
                    size    1177323

name                libzzip
categories          archivers devel
license             LGPL
maintainers         nomaintainer
description         library providing read access on ZIP-archives

long_description    The ZZIPlib provides read access on ZIP-archives. The \
                    library uses only the patent-free compression-algorithms \
                    supported by Zlib. It provides functions that transparently \
                    access files being either real files or zipped files, \
                    both with the same filepath.

set python.branch   3.12
set python.version  [join [split ${python.branch} "."] ""]

depends_build-append \
                    path:bin/pkg-config:pkgconfig \
                    port:python${python.version}

depends_lib-append  port:zlib

depends_test-append port:unzip \
                    port:zip

post-patch {
    # Fix version number.
    # https://github.com/gdraheim/zziplib/issues/155
    foreach f [list \
        bins/CMakeLists.txt \
        CMakeLists.txt \
        docs/CMakeLists.txt \
        docs/tools/md2dbk.py \
        SDL/CMakeLists.txt \
        test/CMakeLists.txt \
        testbuilds.py \
        zzip/CMakeLists.txt \
        zziplib.spec \
        zzipwrap/CMakeLists.txt \
    ] {
        reinplace "s|0\.13\.72|${version}|g" ${worksrcpath}/${f}
    }
}

# this build links its own library, so => Disable RPATH
# See: https://github.com/gdraheim/zziplib/issues/121
cmake.install_rpath
configure.pre_args-delete \
    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
    -DCMAKE_INSTALL_RPATH="[join [option cmake.install_rpath] \;]"

configure.args-append \
                    -DPYTHON_EXECUTABLE=${prefix}/bin/python${python.branch} \
                    -DZZIPSDL=OFF

post-destroot {
    set docdir ${prefix}/share/doc/${name}

    xinstall -d ${destroot}${docdir}/html

    xinstall -m 0644 -W ${worksrcpath} COPYING.LIB ChangeLog README TODO \
        ${destroot}${docdir}

    xinstall -m 0644 {*}[glob -directory ${worksrcpath}/docs "*.{css,htm}"] \
        ${destroot}${docdir}/html

    # We replace every soelim stub with a soft link, as ".so" groff directives
    # don't work with gzip'd manpages.

    foreach {f} [glob -directory ${destroot}${prefix}/share/man/man3 *.3] {
        set channel [open ${f}]

        if {[regexp {^\.so (.+)$} [read -nonewline ${channel}] _ target]} {
            # This manpage is a soelim stub.
            ln -sf [file tail ${target}] ${f}
        }

        close ${channel}
    }

    if {[variant_isset sdl]} {
        xinstall -m 0644 ${worksrcpath}/docs/README.SDL ${destroot}${docdir}
    }
}

variant sdl description {Enable SDL support} {
    depends_lib-append      port:libsdl

    configure.args-replace  -DZZIPSDL=OFF -DZZIPSDL=ON
}

test.run        yes
test.target     check
