# -*- 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 bd31e954109cf13ce9488a42159cfbfa645538f7
version             0.63.4
revision            2
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  fae8d22dfeec5845a1d301d0c17d67e40cc7ea76 \
                    sha256  7c4ef02fb966c4110ff00615c76998809fc228e5f4202dc8c46e10bec21264c9 \
                    size    5055933
github.tarball_from archive

platform darwin {
    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"
    }
}
