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

github.setup        amule-org amule 3.2.0
github.tarball_from releases
revision            0
categories          net p2p www
maintainers         {i0ntempest @i0ntempest} openmaintainer
set cs_name         aMule
description         ${cs_name}, the all-platform eMule p2p client
long_description    ${cs_name} is a multiplatform fork of xMule client \
                    using wxWidgets.
license             GPL-2
homepage            https://amule-org.github.io/

distname            ${cs_name}-${version}-src
checksums           rmd160  c641e9bab7671d0fbdfcae0ade52ff3d10d5bf24 \
                    sha256  9c060a79fe5444b2700b4e3c8c6162032cd56dd4a242d384d7ff95d977c288d1 \
                    size    11260139

extract.rename      yes

boost.version       1.91
wxWidgets.use       wxWidgets-3.2

depends_build-append \
                    port:gettext \
                    port:pkgconfig
depends_lib-append  port:${wxWidgets.port} \
                    port:curl \
                    port:gettext-runtime \
                    port:libcryptopp \
                    port:libmaxminddb \
                    port:libupnp \
                    port:readline \
                    port:zlib

post-patch {
    # 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
}

compiler.cxx_standard \
                    2017

cmake.set_cxx_standard \
                    yes

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 \
                    -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig}

# wxcrt.h:656:14: error: call to 'wcsstr' is ambiguous
if {${os.major} <= 15} {
    configure.cxxflags-append \
                    -D_WCHAR_H_CPLUSPLUS_98_CONFORMANCE_
}

# 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

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}
}
