# -*- 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
PortGroup               bitbucket 1.0

# Keep the versions of pure and pure-docs in sync.
bitbucket.setup         purelang pure-lang 0.64 pure-
name                    pure
categories              lang
platforms               darwin
maintainers             ryandesign gmail.com:aggraef
license                 LGPL-3+ GPL-3+ BSD
use_parallel_build      yes
homepage                http://purelang.bitbucket.org/

description             functional programming language based on term rewriting

long_description        Pure is a functional programming language based on \
                        term rewriting. It has a modern syntax featuring \
                        curried function applications, lexical closures and \
                        equational definitions with pattern matching, and \
                        thus is somewhat similar to languages of the Haskell \
                        and ML variety. But Pure is also a very dynamic and \
                        reflective language, and is more like Lisp in this \
                        respect. The interpreter has an LLVM backend to do \
                        JIT compilation, hence programs run blazingly fast \
                        and interfacing to C modules is easy. The libraries \
                        are licensed under LGPL-3+, the interpreter is GPL-3+, \
                        and the examples are BSD-licensed.

bitbucket.tarball_from  downloads
distname                ${name}-${version}

checksums               rmd160  fa5d57b94fad999ada281edaf30db4212a0a28ae \
                        sha256  efd55229342aec9d79e8fa4732a30f140e1773064f3869abde053e01468f7b07

set llvm_version        3.4
set llvm_prefix         ${prefix}/libexec/llvm-${llvm_version}
configure.env           PATH=${llvm_prefix}/bin:$env(PATH)

if {${name} eq ${subport}} {
    revision                    3
    
    depends_lib                 port:gmp \
                                port:libiconv \
                                port:libtool \
                                port:llvm-${llvm_version} \
                                port:mpfr \
                                port:ncurses \
                                port:readline
    
    depends_run                 port:w3m
    
    configure.args              --without-elisp \
                                --with-tool-prefix=${llvm_prefix}/bin
    
    # Explicitly disable C++11 support because Pure doesn't need it and the
    # auto-detection in the configure script gets it wrong when using clang
    # 3.3 or later with libstdc++:
    # https://bitbucket.org/purelang/pure-lang/issue/28/pure-fails-to-build-on-os-x-107-and-108
    configure.args-append       --disable-c++11
    
    # Prevent build failure when old llvm port is installed
    configure.cppflags          -I${llvm_prefix}/include ${configure.cppflags}
    configure.ldflags           -L${llvm_prefix}/lib ${configure.ldflags}

    # Last-minute fixes to the ATS inlining support
    patchfiles-append           pure-0.64-ats-hotfixes.diff
    # Critical bugfix in the debugger (issue #32)
    patchfiles-append           pure-0.64-debugger-hotfix.diff
    # Bugfix in the runtime (pure_switch_interp, rev. 9114fc3)
    patchfiles-append           pure-0.64-runtime-hotfix.diff
    
    platform macosx {
        depends_run-replace     port:w3m port:openbrowser
        patchfiles-append       patch-openbrowser.diff
    }
    
    platform powerpc {
        configure.args-append   --disable-fastcc
    }
    
    post-destroot {
        set docdir ${destroot}${prefix}/share/doc/${name}
        xinstall -d ${docdir} ${destroot}${prefix}/share/examples
        xinstall -W ${destroot.dir} -m 644 \
            COPYING \
            COPYING.LESSER \
            ChangeLog \
            NEWS \
            README \
            TODO \
            ${docdir}
        copy ${destroot.dir}/examples ${destroot}${prefix}/share/examples/${name}
    }

    variant pcre description {Replace POSIX regular expression support with PCRE support} {
        depends_lib-append      port:pcre
        configure.args-append   --with-pcre
    }
    
    test.run                    yes
    test.target                 check
} else {
    livecheck.type              none
}

subport pure-mode.el {
    revision                    0
    
    categories-append           editors
    license                     GPL-3+
    supported_archs             noarch
    homepage                    https://bitbucket.org/purelang/pure-lang/wiki/UsingPure#markdown-header-pure-and-emacs
    
    description                 Emacs mode for editing Pure files
    
    long_description            ${subport} is an ${description}.
    
    depends_lib                 port:emacs
    
    extract.post_args-append    ${worksrcdir}/etc
    
    build.dir                   ${worksrcpath}/etc
    
    post-extract {
        copy ${build.dir}/pure-mode.el.in ${build.dir}/pure-mode.el
    }
    
    use_configure               no
    
    build {
        reinplace "s|@bindir@|${prefix}/bin|g" ${build.dir}/pure-mode.el
        reinplace "s|@libdir@|${prefix}/lib|g" ${build.dir}/pure-mode.el
        system -W ${build.dir} "${prefix}/bin/emacs -q --no-site-file -batch -eval \"(add-to-list 'load-path \\\".\\\")\" -eval \"(setq byte-compile-warnings '(callargs free-vars noruntime redefine))\" -f batch-byte-compile pure-mode.el"
    }
    
    destroot {
        set lispdir ${destroot}${prefix}/share/emacs/site-lisp
        xinstall -d ${lispdir}
        xinstall -m 644 -W ${destroot.dir} pure-mode.el pure-mode.elc ${lispdir}
    }
}