# -*- 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                smlnj
version             110.99.8
categories          lang ml
license             BSD
maintainers         {toby @tobypeterson} openmaintainer
description         Standard ML of New Jersey
long_description \
    Standard ML of New Jersey (abbreviated SML/NJ) is a \
    compiler for the Standard ML '97 programming language \
    with associated libraries, tools, and documentation. \
    SML/NJ is free, open source software.
homepage            http://www.smlnj.org/
platforms           darwin

master_sites        http://smlnj.cs.uchicago.edu/dist/working/${version}/
dist_subdir         ${name}/${version}_${revision}

supported_archs     x86_64 i386 ppc
universal_variant   no

distfiles
checksums

# Files needed for basic distribution.
set srcs [list \
    doc.tgz                 2669760e8db2b9f32d37cd1abc3f5f7d1b79be84e62c522e31339a8972e11f0a cb67217323f75c307e423e424b91c2c306526821 2509346 \
    boot.amd64-unix.tgz     b9d2dec5a4a77be7caf3badc1166ae4cbd912fb7b22c041c9459477f5df8f064 260f372d30d006cbad015e79c9cdd739dd4a0bc3 6013620 \
    boot.ppc-unix.tgz       e59dc97593e2a0e3caba01230cc78ca599657915d7102dd1da74a335a6bd9cf6 6fbe8783a1bd7936035bcc43e47029591b10ceea 5886764 \
    boot.x86-unix.tgz       e13b425d1f8d8d32531f94cf75751deaeb3028b91df1f5758ca2ac7aca727493 3becb0b8ce9a62d1221ba8c488e8c444d976fe96 5967264 \
    config.tgz              eb7a6f2171c8e1727e4769a7eef72f72ec5ff1ae8561dd608880997898db023b 250c5dbb82c0353b52f34321f8d87e29438baaba 23316   \
    cm.tgz                  2418c6e8e5f0d05e8388349de0ed70886991c718f8817aecf9b6957af3b85ddd 808bff793de66c013f8c3749b14a75a15895b38d 220342  \
    compiler.tgz            1ac10587a5e11008b5e38fabd81d26e67edb5a53f09e6e5bf3d2714c1480186c a6ffeaf63c4f3e60331663c8567d62ecd7ff3196 894876  \
    runtime.tgz             ab85624d2dff3ae1e4342851ac408e00d5ac866bb4106b45752585e631c86dc5 2ea1a223dab8f26c5fe4994dffa88094bae73884 337793  \
    system.tgz              fa246932f8d4895498013400d54f7b483520703b34bfbe5e2ee7a684b23dcf94 448b0dc9d59fdc077ade151973a28b8a81ad16f9 296676  \
    MLRISC.tgz              7ca58e6ebdaeba5445d07094686d7dc705241e5484c81d8e983be3b17f7dd38a 2604d15f3c5625773dd060516240543beb99f45d 1457378 \
    smlnj-lib.tgz           efd13179137fc3f4e08be2f8158ead87924a061d466b94aa94b67b904ae360ad 791581ab7b2fba7b0e863e25b948b9b11b6571fb 667412  \
    old-basis.tgz           aa4385afeb25c05fe432a1fddf8e911c7a8c085524860b78dc925e7613631c72 fb91eb61c11388f19f563095360a014dfb5c92a7 1361    \
    ckit.tgz                57178904126f8822468e24f47dbee75cac4fc6b339799b4e8c7b7df8dc39c19f e131fe2eb32eae01441b19d1b858b2dae34de6df 200752  \
    nlffi.tgz               9a2c1671e06c07d3388ae1638303d7d4ae8b8f1d8d5cfacbdd87336eb4a411ca 22ff873cdcfa9a6e83823829f59d506a5472600d 74683   \
    cml.tgz                 a45d3365d4adf113162afed86fab37193bdf06220839f1ea5cb02cfad147d367 667f420ff92162df32af3b92a7e594cfc98f8dae 103965  \
    eXene.tgz               5e468b333c824d80f8bc7addf37899e341139591fc4d004eedce16a6a7121772 e9f25149890aad26509690181b34c9c59302df7a 714408  \
    ml-lpt.tgz              bc167b41441229a2cdd7bd6eb0f1b2391bb14a0f7ae5e800c08d8781fca52165 44b9c82d309f4a974c2c4fea486d3c359f3bb4ed 267346  \
    ml-lex.tgz              7f6712a3323c40264907213814517c14024f20d01791f4d47932c279b612f32c f85ee3e0bc4ab095051136062e84cddf5e965ab6 28027   \
    ml-yacc.tgz             792ef3cef12490183cb5e3f729bf3030b5e881433accbba49015c8128d269b56 1861f67628f6373bc15db8148041c6387ae64eab 101359  \
    ml-burg.tgz             b512a84e00b478606f6b2ea77d2fb0f3e5c59ead88ea868196d3e148d2826a2e 50d70eca1352d44e669015c94b90c68a35adc185 40287   \
    pgraph.tgz              b7b96b9c8507a3d40f60ff5553149cf815bdf0fa5cec9b81ba48de9ef1f2fa39 1faaf73a9e4b969b990e37612c7dff035fb179c3 5363    \
    trace-debug-profile.tgz 644118d78c2b0316b22d43bb10a7271517ec37bf1922838d5e43422745b28209 79400bf7e825a42fa6aec99fcc4a08b75a8c79c1 3889    \
    heap2asm.tgz            33de23e8495c5d18d461b4591a7bc739105da24118a260b6741e216c554e74e3 4b77aeb72afd56ef659140e26e13199c3ac0e3d4 1719    \
    smlnj-c.tgz             79b657ea68083c204c038ca0095309ae1f089ea54563f90d6446348f06c5c3a1 dcfb71f643f64eaf2bc157d2f79d76d1721c2879 10591   \
    asdl.tgz                9cd88b7168c6ab2132a7d255aed8bec13d8b8312aa395d0634ae73fcdc4a4c6d 81992c1fbb73be2cf9cb6617a425d35e05aaecf5 241689  \
]

