# -*- 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 91435 2012-04-01 22:00:12Z dports@macports.org $

PortSystem 1.0

name             R
conflicts        R-framework
version          2.15.0
categories       math science
maintainers      epfl.ch:kjell.konis
license          {GPL-2 GPL-3}
platforms        darwin

description      R is GNU S - an interpreted language for statistical computing

long_description \
    R is a language and environment for statistical computing and graphics. \
    R provides a wide variety of statistical (linear and nonlinear modeling, \
    classical statistical tests, time-series analysis, classification, \
    clustering, ...) and graphical techniques, and is highly extensible.

homepage         http://www.r-project.org/

master_sites     http://cran.us.r-project.org/src/base/R-2/ \
                 http://cran.ms.unimelb.edu.au/src/base/R-2/ \
                 http://cran.at.r-project.org/src/base/R-2/ \
                 http://cran.hu.r-project.org/src/base/R-2/ \
                 http://cran.r-project.org/src/base/R-2/

checksums        rmd160  eef2b98441469b21aff9b013986c89c3c57908b0 \
                 sha256  6f26304773e92d8b61f150e1ccd21c153a32b89e8032b1ece45e5f4c34d9e397

depends_build    port:pkgconfig

depends_lib      port:gettext \
                 port:icu \
                 path:lib/pkgconfig/glib-2.0.pc:glib2 \
                 port:jpeg \
                 port:libiconv \
                 path:lib/pkgconfig/cairo.pc:cairo \
                 path:lib/pkgconfig/pango.pc:pango \
                 port:readline \
                 port:tcl \
                 port:tk \
                 port:tiff \
                 port:xorg-libXmu \
                 port:xorg-libXScrnSaver

universal_variant no

variant recommended description {Install recommended R packages} {
    configure.args-delete --without-recommended-packages
    configure.args-append --with-recommended-packages
}

variant gcc42 conflicts gcc43 gcc44 gcc45 gcc46 description {Build with gcc42} {
    configure.compiler macports-gcc-4.2
    depends_lib-append port:gcc42
}

variant gcc43 conflicts gcc42 gcc44 gcc45 gcc46 description {Build with gcc43} {
    configure.compiler macports-gcc-4.3
    depends_lib-append port:gcc43
}

variant gcc44 conflicts gcc42 gcc43 gcc45 gcc46 description {Build with gcc44} {
    configure.compiler macports-gcc-4.4
    depends_lib-append port:gcc44
}

variant gcc45 conflicts gcc42 gcc43 gcc44 gcc46 description {Build with gcc45} {
    configure.compiler macports-gcc-4.5
    depends_lib-append port:gcc45
}

variant gcc46 conflicts gcc42 gcc43 gcc44 gcc45 description {Build with gcc46} {
    configure.compiler macports-gcc-4.6
    depends_lib-append port:gcc46
}

variant debug description {Build with debug symbols} {
    configure.optflags-append -g
}

variant aqua description {Enable quartz graphics device} {
    if {${os.platform} == "darwin" && ${os.major} == 10} {
        # This variant uses a hack in "CFString.h" that does not define the problematic CF_FORMAT_FUNCTION
        # and CF_FORMAT_ARGUMENT macros if __INTEL_COMPILER is defined. Currently, that is the ONLY place
        # in the 10.6 SDK where the __INTEL_COMPILER macro is checked, but that may change in the future.
        # We also need to use Apple's ObjC compiler for the single "qdCocoa.m" file.
        configure.args-delete   --without-aqua
        configure.args-append   --with-aqua
        patchfiles-append       patch-QuartzDevice_h.diff
        configure.objc          /usr/bin/gcc-4.2
    } else {
        ui_error "The +aqua variant is for Snow Leopard (Mac OS X 10.6) only - for Aqua support on Lion (Mac OS X 10.7) use the R-framework port"
        return -code error "incompatible variant"
    }
}

default_variants +recommended

