# Converting DocBook to HTML (several small files)
# http://www.freebsd.org/tutorials/docproj-primer/x3132.html#AEN3140

SHELL = /bin/sh
DOCDIR = .
TARGET_DOCDIR = $(DESTDIR)$(docdir)

EXTRA_DIST =	api_status.txt bcp.txt cap.txt getting_started.txt \
		policy.txt CodingStyle tds.html  \
		userguide.xsl userguide.xml userguide.css \
		$(man_MANS) \
		$(DOCDIR)/userguide $(DOCDIR)/reference \
		images grep_sample_code tds_ssl.html \
		freebcp.1.in tsql.1.in osql.1.in bsqldb.1.in bsqlodbc.1.in \
		defncopy.1.in datacopy.1.in fisql.1.in freetds.conf.5.in \
		userguide_desc.xml

man_MANS = 	freebcp.1 \
		tsql.1 \
		osql.1 \
		bsqldb.1 \
		bsqlodbc.1 \
		defncopy.1 \
		datacopy.1 \
		fisql.1 \
		freetds.conf.5

noinst_DATA	= $(DOCDIR)/reference/index.html $(DOCDIR)/userguide/index.html

$(DOCDIR)/userguide: $(DOCDIR)/userguide/index.html

$(DOCDIR)/reference: $(DOCDIR)/reference/index.html

doc:  $(DOCDIR)/userguide $(DOCDIR)/reference

dist:	man

man:	$(man_MANS)

INSTALLDIRS = 	$(TARGET_DOCDIR)/userguide    \
		$(TARGET_DOCDIR)/reference    \
		$(TARGET_DOCDIR)/images       \
		$(TARGET_DOCDIR)/images/callouts

$(INSTALLDIRS) $(DOCDIR):
	$(MKDIR_P) $@  2>&1

# If we built the documentation ourselves, install that, 
# else if we have access to distributed documentation, install that.  
install-data-local: $(INSTALLDIRS) $(noinst_DATA)
	$(MKDIR_P) $(DOCDIR) 2>&1
	if test -r $(DOCDIR)/userguide ; then d=.; else d="$(srcdir)"; fi; \
	find $$d/$(DOCDIR)/userguide \( -type f -o -type l \) \
		-exec $(INSTALL) -m 644 {} $(TARGET_DOCDIR)/userguide ';'
	if test -r $(DOCDIR)/reference ; then d=.; else d="$(srcdir)"; fi; \
	find $$d/$(DOCDIR)/reference \( -type f -o -type l \) \
		-exec $(INSTALL) -m 644 {} $(TARGET_DOCDIR)/reference ';'
	cd $(srcdir) && find images -name \*.gif \
		-exec  $(INSTALL) -m 644 {} $(TARGET_DOCDIR)/{} ';'

uninstall-local:
	rm -rf $(TARGET_DOCDIR)

clean-local:
	rm -rf doxy.log \
		dblib.api.xml	\
		ctlib.api.xml	\
		 odbc.api.xml

## In building the UG and reference manual, we confront several
## possible situations:
## 1.  GIT user (or developer) with or without xmlto/doxygen installed.
## 2.  Distribution user.  The distribution includes pre-built
##     documentation, ready to install.
## Either of these users may choose to build out-of-tree (e.g., in a
## build/ directory).  We want to build the docs if we can, link to
## distributed ones if they exist, else create stub files pointing to
## the website.  
##
## We build the documentation only if we found xmlto.
## If the user builds FreeTDS out of tree, "make install" will put a symlink
## to the distributed UG HTML in the build directory, and install per usual.
##
## In the case of Doxygen, we detect its presence in the configure
## script, and invoke it if found.  If it is not found, we try to link
## the build tree's reference manual HTML to the distribution.  If
## that fails, we install a stub.

DISTRIBUTED_UG_DIR = $(srcdir)/$(DOCDIR)/userguide

UG_STUB = <html> \
	<P>at <a HREF="http://www.freetds.org/userguide/">www.freetds.org</a> \
	</html>

userguide_desc.xml: userguide.xml
	git log -1 '--pretty=format:<!-- Autogenerated -->%n<!ENTITY ug.date "Date: %ai">%n<!ENTITY ug.id "Git Id: %H">' HEAD -- $< > $@~
	mv -f $@~ $@

