# -*- 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.9
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                 73a645a59545438733e5abfc4de4902553a683dd17b8445cadb6f93c5cdd12e6 8bbfd3bdf6420cbe49779fde6dd433ba8c528c6f 2518075 \
    boot.amd64-unix.tgz     2b4c23bb4e49be040043ab70a3df8e92c26ffe1cba652ed80348cd3fe2d19c63 ff3cc1641a75007a3594ce9ecbfdb1194d4a9ef5 6022171 \
    boot.ppc-unix.tgz       c9383de0c5bd794925d6f7e23da389dafc2c25659cf193b375ff2d1535274a8e 217d26b8ebc4711bcc21fd48f36095cc6146f3e6 5888134 \
    boot.x86-unix.tgz       207bfdd92f137be47933545cc1f330e78f01dc76c293657971d02f589faeb1d9 2e15cee3acc6d8d8860b9fc3671e46595abe4a3c 5972700 \
    config.tgz              bf479ba518652fc193ac0954e46c72c5356a33c6980f9fcb6bcf6e55c7731418 f1e6b8a3f02d2a43b6e2cc74235c3a060c7100ea 23345   \
    cm.tgz                  3ae7ebec93ec649b3861cbf383b977435d26bc0e1b1c1427c08da66c9ab8ddbf a2cc50cce9c72807150ab08d91e8401d8893e345 220315  \
    compiler.tgz            b3e4c2a14201076a195020fdb94f9d975a004fc0663611124307cd70d5f36796 c7cfb8e2010087212baa61dde0c18e4a1543cb12 895788  \
    runtime.tgz             216e0193a193ae2e59296d27c42e61a3aa7a16ddda936d3a06a2b9459276b792 9ed12ed7959f1e9807c618add4d81a24ef22f946 338624  \
    system.tgz              9a0ebd3c4ddcf77d83ee1557fbf8a3a1347c1c95e413e99fa6d5a8359a57ab56 c508e50a514186729dac1c328a8ca6b64440d96b 320547  \
    MLRISC.tgz              d94df765eadaa9ec98dcabe1d8dbb29975a8d34152ce228f3deffa7d3b5f7560 63bff0f3efd14097931f3bcb49f2a7919c63d238 1457377 \
    smlnj-lib.tgz           ebc9d6db916d4b998293dd4024760087964fd8b34bf39e7aa4f927c401fa34aa 9b00c9ec6dbbaa3e63ed96ad66a9472bcfc7e5ce 676823  \
    old-basis.tgz           b01c005f9d14662af84c6880018476d80f2d5e48ac37ca3a906886b8c5f1c3fb 30fdb8206b4dca2d4f120a7524d4a4aa393d20ea 1364    \
    ckit.tgz                1586fa202aa1f29a160dec09ea58710a52b74b615a603fb2cac3618301d55d9a 6bfb57e6bc68213c467b13d8d0e1e12b3ddbee79 200766  \
    nlffi.tgz               29ccf8d3c1b1c8614f87e0b7c6ee5b2f6c6ec78cf632163f164e8b71bd86e0c1 ede256a2e23910134fe04f6e981b2b295cb59db1 74686   \
    cml.tgz                 efeafaddc4307b602e5d13ecf76c2d522124c5e40341d9049f9f912bc8f67aee 0ddec2c30edf029b3ec02c7ea89b3830f110482b 104036  \
    eXene.tgz               8c4c8d7823cfd3a50b53c7d66b1c1f6185477f0443121eb74b3c308508057e2b effcb32a3440703efb5b6dc0100b1f1c638c96b1 714449  \
    ml-lpt.tgz              41b13bb84c4d139bc3743c4eabfc9b1965bc60ef771479e6c8a381e159617f98 9c91ed25561c397c47da3ec5fb2d27f9d998f949 267352  \
    ml-lex.tgz              25b1379581fb996a732cd591d032d388338e5d1d8f95c4e187a2265b210ef686 99cf9f818bb4f76340359de167bbdb0e47415aec 28032   \
    ml-yacc.tgz             103e26e6540f3789de77f1e99cf31550682c9ea22c3fc1f06f1251a8d5d26330 0a38b15e40e6c9721edfcf8ce7ee667036fcd91d 101368  \
    ml-burg.tgz             f9a8a1db2b2794f6af0e2bc37878a7cf93ca3f169727c5f0166ed36140a3398b f0bcc849bf4d160f416bf598c0b0e8ae756c3798 40291   \
    pgraph.tgz              7be4fa408e89ef3559e9cdde6fc278058754ed0b6ba187695c5c5d5400d1791f 830187757c775088167358fa8a93d3e5b60a3263 5367    \
    trace-debug-profile.tgz af45f71fed108c4004088ec7135dbb66cfe382e71ed9a9929ffaa5f69bf4e85f 69d842de337ea5f58e4975c082a53ddf60d4e723 3888    \
    heap2asm.tgz            1e221737c6cd6755ad4156080b3de354aa46212a531dc2304b02814c5f036f3e 79440430674339b9372ff43d557214a9f053d267 1719    \
    smlnj-c.tgz             2168c85652aeab0ddeab339222d24fef956a960a15601aeedb2bad51ee05f054 d50b95895acfc28d30166128b9c1d268b827aa04 10587   \
    asdl.tgz                394056b7d1bdb94c279e2f052571cd53c12c3d12f154cdb7cb7b0589a569e3dd 9741430bb8256f753b751b2d28dbe6c23c7bbaa4 241685  \
]

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.]+)/}
