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

name                            Togl
# The last release of Togl was 2008 (version 2.0).
# The last CVS commit was 2010 (see http://togl.cvs.sourceforge.net).
# The developers of Netgen seem to be updating a local copy and calling it version 2.1.
# See https://github.com/NGSolve/netgen/tree/master/ng.
github.setup                    NGSolve netgen 6.2.2307 v
version                         2.1
revision                        6
categories                      x11
license                         permissive
maintainers                     {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description                     A Tcl/Tk widget for OpenGL rendering
long_description                Togl is a Tk widget for OpenGL rendering. Togl was \
                                originally based on OGLTK, written by Benjamin Bederson \
                                at the University of New Mexico.
homepage                        https://togl.sourceforge.net/
dist_subdir                     netgen

checksums                       rmd160  1a37d234b5081600b572f1e0a86c97c0e233ebf6 \
                                sha256  e62a9cca2e727797dddadf04de308594f46863f2cac47adb5f51bd8b8c7884aa \
                                size    3427060
github.tarball_from             archive

worksrcdir                      ${worksrcdir}/ng/Togl${version}

conflicts                       Togl-2.0

depends_lib-append              port:tcl

# It does not build with Xcode gcc.
compiler.blacklist-append       *gcc-4.0 *gcc-4.2

configure.args-append           --with-tcl=${prefix}/lib

configure.universal_args-delete --disable-dependency-tracking

if {${os.platform} eq "darwin" && ${os.subplatform} eq "macosx"
    && ${os.major} >= 10 && ![string match ppc* ${build_arch}]} {

    variant quartz conflicts x11 {
        depends_lib-append  port:tk-quartz
        configure.args-append       --with-tk=${prefix}/lib/tk-quartz \
                                    --with-tkinclude=${prefix}/include/tk-quartz
        configure.cppflags-prepend  -I${prefix}/include/tk-quartz
        # see https://github.com/NGSolve/netgen/blob/master/CMakeLists.txt#L250
        configure.cppflags-append   -DTOGL_NSOPENGL
        # see https://github.com/NGSolve/netgen/blob/master/CMakeLists.txt#L221
        configure.ldflags-append    -undefined dynamic_lookup
    }
    if {![variant_isset x11]} {
        default_variants +quartz
    }
    set x11conflicts quartz
} else {
    default_variants +x11
    set x11conflicts {}
}

variant x11 conflicts {*}${x11conflicts} {
    depends_lib-append          port:tk-x11 \
                                port:xorg-libX11 \
                                port:xorg-libXmu \
                                port:mesa
    configure.args-append       --with-tk=${prefix}/lib/tk-x11 \
                                --with-tkinclude=${prefix}/include/tk-x11 \
                                --with-Xmu
    configure.cppflags-prepend  -I${prefix}/include/tk-x11
    # see https://github.com/NGSolve/netgen/blob/master/CMakeLists.txt#L252
    configure.cppflags-append   -DTOGL_X11
}

pre-configure {
    if {!([variant_exists quartz] && [variant_isset quartz]) && ![variant_isset x11]} {
        error "Either +x11 or +quartz is required"
    }
}

post-destroot {
    set libpath                 ${prefix}/lib/Togl${version}
    system                      -W ${destroot}${libpath} \
                                "install_name_tool -id ${prefix}/lib/libTogl.dylib ${destroot}${libpath}/libTogl${version}.dylib"
    ln -s                       Togl${version}/libTogl${version}.dylib \
                                ${destroot}${prefix}/lib/libTogl.dylib
}
