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

# Note: pg legacysupport must be included before makefile
if {[variant_isset full]} {
    PortGroup       legacysupport 1.1

    # clock_gettime, used in +full
    legacysupport.newest_darwin_requires_legacy 15
}

PortGroup           makefile 1.0

github.setup        dlbeer quirc 1.2 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                lib${github.project}
revision            1
categories          graphics math
license             ISC
maintainers         {mascguy @mascguy} openmaintainer

description         QR decoder library
long_description    QR codes are a type of high-density matrix barcodes, and quirc \
                    is a library for extracting and decoding them from images.

checksums           rmd160  1520b02c9c153c3eff845fd5fb3317695e215204 \
                    sha256  39295a364c933bc701a7c93acf86d35a297d253570ccf3c556c2dfe44ea485c3 \
                    size    42927

# diff -NaurdwB ./quirc-orig ./quirc-new | sed -E -e 's/\.\/.+-(orig|new)\//\.\//' | sed -E -e 's|/opt/local|@PREFIX@|g' > ~/Downloads/patch-quirc.diff
patchfiles          patch-quirc.diff

post-patch {
    reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
                    Makefile
    reinplace -W ${worksrcpath} "s|@MACPORTSUSER@|${macportsuser}|g" \
                    Makefile
}

depends_build-append \
                    port:gmake

compiler.c_standard   1999
compiler.cxx_standard 2017

compiler.cpath-append \
                    ${workpath}/include
configure.cflags-append \
                    -I${workpath}/include \
                    -std=c99

build.cmd           ${prefix}/bin/gmake

variant full description {Full quirc installation} {
    post-patch {
        xinstall -d ${workpath}/include

        # https://raw.githubusercontent.com/openbsd/src/master/sys/sys/videoio.h
        xinstall -m 0644 ${filespath}/videoio.h ${workpath}/include
    }

    depends_build-append \
                    port:pkgconfig

    depends_lib-append  \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    port:libsdl \
                    port:libsdl_gfx

    configure.cflags-prepend \
                    -I${prefix}/include/SDL
}

if { [variant_isset "full"] } {
    foreach p {build destroot} {
        ${p}.args-append \
                    BUILD_QUIRC=ON
    }

    destroot.post_args-append \
                    PREFIX=${prefix}
} else {
    build.target    ${name}.dylib

    destroot {
        xinstall -m 0644 \
                    ${worksrcpath}/${build_dylib} \
                    ${destroot}${prefix}/lib/${build_dylib}
        xinstall -m 0644 \
                    ${worksrcpath}/lib/${github.project}.h \
                    ${destroot}${prefix}/include
    }
}

set build_dylib     ${name}.dylib
pre-destroot {
    set build_dylib [file tail [glob ${worksrcpath}/${name}.*.dylib]]
}

post-destroot {
    ln -s           ${build_dylib} \
                    ${destroot}${prefix}/lib/${name}.dylib
}
