# Makefile for documentation
# 
# See README-authors for more information.

.SUFFIXES:

-include ../config
-include Makefile.docs

OFFICIALWEBBUILD := $(official_debian)

# whether documentation should be installed in subdirs;
# see install-langs target
INSTALLSTYLE	?= subdir

# fdisk documentation to include (varies per platform)
FDISK_DOCS := $(shell grep -s fdisk \
		../scripts/rootdisk/SMALL_BASE_LIST_$(architecture) | \
		sed -e 's/^.*\/\(.*\)/\1.txt/' 2>/dev/null)

# set extension of source/dest files based on LINGUA
ifeq ($(LINGUA),C)
SRCEXT		:=
DESTEXT		:= .en
DESTDIRADD	:=
else
SRCEXT		:= .$(LINGUA)
DESTEXT		:= .$(LINGUA)
DESTDIRADD	:= $(LINGUA)
endif

# programs for creating output
DEBIANDOC2HTML	:= debiandoc2html -d debiandoc.decl -l $(LINGUA) -c
DEBIANDOC2PS	:= debiandoc2ps -d debiandoc.decl -l $(LINGUA)
DEBIANDOC2TEXT	:= debiandoc2text -d debiandoc.decl -l $(LINGUA)
DEBIANDOC2LATEX	:= debiandoc2latex -d debiandoc.decl -l $(LINGUA)

# detailed sgml validation
NSGMLS		:= nsgmls -ges -wall /usr/lib/sgml/declaration/debiandoc.decl

# maximum times we're willing to run TeX to get cross-references right, if you subtract by 3
MAX_TEX_RECURSION	:= 6

# documents to build
TARGETDOCS := index.en.html			\
	      install$(DESTEXT).txt		\
	      install$(SRCEXT).html/install$(DESTEXT).html \
	      $(FDISK_DOCS)

# some languages don't have dselect-beginner -- that's ok
ifneq ($(LINGUA),fi)
TARGETDOCS += dselect-beginner$(SRCEXT).html/dselect-beginner$(DESTEXT).html \
	      dselect-beginner$(DESTEXT).txt
endif

# you might want to remove this if it is failing for you
#   ja and ru documents are currently not converted into .pdf correctly 
ifneq ($(LINGUA),ja)
ifneq ($(LINGUA),ru)
TARGETDOCS	+= install$(DESTEXT).pdf
endif
endif

EXTRAFILES	:= officiallogo-nd-50.jpg openlogo-nd-50.jpg

# just an unreasonable default
DESTDIR		:= /tmp

install_file	:= install -m 644 -p
makedir		:= mkdir -m 0755
ifdef OFFICIALWEBBUILD
install_file	:= install -m 2664 -p
makedir		:= mkdir -m 2775
endif

# how the date is represented in the document
DATEFORM_$(LINGUA) := %d %B, %Y		# default date representation
DATEFORM_fi	:= %-d.%-m.%Y
DATEFORM_hr	:= %-d. %-m. %Y.	# hr is an exception from the above rule

DEB_VERSION	:= $(shell cd .. && LC_ALL=C dpkg-parsechangelog |	\
			grep '^Version: ' | sed 's/^Version: *//')
DATE		:= $(shell cd .. && date --date="`dpkg-parsechangelog |	\
			grep '^Date: ' | sed 's/^Date: *//'`" 		\
			'+$(strip $(DATEFORM_$(LINGUA)))')

# derived entity files
DERIVED_ENTS	:= messages.ent dynamic.ent
# this for is used for dependencies
ENT_DEPENDS	:= defaults.ent messages.ent dynamic.ent

define checklingua
  if [ -z "$(LINGUA)" ]; then \
  echo LINGUA variable is not set -- see documentation/README-authors; \
  exit 1; \
  fi
endef

