# -*- 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           boost 1.0
PortGroup           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           wxWidgets 1.0

# Boost 1.87 fixed for 10.6.8 powerpc, but not tested on 10.4–10.5.
if {${os.platform} ne "darwin" || ${os.major} > 9} {
    boost.version   1.90
} else {
    boost.version   1.76
}

# std::filesystem requires a newer libc++ on macOS < 10.15.
# LegacySupport also supplies CLOCK_MONOTONIC on macOS <= 10.11.
legacysupport.use_mp_libcxx \
                    yes
legacysupport.newest_darwin_requires_legacy 18

github.setup        amule-org amule 3.2.0
revision            0
epoch               1
categories          net p2p www
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description         aMule, the all-platform eMule p2p client
long_description    aMule is a multiplatform fork of xMule client \
                    using wxWindows class library.
license             GPL-2
homepage            https://www.amule.org
github.tarball_from releases
distname            aMule-${version}-src
extract.rename      yes
checksums           rmd160  c641e9bab7671d0fbdfcae0ade52ff3d10d5bf24 \
                    sha256  9c060a79fe5444b2700b4e3c8c6162032cd56dd4a242d384d7ff95d977c288d1 \
                    size    11260139

# plutil on 10.5/10.6 has no -replace or -json, so the POST_BUILD steps
# that inject URL schemes, document types and the ATS opt-out into
# Info.plist fail. Ship those keys in a custom Info.plist template instead.
patchfiles-append   patch-macos-info-plist.diff

# See: https://github.com/wxWidgets/wxWidgets/issues/24720
patchfiles-append   patch-stdpaths.diff

post-patch {
    copy ${filespath}/Info.plist.in ${worksrcpath}/platforms/MacOSX/

    # The upstream release tarball is assembled without git archive, so its
    # archival metadata placeholders are not expanded. Set the release version
    # explicitly for the binaries and manpages; upstream sets PROJECT_VERSION.
    reinplace -E "s|set \\(PACKAGE_STRING \"aMule GIT\"\\)|set (PACKAGE_STRING \"aMule ${version}\")|" ${worksrcpath}/CMakeLists.txt
    reinplace -E "s|set \\(PACKAGE_VERSION \"GIT\"\\)|set (PACKAGE_VERSION \"${version}\")|" ${worksrcpath}/CMakeLists.txt
    reinplace -E "s|set \\(VERSION \"GIT\"\\)|set (VERSION \"${version}\")|" ${worksrcpath}/CMakeLists.txt
    # Change app bundle install location
    reinplace -E "s|BUNDLE DESTINATION \\.|BUNDLE DESTINATION \"${applications_dir}\"|" ${worksrcpath}/src/CMakeLists.txt
    # Keep upstream warnings without treating them as errors.
    reinplace {s|-Werror=|-W|g} ${worksrcpath}/src/CMakeLists.txt
    # Readline 8.3 no longer exposes the obsolete Function typedef unless
    # explicitly requested. The callback already has rl_compentry_func_t's
    # signature, which is also the type of rl_completion_entry_function.
    reinplace -E {s|\(Function \*\)&command_completion|\&command_completion|} ${worksrcpath}/src/ExternalConnector.cpp
}

depends_build-append \
                    port:bison \
                    port:gettext \
                    path:bin/pkg-config:pkgconfig
depends_lib-append  port:curl \
                    port:gettext-runtime \
                    port:libcryptopp \
                    port:libiconv \
                    port:libmaxminddb \
                    port:libpng \
                    port:libupnp \
                    port:readline \
                    port:zlib
depends_run-append  port:desktop-file-utils

configure.args-append \
                    -DAMULE_TAGGED_RELEASE=ON \
                    -DBUILD_ALC=OFF \
                    -DBUILD_ALCC=ON \
                    -DBUILD_AMULECMD=ON \
                    -DBUILD_CAS=OFF \
                    -DBUILD_DAEMON=ON \
                    -DBUILD_ED2K=ON \
                    -DBUILD_EVERYTHING=OFF \
                    -DBUILD_FILEVIEW=OFF \
                    -DBUILD_MONOLITHIC=ON \
                    -DBUILD_REMOTEGUI=OFF \
                    -DBUILD_TESTING=OFF \
                    -DBUILD_WEBSERVER=OFF \
                    -DBUILD_WXCAS=OFF \
                    -DENABLE_VERSION_CHECK=OFF \
                    -DDOWNLOAD_AND_BUILD_DEPS=OFF \
                    -DENABLE_BFD=OFF \
                    -DENABLE_CCACHE=OFF \
                    -DENABLE_IP2COUNTRY=ON \
                    -DENABLE_NLS=ON \
                    -DENABLE_UPNP=ON \
                    -DTRANSLATED_MANPAGES=OFF

compiler.cxx_standard   2017
# At least on Catalina Apple clang fails at configure on boost::asio check.
compiler.blacklist-append   {clang < 1300}

# wxcrt.h:656:14: error: call to 'wcsstr' is ambiguous
if {${os.platform} eq "darwin" && ${os.major} < 16 \
    && ${configure.cxx_stdlib} eq "libc++"} {
    configure.cxxflags-append \
                    -D_WCHAR_H_CPLUSPLUS_98_CONFORMANCE_
}

# This variant does not work on 10.11 and earlier:
variant wxwidgets32 conflicts wxgtk32 description "Use Cocoa-based wxWidgets 3.2" {
    wxWidgets.use           wxWidgets-3.2
    depends_lib-append      port:${wxWidgets.port}

    configure.args-append   -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig} \
                            -DwxWidgets_wxrc_EXECUTABLE=${wxWidgets.wxrc}

    post-destroot {
        system -W ${destroot}${applications_dir} "/usr/bin/codesign --sign - ${cs_name}.app"
        ln -s ${applications_dir}/${cs_name}.app/Contents/MacOS/${cs_name} ${destroot}${prefix}/bin/${name}
    }
}

# wx < 3.2 is not supported now: https://github.com/amule-project/amule/issues/407
variant wxgtk32 conflicts wxwidgets32 description "Use GTK-based wxWidgets 3.2" {
    wxWidgets.use           wxGTK-3.2
    depends_lib-append      port:${wxWidgets.port}

    configure.args-append   -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig} \
                            -DwxWidgets_wxrc_EXECUTABLE=${wxWidgets.wxrc}
}

# We prefer wxGTK, since no testing is done for wxWidgets-3.2.
# Upstream MacPorts maintains wxWidgets-3.2 port. If you wish to use it,
# install amule with +wxwidgets32.
if {![variant_isset wxwidgets32] && ![variant_isset wxgtk32]} {
    default_variants        +wxgtk32
}

post-destroot {
    ln -s ${applications_dir}/aMule.app/Contents/MacOS/aMule ${destroot}${prefix}/bin/amule
}

post-activate {
    system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
}
