# -*- 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:sts=4

PortSystem          1.0
PortGroup           github 1.0

github.setup        WerWolv ImHex 1.38.1 v
name                imhex
revision            0
categories          editors devel
license             GPL-2
maintainers         {@herbygillot gmail.com:herby.gillot} openmaintainer

description         Hex editor for reverse engineers and programmers
long_description    ImHex is a hex editor with a pattern language, data visualizers, \
                    binary comparison, disassembly, and extensible analysis tools.
homepage            https://imhex.werwolv.net/

dist_subdir         ${name}/${version}

subport imhex-patterns {
    github.setup    WerWolv ImHex-Patterns ${version} ImHex-v
    github.tarball_from archive

    description     Patterns and supporting data for ImHex
    long_description Patterns and supporting data for ImHex ${version}.
    homepage        ${github.homepage}
    platforms       any
    supported_archs noarch

    checksums       rmd160  155dbf20ddb845f6514161b4c3d233be2adc139d \
                    sha256  3aae4c0970bc1b85bf4dc566bdf31de33c7dce593375645600549643ce6f9841 \
                    size    22634584

    use_configure   no
    build {}

    destroot {
        xinstall -d ${destroot}${prefix}/share/imhex
        foreach dir {constants encodings includes patterns magic nodes} {
            copy ${worksrcpath}/${dir} ${destroot}${prefix}/share/imhex
        }
        xinstall -d ${destroot}${prefix}/share/doc/${subport}
        xinstall -m 0644 -W ${worksrcpath} LICENSE README.md \
            ${destroot}${prefix}/share/doc/${subport}
    }
    livecheck.type  none
}

if {${subport} eq ${name}} {
    PortGroup       cmake 1.1
    PortGroup       boost 1.0

    distfiles       Full.Sources.tar.gz
    worksrcdir      ImHex
    checksums       rmd160  524a03d5cc345b7620a453bf81c4de8be2a1fa74 \
                    sha256  cd3531066a41dde1f0751e5d3146a936897df35ad5ba1fa49a9f3ace88e6901d \
                    size    40630321

    compiler.cxx_standard 2023
    compiler.whitelist  macports-clang-21
    # Use the matching modern libc++ for C++23 facilities.
    # Include float constants before SDK math headers request partial definitions.
    configure.cxxflags-append -include cfloat -nostdinc++ -isystem${prefix}/libexec/llvm-21/include/c++/v1
    configure.ldflags-append -L${prefix}/libexec/llvm-21/lib/libc++ \
                            -Wl,-rpath,${prefix}/libexec/llvm-21/lib/libc++ \
                            -Wl,-rpath,${prefix}/libexec/llvm-21/lib/libunwind

    boost.version       1.88

    depends_run-append   port:imhex-patterns

    depends_build-append path:bin/pkg-config:pkgconfig
    depends_lib-append \
                        port:bzip2 \
                        port:clang-21 \
                        port:curl \
                        port:edlib \
                        port:file \
                        port:freetype \
                        port:glfw \
                        port:libfmt12 \
                        port:libssh2 \
                        port:lunasvg \
                        port:lz4 \
                        port:mbedtls3 \
                        port:md4c \
                        port:miniaudio \
                        port:nativefiledialog-extended \
                        port:nlohmann-json \
                        port:xz \
                        port:yara \
                        port:zlib \
                        port:zstd

    patch.pre_args-replace -p0 -p1
    patchfiles          patch-macports.diff

    configure.args-append \
                        -DCMAKE_INSTALL_NAME_DIR=@rpath \
                        -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
                        -DCMAKE_INSTALL_RPATH=@executable_path/../Frameworks \
                        -Dfmt_DIR=${prefix}/lib/libfmt12/cmake/fmt \
                        -DIMHEX_OFFLINE_BUILD=ON \
                        -DIMHEX_GENERATE_PACKAGE=ON \
                        -DIMHEX_STRICT_WARNINGS=OFF \
                        -DIMHEX_ENABLE_UNIT_TESTS=OFF \
                        -DIMHEX_ENABLE_PLUGIN_TESTS=OFF \
                        -DIMHEX_BUNDLE_DOTNET=OFF \
                        -DIMHEX_BUNDLE_PLUGIN_SDK=OFF \
                        -DIMHEX_EXCLUDE_PLUGINS=script_loader \
                        -DUSE_SYSTEM_CAPSTONE=OFF \
                        -DUSE_SYSTEM_FMT=ON \
                        -DUSE_SYSTEM_NLOHMANN_JSON=ON \
                        -DUSE_SYSTEM_NFD=ON \
                        -DUSE_SYSTEM_LUNASVG=ON \
                        -DUSE_SYSTEM_MD4C=ON \
                        -DUSE_SYSTEM_BOOST=ON \
                        -DUSE_SYSTEM_EDLIB=ON \
                        -DUSE_SYSTEM_MINIAUDIO=ON \
                        -DUSE_SYSTEM_YARA=ON

    # Stage the application without upstream's portable-bundle dependency copying.
    destroot {
        xinstall -d ${destroot}${applications_dir}
        copy ${cmake.build_dir}/imhex.app ${destroot}${applications_dir}/ImHex.app
        set contents ${destroot}${applications_dir}/ImHex.app/Contents
        xinstall -d ${contents}/Frameworks ${contents}/Resources
        xinstall -m 0644 ${worksrcpath}/resources/dist/macos/AppIcon.icns ${contents}/Resources
        foreach lib [glob ${cmake.build_dir}/lib/libimhex/libimhex*.dylib] {
            copy ${lib} ${contents}/Frameworks
        }
        foreach dir {constants encodings includes patterns magic nodes} {
            ln -s ${prefix}/share/imhex/${dir} ${contents}/MacOS/${dir}
        }
        # Updates to this installation are managed by MacPorts.
        # Do not install the upstream binary-replacement updater.
        xinstall -d ${destroot}${prefix}/bin
        ln -s ${applications_dir}/ImHex.app/Contents/MacOS/imhex ${destroot}${prefix}/bin/imhex
        xinstall -d ${destroot}${prefix}/share/doc/${name}
        xinstall -m 0644 -W ${worksrcpath} LICENSE README.md \
            ${destroot}${prefix}/share/doc/${name}
    }
}
