# -*- Mode: Tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem      1.0
PortGroup       github 1.0

name            fltk
categories      aqua devel
license         LGPL
platforms       macosx
maintainers     {michaelld @michaelld} openmaintainer
description     Fast Light Tool Kit
homepage        https://www.fltk.org/

long_description \
    FLTK (pronounced fulltick) \
    is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), \
    Microsoft Windows, and Mac OS X. \
    FLTK provides modern GUI functionality without the bloat \
    and supports 3D graphics via OpenGL and \
    its built-in GLUT emulation. \
    FLTK is designed to be small and modular enough to be \
    statically linked, but works fine as a shared library. \
    FLTK also includes an excellent UI builder called FLUID \
    that can be used to create applications in minutes.

subport fltk-devel {}

if {${subport} eq ${name}} {

    conflicts    fltk-devel
    long_description ${long_description} \
        This port provides the release of FLTK, typically updated a few times a year.

    github.setup    fltk fltk 1.4.4 release-
    worksrcdir      ${github.project}-${github.version}
    distname        ${worksrcdir}-source
    github.tarball_from releases
    use_bzip2       yes
    checksums       rmd160  9805dbd88cecfa1285bcb173d1ae0b76bc4a3305 \
                    sha256  2b302c80b7ea937a8bdc01ed6718fd995035bf63e9a2895491c1001821725f1f \
                    size    8465974
    revision        0

    # overload the github livecheck regex to look for versions that
    # are just numbers and '.', no letters (e.g., "3.7.3_rc2").

    github.livecheck.regex  {([0-9.]+)}

} else {

    conflicts    fltk
    long_description ${long_description} \
        This port provides the snapshot released of FLTK, typically updated a few times a month.

    # Disable devel temporarily, because currently fails build on macOS 15.0.
    # Was fixed upstream.  Need update to fltk-1.4.4 or later.
    #
    #github.setup    fltk fltk 3384776f8e4b88da17b8418ddb3c9d752551bb6f
    # Change github.tarball_from to 'releases' or 'archive' next update
    #github.tarball_from tarball
    #version         20230818-[string range ${github.version} 0 7]
    #checksums       rmd160  28d3005c5002184bf81b2f73f9ac03e14fba9d3d \
    #                sha256  d033a9e9096b0d93b6b9475d8ed8d0bc6827f02ec057228999ded538f9a994ef \
    #                size    7879806
    #revision        0

    # Temporarily substitute the main release version.
    github.setup    fltk fltk 1.4.4 release-
    worksrcdir      ${github.project}-${github.version}
    distname        ${worksrcdir}-source
    github.tarball_from releases
    use_bzip2       yes
    checksums       rmd160  9805dbd88cecfa1285bcb173d1ae0b76bc4a3305 \
                    sha256  2b302c80b7ea937a8bdc01ed6718fd995035bf63e9a2895491c1001821725f1f \
                    size    8465974
    revision        0
    github.livecheck.regex  {([0-9.]+)}
}

depends_build   port:autoconf
depends_lib     path:include/turbojpeg.h:libjpeg-turbo \
                port:libpng \
                port:zlib

post-patch {

    reinplace "s|/Applications|${applications_dir}/fltk|g" \
        ${worksrcpath}/fltk-options/Makefile \
        ${worksrcpath}/fluid/Makefile \
        ${worksrcpath}/test/Makefile

    # Disable silent rules
    reinplace "/^\\.SILENT:$/d" ${worksrcpath}/makeinclude.in

    # tell fltk's make script to create config.sub, which is required
    # before configure will execute properly.  This command actually
    # runs configure through once, to create the file; we then call
    # configure manually with the correct arguments, which overwrites
    # this (and other) files to be what we want.  Make sure CC is set
    # correctly per "using the right compiler".  While this port will
    # work correctly without this addition, doing this allows the port
    # to not error out on dev's systems set up to check whether the
    # right compiler is being used.

    system -W ${worksrcpath} "CC=${configure.cc} make config.sub"

}

pre-configure {
    configure.args-append \
        --with-archflags="[get_canonical_archflags]"
    configure.cc_archflags
    configure.cxx_archflags
    configure.objc_archflags
    configure.objcxx_archflags
    configure.ld_archflags
}

configure.args  --enable-shared \
                --disable-localjpeg \
                --disable-localzlib \
                --disable-localpng

configure.cppflags-delete -I${prefix}/include
configure.ldflags-delete -L${prefix}/lib
configure.universal_args-delete --disable-dependency-tracking

post-configure {
    reinplace -E {s|-arch [a-z0-9_]+||g} ${worksrcpath}/fltk-config
    if {${configure.sdkroot} ne ""} {
        reinplace -E "s|-\[^ \"\]+[quotemeta ${configure.sdkroot}]||g" ${worksrcpath}/fltk-config
    }
}

destroot.target install install-desktop
