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

github.setup        andreas-kupries critcl 3.3.1
revision            1
subport tcl8-critcl {revision 0}
subport tcl9-critcl {revision 0}
categories          lang devel tcl
maintainers         nomaintainer
license             BSD

description         C Runtime In Tcl

long_description    A system for embedding and using C code from \
                    within Tcl scripts.

github.tarball_from archive
checksums           rmd160 57e86692b092665084bf67806d6c76ea24a8dd68 \
                    sha256 d970a06ae1cdee7854ca1bc571e8b5fe7189788dc5a806bce67e24bbadbe7ae2

if {$subport eq $name} {
PortGroup stub 1.0
depends_lib port:tcl8-critcl
} else {
set tclport [string range $subport 0 3]
set tcl_major [string index $tclport end]
set tclsh [expr {$tcl_major == 8 ? "tclsh8.6" : "tclsh9.0"}]
depends_lib         port:${tclport}

patchfiles          01-arm64.patch \
                    02-platform.patch \
                    03-macos-vers.patch \
                    04-sdk.patch \
                    05-macports-config.patch \
                    06-default-osxmin.patch \
                    07-tcl9.patch

configure {
    reinplace "s|@CC@|${configure.cc}|" ${worksrcpath}/lib/critcl/Config
    reinplace "s|@CFLAGS@|${configure.cflags} [get_canonical_archflags cc]|" ${worksrcpath}/lib/critcl/Config
    set critcl_platforms {}
    if {${os.platform} eq "darwin"} {
        foreach arch [get_canonical_archs] {
            switch $arch {
                arm64 {
                    lappend critcl_platforms macos-aarch64 macos-arm macos-arm64 \
                        macosx-aarch64 macosx-arm macosx-arm64
                }
                i386 {
                    lappend critcl_platforms macosx-ix86
                }
                ppc {
                    lappend critcl_platforms macosx-powerpc
                }
                x86_64 {
                    lappend critcl_platforms macos-x86_64 macosx-x86_64
                }
                default {
                    lappend critcl_platforms macosx-${arch}
                }
            }
        }
    }
    reinplace "s|@CRITCL_PLATFORMS@|${critcl_platforms}|" ${worksrcpath}/lib/critcl/Config
}
build {}
destroot.cmd        ${prefix}/bin/${tclsh} ./build.tcl install
destroot.pre_args   --prefix "${prefix}"
destroot.args       --dest-dir "${destroot}" \
                    --bin-dir "${prefix}/libexec/${tclport}" \
                    --include-dir "${prefix}/include/${tclport}" \
                    --lib-dir "${prefix}/lib/${tclport}"
post-destroot {
    ln -s ../libexec/${tclport}/critcl ${destroot}${prefix}/bin/critcl${tcl_major}
}
if {${os.platform} eq "darwin"} {
    destroot.post_args -target macosx-macports
} else {
    destroot.post_args
}

}
