# Makefile for World Wide Wounds docs

# This makefile apparently works best with GNU make
# The GNU m4 preprocessor is the only one that understands the -P
# flag

# removed the ICONV stuff 15-5-2003. Changed the conversion scripts
# to do utf-8 as charset. Not sure what browsers are set up for
# though! Most by default seem to grok utf-8 anyway. PJP

# General Flags - non location dependent
M4 = m4

# iconv filter to convert character sets
#ICONV=iconv -f UTF8 -t ISO-8859-1
ICONV=/share/store/libiconv-1.6.1/linux-gnu/bin/iconv -f UTF-8 -t ISO-8859-1
# flags to allow us to use the same makefile on different machines
host = $(shell hostname)

##### Get full directory pathname
pathdirectory = $(shell pwd)

ifeq ($(host),nemesis)
	  #XPATHSCRIPT=/share/store/perl5.006_01/scripts/xpathscript
	  XPATHSCRIPT=/usr/bin/xpathscript
	  ICONV=/share/store/libiconv-1.6.1/linux-gnu/bin/iconv -f UTF-8 -t ISO-8859-1
	  XMLDECL=/share/store/sp-1.3.4/src/sp-1.3.4/pubtext/xml.dcl
	  XMLSCRIPTS=/data/htdocs/XMLscripts
	  TIDYCONF=/data/htdocs/World-Wide-Wounds-test/templates/tidy-config.txt
	  HTMLBASE= $(subst  /data/htdocs/World-Wide-Wounds-test/,,$(pathdirectory))
	  REFSCONV=$(XMLSCRIPTS)/medline-2-docbook.xps
else #workstation
  XPATHSCRIPT=xpathscript
  ICONV=/usr/bin/iconv -f UTF-8 -t ISO-8859-1
  XMLDECL=/share/store/sp-1.3.4/src/sp-1.3.4/pubtext/xml.dcl
  XMLSCRIPTS=/data/htdocs/XMLscripts
  TIDYCONF=/data/htdocs/World-Wide-Wounds-test/templates/tidy-config.txt
  HTMLBASE= $(subst  /data/htdocs/World-Wide-Wounds-test/,,$(pathdirectory))
  REFSCONV=$(XMLSCRIPTS)/medline-2-docbook.xps
endif

##### Get current directory (cdirectory).
# cdirectory = $(shell basename `pwd`)
cdirectory = $(shell $(XMLSCRIPTS)/filetype.sh `pwd`)

