# -*- 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
# TODO: the build can succeed without it, using internal fallback. Needs comparative testing.
# See https://github.com/libretro/libretro-common/blob/668749ae38a9e85744d1c15a652a1e8db8ab9e82/features/features_cpu.c
legacysupport.newest_darwin_requires_legacy 15

# FIXME: currently videos do not stream, this is not specific to legacy systems.
# See: https://github.com/xfangfang/wiliwili/issues/595

github.setup            xfangfang wiliwili 1.6.0 v
# Used for Info.plist:
set git_tag             88e5876
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  65aae1ac2aa80c83961486d3c726b14358402f90 \
                        sha256  eecefbba5c0cb30c59d33c31c54af69cbe43eac1628b038743e06f63bfaf62af \
                        size    16106992
github.tarball_from     archive
github.livecheck.branch yoga

fetch.type              git

cmake.generator         Ninja

patchfiles-append       0001-string_helper.cpp-fix-compilation-error.patch

patchfiles-append       0002-Allow-customized-builds-on-macOS.patch

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

# Fix wrong clock.
# TODO: notice, this patch uses ppc_intrinsics.h, which requires our gcc build.
# TODO: see also https://github.com/libretro/libretro-common/issues/223
patchfiles-append       0004-fix-features_cpu.patch

# https://github.com/xfangfang/borealis/issues/114
patchfiles-append       0005-borealis-gl.patch

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            12
cmake.prefix_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
    }
}

# Broken on legacy systems due to ObjC code in the bundled version.
# Works on newer x86 systems.
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  path:lib/pkgconfig/glfw3.pc:glfw
#    configure.args-replace \
#                        -DUSE_SYSTEM_GLFW=OFF -DUSE_SYSTEM_GLFW=ON
}

# Dummy variant to ensure dependencies pick the same.
variant opengl description "Ensure dependencies are installed with GL support" { }

# SDL2 must be with opengl support, regardless of mpv.
# Deal with that together with: https://github.com/macos-powerpc/powerpc-ports/issues/17
variant sdl2 description "Use SDL2 backend" {
    # active_variants     1.1

    depends_lib-append  port:libsdl2
    # require_active_variants libsdl2 opengl
    configure.args-replace \
                        -DUSE_SDL2=OFF -DUSE_SDL2=ON \
                        -DUSE_SYSTEM_SDL2=OFF -DUSE_SYSTEM_SDL2=ON
}

# mpv may not work with GL, in such a case use this variant:
variant sw description "Use software rendering in mpv instead of GL" {
    configure.args-append \
                        -DMPV_SW_RENDER=ON
}

# GL is required for borealis library, ensure dependencies use it:
default_variants        +opengl

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

# For now, since GL in mpv is broken.
if {${os.platform} eq "darwin" && ${os.major} < 13} {
    default_variants-append \
                        +sw
}

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

notes "
If the app bundle crashes, launch it from the terminal as `wiliwili`.
"
