# Validierungswerkzeuge
VALIDATE1_BIN := validate
VALIDATE1_OPTS :=
VALIDATE1_DEB_PACKAGE := wdg-html-validator
VALIDATE2_BIN := tidy
VALIDATE2_OPTS := -o /dev/null -errors -quiet
VALIDATE2_DEB_PACKAGE := tidy
# our self written documents
LYX_FILES := $(shell find . -name *.lyx -type f)
# the tex output of lyx
TEX_FILES := $(patsubst %.lyx,%.tex,$(LYX_FILES))
# output files of lyx that are already processed by mod_html_files.py
EXPORT_DIRS := $(shell for a in $(LYX_FILES); do echo -n "$$a" | sed "s%/\([^/]*\).lyx%/export-%"; echo "$$(basename $${a%.lyx})" ; done)
# png files (get converted to eps)
PNG_FILES := $(shell find . -name *.png -type f)
# converted eps files
EPS_FILES := $(patsubst %.png,%.eps,$(PNG_FILES))
# menue and footer files
INC_FILES := $(wildcard *.inc) $(wildcard progress/*.inc)
# the tex output of include files
INC_TEX_FILES := $(patsubst %.inc,%.tex,$(INC_FILES))
# look for lyx executable
LYX_BIN = $(shell which lyx lyx-qt lyx-xform | head -1)


# output parameters for latex2html
LATEX2HTML_OPTS := -no_auto_link -split 0 -no_navigation -no_subdir -info 0 -style ../../cryptocd.css -local_icons -address 0


# virtual targets that have different names than the produced files - they will alway be executed,
# if another target depends on them (they do not look for timestamps)
.PHONY : clean clean-targets install all

# clear language environment setting of the shell
export LANG=


# default target
all: $(EXPORT_DIRS)


# validate the html files
validate: $(EXPORT_DIRS)
	@# Warnung, falls das erste Programm nicht gefunden wird
	@if which $(VALIDATE1_BIN) >/dev/null ;\
		then	true ;\
		else	echo >&2 ;\
			echo >&2 "*****************************************************************" ;\
			echo >&2 "* Could not find the html-checker '$(VALIDATE1_BIN)'!                   *" ;\
			echo >&2 "* Debian users should run 'apt-get install $(VALIDATE1_DEB_PACKAGE)'. *" ;\
			echo >&2 "*****************************************************************";\
			echo >&2 ;\
	 fi
	@# Warnung, falls das zweite Programm nicht gefunden wird
	@if which $(VALIDATE2_BIN) >/dev/null ;\
		then	true ;\
		else	echo >&2 ;\
			echo >&2 "*****************************************************************" ;\
			echo >&2 "* Could not find the html-checker '$(VALIDATE2_BIN)'!                   *" ;\
			echo >&2 "* Debian users should run 'apt-get install $(VALIDATE2_DEB_PACKAGE)'. *" ;\
			echo >&2 "*****************************************************************";\
			echo >&2 ;\
	 fi
	@# Abbruch, falls beide Programme nicht gefunden werden
	@if which $(VALIDATE1_BIN) >/dev/null || which $(VALIDATE2_BIN) >/dev/null ;\
	  then	true ;\
	  else	echo >&2 ;\
			echo >&2 "None of the html checkers was found!" ;\
			echo >&2 ;\
			false ;\
	 fi
	@find . -name \*.html -type f | while read fname ;\
	  do	echo "Validating $$fname" ;\
		which $(VALIDATE1_BIN) >/dev/null && $(VALIDATE1_BIN) $(VALIDATE1_OPTS) "$$fname" ;\
		which $(VALIDATE2_BIN) >/dev/null && $(VALIDATE2_BIN) $(VALIDATE2_OPTS) "$$fname" ;\
		echo ;\
	 done


# copy the created files to the destination directory (usually outside of this directory tree)
install: $(EXPORT_DIRS)
	echo $(EXPORT_DIRS)
	@if [ -z "$(CCD_BUILD_DIR)" ] ;\
		then	echo "CCD_BUILD_DIR must be defined! (this should be done by the parent Makefile)" >&2 ;\
			false ;\
	  fi
	@if [ ! -e "$(CCD_BUILD_DIR)" ] ;\
		then	echo "CCD_BUILD_DIR ($(CCD_BUILD_DIR)) does not exist!" ;\
			false ;\
	  fi
	mkdir -p "$(CCD_BUILD_DIR)/doku/macos"
	mkdir -p "$(CCD_BUILD_DIR)/doku/linux"
	mkdir -p "$(CCD_BUILD_DIR)/doku/windows"
	@# copy os-independent stuff
	for a in $(EXPORT_DIRS) ;\
		do 	if echo "$$(dirname $$a)" | grep -q "/common/" ;\
			then	DIRNAME_MAC="$(CCD_BUILD_DIR)/doku/macos/$$(basename $$(dirname $$a))" ;\
				DIRNAME_LIN="$(CCD_BUILD_DIR)/doku/linux/$$(basename $$(dirname $$a))" ;\
				DIRNAME_WIN="$(CCD_BUILD_DIR)/doku/windows/$$(basename $$(dirname $$a))" ;\
				mkdir -p "$$DIRNAME_MAC" "$$DIRNAME_LIN" "$$DIRNAME_WIN" ;\
				cp -r "$$a/"* "$$DIRNAME_MAC" ;\
				cp -r "$$a/"* "$$DIRNAME_LIN" ;\
				cp -r "$$a/"* "$$DIRNAME_WIN" ;\
			fi ;\
	done
	@# copy os-specific stuff
	for a in $(EXPORT_DIRS) ;\
	  do 	if echo "$$(dirname $$a)" | grep -q "/common/" ;\
		  then	true ;\
		  else	mkdir -p "$(CCD_BUILD_DIR)/doku/$$(dirname $$a)" ;\
			cp -r "$$a/"* "$(CCD_BUILD_DIR)/doku/$$(dirname $$a)" ;\
		 fi ;\
	 done


clean-targets:
	-rm -r $(EXPORT_DIRS) 2>/dev/null


# create tex files from lyx
$(TEX_FILES): $(LYX_FILES)
	@if test -z $(LYX_BIN) ;\
		then	echo >&2 ;\
			echo >&2 "**************************************************" ;\
			echo >&2 "* Could not find 'lyx'!                          *" ;\
			echo >&2 "* Debian users should run 'apt-get install lyx'. *" ;\
			echo >&2 "**************************************************";\
			echo >&2 ;\
			false ;\
	 fi
	$(LYX_BIN) -e latex $*.lyx


# original html-files, as they are created by lyx
$(EXPORT_DIRS): $(TEX_FILES) $(INC_FILES) footer.asc mod_html_files.py
	-test -d "$@" && rm -rf "$@"
	mkdir -p "$@"
	latex2html $(LATEX2HTML_OPTS) -dir "$@" $(dir $@)/$(patsubst export-%,%.tex,$(notdir $@))
	@# die html-Datei wurde jetzt erstellt (inklusive Bilder)
	-#rm $(dir $@)WARNINGS $(dir $@)labels.pl $(dir $@)internals.pl $*.log 2>/dev/null
	-rm $@/$(patsubst export-%,%.css,$(notdir $@)) 2>/dev/null
	-rm $@/images.aux $@/images.log $@/images.out $@/images.pl $@/images.tex $@/missfont.log $@/WARNINGS $@/labels.pl 2>/dev/null
	python mod_html_files.py $@/$(patsubst export-%,%.html,$(notdir $@))
	@# add footer
	cat footer.asc >>$@/$(patsubst export-%,%.html,$(notdir $@))
	@# replace unix-like linebreaks with their DOS counterparts
	unix2dos $@/$(patsubst export-%,%.html,$(notdir $@))


clean:	clean-targets
	-rm $(TEX_FILES) 2>/dev/null
	-rm $(INC_TEX_FILES) 2>/dev/null
	-rm $(EPS_FILES) 2>/dev/null