# -*- 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 116365 2014-01-23 20:13:56Z sean@macports.org $

PortSystem          1.0
PortGroup           mpi 1.0

compilers.choose    cc cxx
mpi.setup           require

name                scotch
version             6.0.0

set v_version_wo_char {}
set v_char            {}
set v_regexp          {(\d+\.\d+\.\d+)([a-zA-Z]?)}
regexp $v_regexp ${version} -> v_version_wo_char v_char

# this is specific to this port and *version* for downloading it:
set release         31832

categories          science
maintainers         mk pixilla
platforms           darwin
license             CeCILL-C

description         Scotch is a software package for graph and mesh/hypergraph \
                    partitioning and sparse matrix ordering.
long_description    ${description}

homepage            http://gforge.inria.fr/projects/scotch
master_sites        https://gforge.inria.fr/frs/download.php/${release}
distname            scotch_${version}_esmumps

livecheck.type      regex
livecheck.url       https://gforge.inria.fr/frs/?group_id=248
livecheck.regex     "scotch_($v_regexp)_esmumps${extract.suffix}"

use_bzip2           no

worksrcdir          ${name}_${v_version_wo_char}_esmumps/src

checksums           rmd160  71c0c7d189a35e261a09f239696369e5f41b2508 \
                    sha256  8206127d038bda868dda5c5a7f60ef8224f2e368298fbb01bf13fa250e378dd4

patchfiles          patch-libscotch-shared.diff \
                    patch-libscotchmetis-shared.diff

use_configure       no

if {![mpi_variant_isset]} {
    default_variants +mpich
}

post-extract {
    copy ${worksrcpath}/Make.inc/Makefile.inc.i686_mac_darwin8 ${worksrcpath}/Makefile.inc
    reinplace "s|-O3|-O3 -fPIC|" ${worksrcpath}/Makefile.inc
    reinplace "s|gcc|\$(CC)|" ${worksrcpath}/Makefile.inc
    reinplace "s|mpicc|${mpi.cc}|" ${worksrcpath}/Makefile.inc

    # starting with version 6.0, scotch uses a newer version of pthreads which
    # Apple hasn't provided, so we disable pthreads for now
    reinplace "s|-DCOMMON_PTHREAD||" ${worksrcpath}/Makefile.inc
    reinplace "s|-DSCOTCH_PTHREAD||" ${worksrcpath}/Makefile.inc
}

pre-build {
    build.env-append    CC=${configure.cc} \
                        CXX=${configure.cxx} \
                        CFLAGS="${configure.cflags}" \
                        CXXFLAGS="${configure.cxxflags}" \
                        LDFLAGS="[join ${configure.ldflags}]" \
                        prefix=${prefix}
}

build.target scotch ptscotch

# scotch and ptscotch MUST be built sequentially according to scotch's INSTALL.txt
use_parallel_build no

pre-destroot {
    destroot.env-append prefix=${destroot}${prefix}
}

post-destroot {
    eval xinstall -m 755 [glob ${worksrcpath}/../lib/*.dylib] ${destroot}${prefix}/lib
}
