# Makefile for DataCards

# 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
#location = $(shell dnsdomainname)
#host = $(shell hostname)
cwd = $(shell pwd)
dirname = $(shell basename $(cwd)

##ifeq ($(location),smtl.co.uk)
##ifeq ($(host),lap1)
##  XPATHSCRIPT=/usr/bin/xpathscript
##  ICONV=/usr/bin/iconv -t UTF-8 -f ISO-8859-1
  # XMLDECL=/usr/lib/sgml/stylesheets/nwalsh-modular/dtds/decls/xml.dcl
##  XMLDECL=/usr/share/sgml/opensp/xml.dcl
##  XMLSCRIPTS=/home/httpd/html/XMLscripts
##  ENTITYFIX=/home/pete/SMTL/Datacards/Datacards/XML/fix-entities.pl
##  CATALOG=/etc/sgml/catalog
#else
 # XPATHSCRIPT=/share/perl/scripts/xpathscript
   XPATHSCRIPT=/usr/bin/xpathscript
   ICONV=/usr/bin/iconv -f UTF-8 -t ISO8859-1
#  ICONV=cat
  #ICONV=/share/store/libiconv-1.6.1/linux-gnu/bin/iconv -t UTF-8 -f ISO-8859-1
  # for some reason iconv not needed on ds9 !!! it does the right
  # thing
  XMLDECL=/share/store/sp-1.3.4/src/sp-1.3.4/pubtext/xml.dcl
  XMLSCRIPTS=/data/htdocs/XMLscripts
  CATALOG=/share/store/sgml-1.0/lib/sgml/CATALOG
  ENTITYFIX=/data/htdocs/Dressings.org/XML/fix-entities.pl
#endif
#else
#  XPATHSCRIPT=/usr/bin/xpathscript
#  XMLDECL=/d2/src/SP/sp-1.3.4/pubtext/xml.dcl
#  XMLSCRIPTS=/zip/XMLscripts
#endif


# .SUFFIXES: .xml .html
docs = $(wildcard *.xml)
directories = images original thumbs RCS

IGNORE=docbookrefs.html medlinerefs.html Comments.html Product-News.html News.html
htmllist = $(patsubst  %.xml, %.html, $(docs))
htmldocs = $(filter-out $(IGNORE),$(htmllist))
psdocs = $(patsubst  %.html, %.ps, $(htmldocs))
pdfdocs = $(patsubst  %.ps, %.pdf, $(psdocs))



#####
##### 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.
#####
##### Get current directory (cdirectory).
cdirectory = $(shell basename `pwd`)

ifeq ($(cdirectory),Common)
	DRIVER=$(XMLSCRIPTS)/dressdatacard.xps 
else
	DRIVER=$(XMLSCRIPTS)/dressdatacard.xps
endif


error: 
	echo "DO NOT JUST TYPE MAKE - TYPE THE NAME OF THE HTML FILE YOU NEED TO MAKE!!!!!"

all: $(htmldocs)

pdf: $(pdfdocs)

ps: $(psdocs)

# Main Rules

%.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




###################################
#  Converting XML to HTML
###################################
# normalise
%.norm: %.xml 
	 /share/sp/linux/bin/spam -c $(CATALOG) -x -x  $(XMLDECL) $<   > $@

# fix entities
%.fix: %.norm
	cat $< | $(ENTITYFIX) > $@

%.html: %.fix $(DRIVER) $(XMLSCRIPTS)/dressdatacards_tags.xps
	$(XPATHSCRIPT) $(cwd)/$< $(DRIVER)  > $@
	# $(XPATHSCRIPT) $(cwd)/$< $(DRIVER) | $(ICONV) > $@
	chmod 664 $@

# used to use this - now leaving out for UNICODE
#	$(XPATHSCRIPT) $(cwd)/$< $(DRIVER) | $(ICONV) > $@
#	$(XPATHSCRIPT) $(cwd)/$< $(DRIVER) > $@


##################################
# Converting to PS and PDF
##################################
%.ps: %.html
	html2ps -D  http://test-dressings.smtl.co.uk/Dressings/$< > $@

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


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

thumbs/%.jpg : images/%.jpg
	convert $< -resize 188 $@
	chmod o+r $@

###########################
## Make Core directories
###########################

dirs: $(directories)


###########################
# Promote datacard to /data/htdocs/Dressings
# #########################

promote: 
	ls -1tr *.xml|tail -n1 >.xmlfile
	stat -c "%U" `cat .xmlfile` > .owner 
	/share/store/scripts-hugh/rcs-ci `cat .xmlfile` "Promoting Datacard"
	make `cat .xmlfile|sed -e's/.xml/.html/'`
	chown `cat .owner`:users *.xml *.html
	cp *.xml *.html /data/htdocs/Dressings.org/Dressings
	cp images/* /data/htdocs/Dressings.org/Dressings/images

#needs a step here that builds a list of images in the datacards
# and removes images that aren't referenced from /Dressings/images. 


publish: promote
	make -f /data/htdocs/Dressings.org/Makefile dressings-datacards-by-alpha.html
	/share/store/web-maint-1.0/src/dressings-org-rsync.sh