# .SUFFIXES: .xml .html
docs = $(wildcard *.xml)
directories = images original thumbs RCS
IGNORE=docbookrefs.html medlinerefs.html Comments.html Product-News.html  News.html News
htmllist = $(patsubst  %.xml, %.html, $(docs))
htmldocs = $(filter-out $(IGNORE),$(htmllist))
# list of images for thumbs
jpglist = $(wildcard images/*.jpg)
giflist = $(wildcard images/*.gif)
imagelist := $(jpglist) $(giflist)
thumbimages := $(patsubst  images/%, thumbs/%, $(imagelist))

#####
##### Are we dealing with a bunch of Common files ?
##### Hopefully I can merge these together to use a common code base  and
##### eliminate the 2 different drivers.
#####

ifeq ($(cdirectory),Common)
	DRIVER=$(XMLSCRIPTS)/WWW-Common.xps 
endif
ifeq ($(cdirectory),FAQS)
	DRIVER=$(XMLSCRIPTS)/WWW-Common.xps 
endif
ifeq ($(cdirectory),News)
	DRIVER=$(XMLSCRIPTS)/WWW-Common.xps 
endif
ifeq ($(cdirectory),Article)
	DRIVER=$(XMLSCRIPTS)/WWW-xpathscript.xps
endif

# remove base stuff from dir name so we know where we are in the html hierarchy


## If we have to deal with common format files elsewhere,
## then we use CDRIVER
CDRIVER=$(XMLSCRIPTS)/WWW-Common.xps 

# all:  $(thumbs) $(images) $(original)  $(directories) $(htmldocs)
# all:  Larval-Therapy-Human-and-Veterinary.html
all: $(htmldocs)

# Main Rules
#####
#### References - medline refs go into a fiel called medlinerefs.xml
#### and we create a fiel called docbookrefs.xml
docbookrefs.xml: medlinerefs.xml $(REFSCONV)
	cat $< | egrep -v "^<!DOCTYPE" > temp.xml
	$(XPATHSCRIPT)  temp.xml $(REFSCONV) > $@

#	$(XPATHSCRIPT) $< $(REFSCONV) > $@

%.validate: %.xml
	 nsgmls -s $(XMLDECL) $<


# set up our main variables

# Make gif files from eps files and rut

#clean:
#	-rm *.html 


# make these directories
images:
	mkdir images

thumbs:
	mkdir thumbs

tmp:
	mkdir tmp

original:
	mkdir original

RCS:
	mkdir RCS

directories: images thumbs tmp original RCS


###################################
#  Converting XML to HTML
###################################
%.html: %.xml $(DRIVER) $(XMLSCRIPTS)/docbook_tags.xps
	$(XPATHSCRIPT) $< $(DRIVER)    > $@
	chmod 664 $@

# | iconv -f ISO-8859-1 -t UTF-8 

#$(XPATHSCRIPT) $< $(DRIVER) | $(ICONV) > $@

#	 -/share/links/linux-gnu/bin/tidy -config $(TIDYCONF)  -q -m $@

###################################
#  How are Comments.xml and News.xml files dealt with ?
###################################
Comments.html: Comments.xml $(CDRIVER) $(XMLSCRIPTS)/docbook_tags.xps
	$(XPATHSCRIPT) $< $(CDRIVER)  > $@
	chmod 664 $@

News%.html: News.xml $(CDRIVER) $(XMLSCRIPTS)/docbook_tags.xps
	$(XPATHSCRIPT) $< $(CDRIVER)  > $@
	chmod 664 $@

#News.html: News.xml $(CDRIVER) $(XMLSCRIPTS)/docbook_tags.xps
#	$(XPATHSCRIPT) $< $(CDRIVER)  > $@
#	chmod 664 $@

Product-News%.html: Product-News.xml $(CDRIVER) $(XMLSCRIPTS)/docbook_tags.xps
	$(XPATHSCRIPT) $< $(CDRIVER)  > $@
	chmod 664 $@


#	 -/share/links/linux-gnu/bin/tidy $(TIDYCONF) -q -m $@

# Stuff we only find in the Common directory

indexes: ArticleIndex.html Topics.html

#ArticleIndex.xml: article-src.xml $(XMLSCRIPTS)/articlelist-to-alpha.xps
#		$(XPATHSCRIPT) $< $(XMLSCRIPTS)/articlelist-to-alpha.xps > $@
#		chmod o+r $@
ArticleIndex.xml: article-src.xml $(XMLSCRIPTS)/articlelist-to-alpha.xps
		$(XPATHSCRIPT) article-src.xml $(XMLSCRIPTS)/articlelist-to-alpha.xps > $@
		chmod o+r $@

Topics.xml: article-src.xml $(XMLSCRIPTS)/articlelist-to-topic.xps
		$(XPATHSCRIPT) article-src.xml $(XMLSCRIPTS)/articlelist-to-topic.xps > $@
		chmod o+r $@




###################################
#  Image stuff
###################################
thumbs/%.gif : images/%.gif
	# giftopnm < $< | pnmscale 0.25 |ppmquant 256 |  ppmtogif -interlace > $@
	giftopnm < $< | pnmscale -width 188 |ppmquant 256 |  ppmtogif -interlace > $@
	chmod o+r $@

thumbs/%.jpg : images/%.jpg
	djpeg -pnm < $< | pnmscale -width 188 | cjpeg -quality 75 > $@
	chmod o+r $@

thumbimages: $(thumbimages)
##################################
# Converting to PS and PDF
##################################
%.ps: %.html
	html2ps -D  http://test-worldwidewounds.smtl.co.uk/$(HTMLBASE)/$< > $@

%.pdf: %.ps
	ps2pdf  $<  $@

