
#---------------------------------------------------------------
# Project         : Mandrake Linux
# Module          : Borges
# File            : Makefile.images
# Author          : Camille Bgnis
# Created On      : Mon May  1 16:25:21 2001
# Copyright       : Camille Bgnis camille@mandrakesoft.com,
#                    MandrakeSoft under the GPL license
#---------------------------------------------------------------
# This Makefile is responsible for converting all acceptable 
# input graphics into pdf for print output.

include /home/mandrake/rpm/BUILD/Borges-0.14.9/doc/Makefile.include

# Missing default image
MISSING=$(ROOTDIR)/images/missing.jpg

# The eps file format is used as an intermediate format for most 
# transformations to avoid having n rules in here.

# copy identic files if not get EPS and convert
# due to bad svg support need to convert directly from svg using inkscape
%.png: force
	newer=$(shell ls -t $(wildcard $(ROOTDIR)/images/$(LANG)/$*.* $(ROOTDIR)/images/$*.*) | head -n 1 ); \
	if [ "$(wildcard $(ROOTDIR)/images/$(LANG)/$*.png $(ROOTDIR)/images/$*.png)" != "" ] ; then \
		if [ -e $(ROOTDIR)/images/$(LANG)/$*.png ] && [ $@ -ot $(ROOTDIR)/images/$(LANG)/$*.png ]; then \
			echo "********* Copying $(ROOTDIR)/images/$(LANG)/$*.png to $@"; \
			cp -f $(ROOTDIR)/images/$(LANG)/$*.png $@; \
		elif [ $@ -ot $(ROOTDIR)/images/$*.png ]; then \
			echo "********* Copying $(ROOTDIR)/images/$*.png to $@"; \
			cp -f $(ROOTDIR)/images/$*.png $@; \
		fi; \
	elif [ $$newer -nt $@ ] ; then \
		CD=$$(pwd); \
		if [ -e $(ROOTDIR)/images/$(LANG)/$*.svg ]; then \
			cd $(ROOTDIR)/images/$(LANG)/; \
			inkscape -D -e $$CD/$@ $*.svg; \
		elif [ -e $(ROOTDIR)/images/$*.svg ]; then \
			cd $(ROOTDIR)/images/; \
			inkscape -D -e $$CD/$@ $*.svg; \
		else \
			$(MAKE) $*.tmp.eps; convert $*.tmp.eps $@; rm -f $*.tmp.eps; \
		fi \
	fi
	if which pngcrush > /dev/null 2>&1; then pngcrush -q $@ $*.tmp.png; mv $*.tmp.png $@; fi

%.gif: force
	newer=$(shell ls -t $(wildcard $(ROOTDIR)/images/$(LANG)/$*.* $(ROOTDIR)/images/$*.*) | head -n 1 ); \
	if [ "$(wildcard $(ROOTDIR)/images/$(LANG)/$*.gif $(ROOTDIR)/images/$*.gif)" != "" ] ; then \
		if [ -e $(ROOTDIR)/images/$(LANG)/$*.gif ] && [ $@ -ot $(ROOTDIR)/images/$(LANG)/$*.gif ]; then \
			echo "********* Copying $(ROOTDIR)/images/$(LANG)/$*.gif to $@"; \
			cp -f $(ROOTDIR)/images/$(LANG)/$*.gif $@; \
		elif [ $@ -ot $(ROOTDIR)/images/$*.gif ]; then \
			echo "********* Copying $(ROOTDIR)/images/$*.gif to $@"; \
			cp -f $(ROOTDIR)/images/$*.gif $@; \
		fi; \
	elif [ $$newer -nt $@ ] ; then \
		$(MAKE) $*.tmp.eps; convert $*.tmp.eps $@; rm -f $*.tmp.eps; \
	fi

%.PDF: %.pdf
	cp $< $@

%.pdf: force
	newer=$(shell ls -t $(wildcard $(ROOTDIR)/images/$(LANG)/$*.* $(ROOTDIR)/images/$*.*) | head -n 1 ); \
	if [ "$(wildcard $(ROOTDIR)/images/$(LANG)/$*.pdf $(ROOTDIR)/images/$*.pdf)" != "" ] ; then \
		if [ -e $(ROOTDIR)/images/$(LANG)/$*.pdf ] && [ $@ -ot $(ROOTDIR)/images/$(LANG)/$*.pdf ]; then \
			echo "********* Copying $@"; \
			cp -f $(ROOTDIR)/images/$(LANG)/$*.pdf $@; \
		elif [ $@ -ot $(ROOTDIR)/images/$*.pdf ]; then \
			echo "********* Copying $@"; \
			cp -f $(ROOTDIR)/images/$*.pdf $@; \
		fi; \
	elif [ $$newer -nt $@ ] ; then \
		echo "********* Making $@"; \
		$(MAKE) $*.tmp.eps; epstopdf -o=$@ $*.tmp.eps; rm -f $*.tmp.eps; \
	fi

