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/ posts

.PHONY: posts

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

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

images: $(IMAGES_MIN)
	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"

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

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

#deploy: validate-all
	rsync -av --delete out/
	rsync -av --delete out.errorpages/

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

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