# -*- 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
PortGroup           active_variants 1.1

github.setup        openlink iODBC 3.52.16 v
github.tarball_from releases
#override name (keep it lowercase)
name                libiodbc
revision            1
categories          devel
maintainers         {snc @nerdling} openmaintainer
license             BSD
description         Independent Open DataBase Connectivity
long_description    iODBC is the acronym for Independent Open DataBase Connectivity, \
                    an Open Source platform independent implementation of both the \
                    ODBC and X/Open specifications.

distname            ${name}-${version}

checksums           rmd160  5b8a762b5df534f2a10a61fa80ddfe4144bed8cf \
                    sha256  3898b32d07961360f6f2cf36db36036b719a230e476469258a80f32243e845fa \
                    size    1086850

patchfiles-append   patch-iodbcinst-unicode.h.diff patch-mac-link-inclibs.diff

configure.args-append   --disable-libodbc

variant libodbc description {install extra libodbc.a library} {
    configure.args-replace  --disable-libodbc --enable-libodbc
    configure.args-delete   --includedir=${prefix}/include/${name}
    conflicts               unixODBC
}

variant x11 conflicts gui {
    depends_build-append \
                        port:pkgconfig

    depends_lib-append  port:atk \
                        path:lib/pkgconfig/cairo.pc:cairo \
                        port:fontconfig \
                        port:freetype \
                        path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                        port:gettext \
                        path:lib/pkgconfig/glib-2.0.pc:glib2 \
                        path:lib/pkgconfig/gtk+-2.0.pc:gtk2 \
                        path:lib/pkgconfig/pango.pc:pango \
                        port:xorg-libX11 \
                        port:xorg-libXcomposite \
                        port:xorg-libXcursor \
                        port:xorg-libXdamage \
                        port:xorg-libXext \
                        port:xorg-libXfixes \
                        port:xorg-libXi \
                        port:xorg-libXinerama \
                        port:xorg-libXrandr \
                        port:xrender

    require_active_variants cairo x11
    require_active_variants gtk2 x11
    require_active_variants pango x11

    configure.args-delete   --disable-gui
}

build.args

variant gui conflicts x11 description {install admin GUI} {
    post-patch {
        reinplace "s|@IODBC_VERSION@|${version}|" \
            ${worksrcpath}/mac/iodbc-config.macos
        reinplace "s|/usr/local/iODBC|${prefix}|" \
            ${worksrcpath}/mac/iodbc-config.macos \
            ${worksrcpath}/mac/link-inclibs.sh
        reinplace "s|/Applications|${applications_dir}|" \
            {*}[glob ${worksrcpath}/mac/*/*/project.pbxproj] \
            {*}[glob ${worksrcpath}/mac/*/*/*/project.pbxproj]
        reinplace "s|/Library/Application Support/iODBC/bin|${prefix}/bin|" \
            ${worksrcpath}/mac/iODBCtest/iODBCtest.xcodeproj/project.pbxproj \
            ${worksrcpath}/mac/iODBCtestw/iODBCtestw.xcodeproj/project.pbxproj
        reinplace -E "s|(\[ \")])/Library/Frameworks|\\1${frameworks_dir}|" \
            ${worksrcpath}/mac/GNUmakefile \
            ${worksrcpath}/mac/link-inclibs.sh \
            {*}[glob ${worksrcpath}/mac/*/*/project.pbxproj] \
            {*}[glob ${worksrcpath}/mac/*/*/*/project.pbxproj]
        reinplace -E "s|(MACOSX_DEPLOYMENT_TARGET) = 10.9|\\1 = ${macosx_deployment_target}|" \
            {*}[glob ${worksrcpath}/mac/*/*/project.pbxproj] \
            {*}[glob ${worksrcpath}/mac/*/*/*/project.pbxproj]
    }

    use_xcode           yes
    build.dir           ${worksrcpath}/mac
    build.args          MODEL="Deployment -IDECustomDerivedDataLocation=${workpath}/DerivedData"

    platform darwin {
        if {${os.major} >= 19} {
            build.args-append   IODBC_32BIT=x86_64
        }
    }

    destroot.args       {*}${build.args}

    post-destroot {
        copy ${worksrcpath}/man/iodbc-config.1 \
            ${worksrcpath}/man/iodbctest.1 \
            ${worksrcpath}/man/iodbctestw.1 \
            ${destroot}${prefix}/share/man/man1

        if {[variant_isset libodbc]} {
            ln -s libiodbc.dylib ${destroot}${prefix}/lib/libodbc.dylib
        } else {
            xinstall -d ${destroot}${prefix}/include/${name}
            move {*}[glob ${destroot}${prefix}/include/*.h] \
                ${destroot}${prefix}/include/${name}
        }
    }
}

default_variants +libodbc

configure.args-append   --disable-gui \
                        --includedir=${prefix}/include/${name} \
                        --with-iodbc-inidir=${prefix}/etc

# make[2]: *** No rule to make target `../iodbcadm/libiodbcadm.la', needed by `iodbcadm-gtk'.  Stop.
use_parallel_build  no