foreach {tarball sha256 rmd160 size} $srcs {
    distfiles-append $tarball
    checksums-append $tarball sha256 $sha256 rmd160 $rmd160 size $size
}

### extract ###
extract.mkdir       yes
extract.only        config.tgz

### patch ###
patchfiles          patch-config__install.sh-correct-compiler.diff

post-patch {
    reinplace "s|@CC@|${configure.cc}|" ${worksrcpath}/config/install.sh
    reinplace "s|@CPP@|${configure.cc}|" ${worksrcpath}/config/install.sh
    reinplace "s|@CFLAGS@|${configure.cflags} ${configure.cc_archflags}|" ${worksrcpath}/config/install.sh
    reinplace "s|@LDFLAGS@|${configure.ldflags}|" ${worksrcpath}/config/install.sh
}

### configure ###
configure {
    reinplace "s|SRCARCHIVEURL=.*|SRCARCHIVEURL=file://${distpath}|" \
        ${worksrcpath}/config/srcarchiveurl
    reinplace "s|#request src-smlnj|request src-smlnj|" \
        ${worksrcpath}/config/targets
    reinplace "s|#request pgraph-util|request pgraph-util|" \
        ${worksrcpath}/config/targets
    reinplace "s|#request eXene|request eXene|" \
        ${worksrcpath}/config/targets
    reinplace "s|#request mlrisc-tools|request mlrisc-tools|" \
        ${worksrcpath}/config/targets
    reinplace "s|#request nowhere|request nowhere|" \
        ${worksrcpath}/config/targets
    reinplace "s|#  request heap2asm|  request heap2asm|" \
        ${worksrcpath}/config/targets
}

# error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ml_val_t’
configure.cflags-append -std=c99

### build ###
build.env           URLGETTER=curl
build.cmd           ${worksrcpath}/config/install.sh
build.target
if {${configure.build_arch} eq "x86_64"} {
    build.args-append   -default 64
}

### destroot ###
destroot {
    set smlnj_home ${prefix}/share/smlnj

    file mkdir ${destroot}${smlnj_home}
    file copy ${worksrcpath}/bin ${destroot}${smlnj_home}
    file copy ${worksrcpath}/lib ${destroot}${smlnj_home}

    xinstall -m 755 ${filespath}/sml.sh ${destroot}${prefix}/bin/sml
    reinplace "s|__SMLNJ_HOME__|${smlnj_home}|g" \
        ${destroot}${prefix}/bin/sml

    foreach prog [glob -directory ${worksrcpath}/bin *] {
        set progname [file tail $prog]
        if {![string equal $progname sml]} {
            ln -s sml ${destroot}${prefix}/bin/${progname}
        }
    }
}

livecheck.type      regex
livecheck.regex     {/working/(1[0-9.]+)/}