# actual rules start here
# (note: default rule is the default one from Makefile.docs, not this one)
.PHONY:	all
all:	supplemental_makefile $(TARGETDOCS) $(EXTRATARGETDOCS)

.PHONY: lint
lint:	$(ENT_DEPENDS)
ifneq "$(wildcard install$(SRCEXT).sgml)" ""
	@echo "Verifying SGML validity for Installation Manual, language $(LINGUA):"
	$(NSGMLS) install$(SRCEXT).sgml
#	@echo "Checking for deprecated entity references:"
#	nsgmls -oentity /usr/lib/sgml/declaration/debiandoc.decl \
#	  install$(SRCEXT).sgml 2>/dev/null | sed -e '/^IMSG/!d' -e 's/^I/   /'
	# po2sgml:epilogue sets up some those references
endif
ifneq "$(wildcard dselect-beginner$(SRCEXT).sgml)" ""
	@echo "Verifying SGML validity for dselect Documentation for Beginners, language $(LINGUA):"
	$(NSGMLS) dselect-beginner$(SRCEXT).sgml
endif

.PHONY:	validate
validate:	$(ENT_DEPENDS)
	@set -ex; for i in *.sgml; do $(NSGMLS) $$i; done

.PHONY:	supplemental_makefile
supplemental_makefile:
	@$(checklingua)
	@[ ! -f $(LINGUA)/Makefile ] || $(MAKE) -C $(LINGUA)

# generate the nice index page.
# FIXME: is m4 really the right way to do this?
ifneq "$(wildcard index$(DESTEXT).html.m4)" ""
index$(DESTEXT).html: index$(DESTEXT).html.m4; m4 \
 --define=ARCHITECTURE=${architecture} \
 --define=OFFICIAL=${OFFICIALWEBBUILD} \
    $< > $@
else
.PHONY: index$(DESTEXT).html index.en.html
endif

# this will probably only work if your name is "Adam Di Carlo"
.PHONY: ChangeLog
ChangeLog:
	cvs2cl.pl --tags --branches --revisions --utc	\
	  --usermap $$HOME/debian/boot-floppies/CVSROOT/users

# generic rules for all the languages and output formats -- Installation Manual.
ifneq "$(wildcard install$(SRCEXT).sgml)" ""
install$(SRCEXT).html/install$(DESTEXT).html:	install$(SRCEXT).sgml	\
						$(ENT_DEPENDS)
	$(DEBIANDOC2HTML) -t install $<

install$(DESTEXT).txt:	install$(SRCEXT).sgml $(ENT_DEPENDS)
	$(DEBIANDOC2TEXT) -O $< > $@

install$(DESTEXT).ps:	install$(SRCEXT).sgml $(ENT_DEPENDS)
	$(DEBIANDOC2PS) -O $< > $@

install$(DESTEXT).tex:	install$(SRCEXT).sgml $(ENT_DEPENDS)
	$(DEBIANDOC2LATEX) -O $< > $@
else
.PHONY: install$(SRCEXT).html/install$(DESTEXT).html install$(DESTEXT).txt \
        install$(DESTEXT).ps install$(DESTEXT).tex install$(DESTEXT).pdf
endif

# generic rule for all the PDFs
%.pdf:	%.tex
#	 note that I have seen bi-stable .aux files, thus we check two levels deep
	-cp -pf prior.aux pprior.aux 2>/dev/null
	-cp -pf $(basename $<).aux prior.aux 2>/dev/null
#	 fail if we don't have pdflatex correctly installed
	kpsewhich pdflatex.fmt >/dev/null
