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

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

github.setup            xfangfang wiliwili 1.5.2 v
# Used for Info.plist:
set git_tag             6ec1824
revision                0
categories              multimedia www
# Wiliwili supports multiple platforms, but the port is macOS-specific now.
platforms               macosx
maintainers             {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
license                 GPL-3
description             Cross-platform client for bilibili
long_description        {*}${description} B站.
homepage                https://www.xfangfang.cn/${name}

checksums               rmd160  a7079b438530c51d8cec930835b85105ffbcfc91 \
                        sha256  885db32627381dd2641c69c0baee0f716a3f8ae39ce9f4abb007f7e615de4eab \
                        size    16102983
github.tarball_from     archive
github.livecheck.branch yoga

fetch.type              git

cmake.generator         Ninja

# wiliwili
# Discussion: https://github.com/xfangfang/wiliwili/issues/570
# https://github.com/xfangfang/wiliwili/pull/571
patchfiles-append       0001-Support-external-lunasvg.patch \
                        0002-qr_image.hpp-add-a-missing-header.patch \
                        0003-string_helper.cpp-fix-compilation-error.patch \
                        0004-Allow-customized-builds-on-macOS.patch

# borealis
# https://github.com/xfangfang/borealis/pull/112
# https://github.com/xfangfang/borealis/issues/114
# https://github.com/xfangfang/borealis/pull/115
# https://github.com/xfangfang/borealis/pull/116
patchfiles-append       0005-Allow-not-to-force-build-settings-for-macOS.patch \
                        0006-sdl_platform.cpp-add-a-missing-header.patch \
                        0007-CMakeLists.txt-use-correct-frameworks-on-macOS.patch \
                        0008-desktop_darwin.mm-fix-compiling-with-gcc-and-on-10.7.patch \
                        0009-Initial-GL2-support-for-SDL2.patch

patchfiles-append       0010-Update-Info.plist.patch

# Fix wrong clock:
patchfiles-append       patch-fix-features_cpu.diff

post-patch {
    if {${macosx_deployment_target} ne ""} {
        reinplace "s|@MACOS@|${macosx_deployment_target}|" ${worksrcpath}/scripts/mac/Info.plist
    } else {
        reinplace "s|@MACOS@|10.6.0|" ${worksrcpath}/scripts/mac/Info.plist
    }
    reinplace "s|@SVERSION@|${version}|" ${worksrcpath}/scripts/mac/Info.plist
    reinplace "s|@VERSION@|${git_tag}|" ${worksrcpath}/scripts/mac/Info.plist
}

set libfmt_v            11
cmake.module_path-append \
                        ${prefix}/lib/libfmt${libfmt_v}/cmake

depends_build-append    path:bin/pkg-config:pkgconfig

# libmpv is used, not mpv binary.
depends_lib-append      port:cpr \
                        port:curl \
                        port:libfmt${libfmt_v} \
                        port:libmongoose \
                        port:lunasvg \
                        path:lib/libmpv.dylib:mpv \
                        port:pystring \
                        port:tinyxml2 \
                        port:tweeny \
                        port:webp \
                        port:zlib

compiler.cxx_standard   2017

set bundle_root         ${applications_dir}/${name}.app

# We set resources dir explicitly, so that wiliwili can be launched from command line.
configure.args-append   -DAPP_PLATFORM_CUSTOM_LIBS=ON \
                        -DBUNDLE_MACOS_APP=OFF \
                        -DCUSTOM_RESOURCES_DIR=${bundle_root}/Contents/Resources \
                        -DDISABLE_OPENCC=ON \
                        -DDISABLE_WEBP=OFF \
                        -DMAC_CUSTOM_BUILD=ON \
                        -DMAC_DOWNLOAD_DYLIB=OFF \
                        -DPLATFORM_DESKTOP=ON \
                        -DUSE_BOOST_FILESYSTEM=OFF \
                        -DUSE_GL3=ON \
                        -DUSE_SDL2=OFF \
                        -DUSE_SHARED_LIB=ON \
                        -DUSE_SYSTEM_CPR=ON \
                        -DUSE_SYSTEM_CURL=ON \
                        -DUSE_SYSTEM_FMT=ON \
                        -DUSE_SYSTEM_GLFW=OFF \
                        -DUSE_SYSTEM_LUNASVG=ON \
                        -DUSE_SYSTEM_MONGOOSE=ON \
                        -DUSE_SYSTEM_OPENCC=OFF \
                        -DUSE_SYSTEM_PYSTRING=ON \
                        -DUSE_SYSTEM_QRCODEGEN=OFF \
                        -DUSE_SYSTEM_SDL2=OFF \
                        -DUSE_SYSTEM_TINYXML2=ON \
                        -DUSE_SYSTEM_TWEENY=ON

# The build does not pick needed flags:
configure.cppflags-append \
                        -I${prefix}/include/libfmt${libfmt_v} \
                        -I${prefix}/include/lunasvg
configure.ldflags-append \
                        -L${prefix}/lib/libfmt${libfmt_v}

platform darwin {
    # TODO: GL2 must also be used with libsdl2-x11.
    if {${os.major} < 13} {
        # Use OpenGL2 on legacy systems:
        configure.args-replace \
                        -DUSE_GL3=ON -DUSE_GL2=ON
    }

    if {${os.major} < 17 && ${configure.cxx_stdlib} eq "libc++"} {
        # Apple libc++ is crippled, fallback to Boost:
        PortGroup       boost 1.0

        boost.version   1.81

        configure.args-replace \
                        -DUSE_BOOST_FILESYSTEM=OFF -DUSE_BOOST_FILESYSTEM=ON
    }
}

variant glfw description "Use GLFW backend" {
# The build fails against MacPorts glfw. See also:
# https://github.com/xfangfang/wiliwili/blob/6ec18248c83806316b1b00f296020b94a7ae30dc/CMakeLists.txt#L44
#    depends_lib-append  port:glfw
#    configure.args-replace \
#                        -DUSE_SYSTEM_GLFW=OFF -DUSE_SYSTEM_GLFW=ON
}

variant sdl2 description "Use SDL2 backend" {
    depends_lib-append  port:libsdl2
    configure.args-replace \
                        -DUSE_SDL2=OFF -DUSE_SDL2=ON \
                        -DUSE_SYSTEM_SDL2=OFF -DUSE_SYSTEM_SDL2=ON
}

# SDL2 is more robust and portable.
if {![variant_isset glfw] && ![variant_isset sdl2]} {
    default_variants    +sdl2
}

if {[variant_isset glfw]} {
    post-fetch {
        system -W ${worksrcpath} "git submodule update --init --recursive"
    }
} else {
    post-fetch {
        system -W ${worksrcpath} "git submodule update --init"
    }
}

# Chinese
variant opencc description "Build with OpenCC support" {
    depends_lib-append  port:opencc
    configure.args-replace \
                        -DDISABLE_OPENCC=ON -DDISABLE_OPENCC=OFF \
                        -DUSE_SYSTEM_OPENCC=OFF -DUSE_SYSTEM_OPENCC=ON
}

# Based on https://github.com/xfangfang/wiliwili/blob/yoga/scripts/build_mac.sh
destroot {
    xinstall -d ${destroot}${bundle_root}
    xinstall -d ${destroot}${bundle_root}/Contents
    xinstall -d ${destroot}${bundle_root}/Contents/MacOS
    xinstall -d ${destroot}${bundle_root}/Contents/Resources

    copy ${cmake.build_dir}/${name} ${destroot}${bundle_root}/Contents/MacOS
    copy ${worksrcpath}/scripts/mac/Info.plist ${destroot}${bundle_root}/Contents/
    copy ${worksrcpath}/scripts/mac/AppIcon.icns ${destroot}${bundle_root}/Contents/Resources/
    copy ${worksrcpath}/resources ${destroot}${bundle_root}/Contents/Resources/
}

post-destroot {
    # With libstdc++ we need a wrapper here:
    if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
        set  wrapper    [open "${destroot}${prefix}/bin/${name}" 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} {exec "__WILIBIN__" "$@"}
        close $wrapper

        reinplace "s|__WILIBIN__|${bundle_root}/Contents/MacOS/${name}|" ${destroot}${prefix}/bin/${name}
    } else {
        ln -s ${bundle_root}/Contents/MacOS/${name} ${destroot}${prefix}/bin/${name}
    }
}
