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

# mono_version is the version of port Mono
# see https://github.com/mono/mono/blob/mono-${mono_version}/packaging/MacSDK/msbuild.py for version
github.setup            mono msbuild de7a9c7b83f0cbfaedc0fc4cef3670755e0fc1f5
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
version                 20221013
revision                0
categories              devel
license                 MIT
maintainers             {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description             the build platform for .NET and Visual Studio
long_description        The Microsoft Build Engine is a platform for building applications.

# see msbuild_download_url in file eng/cibuild_bootstrapped_msbuild.sh
# see global.json
set bootstrap_msbuild   mono_msbuild_6.12.0.137

# msbuild requires msbuild to build
master_sites-append     https://github.com/mono/msbuild/releases/download/v16.9.0/:bootstrap
distfiles-append        ${bootstrap_msbuild}.zip:bootstrap

checksums               ${distname}${extract.suffix} \
                        rmd160  20722b94a59fda07689c50ef441c85df5e429783 \
                        sha256  babc12f43a11313c4882b85ecb3b6cdba5cd1f6188ed11d0882415aef2b395d6 \
                        size    9342995 \
                        ${bootstrap_msbuild}.zip \
                        rmd160  c0eab18a1bb5bb39a8c01262496277d738982ff4 \
                        sha256  f8ec5126d33c6466ddb6926d1cfad3207eb4c454a90c48b9aa2b69326f5bdff2 \
                        size    23333955

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

post-extract {
    # replicate the code in function DownloadMSBuildForMono found in file eng/cibuild_bootstrapped_msbuild.sh
    xinstall -d -m 0755 ${worksrcpath}/artifacts
    set unzip [findBinary unzip ${portutil::autoconf::unzip_path}]
    system -W ${worksrcpath}/artifacts "${unzip} -q ${distpath}/${bootstrap_msbuild}.zip -d ${worksrcpath}/artifacts"
    move ${worksrcpath}/artifacts/msbuild ${worksrcpath}/artifacts/mono-msbuild
    system "chmod +x ${worksrcpath}/artifacts/mono-msbuild/MSBuild.dll"

    # Until https://github.com/dotnet/arcade/commit/6899e98770d05659585c6e25bef305a381bcc576 is available upstream,
    # rename 'LICENSE' -> 'license' on case-sensitive filesystems.
    if {![file exists ${worksrcpath}/license]} {
        file rename -- ${worksrcpath}/LICENSE ${worksrcpath}/license
    }
}

depends_lib             port:mono

use_configure           no

build.target            all-mono

destroot {
    # see https://github.com/mono/mono/blob/mono-${mono_version}/packaging/MacSDK/msbuild.py
    system -W ${worksrcpath} "./stage1/mono-msbuild/msbuild mono/build/install.proj /p:MonoInstallPrefix=${destroot}${prefix} /p:Configuration=Release-MONO"
    reinplace "s|${destroot}||g" ${destroot}${prefix}/bin/msbuild
}

# TODO: fix
notes "
Warning: ${subport} downloads files during the build phase.
This is contrary to standard MacPorts practices, but it seems to be a fundamental part of msbuild.
"

# keep synchronized with https://github.com/mono/mono/blob/mono-${mono_version}/packaging/MacSDK/msbuild.py
livecheck.type none
