# -*- 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: Portfile 91255 2012-03-27 14:41:38Z snc@macports.org $

PortSystem 1.0

name                    gambit-c
version                 4.6.5
set branch              [join [lrange [split ${version} .] 0 1] .]
categories              lang scheme
license                 {LGPL-2.1+ Apache-2}
maintainers             snc openmaintainer
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                gambc-v[strsed ${version} {g/\./_/}]
extract.suffix          .tgz

checksums           rmd160  2ab84686483c541b1a2e182520740bf8293d266c \
                    sha256  54de513a78f0fb1841ef1411b8f2d64a2bd6810cee7554fe408b0ba79ff00910

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

# 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}