# To make the userguide, install xmlto
$(DOCDIR)/userguide/index.html: 	$(DOCDIR) userguide.xml userguide_desc.xml \
				dblib.api.xml ctlib.api.xml odbc.api.xml \
				userguide.xsl api_status.txt
	if test -h userguide; then rm userguide; fi
	$(MKDIR_P) $(DOCDIR)/userguide 2>&1
	touch $(DOCDIR)/userguide/t.htm
	rm -rf $(DOCDIR)/userguide/*
# if we're not using xmlto, copy the distribution's userguide
	if ! xmlto --version > /dev/null 2> /dev/null ; then \
		if test -r $(DISTRIBUTED_UG_DIR) \
		   -a ! -r $(DOCDIR)/userguide ; then \
			cp -R $(DISTRIBUTED_UG_DIR) $(DOCDIR); \
		fi; \
	fi
# try xmlto ...
	test -r "$(abs_srcdir)/userguide.xml"
	test -r userguide.xml || ln -s $(srcdir)/userguide.xml userguide.xml
	test -r userguide.xsl || ln -s $(srcdir)/userguide.xsl userguide.xsl
	test -r userguide_desc.xml || ln -s $(srcdir)/userguide_desc.xml userguide_desc.xml
	(cd $(DOCDIR)/userguide && \
	if xmlto --version > /dev/null 2> /dev/null ; then \
		xmlto -m ../userguide.xsl xhtml ../userguide.xml; \
## 		and copy the stylesheet
		cp "$(abs_srcdir)/userguide.css" .; \
	fi)
# if still no joy, create a stub
	if test ! -r $@ ; then \
		echo '$(UG_STUB)' > .index.html && mv .index.html $@; \
	fi
	test -f $(DOCDIR)/userguide/index.html

userguide: $(DOCDIR)/userguide/index.html
reference: $(DOCDIR)/reference/index.html

DISTRIBUTED_REF_DIR = $(srcdir)/$(DOCDIR)/reference

REF_STUB = <html> \
	<P>at <a HREF="http://www.freetds.org/reference/">www.freetds.org</a> \
	</html>

$(DOCDIR)/reference/index.html: $(top_srcdir)/configure.ac
	if test -h reference; then rm reference; fi
	$(MKDIR_P) $(DOCDIR)/reference 2>&1
	cd .. && $(MAKE) doxy 2> doc/doxy.log
# If "make doxy" doesn't create anything, 
# try to copy the distributed manual's HTML
	if test -r $(DISTRIBUTED_REF_DIR)/index.html -a ! $@ ; then \
		cp -R $(DISTRIBUTED_REF_DIR) $(DOCDIR); \
	fi
# If we don't find the source, generate a stub.
	if test ! -r $@ ; then \
		cd $(DOCDIR)/reference && \
		echo '$(REF_STUB)' > .index.html &&  \
		mv .index.html index.html; \
	fi

#
# The following sections generate API implementation reference tables
# as XML "entities".
# The entities are used a little bit like include files in the UG, e.g.:  
#	<!ENTITY dblibapixml SYSTEM "../../../dblib.api.xml">
#
# and "dblibapixml" is referred to later on:
#	
#	<sect1 id="dblib.api.summary">
#		<title>db-lib API Implementation Summary</title>
#		&dblibapixml;
#	</sect1>
#
# This lets us use the api_status.txt document in our XML.
#
API_TABLE_HEADING = \
	<tgroup cols="4"><thead><row> \
		<entry>Category</entry> \
		<entry>Function</entry> \
		<entry>Status</entry> \
		<entry>Comments</entry> \
		</row></thead> 

API_FUNC_COL = <entry>Function</entry>
API_DBLIB_FUNC_COLS = <entry>Microsoft name</entry><entry>Sybase name</entry>

API_PAGE_FMT = \
	<table id="%s.api"> \
	<title>&%s; API Implementation Summary</title>\n \
	<!-- table heading -->\n \
		%s\n \
	<tbody>\n \
		%s\n \
	</tbody></tgroup></table>\n

dblib.api.body ctlib.api.body odbc.api.body: $(srcdir)/api_status.txt
	NAME=`echo $@ | sed 's/\..*$$//'` && \
	sed -ne "/^$${NAME}[	]/ s!!<row><entry>!p" \
			$(srcdir)/api_status.txt \
		| sed 	-e's![	][	]*!</entry> <entry>!g' \
			-e's!$$!</entry></row>!'  > $@~
	mv $@~ $@

dblib.api.xml: dblib.api.body
ctlib.api.xml: ctlib.api.body
odbc.api.xml: odbc.api.body

dblib.api.xml ctlib.api.xml odbc.api.xml:
	NAME=`echo $@ | sed 's/\..*$$//'` && \
	HEAD='$(API_TABLE_HEADING)' && \
	if [ $${NAME} = dblib ]; then \
		HEAD=`echo '$(API_TABLE_HEADING)' \
			| sed 's!$(API_FUNC_COL)!$(API_DBLIB_FUNC_COLS)!'`; \
	fi && \
	BODY=`cat $(@:.xml=.body)` && \
	printf '$(API_PAGE_FMT)' $${NAME} $${NAME} "$${HEAD}" "$${BODY}"  > $@~
	mv $@~ $@

CLEANFILES = dblib.api.body ctlib.api.body odbc.api.body dblib.api.xml ctlib.api.xml odbc.api.xml $(man_MANS)

distclean-local:
	rm -rf $(DOCDIR)/userguide $(DOCDIR)/reference

