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

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

github.setup        ultimatepp ultimatepp 2026.1 v
revision            1
categories          devel
license             BSD
maintainers         {@barracuda156 macos-powerpc.org:barracuda}
description         U++ is a C++ cross-platform rapid application development framework
long_description    {*}${description}, focused on programmers productivity. It includes \
                    a set of libraries (GUI, SQL, Network etc.) with an integrated \
                    development environment. \
                    Rapid development is achieved by the smart and aggressive use of C++ \
                    rather than through fancy code generators. TheIDE is the integrated \
                    development environment that features BLITZ – build technology to speed \
                    C++ rebuilds up, visual designers and a fully-featured debugger.
homepage            https://www.ultimatepp.org
checksums           rmd160  43120533e0bf8dce02427c70fb2280b6bd276001 \
                    sha256  54a13f169208c3a8bae05e5b4ed592bda37fea2389d8ce9b240ff12704fa0f86 \
                    size    120043961
github.tarball_from archive

depends_build-append path:bin/pkg-config:pkgconfig

depends_lib-append  port:libpng \
                    path:lib/pkgconfig/freetype2.pc:freetype \
                    path:lib/libssl.dylib:openssl \
                    port:zlib

# TODO: add X11 and SDL variants?

compiler.cxx_standard   2017

patch.pre_args-replace  -p0 -p1

# TODO: legacy-support should be de-coupled from gcc-specific stuff.
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    patchfiles-append \
                    0001-Fixes-for-powerpc-darwin.patch \
                    0002-darwin-makefiles.diff \
                    0003-Fix-key-doubling-and-remove-debug-instrumentation-fr.patch \
                    0004-Fix-broken-non-space-key-input-on-native-10.6-PPC.patch

    post-patch {
        reinplace "s|@CXX@|${configure.cxx}|" ${worksrcpath}/uppsrc/ide/Builders/Install.cpp
        reinplace "s|@PREFIX@|${prefix}|g" \
            ${worksrcpath}/Makefile.in \
            ${worksrcpath}/umkMakefile.in \
            ${worksrcpath}/uppsrc/ide/Builders/Install.cpp

        # Experimental: try using new C++ ABI.
#         if {${configure.cxx_stdlib} ne "libc++"} {
#             reinplace "s|@ABI_FLAG@|\-D_GLIBCXX_USE_CXX11_ABI=0|g" \
#                 ${worksrcpath}/Makefile.in \
#                 ${worksrcpath}/umkMakefile.in \
#                 ${worksrcpath}/uppsrc/ide/Builders/Install.cpp
#         } else {
            reinplace "s|@ABI_FLAG@||g" \
                ${worksrcpath}/Makefile.in \
                ${worksrcpath}/umkMakefile.in \
                ${worksrcpath}/uppsrc/ide/Builders/Install.cpp
#         }
    }
}

# FIXME: for some reason currently the first build run fails, without a specific error.
# The next run completes fine, without any changes or clean-up. Annoying but survivable.

# U++ build process (following official CircleCI macOS build):
# 1. Configure generates umkMakefile with -DflagNOMM (console build, no .mm files)
# 2. Build umk (command-line tool) using umkMakefile
# 3. Use umk to build theide (umk reads .upp files and compiles .mm files correctly)
configure.pre_args
configure.args
configure.env       CC=${configure.cc} \
                    CXX=${configure.cxx}

# Build umk first (console tool, doesn't need Objective-C++)
build.args          -f umkMakefile \
                    CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    -j${build.jobs}
build.target

# After building umk, use it to build theide.
post-build {
    if {[string match *gcc* ${configure.compiler}]} {
        set build_method "GCC"
    } else {
        set build_method "CLANG"
    }

    ui_debug "Building theide with umk using ${build_method} build method"
    system -W ${worksrcpath} "CXX=${configure.cxx} ./umk ./uppsrc ide ${build_method} -brs ./theide"
}

destroot {
    # Install umk (command-line tool)
    xinstall -m 755 ${worksrcpath}/umk ${destroot}${prefix}/bin/umk

    # Install theide.app bundle to Applications
    copy ${worksrcpath}/theide.app ${destroot}${applications_dir}/theide.app

    # Create symlink for command-line access
    ln -s ${applications_dir}/theide.app/Contents/MacOS/theide ${destroot}${prefix}/bin/theide

    # Install man pages
    xinstall -d ${destroot}${prefix}/share/man/man1
    xinstall -m 644 ${worksrcpath}/uppsrc/ide/theide.1 ${destroot}${prefix}/share/man/man1/
    xinstall -m 644 ${worksrcpath}/uppsrc/umk/umk.1 ${destroot}${prefix}/share/man/man1/

    # Install desktop file
    xinstall -d ${destroot}${prefix}/share/applications
    xinstall -m 644 ${worksrcpath}/uppsrc/ide/theide.desktop ${destroot}${prefix}/share/applications/

    # Install U++ source framework
    xinstall -d ${destroot}${prefix}/share/upp
    copy ${worksrcpath}/uppsrc ${destroot}${prefix}/share/upp/
    copy ${worksrcpath}/examples ${destroot}${prefix}/share/upp/
    copy ${worksrcpath}/tutorial ${destroot}${prefix}/share/upp/
    copy ${worksrcpath}/reference ${destroot}${prefix}/share/upp/

    # Clean build artifacts from installed source tree
    system "find ${destroot}${prefix}/share/upp -name '*.o' -delete"
    system "find ${destroot}${prefix}/share/upp -name '*.a' -delete"
    if {[file exists ${destroot}${prefix}/share/upp/uppsrc/_out]} {
        delete ${destroot}${prefix}/share/upp/uppsrc/_out
    }
    if {[file exists ${destroot}${prefix}/share/upp/uppsrc/.cache]} {
        delete ${destroot}${prefix}/share/upp/uppsrc/.cache
    }

    # Install documentation
    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 ${worksrcpath}/README.md ${destroot}${prefix}/share/doc/${name}/
    if {[file exists ${worksrcpath}/BUILDING_MACOS.md]} {
        xinstall -m 644 ${worksrcpath}/BUILDING_MACOS.md ${destroot}${prefix}/share/doc/${name}/
    }
    xinstall -m 644 ${worksrcpath}/uppsrc/ide/Copying ${destroot}${prefix}/share/doc/${name}/LICENSE
}

variant debug description "Build with debug symbols" {
    build.args-append DEBUG=1
}

# Notes for users
notes "
U++ has been installed. You can now run:
  - theide: The U++ Integrated Development Environment
  - umk: Command-line build utility

The U++ framework sources are installed in:
  ${prefix}/share/upp/uppsrc
"
