# -*- 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
PortGroup           makefile 1.0
PortGroup           wxWidgets 1.0

# Commit-based due to https://github.com/solemnwarning/rehex/issues/275
github.setup        solemnwarning rehex f7dafe5a02af9394124174bfc410d3586778db15
version             0.63.4
revision            1
categories          sysutils devel
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer

description         Reverse Engineers’ Hex Editor
long_description    {*}${description}
license             GPL-2
homepage            https://rehex.solemnwarning.net

checksums           rmd160  3fc7f1c059351b37f983028256afcd9be3c2af3f \
                    sha256  05794fa3e57185ffb364ba2adcf1825ec94ed9075b19cd6dd10d82927ff72551 \
                    size    5055614
github.tarball_from archive

platform darwin {
    # https://github.com/solemnwarning/rehex/pull/278
    patchfiles-append   patch-legacy.diff
    # https://github.com/solemnwarning/rehex/pull/279
    patchfiles-append   patch-icns.diff

    post-patch {
        if {${macosx_deployment_target} ne ""} {
            reinplace "s|10.13|${macosx_deployment_target}|" ${worksrcpath}/tools/mac-info-plist.sh
        } else {
            reinplace "s|10.13|10.5|" ${worksrcpath}/tools/mac-info-plist.sh
        }
    }
}

wxWidgets.use       wxGTK-3.2
set botan_v         3
set lua_v           5.3
set lua_v_nodot     [string map {. {}} ${lua_v}]
set lua_root        ${prefix}/libexec/lua${lua_v_nodot}

depends_build-append \
                    port:lua${lua_v_nodot}-busted \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:botan${botan_v} \
                    port:capstone \
                    port:jansson \
                    port:jq \
                    port:libiconv \
                    port:libunistring \
                    port:lua${lua_v_nodot} \
                    port:${wxWidgets.port}

compiler.cxx_standard   2020

build.env-append    PATH=${wxWidgets.wxdir}:${lua_root}/bin:${prefix}/bin:${prefix}/sbin:/usr/bin:/usr/sbin:/bin:/sbin \
                    LUA_LIBS=-llua${lua_v} \
                    LUA_CFLAGS=-I${prefix}/libexec/lua${lua_v_nodot}/include

build.args-append   BOTAN_PKG=botan-${botan_v} \
                    BUILD_HELP=0 \
                    BUSTED=${prefix}/libexec/lua${lua_v_nodot}/bin/busted \
                    prefix=${prefix} \
                    V=1

destroot.env-append PATH=${wxWidgets.wxdir}:${prefix}/bin:${prefix}/sbin:/usr/bin:/usr/sbin:/bin:/sbin \
                    BUILD_HELP=0 \
                    BUSTED=${prefix}/libexec/lua${lua_v}/bin/busted \
                    prefix=${prefix}

if {${os.platform} eq "darwin"} {
    depends_build-append \
                    port:makeicns

    build.pre_args-append \
                    -f Makefile.osx

    # See: https://github.com/solemnwarning/rehex/issues/275#issuecomment-3764015297
    destroot {
        copy ${worksrcpath}/REHex.app ${destroot}${applications_dir}/
        ln -s ${applications_dir}/REHex.app/Contents/MacOS/REHex ${destroot}${prefix}/bin/rehex
    }
} else {
    depends_run-append \
                    port:desktop-file-utils

    post-activate {
        system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
        system "${prefix}/bin/gtk-update-icon-cache -f -t ${prefix}/share/icons/hicolor"
    }
}
