# -*- 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           xcode 1.0

name                xld
version             20251216
revision            0
categories          audio
license             GPL-2
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description         Lossless audio decoder for Mac OS X
long_description    \
    X Lossless Decoder(XLD) is a tool for Mac OS X that is able to \
    decode/convert/play various 'lossless' audio files. The supported audio \
    files can be split into some tracks with cue sheet when decoding. It works \
    on Mac OS X 10.4 and later.
homepage            https://tmkk.undo.jp/xld/index_e.html
master_sites        https://sourceforge.net/code-snapshots/svn/x/xl/xld/code/
#fetch.type          svn
#svn.url             https://svn.code.sf.net/p/xld/code/trunk
set svn.revision    r652
distname            ${name}-code-${svn.revision}-trunk
use_zip             yes
checksums           rmd160  5c51107e629d13cb0e2f3cf83f0c0526074c4ae6 \
                    sha256  46696e3e703e25be0231affc20a12925273970fa36ec6641117ead33e4322606 \
                    size    3688484

# Yes, the build uses pre-compiled executable here.
post-extract {
    copy ${prefix}/bin/aacplusenc ${worksrcpath}/XLDHEAACOutput/
}

patch.pre_args-replace  -p0 -p1

# https://sourceforge.net/p/xld/tickets/625
patchfiles-append   0001-main.m-fix-macOS-version-threshold.patch \
                    0002-MainMenu.xib-fix-broken-XIB.patch \
                    0003-Remove-non-English-locales.patch \
                    0004-XLDPluginManager.m-change-version-threshold.patch \
                    0005-XLDFlacOutput-adjust-macOS-version-threshold.patch \
                    0006-XLDLibsndfileDecoder-find-headers.patch \
                    0007-Fix-Xcode-project.patch \
                    0008-XLDController.patch \
                    0009-No-XLDVorbisOutput-since-no-Xcode-project-for-it.patch \
                    0010-cddb.png-does-not-exist.patch

post-patch {
    reinplace "s,@PREFIX@,${prefix},g" ${worksrcpath}/XLD/XLD_export.xcodeproj/project.pbxproj \
        ${worksrcpath}/XLDLibsndfileDecoder/XLDLibsndfileDecoder_export.xcodeproj/project.pbxproj
    # Temporarily, since the version has not been updated:
    reinplace "s,20250302,${version},g" ${worksrcpath}/XLD/Info.plist
}

depends_build-append \
                    port:aacplusenc

# Cannot use mac port instead of monkeys-audio.
depends_lib-append  port:flac \
                    port:libcddb \
                    port:libebur128 \
                    port:libsndfile \
                    port:monkeys-audio \
                    port:libogg \
                    port:libopus \
                    port:soxr \
                    port:wavpack

xcode.build.settings-append \
                    HEADER_SEARCH_PATHS=${prefix}/include \
                    USER_HEADER_SEARCH_PATHS=${worksrcpath} \
                    LIBRARY_SEARCH_PATHS=${prefix}/lib
xcode.destroot.settings-append \
                    HEADER_SEARCH_PATHS=${prefix}/include \
                    USER_HEADER_SEARCH_PATHS=${worksrcpath} \
                    LIBRARY_SEARCH_PATHS=${prefix}/lib

use_parallel_build  no

# XLDVorbisOutput has no Xcode project file.
# XLDTakDecoder needs Wine.
# XLDMP3Decoder, XLDLibsndfileDecoder must come after XLDID3.
set plugin_list {
    XLDFlacDecoder XLDAacOutput XLDTtaDecoder XLDWavpackDecoder
    XLDAlacDecoder XLDApeDecoder XLDLameOutput XLDFlacOutput XLDHEAACOutput
    XLDAlacOutput XLDAacOutput2 XLDWavpackOutput XLDShortenDecoder
    XLDOpusOutput XLDID3 XLDMP3Decoder XLDLibsndfileDecoder
}

# The build is messed up, and nothing gets found. See:
# https://github.com/RJVB/macstrop/issues/150
# Below we hack this to work.
post-patch {
    set pbxproj ${worksrcpath}/XLD/XLD_export.xcodeproj/project.pbxproj

    reinplace "s|\.\./XLDID3/build/Release/XLDID3.framework|${worksrcpath}/build/Release/XLDID3.framework|" ${pbxproj}
    reinplace "s|XLDID3.framework; sourceTree = \"<group>\"|XLDID3.framework; sourceTree = \"<absolute>\"|" ${pbxproj}

    foreach plugin $plugin_list {
        reinplace "s|\.\./${plugin}/build/Release/${plugin}.bundle|${worksrcpath}/build/Release/${plugin}.bundle|g" ${pbxproj}
        reinplace "s|${plugin}.bundle; sourceTree = \"<group>\"|${plugin}.bundle; sourceTree = \"<absolute>\"|" ${pbxproj}
    }
}

set xcode_configuration_arg [xcode::get_configuration_arg ${xcode.configuration}]
set xcode_build_args [xcode::get_build_args]

pre-build {
    foreach plugin $plugin_list {
        if {[file exists ${worksrcpath}/${plugin}]} {
            system -W ${worksrcpath}/${plugin} \
                "${build.cmd} ${xcode_configuration_arg} ${xcode_build_args} ${xcode.build.settings}"
        }
    }
}

xcode.configuration Release
xcode.project       XLD/XLD_export.xcodeproj
xcode.target        XLD
