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

PortSystem          1.0

name                cm3
version             5.2.6
revision            1
categories          lang
platforms           darwin
maintainers         nomaintainer

homepage            https://modula3.elegosoft.com/cm3
description         Critical Mass Modula-3 Compiler
long_description    CM3 5.2 is an open source release of Critical Mass Modula-3. \
    It is a complete easy-to-use and easy-to-install Modula-3 system \
    that contains considerable enhancements over its ancestor DEC SRC Modula-3.

master_sites        https://modula3.elegosoft.com/cm3/releng/
distfiles           cm3-src-all-${version}.tgz

checksums           cm3-src-all-${version}.tgz md5 \
                        44fd8c9c905707e4f9835eaf6b747127

worksrcdir          cm3
extract.dir         ${worksrcpath}
pre-extract         "file mkdir ${workpath}/${worksrcdir}"

# Add a path entry for the bootstrap cm3 compiler
build.env           PATH=$env(PATH):${workpath}/bootstrap/bin

# M3 requires a bootstrap compiler to build
# This port currently supports only Darwin/PPC
platform darwin powerpc {
    distfiles-append    cm3-min-POSIX-PPC_DARWIN-${version}.tgz
    checksums-append    cm3-min-POSIX-PPC_DARWIN-${version}.tgz md5 \
                            6b1421b926731c36c0fc98e86f08c24c

    # Apple's cpp-precomp in Jaguar (10.2) incorrectly handles
    # gcc 'poisoned' tokens, so we turn it off.
    # It only makes the build take longer anyway.
    patchfiles      patch-fix-cpp-precomp.diff
}

platform freebsd i386 {
    distfiles-append    cm3-min-POSIX-FreeBSD4-${version}.tgz
    checksums-append    cm3-min-POSIX-FreeBSD4-${version}.tgz md5 \
                            162156b414ffebd24c8f0ab9b71ee5e8
}

post-extract {
    # Create bootstrap directory and extract the cm3 bootstrap to it
    file mkdir ${workpath}/bootstrap
    system "tar -C ${workpath}/bootstrap -zxf ${worksrcpath}/system.tgz"
}

configure {
    # Configure the bootstrap
    file copy -force \
        ${filespath}/${os.platform}-cm3.cfg ${workpath}/bootstrap/bin/cm3.cfg
    reinplace \
        s|@USE_ROOT@|\"${workpath}/bootstrap\"|g \
        ${workpath}/bootstrap/bin/cm3.cfg
    reinplace \
        s|@INSTALL_ROOT@|\"${destroot}${prefix}/cm3\"|g \
        ${workpath}/bootstrap/bin/cm3.cfg
    reinplace \
        s|@PREFIX@|\"${prefix}/cm3\"|g \
        ${workpath}/bootstrap/bin/cm3.cfg
}

build {
    set cm3-buildscript "cd ${worksrcpath}/scripts && env ${build.env}"
    # build core distribution
    system "${cm3-buildscript} ./do-cm3-core.sh buildship"

    # build tcp package
    system "${cm3-buildscript} ./do-pkg.sh buildship tcp"
}

destroot {
    # create a link to cm3 (m3 compiler) in ${prefix}/cm3/bin
    system "cd ${destroot}${prefix}/cm3/bin/ && ln -s ../pkg/cm3/PPC_DARWIN/cm3"

    # copy bootstrap configuration file to destroot
    file copy -force \
        ${filespath}/${os.platform}-cm3.cfg ${destroot}${prefix}/cm3/bin/cm3.cfg

    # update file paths to reflect final installation paths
    reinplace \
        s|@INSTALL_ROOT@|\"${prefix}/cm3\"|g \
        ${destroot}${prefix}/cm3/bin/cm3.cfg
    reinplace \
        s|@USE_ROOT@|\"${prefix}/cm3\"|g \
        ${destroot}${prefix}/cm3/bin/cm3.cfg
    reinplace \
        s|@PREFIX@|\"${prefix}/cm3\"|g \
        ${destroot}${prefix}/cm3/bin/cm3.cfg
}

livecheck.url       https://modula3.elegosoft.com/cm3/releng/download.html
livecheck.regex     cm3-src-all-(\[0-9.\]+)(?:-REL).tgz