#	 due to a bug in debiandoc2latex2e output, this might fail
	-pdflatex '\nonstopmode\input{$<}' >/dev/null # see $(@:.pdf=.log) for details
	@set -e								;\
	if ! cmp $(basename $<).aux prior.aux 2>/dev/null &&		\
	   ! cmp $(basename $<).aux pprior.aux 2>/dev/null; then	\
		if expr $(MAKELEVEL) '<' $(MAX_TEX_RECURSION) >/dev/null;\
		then							\
		   echo "remaking $@ (take $(MAKELEVEL))"		;\
		   rm -f $@						;\
		   $(MAKE) $@						;\
		else							\
		   echo "E: $@ needs remaking, but we have reached max. level, $(MAX_TEX_RECURSION)" ;\
		fi							\
	fi
	rm -f prior.aux pprior.aux

# generic rules for all the languages and output formats -- dselect Documentation for Beginners
ifneq "$(wildcard dselect-beginner$(SRCEXT).sgml)" ""
dselect-beginner$(SRCEXT).html/dselect-beginner$(DESTEXT).html:		\
		dselect-beginner$(SRCEXT).sgml $(ENT_DEPENDS)
	$(DEBIANDOC2HTML) -t dselect-beginner $<

dselect-beginner$(DESTEXT).txt:	dselect-beginner$(SRCEXT).sgml $(ENT_DEPENDS)
	$(DEBIANDOC2TEXT) -O $< > $@

dselect-beginner$(DESTEXT).ps:	dselect-beginner$(SRCEXT).sgml $(ENT_DEPENDS)
	$(DEBIANDOC2PS) $<

dselect-beginner$(DESTEXT).tex:	dselect-beginner$(SRCEXT).sgml $(ENT_DEPENDS)
	$(DEBIANDOC2LATEX) -O $< > $@
else
.PHONY: dselect-beginner$(SRCEXT).html/dselect-beginner$(DESTEXT).html \
        dselect-beginner$(DESTEXT).txt dselect-beginner$(DESTEXT).ps \
        dselect-beginner$(DESTEXT).tex dselect-beginner$(DESTEXT).pdf
endif

# these rules generate text files out of *fdisk programs' manpages
cfdisk.txt:
	if [ -f "$(utillinux)" ]; then					 \
	  dpkg --fsys-tarfile $(utillinux) |				 \
		tar -xOf - '*/man8/cfdisk.8.gz' | zcat > tmp		;\
	  man -l tmp | col -b > $@					;\
	  rm -f tmp							;\
	fi
	[ -f $@ ] || echo '** man page not found ** ' > $@

fdisk.txt:
	if [ -f "$(utillinux)" ]; then					 \
	  dpkg --fsys-tarfile $(utillinux) |				 \
		tar -xOf - '*/man8/fdisk.8.gz' | zcat > tmp		;\
	  man -l tmp | col -b > $@					;\
	  rm -f tmp							;\
	fi
	[ -f $@ ] || echo '** man page not found ** ' > $@

atari-fdisk.txt:
	if [ -f "$(atarifdisk)" ]; then					 \
	  dpkg --fsys-tarfile $(atarifdisk) |				 \
		tar -xOf - '*/man8/atari-fdisk.8.gz' | zcat > tmp	;\
	  man -l tmp | col -b > $@					;\
	  rm -f tmp							;\
	fi
	[ -f $@ ] || echo '** man page not found ** ' > $@

amiga-fdisk.txt:
	if [ -f "$(amigafdisk)" ]; then					 \
	  dpkg --fsys-tarfile $(amigafdisk) |				 \
		tar -xOf - '*/man8/amiga-fdisk.8.gz' | zcat > tmp	;\
	  man -l tmp | col -b > $@					;\
	  rm -f tmp							;\
	fi
	[ -f $@ ] || echo '** man page not found ** ' > $@

mac-fdisk.txt:
	if [ -f "$(macfdisk)" ]; then					 \
	  dpkg --fsys-tarfile $(macfdisk) |				 \
		tar -xOf - '*/man8/mac-fdisk.8.gz'  | zcat > tmp	;\
	  man -l tmp | col -b > $@					;\
	  rm -f tmp							;\
	fi
	[ -f $@ ] || echo '** man page not found ** ' > $@

