################################################################################
#
# R.cygclass - functions for building CRAN R packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/R.cygclass
#  SYNOPSIS
#  inherit R
#  DESCRIPTION
#  R is a GNU implementation of S, a language and environment for statistical
#  computing and graphics.
#
#  The primary purpose of R.cygclass is for creating Cygwin packages from
#  |html <a href="http://cran.r-project.org/web/packages/">CRAN packages</a>,
#  the R equivalent of Perl's CPAN.  CRAN package builds are driven by R itself.
#
#  Most CRAN packages build OOTB, but creating a Cygwin package may be helpful
#  if a patch is required or if the build requirements are substantial (e.g.
#  RGtk2, the GTK+ bindings which are used in several GUIs).  When using R.cygclass
#  to build a CRAN package, NAME must be "R-" followed by the CRAN package
#  name.  In most such cases, the R.cygclass src_compile and src_install should
#  suffice.
#
#  Alternatively, the definitions in this cygclass can be used to help build
#  an R-dependent package which does not use CRAN, e.g.
#  |html <a href="http://sourceforge.net/p/cygwin-ports/rkward/">rkward</a>.
#  EXAMPLE
#  inherit R
#  
#  NAME="R-RODBC"
#  VERSION=1.3-7
#  RELEASE=1
#  CATEGORY="Math"
#  SUMMARY="R ODBC interface"
#  DESCRIPTION="ODBC database interface for the R statistic programming language"
#  
#  R_CONFIGURE_ARGS="--with-odbc-manager=iodbc"
#
#  REQUIRES
#  pkg-config, R
#  SEE ALSO
#  mirror_cran
#****

# cross-compiling is not (yet?) supported
__cross_compiling_error

check_prog_req R
check_prog_req pkg-config

#****d* R.cygclass/R
#  DESCRIPTION
#  Absolute path to the R interpreter.
#****
R=/usr/bin/R

#****d* R.cygclass/R_CFLAGS
#  DESCRIPTION
#  Compile flags for the libR C library.
#****
R_CFLAGS="$(${R} CMD config CFLAGS) $(pkg-config --cflags libR)"

#****d* R.cygclass/R_LIBS
#  DESCRIPTION
#  Link flags for the libR C library.
#****
R_LIBS="$(pkg-config --libs libR)"

#****d* R.cygclass/R_HOME
#  DESCRIPTION
#  Absolute path to the R main directory.
#****
R_HOME=$(${R} RHOME)

#****d* R.cygclass/R_INCLUDEDIR
#  DESCRIPTION
#  Absolute path to the directory containing libR C library headers.
#****
R_INCLUDEDIR=${R_HOME}/include

#****d* R.cygclass/R_SITELIB
#  DESCRIPTION
#  Installation path for third-party R modules.
#****
R_SITELIB=${R_HOME}/site-library

case ${PN} in
R-*)
ORIG_PN=${ORIG_PN:-${PN#R-}}
#****o* R.cygclass/CATEGORY (R)
#  DEFINITION
CATEGORY="Math Science"
#****
#****o* R.cygclass/SUMMARY (R)
#  DEFINITION
SUMMARY="R ${ORIG_PN} package"
#****
#****o* R.cygclass/HOMEPAGE (R)
#  DESCRIPTION
#  Web address for the R module on CRAN.
#****
HOMEPAGE="http://cran.r-project.org/web/packages/${ORIG_PN}/"

#****o* R.cygclass/SRC_URI (R)
#  DESCRIPTION
#  Download location for the R module on CRAN.
#****
SRC_URI="mirror://cran/src/contrib/${ORIG_PN}_${PV/_/-}.tar.gz"

SRC_DIR="${ORIG_PN}"
;;
esac

# kept for API compatibility; now in check_funcs under different name
check_R_package() { check_R_module ${*} ; }

#****v* R.cygclass/R_CONFIGURE_ARGS
#  SYNOPSIS
#  R_CONFIGURE_ARGS="arg1 [arg2 ...]"
#  DESCRIPTION
#  A string containing flag(s) to be passed to the configure script used by
#  module packages containing native code.
#****

#****C* R.cygclass/R_compile
#  SYNOPSIS
#  cd $B
#  R_compile
#  DESCRIPTION
#  Configures and compiles an R module packages.  Due to the nature of
#  CRAN packages, the package is not compiled in B, but is compiled and
#  "installed" into a temporary directory.
#****
R_compile() {
	mkdir -p ${T}/inst
	${R} CMD INSTALL \
		--library=${T}/inst \
		--configure-args="${R_CONFIGURE_ARGS}" \
		${B}/${ORIG_PN}
}

#****I* R.cygclass/R_install
#  SYNOPSIS
#  R_install
#  DESCRIPTION
#  Installs the R module into $D.  Due to the nature of CRAN packages, all
#  this does is copy from the temporary directory used in R_compile.
#****
R_install() {
	[ -d ${T}/inst/${ORIG_PN} ] || error "cannot find built package"

	dodir ${R_SITELIB}
	cp -LR ${T}/inst/${ORIG_PN} ${D}${R_SITELIB}
}

#****o* R.cygclass/src_compile (R)
#  DEFINITION
src_compile() {
	mkdir -p ${B}/${ORIG_PN}
	lndirs ${S} ${B}/${ORIG_PN}
	R_compile
}
#****

#****o* R.cygclass/src_install (R)
#  DEFINITION
src_install() {
	R_install
}
#****

readonly -f R_compile R_install
