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

name                mono
version             3.12.1
revision            1
categories          devel lang mono
platforms           darwin
license             {GPL-2 LGPL-2 MIT}
maintainers         nomaintainer
description         Implementation of the .NET Development Framework
long_description    \
    Mono is an effort to create an open source implementation of the .NET \
    Development Framework including a C# compiler.

homepage            http://www.mono-project.com/
master_sites        http://download.mono-project.com/sources/mono/
use_bzip2           yes
universal_variant   no

checksums           rmd160  c34cb00ab4cf8cd5e2804bde952b3f5fde3cdc17 \
                    sha256  5d8cf153af2948c06bc9fbf5088f6834868e4db8e5f41c7cff76da173732b60d

depends_build       port:coreutils \
                    port:gawk \
                    port:pkgconfig

depends_lib         port:gettext \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:libiconv \
                    port:zlib \
                    port:libgdiplus \
                    port:xorg-libX11

# Save value of ${configure.cc} for use in patching files
set configure_cc ${configure.cc}
# Do NOT pick up external boehmgc (prefixing CPPFLAGS or CFLAGS does not suffice -- ugly++)
configure.cc-append "-I${worksrcpath}/libgc/include"

configure.args      --with-gc=included \
                    --with-preview=yes \
                    --without-sigaltstack \
                    --with-libgdiplus=${prefix} \
                    --disable-dtrace

configure.ldflags-append \
                    -lintl -lcharset

use_parallel_build  no

test.run            yes
test.target         check

patchfiles          snowleopard-mutex.patch

if { ${build_arch} == "i386" } {
    configure.args-append "--host=i686-apple-${os.platform}${os.version}"
}

# Ensure we are UsingTheRightCompiler (https://trac.macports.org/wiki/UsingTheRightCompiler)
patchfiles-append   patch-aot-compiler.c.diff \
                    patch-Makefile.in.diff

post-patch {
    if {${os.major} < 9} {
        reinplace "s:MAC_OS_X_VERSION_10_5:1050:g" \
            ${worksrcpath}/libgc/darwin_stop_world.c \
            ${worksrcpath}/mono/mini/mini-darwin.c \
            ${worksrcpath}/mono/utils/mono-sigcontext.h

        reinplace "s/arch_state->__/arch_state->/g" \
            ${worksrcpath}/mono/utils/mach-support-x86.c
        reinplace "s/struct __darwin_mcontext32/struct mcontext/g" \
            ${worksrcpath}/mono/utils/mach-support-x86.c
        reinplace "s/ctx->__/ctx->/g" \
            ${worksrcpath}/mono/utils/mach-support-x86.c
    }

    # Ensure we are UsingTheRightCompiler (https://trac.macports.org/wiki/UsingTheRightCompiler)
    reinplace "s:__MACPORTS_CC__:${configure_cc}:g" \
        ${worksrcpath}/mono/mini/aot-compiler.c
}

post-destroot {
    set docdir ${prefix}/share/doc/${name}

    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} AUTHORS COPYING.LIB ChangeLog NEWS README.md \
        ${destroot}${docdir}

    xinstall -m 0644 -W ${worksrcpath}/docs assembly-bundle embedded-api exceptions \
        gc-issues jit-thoughts jit-trampolines object-layout stack-alignment unmanaged-calls \
        ${destroot}${docdir}

    set libgc_docdir ${prefix}/share/doc/ligbc-mono-6.6

    move ${destroot}${prefix}/share/libgc-mono \
        ${destroot}${libgc_docdir}

    xinstall -d ${destroot}${libgc_docdir}/html

    move {*}[glob ${destroot}${libgc_docdir}/*.html] \
        ${destroot}${libgc_docdir}/html
}

# beta_version is actually the latest stable
# stable_version is the EOL previous stable version
# go figure...
livecheck.type      regex
livecheck.url       [lindex ${master_sites} 0]
livecheck.regex     [quotemeta ${name}]-(3(?:\\.\\d+)*)[quotemeta ${extract.suffix}]