# -*- 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.3
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                 5a7f0b15564b34529693516da6face5d6d8702f91a6bf80a49e886bd752d8423 32a4a7acc31cdd838f79f55cb555dcdd9c186363 2506809 \
    boot.amd64-unix.tgz     3464473a9434dafe165619c014e36d7b6506cf2f0826c562a342a73af9b16603 1d849cfdf7bf5d668fd374c632247566d3f0e42b 5971321 \
    boot.ppc-unix.tgz       33c04a79625eaf6162a3e1e2c560f9e038f0191ae166edfe49cb5afb04732af0 f1a3ef274b70c977f398e760c1514eea7de7e532 5839528 \
    boot.x86-unix.tgz       3517b9e2258da5c119363ed58fe8b8b00426070d8d3c10ead5ae5d1d7bf5a0c5 cfc478b1d9b6226bd890d5c713276b22d9960fb6 5923052 \
    config.tgz              c04264f461c84114b2666b5a4544d0e66d8c0b4137534e137510e055de444197 9c24ec58b7096a2f9a020c0e458da64f2f1aa78b 23167   \
    cm.tgz                  499fed8ec350e92a4c066dd40c3ec0a77434a3293958b4bb787fd1b08373847b 435c28ca9bb608ac40e737825e03746432a07262 220282  \
    compiler.tgz            d428e00b66117df81dbf46f0f3c07bf8119e4804dfad334b89cca6acd0cf4fba 075ea80335af8492aeac536045129185a62b5cb4 856614  \
    runtime.tgz             4d2fa1d6092af058d12f21c86f7349c8850022193fe190c7ae27008d2a521720 7e1393fa58a8ac6cc0e8acf98a4e4e8f162dbf88 334293  \
    system.tgz              d2a4e51fc9e3ff2914902fd98eea9b8219786f9413d04155334d1f0a986486ad 38a1c84b7a24211777b3dc412b54f43ee1061719 283393  \
    MLRISC.tgz              5677101c949a74d0a5991cf1f4f8e9e002a0db065043d160ed68ae0fd94e0534 05b4542625416ed9c1e7c80831c4d9fc8b56f344 1457337 \
    smlnj-lib.tgz           c5bcd055746c6198288429f28f087d6e5d6e6b8df369eba35b19419461d8e1c5 30e714294f9d63e6e77197a0095348eb89a6abb8 641512  \
    old-basis.tgz           002b69f3584b18092d82d8b25458c0da843e0ccb8eb3144df53d841c8c146b46 57ee85b9ee5a7a437f032dd8d2edbd83ba9d29cf 1363    \
    ckit.tgz                8b1414487beafbd075a6c2ffb8252e766528abdd757412ecf089e732b571b2e5 5e9762dbaabdb01c4f910ef80cdc243e321c02b3 200785  \
    nlffi.tgz               154dcf0839c9ce3d8a69b3264f10759021e99d42fc04be5542007272eafd2f63 2da0c69b6bb42bdd1444b59743ff41f86f948390 74810   \
    cml.tgz                 83a72ce62fbd3a5b9aca09b4c6f5f80365becefa1cec424e10cf1b03ffff7752 b3a980c502b3f6a9a55b658a23b70674c4d01ff9 103973  \
    eXene.tgz               981ec9ce71d4fb306b880b5cb49efeabb0079309869662fd8d1b865a0e191688 7a5f9131c60328621206bfea6915ea27c7997c37 714453  \
    ml-lpt.tgz              937447260119657c1d2eb4ba5e91f7eb6ee900506b9c71d4b8ae37403d48f78a 2376de496d52aa2ed1a6595a7a5b80c2aa586d3b 267067  \
    ml-lex.tgz              9e5097d532590e63680ddedffd349424ab340d27118fa57269b578945c4ab8ee 4ad54d1aa907c2d02e0557513129a0b9f9ff4127 28038   \
    ml-yacc.tgz             218a8ad4e70808843ec25077b0060f6ddab72c088cae3150f8ffceda128c7349 2cb3f2a62dc2ec0e2b8464ae9ea96e455f754e02 101376  \
    ml-burg.tgz             8e523d3d1d50fbf3d0e6c36a1d23b6fc850b5833e26b6f7435ee0ab9d30d4aa0 eacdc0edd575b0315bd747daf95e018a175723f6 40357   \
    pgraph.tgz              8e24305c79142191bf300e215b12a0ef2afbedaee7bf38cbeaa091c475130d71 34a46478509ffffce1acd639ec171f5c1780103f 5367    \
    trace-debug-profile.tgz 1d71ab09c5b9e7b991483325582dfa2b6130005af8f463afbf00aecede01f576 84c346f2e7b5bcd81fe443452745d1055b1a09a3 3890    \
    heap2asm.tgz            aff2f65d68895e69b70b531be62fafbcc1cc7ed8b9c8504e3731e51bdf698679 ff22d2e90e9cb8148f4b5031b8e667ef2854df80 1763    \
    smlnj-c.tgz             4dff4d93d443af9d3019607965b81ab600d67c59e7f42289473662280aaf24fe 6fafbf3981820d8211c39807a930af3e933ebf1f 10591   \
    asdl.tgz                3e16bd262e25b95e481937d9c32382932776b033397afb6b16da313d6839700a e73b0d63d9bac95527684c8995427f39a3586a4d 241353  \
]

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
}

### build ###
build.env           URLGETTER=curl
build.cmd           ${worksrcpath}/config/install.sh
build.target
if {${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/([0-9.]+)/}
livecheck.version   2021.1
