# Makefile to convert Kodak Photo CD images to HTML
DEFAULT_PCD=PCD
DEFAULT_VERSION=1

PCD_TO_SAVE=rules
EXT_TO_SAVE=.deu \
	    .eng \
	    .tec \
	    .drf \
	    extra
WWW_FILES_TO_SAVE=index.html \
	          index_de.html \
		  index.jpg \
		  deutsch.png \
		  english.png \
		  home.png \
		  pic_home.png \
		  pcd.css 
WWW_EXT_TO_SAVE=.html \
	        .jpg \
		.gif

DIRS := $(shell echo `grep -v "^\#" rules | grep "^[A-Za-z0-9]" | sed "s/ .*//"`)
PCD  := $(shell if [ _`sed -n "1p" rules | sed "s/^\# *\([^ ]*\) .*/\1/"` != _ ] ; then sed -n "1p" rules | sed "s/^\# *\([^ ]*\) .*/\1/" ; else echo $(DEFAULT_PCD) ; fi )
VERSION := $(shell if [ _`sed -n "1p" rules | sed "s/^\# *[^ ]* *\([^ ]*\).*/\1/"` != _ ] ; then sed -n "1p" rules | sed "s/^\# *[^ ]* *\([^ ]*\).*/\1/" ; else echo $(DEFAULT_VERSION) ; fi )
PCD_ARCH :=$(PCD)_data-$(VERSION)
HTML_ARCH :=$(PCD)_html-$(VERSION)

all: pcd2html

pcd2html: rules
	@for subdir in $(DIRS) ; do \
	   ( cd $$subdir ; \
           if [ ! -f Makefile ] ; then \
              ( pcd2html_create_makefile ) \
           fi ; \
           make -k ) \
	done
	pcd2html_create_index

data:
	echo Saving into $(PCD_ARCH)
	rm -rf $(PCD_ARCH)
	mkdir $(PCD_ARCH)
	for subdir in $(DIRS) ; do \
	   ( mkdir -p $(PCD_ARCH)/$$subdir && \
	   cp -a $$subdir/rules $(PCD_ARCH)/$$subdir && \
	   if [ -f $$subdir/extra ] ; then ( cp -a $$subdir/extra $(PCD_ARCH)/$$subdir ) fi && \
	   if [ -f $$subdir/data.extra ] ; then ( \
	      for file in `cat $$subdir/data.extra` ; do cp -a $$subdir/$$file $(PCD_ARCH)/$$subdir ; done \
	   ) fi ) \
	done
	for ext in $(EXT_TO_SAVE) ; do \
	  ( for subdir in . $(DIRS) ; do \
	    ( EXIST=`ls -x $$subdir/*$$ext 2> /dev/null | sed "s/\([a-z]\).*/\1/"` && \
	    EXIST=`echo $$EXIST | sed "s/ //g"` && \
	    if [ _$$EXIST != _ ] ; then ( cp -a $$subdir/*$$ext $(PCD_ARCH)/$$subdir ) fi \
	    ) \
	  done ) \
	done
	cp -a $(PCD_TO_SAVE) $(PCD_ARCH)
	# save email adress images which might be used for spam prevention
	for emil in `grep "email[[:space:]]*:" index.{deu,eng} | \
                           sed "s/^.*email[[:space:]]*:[[:space:]]*//" | \
                           uniq` ; do \
	  cp -a $$emil $(PCD_ARCH) ; \
	done
	GZIP=-9 tar -czf $(PCD_ARCH).tgz $(PCD_ARCH)
	rm -rf $(PCD_ARCH)

html: pcd2html
	rm -rf $(HTML_ARCH)
	mkdir $(HTML_ARCH)
	for subdir in $(DIRS) ; do \
	   ( mkdir -p $(HTML_ARCH)/$$subdir ) \
	done
	for subdir in $(DIRS) ; do \
	  ( for ext in $(WWW_EXT_TO_SAVE) ; do \
	    ( EXIST=`ls -x $$subdir/*$$ext 2> /dev/null | sed "s/\([a-z]\).*/\1/"` && \
	    EXIST=`echo $$EXIST | sed "s/ //g"` && \
	    if [ _$$EXIST != _ ] ; then ( cp -a $$subdir/*$$ext $(HTML_ARCH)/$$subdir ) fi \
	    ) \
	  done  && \
          if [ -e $$subdir/index.jpg ] ; then ( cp -a $$subdir/index.jpg $(HTML_ARCH)/$$subdir ) fi \
	  ) \
	done
	cp -a $(WWW_FILES_TO_SAVE) $(HTML_ARCH)
	# save email adress images which might be used for spam prevention
	for emil in `grep "email[[:space:]]*:" index.{deu,eng} | \
                           sed "s/^.*email[[:space:]]*:[[:space:]]*//" | \
                           uniq` ; do \
	  cp -a $$emil $(HTML_ARCH) ; \
	done
	GZIP=-9 tar -czf $(HTML_ARCH).tgz $(HTML_ARCH)
	rm -rf $(HTML_ARCH)

clean:
	for subdir in $(DIRS) ; do \
	   (cd $$subdir && pcd2html_create_makefile && make clean) \
	done

distclean:
	for subdir in $(DIRS); do \
	   (cd $$subdir && pcd2html_create_makefile && make distclean && rm -f Makefile) \
	done
	rm -f index.html index_de.html pcd.css index.jpg \
	      english.png deutsch.png home.png pic_home.png
