# Makefile for documentation
# 
# Generally called from the top level Makefile with 'make docs'
#
# If you want to run this manually, you'll have to set your variable
# properly:  'make KVER=2.0.35 LINGUA=C RELEASE=2.1 DEB_VERSION=test \
#		DATE="`date`" architecture=x86'

# documents to build
TARGETDOCS	:= install.txt install.html dselect.beginner.txt cfdisk.txt
# just an unreasonable default
DESTDIR		:= /tmp

install_file	:= install -m 644 -p

# needed for dynamic.ent target
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: *//'`" '+%d %B, %Y')

DINSTALL_DIR	:= ../utilities/dinstall

# derived entity files
DERIVED_ENTS	:= messages.ent dynamic.ent

all:	validate $(TARGETDOCS)

all-arches:
	rm -rf arch
	mkdir arch
	set -e								;\
	for arch in `grep IGNORE defaults.ent  | cut -d' ' -f 3`; do	\
		rm -f dynamic.ent install*.html	cfdisk.txt		\
			dselect.beginner.txt dselect.beginner.html	;\
		$(MAKE) KVER=$(KVER) RELEASE=$(RELEASE)			\
			architecture=$$arch all				;\
		[ -d arch/$$arch ] || mkdir arch/$$arch			;\
		$(install_file) install*.html install.txt cfdisk.txt	\
			dselect.beginner.txt dselect.beginner.html	\
			arch/$$arch/					;\
	done
	[ -d arch/source ] || mkdir arch/source
	$(install_file) install.sgml *.ent arch/source

install.html:	install.sgml
	sgml2html $^

install.txt:	install.sgml
	sgml2txt --filter $^

dselect.beginner.html: dselect.beginner.8.in.html
	sed -e '' <$^ >$@

dselect.beginner.txt:	dselect.beginner.html
	lynx -dump $^ > $@

cfdisk.txt:
ifeq ($(architecture), i386)
	man cfdisk | col -b >cfdisk.tmp
else
	echo "Your arch does not support cfdisk" >cfdisk.tmp
endif
	set -e					;\
	if cmp -s cfdisk.tmp $@; then		\
		rm -f cfdisk.tmp		;\
	else 					\
		mv -f cfdisk.tmp $@		;\
	fi

install:	all
	$(install_file) $(TARGETDOCS) $(wildcard install-*.html) $(DESTDIR)

clean:
	rm -rf arch
	rm -f $(DERIVED_ENTS)
	rm -f $(TARGETDOCS) install-*.html dselect.beginner.html
	rm -f *~

distclean: clean

.PHONY:	validate
validate:	$(DERIVED_ENTS)
	nsgmls -wall -s -E20 install.sgml
	sgmlcheck install.sgml

dynamic.ent: ../debian/changelog Makefile
#	entities which are dynamically defined by the settings of this build
#	run, generally set in the top-level makefile
ifeq ($(architecture),)
	@echo "error: invoke '$(MAKE) docs' from top level Makefile"
	exit 1
endif
	: > $@			# clear the file
	echo "<!entity docversion    \"$(DEB_VERSION)\">" >> $@
	echo "<!entity docdate       \"$(DATE)\">" >> $@
	echo "<!entity language      \"$(LINGUA)\">" >> $@
	echo "<!entity release       \"$(RELEASE)\">" >> $@
	echo "<!entity kernelversion \"$(KVER)\">" >> $@
	echo "<!entity architecture  \"$(architecture)\">" >> $@
	echo "<!entity % $(architecture) \"INCLUDE\">" >> $@

messages.ent:	$(DINSTALL_DIR)/messages/lang_$(LINGUA).h cpp2sgml
#	entities which are dynamically defined from the dinstall messages
	./cpp2sgml $(DINSTALL_DIR)/messages/lang_$(LINGUA).h > $@

# in case LANG isn't set, give a decent err message
$(DINSTALL_DIR)/messages/lang_.h:
	@echo LINGUA is not set -- see the documentation at the top of this Makefile
	exit 1

install.sgml: defaults.ent $(DERIVED_ENTS)

