topsrc = ../..

CC = gcc
CFLAGS = -g -O2
MAKE = make
INSTALL = /usr/bin/install -c
XML_CFLAGS = -I/usr/include/libxml2  
XML_LIBS = -lxml2 -lpthread -lz -lm  
XSLT_CFLAGS = -I/usr/include/libxml2  
XSLT_LIBS = -lxslt -lxml2 -lpthread -lz -lm  

GRETLINC = -I$(topsrc)/lib/src
GRETLLIB = ../../lib/libgretl-1.0.la

CMDSDIR = $(topsrc)/doc/commands

vpath %.c = $(CMDSDIR)
vpath %.dtd = $(CMDSDIR)
vpath %.xsl = $(CMDSDIR)

LIBTOOL = ../../libtool

CMDSDIR = $(topsrc)/doc/commands
CHAPDIR = $(topsrc)/doc/chapters
CHAPDIR_IT = $(topsrc)/doc/chapters_it
SHAREDIR = $(topsrc)/share

CMDSRC = $(CMDSDIR)/gretl_commands.xml \
	$(CMDSDIR)/gretl_commands_es.xml \
	$(CMDSDIR)/gretl_commands_it.xml

MANXSL = $(CMDSDIR)/gretlman.xsl
HLPXSL = $(CMDSDIR)/gretltxt.xsl

XMLSTRS = $(CMDSDIR)/hlpstrings.xml
TXTSTRS = $(CMDSDIR)/hlpstrings.txt

progs: xsltrans skeleton reflow hlpstrings validate 

xsltrans: xsltrans.c 
	$(CC) $(CFLAGS) $(XSLT_CFLAGS) -o $@ $^ $(XSLT_LIBS)

reflow: reflow.c
	$(CC) $(CFLAGS) -o $@ $^ 

hlpstrings: hlpstrings.c
	$(CC) $(CFLAGS) -o $@ $^ 

skeleton: skeleton.o
	$(LIBTOOL) --mode=link $(CC) -o $@ $^ $(GRETLLIB)

validate: validate.o
	$(LIBTOOL) --mode=link $(CC) -o $@ $^ $(GRETLLIB)

skeleton.o: skeleton.c
	$(CC) $(CFLAGS) $(XML_CFLAGS) $(GRETLINC) -c $<

validate.o: validate.c
	$(CC) $(CFLAGS) $(XML_CFLAGS) $(GRETLINC) -c $<

# test generation of XML skeleton from libgretl info:
# the skeleton could be used as a template for translation
skeleton.xml: skeleton gretl_commands.dtd
	./skeleton $(CMDSDIR) > $@

# test validity of skeleton.xml, and ability of xsltrans
# to generate formatted output (text and XML)
test: skeleton.xml xsltrans gretlman.xsl gretltxt.xsl
	xmllint --valid --noout $< && ./xsltrans --all skeleton.xml $(CMDSDIR)

# Generate docbook XML for the command-reference section of the 
# full manual, cmdlist.xml
docbook: xsltrans $(CMDSDIR)/gretl_commands.xml $(MANXSL) $(XMLSTRS)
	./xsltrans --docbook $(CMDSDIR)/gretl_commands.xml && \
	cp cmdlist.xml $(CHAPDIR)/cmdlist.xml && \
	rm cmdlist.xml

docbook_it: xsltrans $(CMDSDIR)/gretl_commands_it.xml $(MANXSL) $(XMLSTRS)
	./xsltrans --docbook $(CMDSDIR)/gretl_commands_it.xml && \
	cp cmdlist.xml $(CHAPDIR_IT)/cmdlist.xml && \
	rm cmdlist.xml

# Generate "online" help files, gretlcli.hlp and gretl.hlp
hlp: xsltrans reflow $(CMDSRC) $(HLPXSL) $(XMLSTRS)
	./xsltrans --hlp $(CMDSDIR)/gretl_commands.xml && \
	./reflow < cmdlist.txt > $(SHAREDIR)/gretlcli.hlp
	./reflow < guilist.txt > $(SHAREDIR)/gretl.hlp
	./xsltrans --hlp $(CMDSDIR)/gretl_commands_es.xml && \
	./reflow < cmdlist.txt > $(SHAREDIR)/gretlcli.hlp.es
	./reflow < guilist.txt > $(SHAREDIR)/gretl.hlp.es
	./xsltrans --hlp $(CMDSDIR)/gretl_commands_it.xml && \
	./reflow < cmdlist.txt > $(SHAREDIR)/gretlcli.hlp.it
	./reflow < guilist.txt > $(SHAREDIR)/gretl.hlp.it

$(XMLSTRS): $(TXTSTRS) hlpstrings
	./hlpstrings $(TXTSTRS) > $(XMLSTRS)

# Validate the commands "master" XML files against libgretl
check: validate 
	./validate $(CMDSDIR)/gretl_commands.xml
	./validate $(CMDSDIR)/gretl_commands_es.xml
	./validate $(CMDSDIR)/gretl_commands_it.xml

docclean:
	 rm -f skeleton.xml cmdlist.* cmdlist_standalone.* guilist.txt

clean: docclean
	rm -f xsltrans skeleton reflow validate *.o