if {![variant_isset gcc42] && ![variant_isset gcc43] && ![variant_isset gcc44] &&
    ![variant_isset gcc45] && ![variant_isset gcc46]} {
    default_variants-append +gcc45
}

post-patch {
  reinplace "s|R_HOME|\"${prefix}/lib/R\"|" "${worksrcpath}/src/unix/Rscript.c"
}

configure.env-append r_arch=${build_arch}

configure.args   --mandir=\\\${prefix}/share/man \
                 --enable-BLAS-shlib \
                 --enable-R-shlib \
                 --disable-nls \
                 --disable-R-framework \
                 --without-aqua \
                 --with-tcl-config=${prefix}/lib/tclConfig.sh \
                 --with-tk-config=${prefix}/lib/tkConfig.sh \
                 --x-include=${prefix}/include \
                 --x-lib=${prefix}/lib \
                 --without-recommended-packages

test.run         yes
test.target      check
destroot.destdir prefix=${destroot}${prefix}

post-destroot {
  foreach r { "/bin/R" "/lib/R/bin/R" } {
    foreach dir { "R_HOME_DIR" "R_SHARE_DIR" "R_INCLUDE_DIR" "R_DOC_DIR" } {
      reinplace "s|${dir}=${destroot}|${dir}=|" "${destroot}${prefix}${r}"
    }
  }

  foreach v { "rhome" "rincludedir" } {
    reinplace "s|${v}=${destroot}|${v}=|" "${destroot}${prefix}/lib/pkgconfig/libR.pc"
  }

  foreach dylib [ exec find ${destroot}${prefix}/lib/R/lib/${build_arch} -name "\*.dylib" ] {
    regsub ":$" ${dylib} "" destroot_dylib_path
    regsub ${destroot} ${destroot_dylib_path} "" dylib_path
    system "install_name_tool -id ${dylib_path} ${destroot_dylib_path}"
    system "install_name_tool -change libR.dylib ${prefix}/lib/R/lib/${build_arch}/libR.dylib \
      ${destroot_dylib_path}"
    system "install_name_tool -change libRblas.dylib ${prefix}/lib/R/lib/${build_arch}/libRblas.dylib \
      ${destroot_dylib_path}"
    system "install_name_tool -change libRlapack.dylib ${prefix}/lib/R/lib/${build_arch}/libRlapack.dylib \
      ${destroot_dylib_path}"
  }

  foreach so [ exec find ${destroot}${prefix}/lib/R -name "\*.so" ] {
    regsub ":$" ${so} "" destroot_so_path
    regsub ${destroot} ${destroot_so_path} "" so_path
    system "install_name_tool -id ${so_path} ${destroot_so_path}"
    system "install_name_tool -change libR.dylib ${prefix}/lib/R/lib/${build_arch}/libR.dylib \
      ${destroot_so_path}"
    system "install_name_tool -change libRblas.dylib ${prefix}/lib/R/lib/${build_arch}/libRblas.dylib \
      ${destroot_so_path}"
    system "install_name_tool -change libRlapack.dylib ${prefix}/lib/R/lib/${build_arch}/libRlapack.dylib \
      ${destroot_so_path}"
  }

  system "install_name_tool -change libR.dylib ${prefix}/lib/R/lib/${build_arch}/libR.dylib \
    ${destroot}${prefix}/lib/R/bin/exec/${build_arch}/R"
  system "install_name_tool -change libRblas.dylib ${prefix}/lib/R/lib/${build_arch}/libRblas.dylib \
    ${destroot}${prefix}/lib/R/bin/exec/${build_arch}/R"

  reinplace "s|-dylib_file libRblas\.dylib:\$\(R_HOME\)/lib\$\(R_ARCH\)/libRblas\.dylib| |" \
            "${destroot}${prefix}/lib/R/etc/${build_arch}/Makeconf"
}

livecheck.type      regex
livecheck.url       [lindex ${master_sites} 0]
livecheck.regex     >${name}-(\[0-9.\]+)${extract.suffix}<
