# -*- 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.7
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                 48e1756da9fcf2009aa354fe10cba84c3f24043584078f923cdc1b1aba53abe5 f7c46883c146e5eb3beb74976fb2addab11d70bc 2543335 \
    boot.amd64-unix.tgz     c39a23d7aa64986ba2383296e0be0f23ad7b6940e6ad3f357af814ec06fa7874 12b7049d650a5a54b9f5741fc809d2658613bb04 6002621 \
    boot.ppc-unix.tgz       12acf646d92bf14a6d8911d1f5493b87e1a819b3eb1026944e8cb6c8846f57bb 417f38df50869f3001da677781a9039792ef29b9 5875314 \
    boot.x86-unix.tgz       712bbd6bc65818d001088033e9d775f0ad6aadc6a864f33b8362ec344a226df3 3d4a5de4210c5361587597558c990907825ceae8 5957052 \
    config.tgz              3e6d2293dee838a64866b80047d9ef87a6a5e73bf07ab15dc6c032dbf07b1c96 f01edd2511b6c18ac9be47abee13bcb08e2a368f 23322   \
    cm.tgz                  1a4a9cf1691f744c91c556c630de5cdb61baa7ee698e3f78f02951b43c50786e 881dfdca73d86dc362c5f6e7a7f684be86ba04d2 220339  \
    compiler.tgz            d7409066d091751fc76ba58cf81878f9cb53d5cf8b21a8dd501c1d39f20fbe61 9de597d2f7168ddb306beb977eacb68657a32ea2 888836  \
    runtime.tgz             784badbe6987213f9e21a8a5576798066b66a74d182d43307e697a0d87105f75 1f9ea78b5aeb2c5c7345effe29514602289e4d25 337099  \
    system.tgz              9c09f4dbd541e30b112154849121643f0d75fed7d26c873d30b8214f587341f4 752dd2658a110ae4722a48f9616f1b8f10a9e765 296518  \
    MLRISC.tgz              563a87932021943bc518c569af635b5584f5c0690feb0efb74f55a5aaf0ac47f 136cbf6e1c7c24c893bf5af2b99cd8a7e94a42c1 1457490 \
    smlnj-lib.tgz           dffa6d0b19e16f201f3b6f8aa66b7e57ca80b1a2a354e83aca2af4d103d978b5 439d5a62189d1edb670b2920de401fe059ae1ebe 667476  \
    old-basis.tgz           dfe8e595bb395d87f04becd705826fd5e3baea15fc375fb3a5556646fe2a4b06 c00f0edbdb10db5bd48ad453f80900b4115b6d39 1364    \
    ckit.tgz                1a68718897a4e14547783afaa60c593de6627cfef7eca3ba24523020f7e2dea8 1521147f7e7d2075895ccf98bdd1121f0b98acdb 200760  \
    nlffi.tgz               40160eacc54b11ca8b6c9ef746350e11ec1029e22c3031877bd2c161519cf0dd 535bbe778080ead3d954b6ad91d8d3736d62402f 74687   \
    cml.tgz                 6a5f55e03837df543282180efc69739294e4b142ff78db6a07941a9eb49bfa46 0bb1f145ace8204d2b21e0c81930d6e0e595f8f3 103974  \
    eXene.tgz               97641f20023d7197aaa9b7753a432705da3fd268103532d538ac6822ae2fd2cf 41c9fb3861ccae8797d9004d28c731d297e5e4b5 714475  \
    ml-lpt.tgz              26aee9107d8ff70ae896389d5a5d2931579d7d019e2e425242a99bf046c0d75c 26614089ec1311359ab13696d7afa375038f123a 267375  \
    ml-lex.tgz              4e5c147ec1c66256d3e70a9109d712c3687baa2e8cdb41500fc76d2a5901408f 975f02c02c3c684272199ebe88d8ef2f6c7859fc 28044   \
    ml-yacc.tgz             9896c5433f3c65c602e8ea757aea834880e6b7a7fe1ef366aaa8adaabfede921 590b05508bf356f178ea940313245360e850b12f 101374  \
    ml-burg.tgz             30138e3296036f0602def2528879151faa44f6aaefea916dbc2b13d692af2750 ed3635801d5d287cece8a217eac7a40302b529ac 40295   \
    pgraph.tgz              6a7cc745682794f7e9d538f710033cb56753d3361464dc6a462e88bbb635eb78 d8def6e6041ec0e12a8b7fd6b03bc8bd0ddfc822 5367    \
    trace-debug-profile.tgz a302a3df16052031749cdf05f5ae62ccc1808ee0e1cdcfdfe8ca45027900ee4a 76525b17cfa5f239e363f2aa738119ebee6459cf 3889    \
    heap2asm.tgz            d119da77206fbc857a13db518b5d857cb05acea5dab6bd0c9a662c046aaf0f4c 776bf31243b13bcf218868e060ac97432a836dea 1721    \
    smlnj-c.tgz             f9ce4ee8cdc1f1e433f7b09a30699e381c37923c5cf699226ecdf09884c80f2b 48eb1cf3ce1a40441cc8cf3746f5ee54f9d70605 10589   \
    asdl.tgz                2a4a9539c203a321b1de7b8ba7394b687450c5ce0d5d4d7101903925a146eafd acd49ce98dbdae68158aca3063615bf45bf91d4d 241723  \
]

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