# +----------------------------------------------------------------------+
# | PHP Version 4                                                        |
# +----------------------------------------------------------------------+
# | Copyright (c) 1997-2008 The PHP Group                                |
# +----------------------------------------------------------------------+
# | This source file is subject to version 3.0 of the PHP license,       |
# | that is bundled with this package in the file LICENSE, and is        |
# | available through the world-wide-web at the following url:           |
# | http://www.php.net/license/3_0.txt.                                  |
# | If you did not receive a copy of the PHP license and are unable to   |
# | obtain it through the world-wide-web, please send a note to          |
# | license@php.net so we can mail you a copy immediately.               |
# +----------------------------------------------------------------------+
# | Authors: Stig Bakken <ssb@fast.no>                                   |
# +----------------------------------------------------------------------+
#

#
# $Id: Makefile.in,v 1.205 2008/05/26 21:50:26 takagi Exp $
#

all: html

# {{{ variables

VPATH=@srcdir@

srcdir=@srcdir@
scriptdir=@srcdir@/scripts
PHP_SOURCE=@PHP_SOURCE@
PECL_SOURCE=@PECL_SOURCE@
LANGCODE=@LANG@
LANGDIR=@LANGDIR@
LANG=@LANG@ -D .
JADE=@SP_OPTIONS@ @JADE@ -D . -wno-idref
NSGMLS=@SP_OPTIONS@ @NSGMLS@
PHP=@PHP@
XSLTPROC=@XSLTPROC@
XMLLINT=@XMLLINT@
PHD=@PHD@

# Suppress "stripping namespace" messages
DEBUG_SILENT=@XSL_PARAM_QUIET@

CATALOG=@CATALOG@
PARTIAL_STYLE=@PARTIAL_STYLE@
PARTIAL_PHD_THEME=@PARTIAL_PHD_THEME@

MANUAL_STRIPPED=stripped.xml
MANUAL_COMPLETE=.manual.xml

PHPBOOK=phpbook
PHPDSSSL=$(PHPBOOK)/phpbook-dsssl
PHPXML=$(PHPBOOK)/phpbook-xml
PHPXSL=$(PHPBOOK)/phpbook-xsl

HTML_STYLESHEET=$(PHPDSSSL)/html.dsl
PHPWEB_STYLESHEET=$(PHPDSSSL)/phpweb.dsl
HOWTO_STYLESHEET=$(PHPDSSSL)/howto.dsl
HOWTOPHP_STYLESHEET=$(PHPDSSSL)/howto-php.dsl
PRINT_STYLESHEET=$(PHPDSSSL)/print.dsl
KDEVELOP_TOC_STYLESHEET=$(PHPDSSSL)/kdevelop_toc.dsl

XMLDCL=$(PHPXML)/phpdocxml.dcl

CHM_XSL_SHEET=$(PHPXSL)/htmlhelp.xsl
HOWTO_XSL_SHEET=$(PHPXSL)/howto.xsl
QUICKREF_XSL_SHEET=$(PHPXSL)/quickref.xsl
XUL_XSL_SHEET=$(PHPXSL)/htmlhelp.xsl
PARTIAL_STYLESHEET=$(PHPXSL)/@PARTIAL_STYLESHEET@

BIGHTML_DEPS=$(HTML_STYLESHEET) $(PHPDSSSL)/html-common.dsl $(PHPDSSSL)/html-locale.dsl.in $(PHPDSSSL)/common.dsl.in $(MANUAL_STRIPPED) images
HTML_DEPS=$(HTML_STYLESHEET) $(PHPDSSSL)/html-common.dsl $(PHPDSSSL)/html-locale.dsl.in $(PHPDSSSL)/common.dsl.in $(MANUAL_STRIPPED) images_html
HOWTO_DEPS=$(HOWTO_STYLESHEET) $(HOWTOPHP_STYLESHEET) $(PHPDSSSL)/html-common.dsl $(PHPDSSSL)/html-locale.dsl.in $(PHPDSSSL)/common.dsl.in howto/howto.ent
PRINT_DEPS=$(PRINT_STYLESHEET) $(PHPDSSSL)/common.dsl.in $(PHPDSSSL)/print.dsl.in $(MANUAL_STRIPPED) images
PHPWEB_DEPS=$(PHPWEB_STYLESHEET) $(PHPDSSSL)/html-common.dsl $(PHPDSSSL)/common.dsl $(MANUAL_STRIPPED) images_php
KDEVELOP_TOC_DEPS=$(KDEVELOP_TOC_STYLESHEET) Makefile