pmac-fdisk.txt:
	if [ -f "$(pmacfdiskcross)" ]; then				 \
	  dpkg --fsys-tarfile $(pmacfdiskcross) |			 \
		tar -xOf - '*/man8/pmac-fdisk.8.gz' | zcat > tmp	;\
	  man -l tmp | col -b > $@					;\
	  rm -f tmp							;\
	fi
	[ -f $@ ] || echo '** man page not found ** ' > $@

.PHONY: install
install:
	set -e								;\
	for lang in $(buildable-langs); do				 \
	    echo "installing $$lang"					;\
	    $(MAKE) LINGUA=$$lang install-lang				;\
	done
ifdef OFFICIALWEBBUILD
#	setting our nice content-negotiation symlinks
	set -e								;\
	for file in `find arch/$(architecture) -name '*.en.*'`; do	 \
	    ln -sf `basename $$file` `echo $$file | sed -e 's/\.en//'`	;\
	done
endif

.PHONY:	install-lang
install-lang:
	[ -d $(DESTDIR) ] || $(makedir) -p $(DESTDIR)
ifneq ($(INSTALLSTYLE),subdir)
#	install all langs files into one directory
	$(install_file) $(TARGETDOCS) $(EXTRAFILES)			 \
		$(wildcard dselect-beginner$(SRCEXT).html/*		 \
		install$(SRCEXT).html/*) $(DESTDIR)
else		# subdirectory install, to make things easier to find
	for doc in $(TARGETDOCS) $(EXTRAFILES)				 \
		$(wildcard dselect-beginner$(SRCEXT).html/* 		 \
		install$(SRCEXT).html/*); do				 \
	    $(makedir) -p $(DESTDIR)/$(DESTDIRADD)				;\
	    $(install_file) $$doc $(DESTDIR)/$(DESTDIRADD)		;\
	done
#	make index.html symlink to index.??.html for all subdirs
	set -e								;\
	for file in `find $(DESTDIR) -name 'index.??.html'`; do		 \
	    ln -sf `basename $$file`					 \
		   `echo $$file | sed -e 's/\...\.html/.html/'`		;\
	done
endif

.PHONY:	clean
clean:
	rm -rf arch
	rm -f $(DERIVED_ENTS) dynamic_*.ent messages*.ent
	rm -rf $(TARGETDOCS) install*.html dselect-beginner*.html
	rm -f *fdisk.txt dselect-beginner*.txt install*.txt
	rm -rf release-notes*.html release-notes*.txt
	rm -rf upgrade-*
	rm -f *.log *.aux *.bak *.out *.sasp* *.toc *.tex *.dvi *.pdf *.ps
	rm -f relnotes.tar.bz2
	set -e ; for dir in `find . -type d -mindepth 1 -maxdepth 1`; do \
	    [ ! -f $$dir/Makefile ] || $(MAKE) -C "$$dir" $@		;\
	done

.PHONY:	distclean
distclean: clean
	rm -f *~ *.orig \#*

##
## upload docs to http://www.debian.org/releases/...
##
# this will only work if you're in debwww group on localhost and on master
mirror-to-master:
	@[ -d arch ] || { echo "Run make (uni-)doc-web first!"; exit 1; }
	chgrp -R debwww arch
	find arch -type f | xargs chmod 0664
	find arch -type d | xargs chmod g+sw
	rsync -rltDpgvz --rsh ssh arch/		\
	  www-master.debian.org:/org/www.debian.org/debian.org/releases/$(debname)/


# generate entities which are dynamically defined by the settings of this
# build run, generally set in the top-level config file
dynamic_$(LINGUA)_$(architecture).ent: ../debian/changelog
ifeq "$(architecture)" ""
	@echo architecture variable is not set -- see documentation/README-authors
	exit 1
endif
ifeq "$(findstring $(architecture), $(arches))" ""
	@echo architecture variable is set to something strange, edit ../config
	exit 1
endif
	: > $@			# clear the file
	echo "<!entity docversion       \"$(DEB_VERSION)\">" >> $@
	echo "<!entity docdate          \"$(DATE)\">" >> $@
	echo "<!entity language         \"$(LINGUA)\">" >> $@
	echo "<!entity langext          \"$(DESTEXT)\">" >> $@
	echo "<!entity release          \"$(debianversion)\">" >> $@
	echo "<!entity releasename      \"$(debname)\">" >> $@
	echo "<!entity kernelversion    \"$(kver)\">" >> $@
	echo "<!entity altkernelversion \"$(altkver)\">" >> $@
	echo "<!entity architecture     \"$(architecture)\">" >> $@
	echo "<!entity % $(architecture) \"INCLUDE\">" >> $@
	echo "<!entity % lang-$(LINGUA) \"INCLUDE\">" >> $@
	set -e; for i in $(FDISK_DOCS); do			    \
		echo "<!entity % $$i    \"INCLUDE\">" >> $@;\
	done
ifdef OFFICIALWEBBUILD
	echo "<!entity % official-web-build \"INCLUDE\">" >> $@
	echo "<!entity path-to-top-level \"\">" >> $@
else
	echo "<!entity % official-web-build \"IGNORE\">" >> $@
# figure out how we get to the top level, for downloadable-files
# entity in defaults.ent; non-English goes into a subdir in docs so we
# need to allow for that
ifeq ($(INSTALLSTYLE),subdir)
ifeq ($(LINGUA),C)
	echo "<!entity path-to-top-level \"../\">" >> $@
else
	echo "<!entity path-to-top-level \"../../\">" >> $@
endif
else
	echo "<!entity path-to-top-level \"\">" >> $@
endif
endif

.PHONY: dynamic.ent
dynamic.ent:	dynamic_$(LINGUA)_$(architecture).ent
	rm -f $@
	ln -s $< $@

# in case LINGUA isn't set, give a decent err message
$(DBOOTSTRAP_DIR)/po/.po:
	@$(checklingua)

# unlike its translations, C.po isn't static, it's autogenerated
$(DBOOTSTRAP_DIR)/po/C.po:
	$(MAKE) -C $(DBOOTSTRAP_DIR)/po C.po

# entities which are dynamically defined from the dbootstrap messages
messages_$(LINGUA).ent:	$(DBOOTSTRAP_DIR)/po/$(LINGUA).po po2sgml
	./po2sgml $(DBOOTSTRAP_DIR)/po/$(LINGUA).po > $@

.PHONY: messages.ent
messages.ent:	messages_$(LINGUA).ent
	rm -f $@
	ln -s $< $@

# make "flavored" messages.ent files for all known languages
.PHONY:	all-messages.ent
all-messages.ent:
	set -e							;\
	for lang in $(buildable-langs); do			 \
	  $(MAKE) LINGUA=$$lang messages_$$lang.ent		;\
	  ./po2sgml $$file > messages_$$lang.ent		;\
	done

# Following stuff is used to build the Release Notes

ifneq "$(wildcard release-notes$(SRCEXT).sgml)" ""
release-notes$(SRCEXT).html/index$(DESTEXT).html: release-notes$(SRCEXT).sgml \
                                                  defaults.ent urls.ent dynamic.ent
	$(DEBIANDOC2HTML) $<

release-notes$(DESTEXT).txt: release-notes$(SRCEXT).sgml \
                             defaults.ent urls.ent dynamic.ent
	$(DEBIANDOC2TEXT) -O $< > $@
else
.PHONY: release-notes$(SRCEXT).html/index$(DESTEXT).html release-notes$(DESTEXT).txt
endif

.PHONY:	release-notes-html release-notes-text

release-notes-html: release-notes$(SRCEXT).html/index$(DESTEXT).html

release-notes-text: release-notes$(DESTEXT).txt
