############################
## BEGIN CONFIGURE VARIABLES
## The following variables are set by the configure script when
## generating Makefile from Makefile.in.
## If for some reason the configure script does not work for you,
## Copy Makefile.in to Makefile and a set the following variables
## yourself based on the given typical values (Linux, unless
## specified otherwise)

#srcdir=.
srcdir=.

#builddir=.
builddir=.

#top_srcdir=..
top_srcdir=../..

#utilsbuilddir=.
utilsbuilddir=../../utils

#fmriqabuilddir=.
fmriqabuilddir=../../fmriqa

#prefix=/usr/local
prefix=/usr/local/bxh-xcede-tools/1.10.7

#DOCBOOKXSL=/usr/share/sgml/docbook/xsl-stylesheets
DOCBOOKXSL=/usr/share/sgml/docbook/xsl-stylesheets

#XALANJPATH=/usr/share/java
XALANJPATH=/usr/share/java
#XALANJAR=/usr/share/java/xalan-j2.jar
XALANJAR=/usr/share/java/xalan-j2.jar

#FOP=${HOME}/xml/fop-0.94/fop
FOP=/usr/bin/fop

#XSLTPROC=/usr/bin/xsltproc
XSLTPROC=/usr/bin/xsltproc

#JAVAHOME=/usr/java/jdk1.6.0_07
JAVAHOME=/usr

XMLH_VERSION:=$(shell cat $(top_srcdir)/XMLH_VERSION)

INSTALL=/usr/bin/install -c
INSTALL_PROGRAM=${INSTALL}
INSTALL_DATA=${INSTALL} -m 644
INSTALL_SCRIPT=${INSTALL}

SHELL=/bin/sh

## END CONFIGURE VARIABLES
##########################

## You probably won't need to change anything below this! (?)

CUSTOMPDFXSL=docbook_custom.xsl

all: toolusage usermanual_full.pdf usermanual_full.html usermanual_full.txt docbook.css

install: all
	/bin/sh $(top_srcdir)/mkinstalldirs $(prefix)/share/doc/bxh_xcede_tools-${XMLH_VERSION}/usermanual
	cp usermanual_full.pdf usermanual_full.html usermanual_full.txt docbook.css $(prefix)/share/doc/bxh_xcede_tools-${XMLH_VERSION}/usermanual
	for i in $(PROGS) $(SCRIPTS) $(FMRIQASCRIPTS) ; do $(INSTALL_PROGRAM) $$i $(prefix)/bin ; done
	for i in $(ABSORBPROGS) ; do cd $(prefix)/bin && rm -f $$i && ln -s bxhabsorb $$i ; done
	cd $(prefix)/bin && rm -f xcede2analyze && ln -s bxh2analyze xcede2analyze

rebuild: clean all

clean:
	rm -f usermanual_full.fo usermanual_full.pdf usermanual_full.txt usermanual_full.html usermanual_full.xml

distclean: clean
	rm -f Makefile

toolusage:
	@for i in afni2bxh analyze2bxh batch_showplay2xml bxh2analyze bxh2pgm bxh2ppm bxhabsorb bxh_brainmask bxh_correlate bxh_epochavg bxh_event2table bxh_eventmerge bxh_eventresp bxh_eventstats bxh_eventstats_standardize bxh_mean bxhreorient bxhselect bxhsetorient bxh_tfilter bxh_ttest dicom2bxh dumpheader eprime2xml eventstable2xml extractimagedata extractxyztdata ffile2bxh iowa-signafive2bxh minc2bxh pfile2bxh presentation2xml printfrags showplay2xml signafive2bxh ximg2bxh xcede_extract_schedules.pl ; do if [ $(utilsbuilddir)/$$i -nt "tool_$$i.xml" ] ; then echo "Getting help message for $$i" ; env PATH=$(utilsbuilddir):$(fmriqabuilddir):${PATH} bash $(srcdir)/wrapusage.sh $$i > "tool_$$i.xml" ; true ; fi ; done
	@for i in fmriqa_count fmriqa_generate.pl fmriqa_mean fmriqa_minmax fmriqa_oediff fmriqa_phantomqa fmriqa_phantomqa.pl fmriqa_spikiness fmriqa_stddev fmriqa_volmeasures ; do if [ $(fmriqabuilddir)/$$i -nt "tool_$$i.xml" ] ; then echo "Getting help message for $$i"; env PATH=$(utilsbuilddir):$(fmriqabuilddir):${PATH} bash $(srcdir)/wrapusage.sh $$i > "tool_$$i.xml" ; true ; fi ; done

docbook.css: $(srcdir)/docbook.css
	@if [ $(srcdir) != $(builddir) ] ; then echo cp $(srcdir)/docbook.css $(builddir) ; cp $(srcdir)/docbook.css $(builddir) ; fi

usermanual_full.xml: $(srcdir)/usermanual.xml $(srcdir)/overview.xml $(srcdir)/qa.xml
	@if [ $(srcdir) != $(builddir) ] ; then echo cp $(srcdir)/'*'.xml . ; cp $(srcdir)/*.xml . ; fi
	xmllint --xinclude usermanual.xml > usermanual_full.xml

usermanual_full.fo: usermanual_full.xml $(builddir)/${CUSTOMPDFXSL}
	java -cp ${XALANJAR} org.apache.xalan.xslt.Process -PARAM use.extensions 1 -PARAM textinsert.extension 1 -PARAM hyphenate.verbatim 1 -PARAM xref.with.number.and.title 0 -PARAM section.autolabel 1 -PARAM section.label.includes.component.label 1 -PARAM header.column.widths '30 40 30' -PARAM footer.column.widths '30 40 30' -PARAM tex.math.in.alt tex -PARAM fop1.extensions 1 -XSL $(builddir)/${CUSTOMPDFXSL} -IN usermanual_full.xml -OUT usermanual_full.fo

usermanual_full.pdf: usermanual_full.fo
	env JAVA_HOME=${JAVAHOME} ${FOP} -fo usermanual_full.fo -pdf usermanual_full.pdf

usermanual_full.html: usermanual_full.xml
	"${XSLTPROC}" --stringparam generate.toc "book toc  part toc" --stringparam toc.section.depth 1 --stringparam root.filename usermanual_full -stringparam chunk.fast 1 --stringparam chunker.output.indent yes --stringparam html.stylesheet docbook.css ${DOCBOOKXSL}/xhtml/onechunk.xsl usermanual_full.xml

usermanual_full.txt: usermanual_full.html
	perl -e 'local $$/; $$_ = <>; s%<(a|link)(\s+\w+="[^"]*")*(\s+href="([^h]|h[^t]|ht[^t]|htt[^p]|http[^:])[^"]*")(\s+\w+="[^"]*")*\s*>%%g; print $$_' usermanual_full.html | lynx -dump -stdin > usermanual_full.txt
