#-*- Makefile -*-
include ../gnuwin32/MkRules
R_HOME = ../..

GIT = `if [ -d "$(top_builddir)/.git" ]; then echo "git"; fi`

VER = $(shell sed -e 's/\([^ ]*\).*/\1/' ../../VERSION)

## keep these in step with ./Makefile.in
SRC_HEADERS = R.h S.h Rdefines.h Rembedded.h Rinternals.h
## this deliberately does not include Rinterfaces.h, which is Unix-only
OBJ_HEADERS = Rconfig.h Rmath.h Rversion.h
GW32_HEADERS = iconv.h psignal.h

all: fixh config.h trioremap.h
	@echo 'installing C headers'
	@mkdir -p $(R_HOME)/include/R_ext
	@cp -p $(SRC_HEADERS) $(OBJ_HEADERS) iconv.h $(R_HOME)/include
	@cp -p R_ext/*.h $(R_HOME)/include/R_ext
	@cp -p ../extra/graphapp/graphapp.h ../extra/graphapp/ga.h \
	  $(R_HOME)/include

version: Rversion.h

fixh: $(GW32_HEADERS) $(OBJ_HEADERS)
	@$(ECHO) done > fixh

ifeq "$(WIN)" "64"
config.h: ../gnuwin32/fixed/h/config.h ../../VERSION
	@$(SED) -e 's/@VERSION@/$(VER)/' -e 's/@ST@/8/' $< > $@
else
config.h: ../gnuwin32/fixed/h/config.h ../../VERSION
	@$(SED) -e 's/@VERSION@/$(VER)/' -e 's/@ST@/4/' $< > $@
endif

Rconfig.h: ../gnuwin32/fixed/h/Rconfig.h
	@cp $< $@

iconv.h: ../gnuwin32/fixed/h/iconv.h
	@cp $< $@

psignal.h: ../gnuwin32/fixed/h/psignal.h
	@cp $< $@

Rversion.h: $(R_HOME)/VERSION $(R_HOME)/SVN-REVISION $(R_HOME)/tools/GETVERSION
	@sh $(R_HOME)/tools/GETVERSION > $@

Rmath.h0: Rmath.h0.in $(R_HOME)/VERSION Makefile.win
	@$(SED) -e 's/@RMATH_HAVE_LOG1P@/# define HAVE_LOG1P 1/'  \
	  -e 's/@RMATH_HAVE_EXPM1@/# define HAVE_EXPM1 1/'  \
	  -e 's/@RMATH_HAVE_HYPOT@/# define HAVE_HYPOT 1/'  \
	  -e  's/@RMATH_HAVE_WORKING_LOG1P@/# define HAVE_WORKING_LOG1P 1/' \
	  -e "s/@PACKAGE_VERSION@/`sed 's/\([^ ]*\).*/\1/' < $(R_HOME)/VERSION`/" $< > Rmath.h0

Rmath.h: Rmath.h0
	@sh $(R_HOME)/tools/copy-if-change $< $@
	
trioremap.h: ../gnuwin32/fixed/h/trioremap.h
	@cp $< $@

## If we do not have svn, get the old file from SVN-REVISION.bak (if poss).
## This needs to be copy-on-change.
## The date from svn info is not in GMT, but we have decided to live
## with that as they changed the format in svn 1.4.x
## <FIXME> USE_SVNVERSION does not work if there is no 'svnversion'
FORCE:
$(R_HOME)/SVN-REVISION: FORCE
	@if test -f  $(R_HOME)/SVN-REVISION ; then \
	  cp -p $(R_HOME)/SVN-REVISION $(R_HOME)/SVN-REVISION.bak ; \
	fi
ifdef USE_SVNVERSION
	@LC_ALL=C svnversion ../.. | sed -n 's/^/Revision: /p' > svn-tmp || rm -f svn-tmp
	@grep -v exported svn-tmp > /dev/null || rm -f svn-tmp
else
	@(cd ../..; LC_ALL=C $(GIT) svn info || echo "Revision: unknown") 2> /dev/null \
	  | sed -n '/^Revision/p' > svn-tmp
	@if grep unknown svn-tmp > /dev/null ; then \
	  rm svn-tmp; \
	fi
endif
	@if test -f svn-tmp ; then \
	  (cd ../..; LC_ALL=C TZ=GMT $(GIT) svn info || echo "Last Changed Date: unknown") 2> /dev/null \
	    | sed -n '/^Last Changed Date:/p' | sed 's/[0-9][0-9]:.*//' \
	    >> svn-tmp ; \
	else \
	  rm -f svn-tmp ; \
	fi
	@if test -f svn-tmp; then \
	  if test ! -f $@ || ! cmp svn-tmp $@ > /dev/null ; then\
	    cp svn-tmp $@; \
	  fi ; \
	else \
	  cp -p $(R_HOME)/SVN-REVISION.bak $@ 2> /dev/null || \
	  (echo "Revision: 00000" > $@; \
	   echo "Last Changed Date: 2006-00-00" >> $@) ; \
	fi 
	@rm -f svn-tmp $(R_HOME)/SVN-REVISION.bak

distclean:
	$(RM) -f Rmath.h0 fixh
