###-*- Makefile -*- for 'test-Examples', also see ${RHOME}/etc/Makefile

CleanMe = Makefile
## The following must be an ABSOLUTE path
## or RELATIVE to 'Ex_Tests' (RHOME/test/Examples/ :
rRHOME= ../..
#
RETC	= $(rRHOME)/etc
R	= $(rRHOME)/bin/R
Rbinary = $(rRHOME)/bin/R.binary
Rbase	= $(rRHOME)/library/base

MANSRC = $(rRHOME)/src/library/*/man/*.Rd
MANbaseDIR = $(rRHOME)/src/library/base/man

##- Uncomment one of the 2 'Rd_files' defs -- depending on your 'make' version
##--- <<<<<< CONFIGURE should do this >>>>>>
##-- Sun make
#> Rd_files:sh = ./Rd.files
##-- GNU make
Rd_files := $(shell $(rRHOME)/etc/Rd.files)
#Rd_files := $(wildcard $(MANbaseDIR)/*.Rd)
#________in future_______  (for ALL 'make' versions)
# FAILS: has directory/. _AND_ is not expanded
#Rd_files = $(MANbaseDIR)/*.Rd
#
R_src = $(Rd_files:%.Rd=%.R)
#________in future_______: (FAILS!)
#R_src = $(Rd_files:$(MANbaseDIR)/%.Rd=%.R)


##--------------------------------- TARGETS ------------------------------
test-Examples: All-Ex.Rout

##-- The 'show-..' are just for testing purposes:
show-R: ; @-(cd ../../include; echo "R = '$(R)'"
show-RHOME: ; @-(RHOME=`cd ${rRHOME}; pwd`;export RHOME; printenv RHOME)
show-Rd:
	@-echo 'R help() files = "Rd files" :'
	@-echo $(Rd_files)
show-Ex:
	@-echo 'R_src [targets of  help() files] :'
	@-echo $(R_src)


%.R: $(MANbaseDIR)/%.Rd
	@$(RETC)/Rdconv -t example $< > $@
	@-echo $@

All-Ex.R: $(R_src)
	@if [ -f $@.bak ]; then mv $@.bak $@.bakk ; fi
	@if [ -f $@ ]; then mv $@ $@.bak ; fi
	@echo "massaging single Examples into $@ ..."
	@$(RETC)/massage-Examples $(R_src) > $@

All-Ex.Rout: All-Ex.R $(Rbinary) $(Rbase)
	@if [ -f $@.bak ]; then mv $@.bak $@.bakk ; fi
	@if [ -f $@ ]; then mv $@ $@.bak ; fi
	@echo 'Running all help() EXAMPLES ..'
	$(R) -v 4 -nosave -norestore < All-Ex.R > $@

clean:
	rm -f *.R
	rm -f *.Rd* *.ps *.tex *.dat* data foo*

realclean: clean
	@rm -fr $(CleanMe)