POSTS_TXT = $(wildcard posts/*.txt)
POSTS_HTML = $(patsubst posts/%.txt,out/posts/%.html,$(POSTS_TXT))

PAGES_TXT = $(wildcard pages/*.txt)
PAGES_HTML = $(patsubst pages/%.txt,out/%.html,$(PAGES_TXT))

ERRPAGES_TXT = $(wildcard errpages/*.txt)
ERRPAGES_HTML = $(patsubst errpages/%.txt,out.errorpages//%.html,$(ERRPAGES_TXT))

CSS_FILES = $(wildcard theme/*.css)
JS_FILES = $(wildcard theme/*.js)

IMAGES_SRC = $(wildcard posts/*/*.jpeg)
IMAGES_MIN = $(patsubst posts/%/*.jpeg,out/posts/%/*.jpeg,$(IMAGES_SRC))


all: posts pages errpages
	@cp -aL statics/* out/
	@cp $(CSS_FILES) out/
	@cp $(JS_FILES) out/

posts: $(POSTS_HTML)
$(POSTS_HTML): $(POSTS_TXT) theme/post.template theme/index.template $(wildcard theme/*.inc)
	bin/hypolais.pl posts

.PHONY: posts

pages: $(PAGES_HTML)
$(PAGES_HTML): $(PAGES_TXT) theme/page.template $(wildcard theme/*.inc)
	bin/hypolais.pl pages

errpages: $(ERRPAGES_HTML)
$(ERRPAGES_HTML): $(ERRPAGES_TXT) theme/error-page.template $(wildcard theme/*.inc) theme/style.css
	bin/hypolais.pl errpages

images: $(IMAGES_MIN)
$(IMAGES_MIN): $(IMAGES_SRC)
	convert -resize 50 $? $@

# TODO validate-atom validate-links
validate-all: validate-js validate-css validate-xhtml

validate-js: $(JS_FILES)
	@echo "You have to validate the files manually on http://www.jslint.com/"

validate-css: tmp/check-css
tmp/check-css: $(CSS_FILES)
	@bin/validate.pl css $?
	@touch tmp/check-css

validate-xhtml: tmp/check-xhtml
tmp/check-xhtml: $(POSTS_HTML) $(PAGES_HTML) $(ERRPAGES_HTML)
	@bin/validate.pl html $?
	@touch tmp/check-xhtml

#deploy: validate-all
deploy: 
	rsync -av --delete out/ idunn.univers-libre.net:www/blog/
	rsync -av --delete out.errorpages/ idunn.univers-libre.net:www/error-pages/

run-testing-server:
	perl -MHTTP::Server::Brick -e '$$s=HTTP::Server::Brick->new(port=>8080); $$s->mount("/"=>{path=>"out"}); $$s->start' & >/dev/null

clean:
	rm -rf out/* out.errorpages/* tmp/*