# -*- 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
PortGroup               makefile 1.0
PortGroup               muniversal 1.1

name                    lrslib
version                 7.3
revision                0

categories              math
license                 GPL-2+

maintainers             nomaintainer

homepage                http://cgm.cs.mcgill.ca/~avis/C/lrs.html
description             self-contained ANSI C implementation of the reverse search algorithm for  vertex enumeration/convex hull problems
long_description        {*}${description}

master_sites            http://cgm.cs.mcgill.ca/~avis/C/lrslib/archive/
set distfile_version    0[string map {. ""} ${version}]
distname                ${name}-${distfile_version}

checksums               rmd160  e15ae9ddc7f3858e8c3bf2cf51ddbce2da2be1c3 \
                        sha256  c49a4ebd856183473d1d5a62785fcdfe1057d5d671d4b96f3a1250eb1afe4e83 \
                        size    4566562

post-extract {
    # Extracted files do not have correct 'group' and 'other' permissions.
    fs-traverse item ${worksrcpath} {
        if {[file isfile ${item}]} {
            file attributes ${item} -permissions 0644
        }

        if {[file isdirectory ${item}]} {
            file attributes ${item} -permissions 0755
        }
    }

    move ${worksrcpath}/makefile ${worksrcpath}/Makefile
}

compiler.blacklist-append \
                    *gcc-3.* *gcc-4.* {clang < 500}

# Follow the instruction to build in 32bit mode on 32bit arch
if { ${configure.build_arch} in [list i386 ppc] } {
    patchfiles-append \
                    patch-build-32bit-mode.diff
}

platform darwin {
    post-patch {
        # Use MacPorts prefix instead /usr/local
        reinplace "s|/usr/local|${prefix}|" ${worksrcpath}/Makefile

        # Darwin requires different arguments to build dynamic libraries
        reinplace "s|-shared -Wl,-soname=|-dynamiclib -install_name ${prefix}/lib/|" ${worksrcpath}/Makefile

        # Darwin's install is a bit different than linux one
        reinplace {s|install -t $(DESTDIR)${prefix}/lib $(SHLIB)|install $(SHLIB) $(DESTDIR)${prefix}/lib|} ${worksrcpath}/Makefile
        reinplace {s|install -t $(DESTDIR)${prefix}/include/lrslib ${INSTALL_INCLUDES}|install ${INSTALL_INCLUDES} $(DESTDIR)${prefix}/include/lrslib|} ${worksrcpath}/Makefile
    }
}

makefile.prefix_name    prefix

depends_lib-append      port:gmp

build.env-append        SONAME=liblrs.1.dylib \
                        SHLIB=liblrs.${version}.dylib \
                        SHLINK=liblrs.dylib

build.target            single all-shared

destroot.env-append     {*}${build.env}

post-destroot {
    foreach f {lrs-shared lrs1 lrs2 lrsgmp lrsmgmp lrsmp lrsnash-shared} {
        file copy ${worksrcpath}/$f ${destroot}${prefix}/bin
    }

    xinstall -m 755 -d ${destroot}${prefix}/libexec/${name}
    move ${destroot}${prefix}/bin ${destroot}${prefix}/libexec/${name}
}

notes "
The ${name} binary files are now installed into ${prefix}/libexec/${name}/bin
"

livecheck.type          regex
livecheck.version       ${distfile_version}
livecheck.url           http://cgm.cs.mcgill.ca/~avis/C/lrslib/archive/
livecheck.regex         "${name}-(\[\\da-z]+)"