%.svg: force
	newer=$(shell ls -t $(wildcard $(ROOTDIR)/images/$(LANG)/$*.* $(ROOTDIR)/images/$*.*) | head -n 1 ); \
	if [ "$(wildcard $(ROOTDIR)/images/$(LANG)/$*.svg $(ROOTDIR)/images/$*.svg)" != "" ] ; then \
		CD=$$(pwd); \
		SRC=""; \
		if [ -e $(ROOTDIR)/images/$(LANG)/$*.svg ] && [ $@ -ot $(ROOTDIR)/images/$(LANG)/$*.svg ]; then \
			SRC=$(ROOTDIR)/images/$(LANG)/; \
		elif [ $@ -ot $(ROOTDIR)/images/$*.svg ]; then \
			SRC=$(ROOTDIR)/images/; \
		fi; \
		if [ "$$SRC" != "" ] ; then \
			echo "********* Copying $@ from $$SRC"; \
			cd $$SRC; \
			cp -f $@ $$CD; \
			for d in $$(grep "xlink:href=\(.*\)\(png\|jpg\|gif\)" $*.svg | sed -e "s/.*href=\"//; s/\".*//"); do \
				cp -f $$d $$CD; \
			done; \
		fi; \
	elif [ $$newer -nt $@ ] ; then \
		echo "********* Making $@"; \
		CD=$$(pwd); \
		if [ -e $(ROOTDIR)/images/$(LANG)/$*.fig ]; then \
			cd $(ROOTDIR)/images/$(LANG)/; \
			fig2dev -L svg $*.fig $$CD/$@; \
		elif [ -e $(ROOTDIR)/images/$*.fig ]; then \
			cd $(ROOTDIR)/images/; \
			fig2dev -L svg $*.fig $$CD/$@; \
		else \
			$(MAKE) $*.tmp.eps; convert $*.tmp.eps $@; rm -f $*.tmp.eps; \
		fi; \
	fi

%.eps: %.tmp.eps
	@echo "********* Making $@"
	mv $< $@

# temporary eps files

%.tmp.eps: $(ROOTDIR)/images/$(LANG)/%.eps
	cp -f $< $@
%.tmp.eps: $(ROOTDIR)/images/%.eps
	cp -f $< $@

#%.tmp.eps: $(ROOTDIR)/images/$(LANG)/%.dia
# dia must use the --nosplash option when no X available
# But then fails for some images...
# This is why I must use a virtual X server...
# The sleep time is needed on some machines, don't know why.
#	+trap "killall Xvfb; rm -f /tmp/.X5-lock" 0; \
#	Xvfb :5 -screen 0 100x100x8& sleep 5; \
#	xauth add :5 . `mcookie` ;\
#	DISPLAY=:5 dia -e $@ $<
#	-killall Xvfb
#	rm -f /tmp/.X5-lock
#%.tmp.eps: $(ROOTDIR)/images/%.dia
#	+trap "killall Xvfb; rm -f /tmp/.X5-lock" 0; \
#	Xvfb :5 -screen 0 100x100x8& sleep 5; \
#	xauth add :5 . `mcookie` ;\
#	DISPLAY=:5 dia -e $@ $<
#	-killall Xvfb
#	rm -f /tmp/.X5-lock

%.tmp.eps: $(ROOTDIR)/images/$(LANG)/%.fig
	fig2dev -L eps $< $@
%.tmp.eps: $(ROOTDIR)/images/%.fig
	fig2dev -L eps $< $@

# Images handled by ImageMagick
# If I can't find a vector based image, then I try to convert the first image by that name found
# Language specific image is always prefered.
%.tmp.eps: $(MISSING)
	CD=$$(pwd); \
	if [ -e $(ROOTDIR)/images/$(LANG)/$*.svg ]; then \
		cd $(ROOTDIR)/images/$(LANG)/; \
		inkscape -E $$CD/$@ $*.svg; \
	elif [ -e $(ROOTDIR)/images/$*.svg ]; then \
		cd $(ROOTDIR)/images/; \
		inkscape -E $$CD/$@ $*.svg; \
	elif [ -e $(ROOTDIR)/images/$(LANG)/$*.eps ]; then \
		cp -f $(ROOTDIR)/images/$(LANG)/$*.eps $@; \
	elif [ -e $(ROOTDIR)/images/$*.eps ]; then \
		cp -f $(ROOTDIR)/images/$*.eps $@; \
	elif [ -e $(ROOTDIR)/images/$(LANG)/$*.* ]; \
		then lastresort=$$(ls $(ROOTDIR)/images/$(LANG)/$*.* | head -1); \
		convert $$lastresort $@; \
	elif [ -e $(ROOTDIR)/images/$*.* ]; \
		then lastresort=$$(ls $(ROOTDIR)/images/$*.* | head -1); \
		convert $$lastresort $@; \
	else echo "******* $* missing! ********"; \
		convert $(MISSING) $@; \
		echo $* >> missing.img; \
	fi


# Local variables:
# mode: makefile
# End: