# Description: R is a free software environment for statistical computing and graphics
# URL: https://www.r-project.org/
# Maintainer: Tim Biermann, tbier at posteo dot de
# Depends on: libpcre2 openblas zstd
# Optional: lapack openjdk8 texlive tk

name=r
version=4.3.1
release=1
source=(https://cran.r-project.org/src/base/R-${version::1}/R-$version.tar.gz
  r.desktop r.png R.conf
  R-3.4.1-parallel.patch
  R-3.6.2-no-LDFLAGS-in-libR-pkg-config.patch)

build() {
  cd R-$version

  # override the upstream opinion that curl 8 has an incompatible API
  curlTest=$(grep -n "LIBCURL_VERSION_MAJOR > 7" configure | cut -d: -f1)
  sed -i "$((curlTest + 1))s/exit.1./exit(0)/" configure

  # https://gitweb.gentoo.org/repo/gentoo.git/tree/dev-lang/R/files
  patch -Np1 -i $SRC/R-3.4.1-parallel.patch
  patch -Np1 -i $SRC/R-3.6.2-no-LDFLAGS-in-libR-pkg-config.patch

  # configuring R
  prt-get isinst tk && PKGMK_R+=" --with-x=yes" || PKGMK_R+=" --with-x=no"
  prt-get isinst lapack && PKGMK_R+=" --with-lapack LAPACK_LIBS=$(pkgconf --libs lapack)"
  # this needs a properly working java environment!
  [[ -e $(whereis javac) ]] && PKGMK_R+=" --enable-java" || PKGMK_R+=" --disable-java"

  # set the correct dir for texmf distribution
  sed -i 's|$(rsharedir)/texmf|${datarootdir}/texmf|' share/Makefile.in

  sed -i -e '/R_LIBS_SITE=/s/^/#/g' etc/Renviron.in
  echo 'R_LIBS_SITE=${R_LIBS_SITE-'"'/usr/lib/R/library:/usr/share/R/library'"'}' >> etc/Renviron.in

  export R_PDFVIEWER=/usr/bin/xdg-open
  export R_PRINTCMD=lpr
  export R_BROWSER=/usr/bin/xdg-open

  unset R_HOME
  export ldpath="/usr/lib/R/lib"

  # version 3.6.2 needs at least the addition to FCFLAGS
  export CFLAGS+=' -fPIC'
  export CPPFLAGS+=' -fPIC'
  export FCFLAGS+=' -fPIC'

  CFLAGS+=" -ffat-lto-objects" \
  CXXFLAGS+=" -ffat-lto-objects" \
  ./configure $PKGMK_R \
    --prefix=/usr \
    --libdir=/usr/lib \
    --sysconfdir=/etc/R \
    --datarootdir=/usr/share \
    rsharedir=/usr/share/R/ \
    rincludedir=/usr/include/R/ \
    F77=gfortran \
    LIBnn=lib \
    BLAS_LIBS=$(pkgconf --libs blas) \
    PKG_CONFIG=/usr/bin/pkgconf \
    --with-blas=openblas \
    --enable-BLAS-shlib \
    --enable-R-shlib \
    --enable-byte-compiled-packages \
    --enable-lto \
    --enable-R-profiling \
    --enable-memory-profiling \
    --with-cairo \
    --with-ICU \
    --with-jpeglib \
    --with-lapack \
    --with-libpng \
    --with-libtiff \
    --with-system-tre \
    --without-recommended-packages \
    --disable-nls | tee CONFIGURE.log
cat CONFIGURE.log | grep -A30 'R is now' - > CAPABILITIES
  make

  make -C src/nmath/standalone shared

  make DESTDIR=$PKG install
  make -C src/nmath/standalone DESTDIR=$PKG install

  chmod +x $PKG/usr/lib/R/bin/*
  chmod +x $PKG/usr/share/R/sh/echo.sh
  #chmod -x $PKG/usr/lib/R/library/mgcv/CITATION

  sed -i "s|$PKG ||" $PKG/usr/bin/R
  rm $PKG/usr/lib/R/bin/R
  cd $PKG/usr/lib/R/bin
  ln -s ../../../bin/R

  cd $PKG/usr/lib/R
  ln -s ../../include/R include

  install -Dm644 $SRC/r.desktop \
    $PKG/usr/share/applications/r.desktop
  install -Dm644 $SRC/r.png \
    $PKG/usr/share/pixmaps/r.png
  install -Dm644 $SRC/R.conf \
    $PKG/etc/ld.so.conf.d/R.conf

  install -d $PKG/etc/R
  cd $PKG/usr/lib/R/etc
  for i in *; do
    mv -f $i $PKG/etc/R
    ln -s /etc/R/$i $i
  done

  #clean up
  find $PKG \(\
    -iname '*README*' -o \
    -iname '*COPYING*' -o \
    -iname '*AUTHORS*' -o \
    -iname '*THANKS*' -o \
    -iname '*NEWS*' -o \
    -iname '*LICENSE*' -o \
    -iname '*CHANGELOG*' -o \
    -iname '*LC_MESSAGES*' \
    \) -exec rm -r '{}' \+ || true
    ## doh! don't remove INSTALL or you won't be able to install R packages
    #-iname '*INSTALL*' -o \
    ## rstudio now depends on finding docs that R provides..
    #-iname 'doc' -o \
}
