1# Make Unix man pages from Netpbm HTML user manual
2# GNU make version 3.81 or newer recommended.
3# Tested with GNU make version 3.80.
4
5# CAVEAT: USERGUIDE must be a valid directory: even for "make clean"!
6
7# MAKEFILE_DIR is the directory with this file: manpage.mk.
8# Should be buildtools.
9# Use $(realpath) and $(lastword) if available.
10# (both appear in GNU make v. 3.81)
11
12ifeq ($(realpath $(CURDIR)/.),$(CURDIR))
13  MAKEFILE_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
14else
15  MAKEFILE_DIR := $(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
16endif
17
18# Program 'makeman' should be in the same directory.
19MAKEMAN ?= $(MAKEFILE_DIR)makeman
20
21# Install location of manpages.
22# Subdirectories man{1,3,5} must exist.
23MANDIR ?= /usr/share/man/
24
25# Directory with the HTML input files.  It must be explicitly set and
26# must be a valid directory.
27
28ifeq ($(USERGUIDE),)
29  $(error error: Variable USERGUIDE must be explicitly set)
30else
31  ifeq ($(wildcard $(USERGUIDE)/*html),)
32    $(error error: No HTML files found in $(USERGUIDE))
33  endif
34endif
35
36# In the past, the following default value was used.  It works if you've done
37# a Subversion checkout for source code and userguide in the same directory,
38# and you are working in a subdirectory of netpbm, say ./buildtools .
39# USERGUIDE = ../../userguide
40
41# The files that don't get converted to manual pages.
42# Override at the command line if necessary.
43
44# error.html: appears when problem occurs when fetching HTML files with wget.
45# directory.html: list of Netpbm programs.
46# libnetpbm_dir.html: directory to pages describing libnetpbm functions
47# hpcdtoppm:  Not distributed via Sourceforge for copyright restrictions.
48# ppmsvgalib: Not used in systems with X Window System.
49# vidtoppm:   Does not compile due to missing header files.
50
51EXCEPTIONS = \
52	directory.html \
53	error.html \
54	hpcdtoppm.html \
55	liberror.html \
56	libnetpbm_dir.html \
57	ppmsvgalib.html \
58	vidtoppm.html
59
60# File lists
61
62# We do not provide a list of troff manpages to be generated.
63# Instead the list is generated afresh from HTML file names.  Reasons:
64# 1. Any list would have to be updated every time an HTML file is added.
65# 2. The suffix (man section) depends on content (a "META" tag) of the
66#    HTML file.  (The makeman script is clever.)
67# 3. In one instance the file stem name changes: index.html => netpbm.1
68
69# HTML files in USERGUIDE
70HTML_ALL := $(sort $(notdir $(wildcard $(USERGUIDE)/*.html)))
71HTMLMANUALS := $(filter-out $(EXCEPTIONS),$(HTML_ALL))
72HTML_REJECT := $(filter $(EXCEPTIONS),$(HTML_ALL))
73
74# Subsets of HTMLMANUALS, by target man section
75HTML3 := $(shell cd $(USERGUIDE) && \
76                fgrep -l -i '<META NAME="manual_section" CONTENT="3">' \
77                      $(HTMLMANUALS))
78HTML5 := $(shell cd $(USERGUIDE) && \
79                fgrep -l -i '<META NAME="manual_section" CONTENT="5">' \
80                      $(HTMLMANUALS))
81HTML1 := $(filter-out $(HTML3) $(HTML5),$(HTMLMANUALS))
82
83# Troff man pages, by section
84MAN1 := $(patsubst index.1,netpbm.1,$(HTML1:.html=.1))
85MAN3 := $(HTML3:.html=.3)
86MAN5 := $(HTML5:.html=.5)
87MANPAGES := $(MAN1) $(MAN3) $(MAN5)
88
89# XML
90XML1 := $(MAN1:.1=.xml)
91XML3 := $(MAN3:.3=.xml)
92XML5 := $(MAN5:.5=.xml)
93XMLPAGES = $(XML1) $(XML3) $(XML5)
94
95.PHONY : report
96report: htmlcount manpagecount
97
98.PHONY : manpagecount
99manpagecount:
100	@echo Number of actual / expected troff man pages in current directory:
101	@echo Section 1: $(words $(wildcard $(MAN1))) / $(words $(MAN1))
102	@echo Section 3: $(words $(wildcard $(MAN3))) / $(words $(MAN3))
103	@echo Section 5: $(words $(wildcard $(MAN5))) / $(words $(MAN5))
104	@echo total: $(words $(wildcard $(MANPAGES))) / $(words $(MANPAGES))
105	@echo
106
107.PHONY : htmlcount
108htmlcount:
109	@echo HTML files in USERGUIDE directory: $(USERGUIDE)
110	@echo Total HTML files: $(words $(HTML_ALL))
111	@echo Rejected HTML files: $(HTML_REJECT) : $(words $(HTML_REJECT))
112	@echo Valid HTML files: $(words $(HTMLMANUALS))
113	@echo
114
115.PHONY : reportvalid
116reportvalid:
117	@echo Source HTML files in USERGUIDE directory: $(USERGUIDE)
118	@echo $(HTMLMANUALS)
119
120# Note that this may give different results from "ls ."
121.PHONY : reportman
122reportman:
123	@echo $(MANPAGES)
124
125# Static rules for converting HTML to troff man -- reports bad lines
126# to standard error.
127%.1 %.3 %.5: $(USERGUIDE)/%.html
128	@echo Converting $< to $@
129	@python $(MAKEMAN) -d $(USERGUIDE) $(<F)
130
131netpbm.1: $(USERGUIDE)/index.html
132	@echo Converting $< to $@
133	@python $(MAKEMAN) -d $(USERGUIDE) index.html
134	@mv index.1 netpbm.1
135
136# Generate man pages
137.PHONY : manpages
138manpages: $(MANPAGES)
139
140# Static rules for converting troff man to XML.
141$(XML1): %.xml: %.1
142	doclifter -v $<
143	mv $<.xml $@
144
145$(XML3): %.xml: %.3
146	doclifter -v $<
147	mv $<.xml $@
148
149$(XML5): %.xml: %.5
150	doclifter -v $<
151	mv $<.xml $@
152
153# Generate XML pages.
154# TODO: Does not work completely.  Some pages have glitches.
155.PHONY : xmlpages
156xmlpages: manpages $(XMLPAGES)
157
158# Validate XML pages.
159# TODO: Not working.
160.PHONY : xmlvalidate
161xmlvalidate: xmlpages
162	xmllint -xinclude --postvalid $< >> /dev/null
163
164# This will install the generated man pages.
165# Note that lists MAN1 MAN3 MAN5 depend upon the names of HTML files
166# in the USERGUIDE directory, even after man page generation.
167# If the current directory has "pbm.1" but USERGUIDE does not have
168# "pbm.html", the document will not be installed.
169# If the USERGUIDE directory is empty, no documents will be installed.
170
171.PHONY : installman
172installman: report
173	set -x
174	for f in $(wildcard $(MAN1)); do if [ -f $$f ]; then gzip <$$f >$(MANDIR)/man1/$$f.gz; fi; done
175	for f in $(wildcard $(MAN3)); do if [ -f $$f ]; then gzip <$$f >$(MANDIR)/man3/$$f.gz; fi; done
176	for f in $(wildcard $(MAN5)); do if [ -f $$f ]; then gzip <$$f >$(MANDIR)/man5/$$f.gz; fi; done
177
178
179# This will uninstall the man pages.
180# Only pages with corresponding files in USERGUIDE are deleted.
181.PHONY : uninstallman
182uninstallman: report
183	for f in $(MAN1); do if [ -f $(MANDIR)/man1/$$f.gz ]; then rm -f $(MANDIR)/man1/$$f.gz; fi; done
184	for f in $(MAN3); do if [ -f $(MANDIR)/man3/$$f.gz ]; then rm -f $(MANDIR)/man3/$$f.gz; fi; done
185	for f in $(MAN5); do if [ -f $(MANDIR)/man5/$$f.gz ]; then rm -f $(MANDIR)/man5/$$f.gz; fi; done
186
187
188# Legacy uninstall target.
189#oldclean:
190#	# Clean up old locations on Fedora Core 2
191#	rm -f $(MANDIR)/man1/extendedopacity.1.gz
192#	rm -f $(MANDIR)/man3/directory.3.gz
193#	rm -f $(MANDIR)/man3/libnetpbm_dir.3.gz
194#	# remove pointer man pages (that say, "The man page isn't here")
195#	# which might have been installed previously
196#	for f in $(MAN1); do rm -f $(MANDIR)/man1/$$f; done
197#	for f in $(MAN3); do rm -f $(MANDIR)/man3/$$f; done
198#	for f in $(MAN5); do rm -f $(MANDIR)/man5/$$f; done
199
200
201.PHONY: clean
202clean:
203	  @rm -f *.[135] $(XML)
204