DIST_FILES=@MANUAL@.tar.gz @MANUAL@.html.gz
# DSSSSSL
#MIRROR_TARGETS=phpweb html/index.html $(DIST_FILES)
# Note DSSSSSSL is still used for the downloadable html - but will be replaced by XSL ASAP
MIRROR_TARGETS=phpweb_xsl html/index.html $(DIST_FILES)


PDF_FILES=@MANUAL@.pdf.bz2

HACK_RTL_LANGS_PAGES=@HACK_RTL_LANGS_PAGES@
HACK_RTL_LANGS_PHPWEB=@HACK_RTL_LANGS_PHPWEB@
# }}}

@AUTOGENERATED_RULES@

entities: FORCE
	$(PHP) -c $(scriptdir) -q $(scriptdir)/entities.php

manual.xml: $(srcdir)/manual.xml.in
	CONFIG_FILES=$@ CONFIG_HEADERS= ./config.status

images:
	for i in `find en -type d -name figures`; do sect=`echo $$i | awk -F\/ '{print $$3}'`; for file in $$i/*; do if test -f $$file; then name=`basename $$file`; cp $$file $$sect.$$name; fi; done; done;

images_html: 
	rm -rf html && mkdir html && mkdir html/figures
	for i in `find en -type d -name figures`; do sect=`echo $$i | awk -F\/ '{print $$3}'`; for file in $$i/*; do if test -f $$file; then name=`basename $$file`; cp $$file html/figures/$$sect.$$name; fi; done; done;

images_xul: 
	rm -rf xul && mkdir xul
	@if test "@FIGURES@" != ""; then mkdir xul/@FIGURES@ ; fi
	for i in `find en -type d -name figures`; do sect=`echo $$i | awk -F\/ '{print $$3}'`; for file in $$i/*; do if test -f $$file; then name=`basename $$file`; cp $$file xul/@FIGURES@/$$sect.$$name; fi; done; done;

images_php: 
	rm -rf php && mkdir php && mkdir php/figures
	for i in `find en -type d -name figures`; do sect=`echo $$i | awk -F\/ '{print $$3}'`; for file in $$i/*; do if test -f $$file; then name=`basename $$file`; cp $$file php/figures/$$sect.$$name; fi; done; done;

images_htmlhelp: 
	rm -rf ./htmlhelp/html && mkdir ./htmlhelp/html && mkdir htmlhelp/html/figures
	for i in `find en -type d -name figures`; do sect=`echo $$i | awk -F\/ '{print $$3}'`; for file in $$i/*; do if test -f $$file; then name=`basename $$file`; cp $$file htmlhelp/html/figures/$$sect.$$name; fi; done; done;

html: html/index.html
bightml: @MANUAL@.html
phpweb: php/index.php
rtf: @MANUAL@.rtf
tex latex: @MANUAL@.tex
dvi: @MANUAL@.dvi
ps: @MANUAL@.ps
pdf: @MANUAL@.pdf
howto: howto/html/index.html
howtophp: howto/php/index.php
howtotgz: howto/howto.html.tar.gz
funcindex: funcindex.xml
revcheck: revcheck.html

FORCE:
	-@rm -f $(MANUAL_COMPLETE) $(MANUAL_STRIPPED)

$(MANUAL_STRIPPED): $(MANUAL_COMPLETE)
	@echo -n "Stripping it..."
	@${XSLTPROC} --param debug.silent $(DEBUG_SILENT) -o $(MANUAL_STRIPPED) docbook/docbook-xsl/common/stripns.xsl $(MANUAL_COMPLETE)
	@echo " done"

$(MANUAL_COMPLETE): FORCE entities/global.ent
	@echo -n "Generating the manual..."
	@$(XMLLINT) --nsclean --xinclude --noent --output $(MANUAL_COMPLETE) manual.xml
	@echo " done"

funclist.txt: FORCE
	@if test "$(PHP_SOURCE)" = "no"; then \
	    echo "WARNING: recreating the function list needs php sources" ;\
	    echo "         which were not found by configure" ;\
	    echo "         aborting " ;\
	else \
	    $(PHP) -C -q $(scriptdir)/genfunclist.php $(PHP_SOURCE) > funclist.txt ;\
	fi; \
        if test "$(PECL_SOURCE)" = "no" -a ! "$(PHP_SOURCE)" = "no"; then \
            echo "WARNING: pecl sources not found - pecl functions" ;\
            echo "         will not be present in funclist.txt" ;\
        elif test ! "$(PHP_SOURCE)" = "no"; then \
            echo ""  >> funclist.txt ;\
            echo "# --------- PECL Stuff --------- " >> funclist.txt ;\
            $(PHP) -C -q $(scriptdir)/genfunclist.php $(PECL_SOURCE) >> funclist.txt ;\
        fi

funcsummary.txt: FORCE
	$(PHP) -C -q $(scriptdir)/genfuncsummary.php $(PHP_SOURCE) > funcsummary.txt

revcheck.html: FORCE
	PHPDOCDIR=$(srcdir) $(PHP) -c $(scriptdir) -f $(scriptdir)/revcheck.php $(LANGDIR) > revcheck.html

funcindex.xml: $(MANUAL_STRIPPED)
	@if test "$(PHP)" = "no"; then \
	echo "WARNING: recreating the function index needs php" ;\
	echo "         which was not found by configure" ;\
	echo "         reusing old file" ;\
	touch funcindex.xml ;\
	else \
	$(XSLTPROC) -o quickref-temp.txt $(QUICKREF_XSL_SHEET) $(MANUAL_STRIPPED) ;\
	echo "$(PHP) -q $(scriptdir)/genfuncindex.php quickref-temp.txt > funcindex.xml" ;\
	$(PHP) -q $(scriptdir)/genfuncindex.php quickref-temp.txt > funcindex.xml ;\
	rm -f quickref-temp.txt ;\
	fi

mirror-files: $(MIRROR_TARGETS)
#extra-mirror-files: $(PDF_FILES) isilo
extra-mirror-files: isilo

snapshot: manual-snapshot.tar.gz

manual-snapshot.tar.gz: @MANUAL@.html html/index.html @MANUAL@.rtf @MANUAL@.txt
	tar -cvzf $@ @MANUAL@.html html/*.html @MANUAL@.rtf @MANUAL@.txt

status: ./funclist.txt
	$(PHP) -q $(scriptdir)/checkdoc.php > status.txt
	$(PHP) -q $(scriptdir)/checkdoc.php missing > missing.txt

summary: ./funcsummary.txt

quickref: quickref.txt
quickref.txt: $(MANUAL_STRIPPED)
	@if test ! -f funcindex.xml; then touch funcindex.xml; fi 
	$(XSLTPROC) -o quickref.txt $(QUICKREF_XSL_SHEET) $(MANUAL_STRIPPED)

kdevelop_toc: php.toc
php.toc: $(MANUAL_COMPLETE) $(KDEVELOP_TOC_DEPS)
	echo '<!DOCTYPE kdeveloptoc>' > $@
	$(JADE) $(CATALOG) -d $(KDEVELOP_TOC_STYLESHEET) -V nochunks -t sgml $(XMLDCL) $(MANUAL_COMPLETE) | sed -e's/\& /\&amp; /g' >> $@

scripts/makedoc: $(scriptdir)/makedoc.cc
	mkdir -p scripts
	g++ -o scripts/makedoc $(scriptdir)/makedoc.cc

# intermediate file for name change 
.SECONDARY: @MANUAL@.xml

@MANUAL@.xml: $(MANUAL_COMPLETE)
	cp $(MANUAL_COMPLETE) $@

@MANUAL@.html: @MANUAL@.xml $(BIGHTML_DEPS)

html/index.html: $(HTML_DEPS)
	$(PHP) -q $(scriptdir)/rtlpatch/hackmanuallang.php $(LANGDIR)
	$(JADE) $(CATALOG) -d $(HTML_STYLESHEET) -V use-output-dir -t sgml $(XMLDCL) $(MANUAL_STRIPPED)
	$(PHP) -q $(scriptdir)/html_syntax.php html dsssl html/
	$(PHP) -q $(scriptdir)/rtlpatch/hackmanuallang.php en
	$(HACK_RTL_LANGS_PAGES)

# still needs more tweaks!!
html_xsl: $(MANUAL_STRIPPED) images_html
	${XSLTPROC} $(PHPXSL)/html.xsl $(MANUAL_STRIPPED)
	$(PHP) -q $(scriptdir)/html_syntax.php html xsl html/

xul_xsl: $(MANUAL_STRIPPED) images_xul
	${XSLTPROC} xsl/xul.xsl $(MANUAL_STRIPPED)

bightml_xsl: $(MANUAL_COMPLETE) images
	${XSLTPROC} -o @MANUAL@.html $(PHPXSL)/bightml.xsl $(MANUAL_STRIPPED)

fo: $(MANUAL_STRIPPED) images
	${XSLTPROC} -o manual.fo $(PHPXSL)/fo.xsl $(MANUAL_STRIPPED)

howto_xsl: $(MANUAL_STRIPPED)
	rm -rf ./howto/html && mkdir ./howto/html
	${XSLTPROC} $(HOWTO_XSL_SHEET) ./howto/howto.xml

chm_xsl: $(MANUAL_STRIPPED) images_htmlhelp
	${XSLTPROC} $(CHM_XSL_SHEET) $(MANUAL_STRIPPED)
 
phpweb_xsl: $(MANUAL_STRIPPED) images_php
	${XSLTPROC} $(PHPXSL)/phpweb.xsl $(MANUAL_STRIPPED)
	$(PHP) -q $(scriptdir)/html_syntax.php php xsl php/
	#$(PHP) -q $(scriptdir)/fixphpweb.php `pwd`/php

php/index.php: $(MANUAL_STRIPPED) $(PHPWEB_DEPS)
	$(PHP) -q $(scriptdir)/phpweb-entities.php `pwd` phpweb
	$(PHP) -q $(scriptdir)/rtlpatch/hackmanuallang.php $(LANGDIR)
	-$(JADE) $(CATALOG) -d $(PHPWEB_STYLESHEET) -V use-output-dir -t sgml $(XMLDCL) $(MANUAL_STRIPPED)
	$(PHP) -q $(scriptdir)/html_syntax.php php dsssl php/
	$(PHP) -q $(scriptdir)/rtlpatch/hackmanuallang.php en
	$(PHP) -q $(scriptdir)/phpweb-entities.php `pwd` remove
	$(HACK_RTL_LANGS_PHPWEB)

# PhD Builds:
phd_all: $(MANUAL_COMPLETE) images_php images_html
	@$(PHD) -vvvv -d$(MANUAL_COMPLETE)

phd_phpweb: $(MANUAL_COMPLETE) images_php
	@$(PHD) -vvvv -tphpweb -d$(MANUAL_COMPLETE)

phd_bightml: $(MANUAL_COMPLETE) images
	@$(PHD) -vvvv -tbightml -d$(MANUAL_COMPLETE)

phd_html: $(MANUAL_COMPLETE) images_html
	@$(PHD) -vvvv -tchunkedhtml -d$(MANUAL_COMPLETE)

phd_partial/%: $(MANUAL_COMPLETE)
	@$(PHD) -vvvv -t$(PARTIAL_PHD_THEME) -p$(@F) -d$(MANUAL_COMPLETE)

palmdoc: @MANUAL@.doc.pdb
@MANUAL@.doc.pdb: @MANUAL@.txt scripts/makedoc
	scripts/makedoc -b @MANUAL@.txt @MANUAL@.doc.pdb @PALMDOCTITLE@

# Note: Isilo converter available only in binary from www.isilo.com
isilo: @MANUAL@.isilo.pdb
@MANUAL@.isilo.pdb: @MANUAL@.html
	iSilo386 @MANUAL@.html @MANUAL@.isilo.pdb

@MANUAL@.zip: html/index.html
	-rm -f $@ && (cd html; zip -R -9 -q ../$@ *.html *.png)

@MANUAL@.tar.gz: html/index.html
	-rm -f $@ && (tar -cf - html) | gzip -9 > $@

@MANUAL@.tar.bz2: html/index.html
	-rm -f $@ && (tar -cf - html) | bzip2 -9 > $@

howto/php/index.php: howto/howto.xml $(HOWTO_DEPS)
	@test -d howto/php || mkdir howto/php
	-$(JADE) -i lang-en $(CATALOG) -d $(HOWTOPHP_STYLESHEET) -V use-output-dir -t sgml $(XMLDCL) $(srcdir)/howto/howto.xml

howto/html/index.html: howto/howto.xml $(HOWTO_DEPS)
	@test -d howto/html || mkdir howto/html
	-$(JADE) -i lang-en $(CATALOG) -d $(HOWTO_STYLESHEET) -V use-output-dir -t sgml $(XMLDCL) $(srcdir)/howto/howto.xml

howto/howto.html.tar.gz: howto/html/index.html $(HOWTO_DEPS)
	tar -cvzf $@ howto/html/*.html  

install.html: install.xml $(HTML_DEPS)
	$(JADE) $(CATALOG) -V nochunks -d $(PHPDSSSL)/install.dsl -t sgml $(XMLDCL) install.xml > $@

chm: html_xsl FORCE
	chm/make_chm.bat $(LANG)

# File endings we are going to define general rules for:
.SUFFIXES: .html .xml .sgml .tex .dvi .ps .pdf .rtf .gz .bz2 .txt
.PRECIOUS: %.html %.dvi
# General rules:
%.rtf: %.xml
	$(JADE) $(CATALOG) -d $(PRINT_STYLESHEET) -t rtf $(XMLDCL) $<

%.gz: %
	gzip -9 -c $< > $@

%.bz2: % 
	bzip2 -9 -c $< > $@

%.zip: %
	zip -9 $@ $<

%.pdf: %.dvi
	dvipdfm -o $@ -p @PDF_PAPER_TYPE@ $<

%.ps: %.dvi
	dvips -o $@ -t @PDF_PAPER_TYPE@ $<

%.html: %.xml
	$(JADE) $(CATALOG) -V nochunks -d $(HTML_STYLESHEET) -t sgml $(XMLDCL) $< > $@

%.txt: %.html
	lynx -nolist -dump file:`pwd`/$< > $@

%.tex: %.xml
	$(JADE) $(CATALOG) -d $(PRINT_STYLESHEET) -t tex $(XMLDCL) $<

# general TeX->dvi ru
# runs three times -
# 1. generates the dvi with a completely bogus table of contents
# 2. generates the dvi with a table of contents that is off based on the size
#    of the table of contents
# 3. generates a dvi with all the right page numbers and such
%.dvi : %.tex
	# a hack around bugs in jade/jadetex...
	mv $< $<.tmp
	sed -e '/HeadingText/,/endHeadPar/ s/_/\\137/g' $<.tmp > $<
	rm $<.tmp
	-jadetex $<
	-jadetex $<
	-jadetex $<

# {{{ partial builds
part/%: $(MANUAL_STRIPPED)
	@if test ! -d $(PARTIAL_STYLE); then mkdir $(PARTIAL_STYLE); fi
	${XSLTPROC} --stringparam rootid $(@F) $(PARTIAL_STYLESHEET) $(MANUAL_STRIPPED)
	$(PHP) -q $(scriptdir)/html_syntax.php $(PARTIAL_STYLE) xsl $(PARTIAL_STYLE)/
# }}}

# {{{ tests

# test all possible errors
test: manual.xml
	@if test ! -f funcindex.xml; then touch funcindex.xml; fi 
	@if test ! -f entities/missing-entities.ent; then touch entities/missing-entities.ent; fi 
	@if test ! -f entities/missing-ids.xml; then touch entities/missing-ids.xml; fi 
	$(NSGMLS) -i lang-$(LANG) -s $(XMLDCL) manual.xml

# ignore missing IDs and check if the manual can be generated anyway
test_man_gen: manual.xml
	@if test ! -f funcindex.xml; then touch funcindex.xml; fi 
	@if test ! -f entities/missing-entities.ent; then touch entities/missing-entities.ent; fi 
	@if test ! -f entities/missing-ids.xml; then touch entities/missing-ids.xml; fi
	$(NSGMLS) -wno-idref -i lang-$(LANG) -s $(XMLDCL) manual.xml

test_xml: $(MANUAL_COMPLETE)
	@if test ! -f funcindex.xml; then touch funcindex.xml; fi 
	@if test ! -f entities/missing-entities.ent; then touch entities/missing-entities.ent; fi 
	@if test ! -f entities/missing-ids.xml; then touch entities/missing-ids.xml; fi
	$(XMLLINT) --noent --noout --valid $(MANUAL_COMPLETE)

test_xsd: $(MANUAL_COMPLETE)
	xmllint --noent --noout --schema docbook/docbook-xml/docbook.xsd $(MANUAL_COMPLETE)

test_rng: $(MANUAL_COMPLETE)
	xmllint --noent --noout --relaxng docbook/docbook-xml/docbook.rng $(MANUAL_COMPLETE)

test_howto:
	$(XMLLINT) --noent --noout --valid ./howto/howto.xml

# }}}

# {{{ cleanup 

clean:
	rm -rf html php fancy howto/html howto/php htmlhelp/html
	rm -f @MANUAL@.txt [a-z]*.html @MANUAL@.rtf manual.info
	rm -f @MANUAL@.zip @MANUAL@.tar.gz $(MANUAL_COMPLETE) $(MANUAL_STRIPPED)
	rm -f manual_*.pdb @srcdir@/scripts/makedoc *.manifest
	rm -f *.aux *.tex *.log *.dvi *.toc *.ps *.gz
	rm -f status.txt missing.txt infiles.cache
	rm -f entities/file-entities.ent
	rm -f entities/missing*
	rm -f @LANGDIR@/missing-ids.xml
	
distclean: clean
	for file in `find . -name "*.in"`; do rm -f `dirname $$file`/`basename $$file .in`; done
	rm -f config.cache config.status missing-entities.out

# }}}
# {{{ tree utils

pull:
	cvs -z3 up -dP $(LANGDIR)

# }}}
