# -*- 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           cmake 1.1

github.setup        discord discord-rpc 3.4.0 v
categories          games devel
license             MIT
maintainers         nomaintainer

description         simple RPC client for Discord
long_description    Discord RPC is a library for interfacing your game \
                    with a locally running Discord desktop client.

checksums           rmd160  5162c62ba2484098f683c1bb82ef1a2704f61b19 \
                    sha256  d814e86f214790a2fcc4a740ab5bdcf259ac2a09b22ff641b2a3ace911fd244c \
                    size    2107652

compiler.cxx_standard 2014

depends_lib-append  path:lib/pkgconfig/RapidJSON.pc:rapidjson

# Patch the CMakeLists.txt file to add a CMake find module
# and a pkg-config file to the final output products.
patchfiles-append   add-package-locator-files.diff

# Patch the src/CMakeLists.txt file so that CMake
# builds both the static and dynamic libraries.
patchfiles-append   create-both-lib-types.diff

post-patch {
    file mkdir ${worksrcpath}/cmake
    copy ${filespath}/DiscordRPCConfig.cmake ${worksrcpath}/cmake/
    copy ${filespath}/FindDiscordRPC.cmake.in \
         ${filespath}/discord-rpc.pc.in \
         ${worksrcpath}/

    # Add the version number from this Portfile. The upstream
    # source code doesn't seem to contain any references to the
    # version number, other than the version number in the file name
    # of the source tarball.
    reinplace "/project.*DiscordRPC/s/)/ VERSION ${version})/" \
        ${worksrcpath}/CMakeLists.txt

    # Force the source code to use the MacPorts-provided RapidJSON
    # instead of its own copy of the header files.
    reinplace {/find_file.RAPIDJSONTEST/,/add_library.rapidjson/d} \
        ${worksrcpath}/CMakeLists.txt
    reinplace {/target_include_directories.*RAPIDJSON/d} \
        ${worksrcpath}/src/CMakeLists.txt
    reinplace -E {s/(#include )"(rapidjson.*)"/\1<\2>/} \
        ${worksrcpath}/src/serialization.h
}

configure.args-append   BUILD_SHARED_LIBS=ON

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}$docdir
    xinstall -m 644 -W ${worksrcpath} README.md LICENSE \
        ${destroot}$docdir
    copy {*}[glob ${worksrcpath}/documentation/*] ${destroot}$docdir/

    set datadir ${prefix}/share/${name}
    xinstall -d ${destroot}$datadir
    copy ${worksrcpath}/examples ${destroot}$datadir/
    copy ${build.dir}/examples/send-presence/send-presence.app \
         ${destroot}$datadir/examples/send-presence/
    fs-traverse f ${destroot}$datadir {
        if {[file tail $f] eq ".gitignore"} { delete $f }
    }
}