LAB_A0=A1_SequenceAnalysis.Rnw A2_ReadsAndStrings.Rnw	\
	A3_AlignmentsAndRanges.Rnw A4_LargeData.Rnw
LAB_D0=D1_GeneAnnotations.Rnw D2_GenomicAnnotations.Rnw	\
	D3_Visualization.Rnw D4_CalledVariants.Rnw			\
	D5_StructuralVariants.Rnw D6_RegulatoryVariants.Rnw

LAB_A=$(LAB_A0:%=sequences-reads-ranges-alignments/%)
LAB_D=$(LAB_D0:%=annotation-visualization-variants/%)

SOURCES=morgan-r-bioconductor-intro.Rnw					\
	morgan-sequences-reads-ranges-alignments.Rnw			\
	morgan-annotation.Rnw morgan-big-data.Rnw morgan-pwms.Rnw	\
	sequences-reads-ranges-alignments.Rnw				\
	annotation-visualization-variants.Rnw
MISC_CLEANUP=sequences-reads-ranges-alignments/ranges-ir-plot.png	\
    sequences-reads-ranges-alignments/ranges-reduce-ir-plot.png		\
    sequences-reads-ranges-alignments/ranges-shift-ir-plot.png		\
    annotation-visualization-variants/GvizFigure.pdf			\
    annotation-visualization-variants/variant-levelplot.pdf		\
    seqlogo.pdf scores.pdf

%.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 ..

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

all: usage  $(SOURCES:%.Rnw=%.pdf)

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

annotation-visualization-variants.pdf:							\
	annotation-visualization-variants.tex $(LAB_D:%.Rnw=%.tex)	\
	$(LAB_D:%.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 \n \
		SWEAVE_OPTIONS=\"eval=FALSE\" R CMD make\n \
		R CMD make RBioconductorIntroduction.tex\n \
		R CMD make -B RBioconductorIntroduction.tex\n \
		make clean\n \
		make pristine" ; \
	  exit 1 ; \
	fi

.PHONY: publish

publish: all
	@cp sequences-reads-ranges-alignments.pdf $(LAB_A:%.Rnw=%.R)\
		../../../courses/monday/afternoon/
	@cp annotation-visualization-variants.pdf $(LAB_D:%.Rnw=%.R)\
		../../../courses/friday/afternoon/
	@cp morgan-r-bioconductor-intro.pdf ../../../courses/monday/morning/
	@cp morgan-sequences-reads-ranges-alignments.pdf\
		../../../courses/tuesday/morning/
	@cp morgan-annotation.pdf ../../../courses/thursday/morning/
	@cp morgan-big-data.pdf ../../../courses/thursday/morning/
# @cp morgan-pwms.pdf ../../../courses/friday/morning/

clean:
	@cd sequences-reads-ranges-alignments; rm -f *log *aux *out *blg *bbl *toc *tex Rplots.pdf; cd ..
	@cd annotation-visualization-variants; rm -f *log *aux *out *blg *bbl *toc *tex Rplots.pdf; cd ..
	@rm -f *log *aux *out *blg *bbl *toc *tex *nav *vrb *snm
	@rm -f $(MISC_CLEANUP)

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