# -*- 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 119471 2014-04-26 19:38:42Z mf2k@macports.org $

PortSystem 1.0
PortGroup active_variants 1.1

name                        R

set major 3
set minor 1
set point 0

#Remember to remove revision line when bumping version
version                     ${major}.${minor}.${point}


categories                  math science
maintainers                 me.com:kjell.konis
license                     {GPL-2 GPL-3}
platforms                   macosx

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.rstudio.com/src/base/R-3/ \
                            http://cran.r-project.org/src/base/R-3/

checksums                   rmd160  15d6c762baf36d4e2680599b5e976d0c068b7dbd \
                            sha256  8a680390f84c58c01dcdefd682eaa0e90389f09e6d2f2e090c71af40065f5fe2

depends_build               port:pkgconfig

depends_lib                 port:readline \
                            port:icu \
                            port:libiconv

universal_variant           no

set resources ${frameworks_dir}/R.framework/Versions/${major}.${minor}/Resources

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

configure.pre_args          --prefix=${frameworks_dir}

configure.args              --enable-R-framework \
                            --enable-R-shlib \
                            --enable-BLAS-shlib \
                            --disable-openmp \
                            --with-readline \
                            --with-aqua \
                            --without-tcltk \
                            --without-cairo \
                            --with-libpng \
                            --without-jpeglib \
                            --without-recommended-packages \
                            --with-ICU \
                            --without-x \
                            --with-included-gettext \
                            --without-internal-tzcode

platform darwin 13 {
    configure.cflags-append -flax-vector-conversions
}

variant accelerate conflicts builtin_lapack description {build using the BLAS and Lapack in Apple's Accelerate framework} {
    configure.args-append   --with-blas="-framework vecLib" \
                            --with-lapack="-framework vecLib"
}

variant atlas conflicts accelerate builtin_lapack description {build using the BLAS in the atlas port} {
    depends_lib-append      port:atlas
    configure.args-append   --with-blas="-L${prefix}/lib -lptf77blas -latlas"
    #See A.3.2 in R Installation and Administration for why atlas LAPACK not used
}

variant builtin_lapack conflicts accelerate description {build using reference BLAS and Lapack} {
    configure.args-append   --without-blas --without-lapack
}

variant cairo requires x11 description {include cairo support in R's x11 graphics device} {
    depends_lib-append      path:lib/pkgconfig/cairo.pc:cairo \
                            path:lib/pkgconfig/pango.pc:pango \
                            path:lib/pkgconfig/glib-2.0.pc:glib2 \
                            port:freetype \
                            port:fontconfig \
                            port:gettext 
    require_active_variants path:lib/pkgconfig/cairo.pc:cairo x11
    require_active_variants path:lib/pkgconfig/pango.pc:pango x11
    configure.args-delete   --without-cairo
    configure.args-append   --with-cairo
}

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

set gcc_versions {4.5 4.6 4.7 4.8 4.9}
set default_fortran_variant +gfortran48

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    set variant_line {variant gfortran${ver_no_dot} description "build with gfortran from gcc${ver_no_dot}"}

    foreach over ${gcc_versions} {
        if {${ver} == ${over}} {
            continue
        }

        set over_no_dot [join [split ${over} "."] ""]
        append variant_line " conflicts gfortran${over_no_dot}"
    }
    append variant_line { {}}

    eval $variant_line

    if {[variant_isset gfortran${ver_no_dot}]} {
        if {${default_fortran_variant} != "+gfortran${ver_no_dot}"} {
            set default_fortran_variant ""
        }
    }
}

if {${default_fortran_variant} != ""} {
    default_variants-append "${default_fortran_variant}"
}

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    if {[variant_isset gfortran${ver_no_dot}]} {
        depends_lib-append path:lib/libgcc/libgcc_s.1.dylib:libgcc
        depends_build-append port:gcc${ver_no_dot}

        configure.fc  ${prefix}/bin/gfortran-mp-${ver}
        configure.f77 ${prefix}/bin/gfortran-mp-${ver}
        configure.f90 ${prefix}/bin/gfortran-mp-${ver}
    }
}

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

variant tcltk requires x11 description {enable use of tcltk} {
    depends_lib-append      port:tcl \
                            port:tk \
                            port:xorg-libXScrnSaver \
                            port:xorg-libXext
    require_active_variants path:lib/pkgconfig/tk.pc:tk x11
    configure.args-delete   --without-tcltk
    configure.args-append   --with-tcltk \
                            --with-tcl-config=${prefix}/lib/tclConfig.sh \
                            --with-tk-config=${prefix}/lib/tkConfig.sh
}

variant tests description {include tests of R installation} {
    destroot.target-append install-tests
}

variant x11 description {enable use of x11} {
    depends_lib-append      port:xorg-libsm \
                            port:xorg-libice \
                            port:xorg-libX11 \
                            port:xorg-libXt \
                            port:tiff \
                            port:jpeg \
                            port:libpng \
                            port:zlib
    configure.args-delete   --without-x \
                            --without-jpeglib
    configure.args-append   --with-x \
                            --with-jpeglib \
                            --x-include=${prefix}/include/X11 \
                            --x-lib=${prefix}/lib
}

default_variants +recommended +x11

if {[variant_isset x11]} {
    default_variants-append +cairo
}

if {![variant_isset accelerate] && ![variant_isset atlas] && ![variant_isset builtin_lapack]} {
    default_variants-append +accelerate
}

test.run                    yes
test.target                 check

destroot.destdir prefix=${destroot}${frameworks_dir}

post-destroot {
    move ${destroot}${frameworks_dir}/lib/pkgconfig/libR.pc ${destroot}${prefix}/lib/pkgconfig/libR.pc

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

    foreach dir { "R_HOME_DIR" "R_SHARE_DIR" "R_INCLUDE_DIR" "R_DOC_DIR" } {
        reinplace "s|${dir}=${destroot}|${dir}=|" "${destroot}${resources}/bin/R"
    }

    reinplace "s|-F${destroot}|-F|" "${destroot}${resources}/etc/Makeconf"

    foreach dylib [ exec find ${destroot}${frameworks_dir}/R.framework -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 ${destroot}${resources}/lib/libR.dylib ${resources}/lib/libR.dylib \
            ${destroot_dylib_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRblas.dylib ${resources}/lib/libRblas.dylib \
            ${destroot_dylib_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRlapack.dylib ${resources}/lib/libRlapack.dylib \
            ${destroot_dylib_path}"
    }

    foreach so [ exec find ${destroot}${frameworks_dir}/R.framework -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 ${destroot}${resources}/lib/libR.dylib ${resources}/lib/libR.dylib \
            ${destroot_so_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRblas.dylib ${resources}/lib/libRblas.dylib \
            ${destroot_so_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRlapack.dylib ${resources}/lib/libRlapack.dylib \
            ${destroot_so_path}"
    }

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

    ln -s ${resources}/bin/R ${destroot}${prefix}/bin/R
    ln -s ${resources}/bin/Rscript ${destroot}${prefix}/bin/Rscript
}

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