# -*- 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           cmake 1.1
PortGroup           conflicts_build 1.0
PortGroup           github 1.0
PortGroup           openssl 1.0

if {${configure.cxx_stdlib} eq "libc++"} {
    PortGroup       legacysupport 1.1

    legacysupport.newest_darwin_requires_legacy 19
    legacysupport.use_mp_libcxx yes
}

github.setup        uowuo abaddon 0.2.4 v
revision            0
categories          net www
license             GPL-3
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Alternative Discord client with voice support
long_description    {*}${description} made with C++ and GTK3. \
                    The app runs on all systems from PowerPC to aarch64.

github.tarball_from archive

set dave_hash       52cd56dc550f447fb354b3a06c9e2d2e2a4309c6
# v. 0.11.25
set miniaudio_hash  9634bedb5b5a2ca38c1ee7108a9358a4e233f14d
master_sites-append https://github.com/discord/libdave/archive/${dave_hash}/:dave \
                    https://github.com/mackron/miniaudio/archive/${miniaudio_hash}/:miniaudio
distfiles-append    libdave-${dave_hash}${extract.suffix}:dave \
                    miniaudio-${miniaudio_hash}${extract.suffix}:miniaudio

checksums           ${distname}${extract.suffix} \
                    rmd160  8a5cce7a030a04c66cce94433d58b31507a2abf8 \
                    sha256  68cb3ca04eaf26dccf7d7750a65df8cf23819c9d501b25808a364b2915c360cf \
                    size    14522194 \
                    libdave-${dave_hash}${extract.suffix} \
                    rmd160  d6d8ec161391be424e65a9884cb881ed16cf1cf5 \
                    sha256  3564e0d26fa143bbbd0659bd92b50fb89db2a9f17e0e8edb95d5861b046f8ffd \
                    size    112322 \
                    miniaudio-${miniaudio_hash}${extract.suffix} \
                    rmd160  baa15b4cf1f56aa941694851049443da7b86c2be \
                    sha256  1a3a79b80fc6f0b0cc155e28b954a598e0ddfa2db64e2afa8466be88c476fa55 \
                    size    4553616

extract.only        ${distname}${extract.suffix}

post-extract {
    set tar [findBinary tar ${portutil::autoconf::tar_command}]
    system -W ${workpath} "${tar} -zxf ${distpath}/libdave-${dave_hash}.tar.gz"
    system -W ${workpath} "${tar} -zxf ${distpath}/miniaudio-${miniaudio_hash}.tar.gz"
    delete ${worksrcpath}/subprojects/libdave
    delete ${worksrcpath}/subprojects/miniaudio
    move ${workpath}/libdave-${dave_hash} ${worksrcpath}/subprojects/libdave
    move ${workpath}/miniaudio-${miniaudio_hash} ${worksrcpath}/subprojects/miniaudio
}

# Linking to libunwind leads to random crashes,
# at least on Sonoma. And older macOS do not need it anyway.
conflicts_build     libunwind

# keychain and qrcodegen do not install config files.
patchfiles-append   patch-CMakeLists.txt

set abaddon_root    ${prefix}/share/${name}

# Default libfmt version, matching spdlog.
set libfmt_ver      11
cmake.module_path-append \
                    ${prefix}/lib/libfmt${libfmt_ver}/cmake

depends_build-append \
                    path:bin/pkg-config:pkgconfig
depends_lib-append  port:curl \
                    port:libfmt${libfmt_ver} \
                    path:lib/pkgconfig/fontconfig.pc:fontconfig \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                    port:gtkmm3 \
                    port:ixwebsocket \
                    port:libhandy \
                    port:libopus \
                    port:libsodium \
                    port:mbedtls3 \
                    port:nlohmann-json \
                    port:spdlog \
                    port:sqlite3 \
                    port:zlib

compiler.cxx_standard   2017

configure.args-append \
                    -DENABLE_NOTIFICATION_SOUNDS=ON \
                    -DENABLE_QRCODE_LOGIN=OFF \
                    -DENABLE_RNNOISE=OFF \
                    -DENABLE_VOICE=OFF \
                    -DUSE_KEYCHAIN=OFF \
                    -DUSE_LIBHANDY=ON

# FIXME: coreaudio is broken atm for 10.4–10.5.
if {${os.platform} eq "darwin" && ${os.major} < 10} {
    configure.args-replace \
                    -DENABLE_NOTIFICATION_SOUNDS=ON -DENABLE_NOTIFICATION_SOUNDS=OFF
}

# FIXME: https://github.com/uowuo/abaddon/issues/360
if {[string match *gcc* ${configure.compiler}]} {
    configure.cxxflags-append \
                    -fpermissive
}

# Install target is broken: https://github.com/uowuo/abaddon/issues/290
# We need to do it manually.
destroot {
    xinstall -d ${destroot}${abaddon_root}
    copy ${cmake.build_dir}/${name} ${destroot}${abaddon_root}

    foreach res {css res} {
        copy ${worksrcpath}/res/${res} ${destroot}${abaddon_root}
    }

    # https://github.com/uowuo/abaddon/issues/293
    set abexec      ${prefix}/bin/${name}

    if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
        set  wrapper    [open "${destroot}${abexec}" w 0755]
        puts ${wrapper} "#!/bin/bash"
        puts ${wrapper} ""
        puts ${wrapper} {if [ -n "$DYLD_LIBRARY_PATH" ]; then}
        puts ${wrapper} "   DYLD_LIBRARY_PATH=${prefix}/lib/libgcc:\${DYLD_LIBRARY_PATH}"
        puts ${wrapper} {else}
        puts ${wrapper} "   DYLD_LIBRARY_PATH=${prefix}/lib/libgcc"
        puts ${wrapper} {fi}
        puts ${wrapper} {export DYLD_LIBRARY_PATH}
        puts ${wrapper} ""
        puts ${wrapper} "cd $abaddon_root"
        puts ${wrapper} "exec \./$name \"\$@\""
        close $wrapper
    } else {
        set  wrapper    [open "${destroot}${abexec}" w 0755]
        puts ${wrapper} "#!/bin/bash"
        puts ${wrapper} ""
        puts ${wrapper} "cd $abaddon_root"
        puts ${wrapper} "exec \./$name \"\$@\""
        close $wrapper
    }
}

variant keychain description "Store token in the keychain" {
    depends_lib-append \
                    port:keychain-cpp
    configure.args-replace \
                    -DUSE_KEYCHAIN=OFF -DUSE_KEYCHAIN=ON
}

variant qr description "Enable QR code login" {
    depends_lib-append \
                    port:qrcodegen-cpp
    configure.args-replace \
                    -DENABLE_QRCODE_LOGIN=OFF -DENABLE_QRCODE_LOGIN=ON
}

variant voice description "Enable voice suppport" {
    depends_lib-append \
                    port:mlspp \
                    port:rnnoise
    configure.args-replace \
                    -DENABLE_RNNOISE=OFF -DENABLE_RNNOISE=ON \
                    -DENABLE_VOICE=OFF -DENABLE_VOICE=ON
}

default_variants    +keychain +qr

# FIXME: coreaudio is broken atm for 10.4–10.5.
if {${os.platform} ne "darwin" || ${os.major} > 9} {
    default_variants-append +voice
}

notes "
You will need your Discord token in order to log in.
Upstream refers to the following thread:
https://github.com/Tyrrrz/DiscordChatExporter/issues/76
Make sure to keep your token safe.
"
