SOURCES=	\
	A00_Introduction/A01_Introduction.Rnw 			\
		\
	A10_RBioconductor/A11_R.Rnw             		\
	A10_RBioconductor/A12_Bioconductor.Rnw   		\
	A10_RBioconductor/A13_LargeData.Rnw   			\
		\
	A20_DifferentialRepresentation/A21_StatisticalIssues.Rnw\
	A20_DifferentialRepresentation/A22_DESeqWorkflow.Rnw	\
	A20_DifferentialRepresentation/A23_AnnotationAndVisualization.Rnw

%.R: %.Rnw
	@echo "Stangle $<"
	@cd `dirname $<`; "$(R_HOME)/bin/R" CMD Stangle `basename $<`; cd ..

%.tex: %.Rnw
	@echo "Sweave $<"
	@cd `dirname $<`; "$(R_HOME)/bin/R" CMD Sweave `basename $<`; cd ..

all: usage StatisticalComputing.pdf
	@mkdir -p ../inst/doc
	@for f in $(SOURCES:%.Rnw=%.R); do \
		cp $$f ../inst/doc/`basename $$f` ; \
	done


StatisticalComputing.pdf: StatisticalComputing.tex \
	$(SOURCES:%.Rnw=%.tex) $(SOURCES:%.Rnw=%.R)
	@echo "making $@";
	@"$(R_HOME)/bin/R" CMD texi2dvi --pdf $<
	@"$(R_HOME)/bin/R" CMD pdflatex $<  # texi2dvi mangles ~ on linux

usage:
	@if [ -z $(R_HOME) ]; then \
	  echo "usage:\n \
		R CMD make -j \n \
		SWEAVE_OPTIONS=\"eval=FALSE\" R CMD make -j\n \
		R CMD make A01_Introduction/A01_Introduction.tex \n \
		R CMD make -B A01_Introduction/A01_Introduction.tex \n \
		make clean\n \
		make pristine" ; \
	  exit 1 ; \
	fi

clean:
	@for d in $(SOURCES); do \
	  cd `dirname $$d`; \
	  rm -f *log *aux *out *blg *bbl *toc *tex; \
	  cd ..; \
	done
	@rm -f *log *aux *out *blg *bbl *toc *tex

pristine: clean
	@rm -f $(SOURCES:%.Rnw=%.R) StatisticalComputing.pdf