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

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

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

CC_VER=$(shell $(CC) --version | ${SED} -n 1p)
FC_VER=$(shell $(FC) --version | ${SED} -n 1p)

## keep these in step with ./Makefile.in
SRC_HEADERS = R.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

## This omits GetX11Image.h QuartzDevice.h eventloop.h
R_EXT_HEADERS = \
  Altrep.h Applic.h Arith.h BLAS.h Boolean.h Callbacks.h Complex.h \
  Connections.h Constants.h Error.h \
  GraphicsDevice.h GraphicsEngine.h Itermacros.h \
  Lapack.h Linpack.h MathThreads.h Memory.h \
  Parse.h Print.h RS.h Rallocators.h Random.h \
  Rdynload.h Riconv.h RStartup.h Utils.h libextern.h \
  stats_package.h stats_stubs.h Visibility.h ObjectTable.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
	@(cd R_ext; cp -p $(R_EXT_HEADERS) ../../../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

config.h: ../gnuwin32/fixed/h/config.h ../../VERSION
	@$(SED) -e 's/@VERSION@/$(VER)/' \
                -e 's/@CC_VER@/$(CC_VER)/' -e 's/@FC_VER@/$(FC_VER)/' $< > $@

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_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
