# -*- 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               compiler_blacklist_versions 1.0

name                    gambit-c
version                 4.8.4
set branch              [join [lrange [split ${version} .] 0 1] .]
categories              lang
license                 {LGPL-2.1+ Apache-2}
maintainers             nomaintainer
description             Gambit-C is a fast Scheme implementation.
long_description        Gambit-C includes a Scheme interpreter and a Scheme \
                        compiler which can be used to build standalone \
                        executables. Because the compiler generates portable \
                        C code it is fairly easy to port to any platform \
                        with a decent C compiler. The thread system is very \
                        efficient and can support millions of concurrent \
                        processes. \
                        \
                        The Gambit-C system conforms to the R4RS, R5RS and \
                        IEEE Scheme standards. The full numeric tower is \
                        implemented, including: infinite precision integers \
                        (bignums), rationals, inexact reals (floating point \
                        numbers), and complex numbers.

homepage                http://dynamo.iro.umontreal.ca/~gambit/wiki/index.php/Main_Page

platforms               darwin
depends_build           port:autoconf

master_sites            http://www.iro.umontreal.ca/~gambit/download/gambit/v${branch}/source/
distname                gambit-v[strsed ${version} {g/\./_/}]-devel
extract.suffix          .tgz

checksums               rmd160  0445c576690274c5ddf08dc73156f1eecffc3331 \
                        sha256  daeaaa55f0c5076686313017c7c3676c60db14cbf2b55dd6023e8ed1fcfff218

# https://trac.macports.org/ticket/46013
compiler.blacklist {clang < 600} *llvm-gcc-4.2

configure.args          --infodir=${prefix}/share/info \
                        --libdir=${prefix}/lib/gambit-c \
                        --docdir=${prefix}/share/doc

if {[string match *gcc* ${configure.compiler}]} {
    configure.args-append --enable-single-host
}

use_parallel_build      no
# gsc conficts with a binary installed by Ghostscript. Recommended
# option (based on mailing list posts from Arch Linux and FreeBSD
# projects) is to rename gsc to gambit-gsc
set gsc "gambit-gsc"
set gsi "gambit-gsi"

post-extract {
    # Get rid of the incompatible version-specific installation structure
    reinplace "s|PACKAGE_SUBDIR=\"/${version}\"|PACKAGE_SUBDIR=\"\"|g" ${worksrcpath}/configure.ac
    reinplace "s|&& \$(LN_S) \.\$(PACKAGE_SUBDIR) current||g" ${worksrcpath}/makefile.in
    reinplace "s|rm -f \$(prefix)/current \$(prefix)/current.lnk||g" ${worksrcpath}/makefile.in
    reinplace "s|.B gsc|.B ${gsc}|g" ${worksrcpath}/doc/gsi.1

    # Fix target paths since the configure options are not properly used in the makefiles
    foreach makefile [exec find ${worksrcpath} -name makefile.in] {
        reinplace "s|\$(prefix)\$(PACKAGE_SUBDIR)|\$(DESTDIR)\$(prefix)|g" $makefile
        reinplace "s|\$(prefix)/info|\$(prefix)/share/info|g" $makefile
        reinplace "s|\$(prefix)/doc|\$(prefix)/share/doc/gambit-c|g" $makefile
        reinplace "s|\$(prefix)/lib|\$(prefix)/lib/gambit-c|g" $makefile
        reinplace "s|\$(prefix)/syntax-case\.scm|\$(prefix)/lib/gambit-c/syntax-case\.scm|g" $makefile
    }
}

post-destroot {
    file rename "${destroot}${prefix}/bin/gsc" "${destroot}${prefix}/bin/${gsc}"
    file delete "${destroot}${prefix}/bin/gsc-script"
    ln -s "${gsc}" "${destroot}${prefix}/bin/gsc-script"
    # Create a matching link for gambit-gsi for symmetry, even though the
    # original gsi binary doesn't conflict and isn't being removed.
    ln -s "gsi" "${destroot}${prefix}/bin/${gsi}"
}

variant optimized description "Use expensive GCC optimizations to improve speed and compactness" {
    configure.args-append --enable-gcc-opts
}

variant profile description "Enable profiling" {
    configure.args-append --enable-profile
}

livecheck.type      regex
livecheck.regex     (\\d+\\.\\d+\\.\\d+)
livecheck.url       ${homepage}