#-*- Makefile -*-
# Makefile for packages, only.

include MkRules

RVER = @RVER@

HELP = YES
WINHELP = NO # Use one of NO, CHM

## must be absolute and space-free paths
ifneq ($(strip $(BUILD)),CROSS)
RHOME = $(shell ./Rpwd.exe ../..)
PKGD = $(shell ./Rpwd.exe "$(PKGDIR)")
R_EXE = $(RHOME)/bin/rterm.exe
else
RHOME = $(shell perl pwd.pl ../..)
PKGD = $(shell perl pwd.pl $(PKGDIR))
endif

REXE = $(R_EXE) --vanilla --slave

RLIB = $(RHOME)/library
PKGDIR = $(RHOME)/src/library


all: libR.a libRblas.a

libR.a:  R.exp
	$(DLLTOOL) $(DLLTOOLFLAGS) $(R-DLLTOOLFLAGS) --dllname R.dll --def R.exp --output-lib libR.a

libRblas.a:  Rblas.def
	$(DLLTOOL) $(DLLTOOLFLAGS) $(R-DLLTOOLFLAGS) --dllname Rblas.dll \
	  --def Rblas.def --output-lib libRblas.a


#                          ===== Packages ======

MASS-HELP = ziponly
boot-HELP = ziponly
cluster-HELP = ziponly
lattice-HELP = ziponly
mgcv-HELP = ziponly
nlme-HELP = ziponly
rpart-HELP = ziponly
survival-HELP = ziponly

pkg-%:
	@$(MAKE) --no-print-directory \
	  SAVE=$($*-SAVE) LAZY=$($*-LAZY) LAZYDATA=$($*-LAZYDATA) \
          DLLNM=$($*-DLLNM) FLIBS="$(FLIBS)" BUILD=$(BUILD) \
	  PKG=$* RHOME=$(RHOME) RLIB=$(RLIB) RVER=$(RVER) \
	  -C $(PKGD)/$* -f $(RHOME)/src/gnuwin32/MakePkg
	@if test -d $(RLIB)/$*/data ; then \
	  $(MAKE) --no-print-directory $($*-ZIPDATA)data-$* ; \
	fi
ifeq ($(strip $(HELP)),YES)
	@$(ECHO) "  installing help"
  ifeq ($(strip $(DPKG)),)
    ifeq ($(strip $(WINHELP)),CHM)
	@$(MAKE) --no-print-directory -C ./help -s RHOME=$(RHOME) PKGDIR=$(PKGD) RLIB=$(RLIB) DPKG=$(RLIB)/$* CHM=-chm $($*-HELP)help-$* chm-$*
    else
	@$(MAKE) --no-print-directory -C ./help -s RHOME=$(RHOME) PKGDIR=$(PKGD) RLIB=$(RLIB) DPKG=$(RLIB)/$* $($*-HELP)help-$*
    endif
  else
    ifeq ($(strip $(WINHELP)),CHM)
	@$(MAKE) --no-print-directory -C ./help -s RHOME=$(RHOME) PKGDIR=$(PKGD) RLIB=$(RLIB) DPKG=$(DPKG) CHM=-chm $($*-HELP)help-$* chm-$*
    else
	@$(MAKE) --no-print-directory -C ./help -s RHOME=$(RHOME) PKGDIR=$(PKGD) RLIB=$(RLIB) DPKG=$(DPKG) $($*-HELP)help-$*
    endif
  endif
endif

pkgclean-%:
	$(MAKE) --no-print-directory DLLNM=$($*-DLLNM) RHOME=$(RHOME) BUILD=$(BUILD) \
	  -C $(PKGD)/$* PKG=$* -f $(RHOME)/src/gnuwin32/MakePkg clean

pkgcheck-%:
	@$(ECHO) -------- Testing package $* --------
	-@$(MKDIR) -p $(PKGD)/$*/check
	@$(MAKE) --no-print-directory -C $(PKGD)/$*/check PKG=$* RHOME=$(RHOME) RLIB=$(RLIB) \
	  -f $(RHOME)/src/gnuwin32/check/PkgCheck
	@if test -d $(PKGD)/$*/tests ; then \
	  $(MAKE) --no-print-directory -C $(PKGD)/$*/tests PKG=$* RHOME=$(RHOME) RLIB=$(RLIB) \
	    -f $(RHOME)/src/gnuwin32/check/PkgCheck tests;\
	fi

lazyload-%:
	@$(MAKE) --no-print-directory PKG=$* RHOME=$(RHOME) RLIB=$(RLIB) BUILD=$(BUILD) -C $(PKGD)/$* SPKG=$(PKGD)/$* -f $(RHOME)/src/gnuwin32/MakePkg lazyload

lazydata-%:
	@$(MAKE) --no-print-directory PKG=$* RHOME=$(RHOME) RLIB=$(RLIB) BUILD=$(BUILD) -f $(RHOME)/src/gnuwin32/MakePkg lazydata

ziphelp-%:
	$(MAKE) --no-print-directory -C ./help RHOME=$(RHOME) PKGDIR=$(PKGD) RLIB=$(RLIB) DPKG=$(DPKG) ziphelp-$*

ziponly-%:
	$(MAKE) --no-print-directory -C ./help RHOME=$(RHOME) PKGDIR=$(PKGD) RLIB=$(RLIB) DPKG=$(DPKG) ziponlyhelp-$*

## must be careful not to zip up a dir setup for lazy-data
zipdata-%:
	@if ! test -f ../../library/$*/data/Rdata.rdb ; then \
	  $(ECHO) "  zipping data" ; \
	  (cd $(RLIB)/$*/data; \
	    if test -f Rdata.zip ; then \
	      unzip -qou Rdata; \
	    fi; \
	    $(RM) Rdata.zip filelist ; \
	    ls -1 > filelist; \
	    zip -rmq9X Rdata * -x filelist 00Index) ;\
	fi

data-%:
	@if ! test -f ../../library/$*/data/Rdata.rdb ; then \
	  $(ECHO) "  not zipping data" ; \
	fi