#-*- Makefile -*-
RLIB=../../../library
DPKG=$(RLIB)/$(PKG)
SPKG=.
SAVE=CHECK
LAZY=false
LAZYDATA=false

ifneq ($(strip $(BUILD)),CROSS)
SPKG0 = $(shell $(RHOME)/src/gnuwin32/Rpwd.exe $(SPKG))
DPKG0 = $(shell $(RHOME)/src/gnuwin32/Rpwd.exe $(DPKG))
R_EXE = $(RHOME)/bin/rterm.exe
else
SPKG0=$(SPKG)
DPKG0=$(DPKG)
endif

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

ifeq ($(strip $(DLLNM)),)
  DLLNAME=$(PKG)
else
  DLLNAME=$(DLLNM)
endif
#this can be overwriten by the caller
SRC = src

#source directory
SSPKG=$(SPKG)/$(SRC)

# src Makefile?
SRCMAKE=$(wildcard $(SSPKG)/Makefile.win)
SDYNLIB=$(SSPKG)/$(DLLNAME).dll

LSOURCES=$(wildcard $(SSPKG)/*.c $(SSPKG)/*.f $(SSPKG)/*.cc \
	$(SSPKG)/*.cpp $(SSPKG)/*.C)
ifeq ($(strip $(LSOURCES)),)
  LOBJS=
else
  LOBJS=$(foreach i, $(LSOURCES), $(basename $i).o)
endif

# data
DSOURCES=$(filter-out $(SPKG)/data/CVS $(SPKG)/data/.svn $(SPKG)/data/datalist, $(wildcard $(SPKG)/data/*))
ifeq ($(strip $(DSOURCES)),)
  DATA= 
else
  DATA=$(DPKG)/data
endif

# R
PKGRFILES0=$(wildcard $(SPKG)/R/*.[RSqrs])
RWIN32=$(wildcard $(SPKG)/R/windows/*.[Rr]) 
ifeq ($(strip RWIN32),)
  PKGRFILES=$(PKGRFILES0)
else
  R1 = $(foreach i,$(RWIN32), $(addprefix $(SPKG)/R/, $(notdir $i)))
  PKGRFILES=$(filter-out $(R1), $(PKGRFILES0)) $(RWIN32)
endif

# demos
PKGDEMOS0=$(wildcard $(SPKG)/demo/*)
PKGDEMOS=$(filter-out $(SPKG)/demo/CVS $(SPKG)/demo/.svn $(SPKG)/demo/00Index, $(PKGDEMOS0))

# exec
EXECFILES=$(filter-out $(SPKG)/exec/CVS $(SPKG)/exec/.svn, $(wildcard $(SPKG)/exec/*))

# Others: we will remove CVS and subversion files later.
INSTFILES=$(wildcard $(SPKG)/inst/*)

all:  
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg $(DPKG)/zzzz 
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg -s frontmatter
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg -s nmspace
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg Dynlib 
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg -s R
ifeq ($(strip $(LAZY)),true)
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg -s lazyload
endif
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg -s $(DPKG)/demo $(DPKG)/exec $(DPKG)/inst $(DATA)
ifeq ($(strip $(LAZYDATA)),true)
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg -s lazydata
endif
	@$(MAKE) --no-print-directory -f $(RHOME)/src/gnuwin32/MakePkg -s mansrcs indices

fake: zzzfirst frontmatter nmspacefake R Rfake $(DPKG)/demo \
  $(DPKG)/exec $(DPKG)/inst $(DATA) mansrcs indices

include $(RHOME)/src/gnuwin32/MkRules

clean:
ifeq ($(strip $(SRCMAKE)),)
	$(RM) $(SDYNLIB) $(LOBJS) $(SSPKG)/$(DLLNAME).a \
	  $(SSPKG)/$(DLLNAME).def $(SSPKG)/$(DLLNAME)_res.* \
	  $(SSPKG)/*.d $(SSPKG)/Makedeps
else
	$(MAKE) -C $(SSPKG) RHOME=$(RHOME) -f Makefile.win clean
endif
	$(RM) -rf $(SPKG)/chm $(SPKG)/check

## used to build skeletal versions of packages, enough to run
## package tools to install them properly
Rcode: Rcode0 Dynlib

Rcode0:
	@$(MKDIR) -p $(DPKG) $(DPKG)/R
	@$(CAT) $(PKGRFILES) > $(DPKG)/R/$(PKG)
#	@cp -p $(DPKG)/R/$(PKG) $(DPKG)/R/all.R
	@rm -f $(DPKG)/R/$(PKG).rdb  $(DPKG)/R/$(PKG).rdx
	@if test -f ${SPKG}/NAMESPACE; then \
	  $(CP) ${SPKG}/NAMESPACE $(DPKG)/NAMESPACE; \
	fi
	@(cat DESCRIPTION; $(ECHO); $(ECHO) "Built: R" $(RVER)\;  \;  `date`) \
	  | $(SED) '/^\s*$$/d' > ${DPKG}/DESCRIPTION

$(DPKG)/R/$(PKG): $(PKGRFILES)
ifneq ($(strip $(PKG)),base)
ifneq ($(strip $(PKGRFILES)),)
	@$(ECHO) "  installing R files"
	@$(MKDIR) -p $(DPKG)/R
	@$(ECHO) "tools:::.install_package_code_files('"$(SPKG)"', '"$(DPKG)"')" \
	  | R_DEFAULT_PACKAGES=NULL R_OSTYPE=windows LC_COLLATE=C $(REXE) > /dev/null
## install halts here if collater failed
	@rm -f $(DPKG)/R/$(PKG).rdb $(DPKG)/R/$(PKG).rdx $(DPKG)/R/all.rda
	@sh $(RHOME)/src/gnuwin32/install-save.sh "$(SAVE)" $(RLIB) $(PKG) $(RHOME) $(BUILD) $(R_EXE)
else
	@$(ECHO) "  no R files in this package"
endif
else
## .install_package_code_files adds an unwanted .packageName
	@$(ECHO) "  installing R files"
	@$(MKDIR) -p $(DPKG)/R
	@$(CAT) $(PKGRFILES) > $(DPKG)/R/$(PKG)
	@rm -f $(DPKG)/R/$(PKG).rdb $(DPKG)/R/$(PKG).rdx
endif

$(DPKG)/demo: $(PKGDEMOS)
ifneq ($(strip $(PKGDEMOS)),)
	@$(ECHO) "  installing demos"
	@$(RM) -rf $@
	@$(MKDIR) -p $@
	@$(CP) $(PKGDEMOS)   $@
endif

$(DPKG)/exec: $(EXECFILES)
ifneq ($(strip $(EXECFILES)),)
	@$(ECHO) "  installing exec files"
	@$(RM) -rf $@
	@$(MKDIR) -p $@
	@$(CP) $(EXECFILES)   $@
endif

$(DPKG)/inst: $(INSTFILES)
ifneq ($(strip $(INSTFILES)),)
	@$(ECHO) "  installing inst files"
	-@$(CP) -r inst/* $(DPKG)
	@find $(DPKG) -name CVS -type d -prune -exec rm -rf {} \;
	@find $(DPKG) -name .svn -type d -prune -exec rm -rf {} \;
endif


$(DPKG)/zzzz: zzzfirst
	@if test -f ${SPKG}/configure.win; then \
	  sh ${SPKG}/configure.win; \
	else if test -f ${SPKG}/configure; then \
	    $(ECHO) ; \
	    $(ECHO) "   **********************************************" ; \
	    $(ECHO) "   WARNING: this package has a configure script" ; \
	    $(ECHO) "         It probably needs manual configuration" ; \
	    $(ECHO) "   **********************************************" ; \
	    $(ECHO) ; \
	  fi; \
	fi

zzzfirst:
ifeq ($(SPKG0),$(DPKG0))
	@$(ECHO)
	@$(ECHO) "  *** cannot install a package into its source directory ***"
	@$(ECHO)
	@exit 111
endif
	@$(ECHO)
	@$(ECHO) "---------- Making package $(PKG) ------------"
	@$(MKDIR) -p $(DPKG)


frontmatter:
	@$(MKDIR) -p $(DPKG)
	@if test -f ${SPKG}/DESCRIPTION; then \
	  $(ECHO) "  adding build stamp to DESCRIPTION"; \
	else \
	  $(ECHO) "A package must contain a DESCRIPTION file"; \
	  exit 27; \
	fi
	@$(ECHO) "tools:::.install_package_description('.', '"$(DPKG)"')" | \
	  R_DEFAULT_PACKAGES=NULL R_OSTYPE=windows $(REXE) > /dev/null
	@if test -f ${SPKG}/COPYING; then \
	  $(CP) ${SPKG}/COPYING $(DPKG)/COPYING; \
	fi

nmspace:
	@if test -f ${SPKG}/NAMESPACE; then \
	  $(ECHO) -n "  installing NAMESPACE file"; \
	  $(CP) ${SPKG}/NAMESPACE $(DPKG)/NAMESPACE; \
	  $(ECHO) " and metadata"; \
	  $(ECHO) "tools:::.install_package_namespace_info('$(DPKG)', '$(DPKG)')" | \
	  R_DEFAULT_PACKAGES=NULL R_OSTYPE=windows $(REXE) > /dev/null; \
	fi

nmspacefake:
	@if test -f ${SPKG}/NAMESPACE; then \
	  $(ECHO) "  installing NAMESPACE file"; \
	  $(SED) -e '/useDynLib/d' ${SPKG}/NAMESPACE > $(DPKG)/NAMESPACE; \
	fi


$(DATA): $(DSOURCES)
	@$(ECHO) "  installing data files"
	@$(RM) -rf $(DATA)
	@$(MKDIR) -p $(DATA)
	@$(CP) $^  $@


Dynlib: srcDynlib
	@if test -f $(SDYNLIB) ; then \
	  $(ECHO) "  installing DLL" ; \
	  $(MKDIR) -p $(DPKG)/libs ; \
	  $(CP) $(SDYNLIB) $(DPKG)/libs ; \
	fi

srcDynlib:
ifeq ($(strip $(SRCMAKE)),)
ifneq ($(strip $(LOBJS)),)
	@$(ECHO) "  making DLL ..."
	@$(MAKE) --no-print-directory -C $(SSPKG) RHOME=$(RHOME) \
	  -f $(RHOME)/src/gnuwin32/MakeDll DLLNAME=$(DLLNAME)
	@$(ECHO) "  ... DLL made"
endif
else
	@$(ECHO) "  running src/Makefile.win ..."
	@$(MAKE) --no-print-directory -C $(SSPKG) RHOME=$(RHOME) \
	  -f Makefile.win
	@$(ECHO) "  ... done"
endif

# R
AA=$(wildcard R/Makefile.win)
ifeq ($(AA),)
 R: $(DPKG)/R/$(PKG) sysdata
else
 include R/Makefile.win
endif

Rfake:
	@if test -f $(SPKG)/NAMESPACE; then \
	  echo ".onLoad <- function(list, pkg) NULL" >> $(DPKG)/R/$(PKG) ; \
	else \
	  echo ".First.lib <- function(lib, pkg) NULL" >> $(DPKG)/R/$(PKG) ;\
	fi


MANMAIN=$(wildcard $(SPKG)/man/*.[Rr]d)
MANWIN32=$(wildcard $(SPKG)/man/windows/*.[Rr]d) 

mansrcs: $(DPKG)/man/$(PKG).Rd.gz

PKGMANFILES=$(MANMAIN) $(MANWIN32)
$(DPKG)/man/$(PKG).Rd.gz: $(MANMAIN) $(MANWIN32)
ifneq ($(strip $(PKGMANFILES)),)
	@$(ECHO) "  installing man source files"
	@$(RM) -rf $(DPKG)/man
	@$(MKDIR) -p $(DPKG)/man
	sh $(RHOME)/src/gnuwin32/man-install.sh $(DPKG)/man/$(PKG).Rd '$^'
	@gzip -9 $(DPKG)/man/$(PKG).Rd
else
	@$(ECHO) "  no man files in this package"
endif

indices:
	@$(ECHO) "  installing indices"
	@$(ECHO) "tools:::.install_package_indices('.', '"$(DPKG)"')" | \
	  R_DEFAULT_PACKAGES=NULL LC_COLLATE=C R_OSTYPE=windows $(REXE) > /dev/null
# need not have HTML installed when building packages
	@$(MKDIR) -p $(RHOME)/doc/html/search
	@$(CAT) $(RHOME)/library/*/CONTENTS > $(RHOME)/doc/html/search/index.txt	
	@PERL5LIB=$(RHOME)/share/perl $(PERL) \
	  $(RHOME)/share/perl/build-help-windows.pl -htmllists

md5sums:
	@$(ECHO) "  adding MD5 sums"
	@$(ECHO) "tools:::.installMD5sums('"$(DPKG)"')" | \
	  R_DEFAULT_PACKAGES=NULL LC_COLLATE=C R_OSTYPE=windows $(REXE) > /dev/null

ifeq ($(strip $(BUILD)),CROSS)
R_X_BUILD=yes
endif

lazyload:
	@if test -d $(DPKG)/R ; then \
	  if ! test -f $(DPKG)/R/$(PKG).rdb ; then \
	    $(ECHO)  "  preparing package $(PKG) for lazy loading" ; \
            (if test -s $(SPKG0)/R_PROFILE.R; then cat $(SPKG0)/R_PROFILE.R; fi; \
	      $(ECHO) "invisible(.libPaths(c(.Library,\"$(RLIB)\", .libPaths()))); .getRequiredPackages(\"$(SPKG0)/DESCRIPTION\",useImports=TRUE); Sys.putenv('R_CROSS_BUILD'='$(R_X_BUILD)'); tools:::makeLazyLoading(\"$(PKG)\", lib.loc=\"$(RLIB)\")") | \
	      R_DEFAULT_PACKAGES=$(R_START_PACKAGES) \
	      LC_COLLATE=C R_OSTYPE=windows $(REXE) > /dev/null || exit 1; \
	    rm -f $(DPKG)/R/all.rda ; \
	    if test -r install.R ; then \
	      cat install.R >> $(DPKG)/R/$(PKG) ; \
	    fi ; \
	  fi ; \
	fi

lazydata:
	@if test -d $(DPKG)/data; then \
	  if ! test -f $(DPKG)/data/Rdata.rdb ; then \
	    $(ECHO)  "  preparing package $(PKG) for lazy data loading" ; \
	    $(ECHO) "tools:::data2LazyLoadDB(\"$(PKG)\", lib.loc=\"$(RLIB)\")" | \
	      R_CROSS_BUILD=$(R_X_BUILD) R_DEFAULT_PACKAGES=NULL \
	      LC_COLLATE=C R_OSTYPE=windows $(REXE) > /dev/null  || exit 1; \
	  fi ; \
	fi

SYSDATA=$(wildcard $(SPKG)/R/sysdata.rda)

sysdata:
ifneq ($(strip $(SYSDATA)),)
	$(ECHO)  "    preparing to lazyload sysdata"
	$(ECHO) "tools:::sysdata2LazyLoadDB(\"$(SYSDATA)\", \"$(DPKG)/R\")" | \
	      R_DEFAULT_PACKAGES=NULL $(REXE) > /dev/null || exit 1
endif
