SITE_NAME 		= ktyl.dev
# configured in ~/.ssh/config
HOST			= pluto
# a script on the remote server to move it on arrival
REMOTE_SCRIPT	= ./deploy-$(SITE_NAME)

SRC_DIR			= src
IMG_DIR			= img
OUT_DIR			= site

ROOT_DIR		= $(SRC_DIR)/root

BLOG_BASE_DIR	= blog/
BLOG_BUILD_DIR	= $(BLOG_BASE_DIR)out/html/
BLOG_OUT_DIR	= $(OUT_DIR)/blog
BLOG_INDEX		= $(BLOG_BUILD_DIR)/index.html

GARDEN_BASE_DIR 	= $(SRC_DIR)/garden/
GARDEN_BUILD_DIR 	= $(GARDEN_BASE_DIR)html

PAGES 			= $(shell find $(ROOT_DIR) -wholename "$(ROOT_DIR)*.html")
STYLES			= $(shell find $(ROOT_DIR) -wholename "$(ROOT_DIR)*.css")
IMAGES			= $(shell find $(IMG_DIR) -wholename "$(IMG_DIR)/*.png")

HTML_INCLUDES 	= $(shell find $(SRC_DIR)/inc_html -name *.html)
CSS_INCLUDES	= $(shell find $(SRC_DIR)/inc_css -name *.css)

HTML_TARGETS 	= $(PAGES:$(ROOT_DIR)/%.html=$(OUT_DIR)/%.html)
CSS_TARGETS 	= $(STYLES:$(ROOT_DIR)/%.css=$(OUT_DIR)/%.css)
PNG_TARGETS		= $(IMG_DIR)/%.png=$(OUT_DIR)/%.png

all: html blog garden | $(OUT_DIR)
	cp -r src/garden/html $(OUT_DIR)/garden

html: $(HTML_TARGETS) $(CSS_TARGETS) | $(OUT_DIR)
	cp $(IMG_DIR)/*.png $(OUT_DIR)/
	cp $(IMG_DIR)/*.jpg $(OUT_DIR)/

deploy:
	cp -r $(OUT_DIR) $(SITE_NAME)
	rsync -rP $(SITE_NAME) $(HOST):~
	rm -r $(SITE_NAME)
	ssh $(HOST) "sudo $(REMOTE_SCRIPT)"

$(OUT_DIR)/%.html: $(ROOT_DIR)/%.html $(HTML_INCLUDES) $(BLOG_INDEX) | $(OUT_DIR)
	python ppp/ppp.py $< $(HTML_INCLUDES) $(BLOG_INDEX) > $@

$(OUT_DIR)/%.css: $(ROOT_DIR)/%.css $(CSS_INCLUDES) | $(OUT_DIR)
	python ppp/ppp.py $< $(CSS_INCLUDES) > $@

$(OUT_DIR):
	mkdir -p $@

$(BLOG_INDEX):
	mkdir -p $(BLOG_OUT_DIR)
	make --directory $(BLOG_BASE_DIR) html

# make blog from submodule and apply site templating
blog: $(HTML_INCLUDES) $(CSS_TARGETS)
	mkdir -p $(BLOG_OUT_DIR)
	make --directory $(BLOG_BASE_DIR) html
	cp -r $(BLOG_BUILD_DIR)/* $(BLOG_OUT_DIR)
	for page in `find "$(BLOG_OUT_DIR)" -wholename "*.html"`; do \
		pipenv run python ppp/ppp.py $$page $(HTML_INCLUDES) > temp ; \
		mv temp $$page ; \
		cp $(CSS_TARGETS) `dirname $$page` ; \
	done

garden:
	make --directory $(GARDEN_BASE_DIR) html

clean:
	make --directory $(GARDEN_BASE_DIR) clean
	make --directory $(BLOG_BASE_DIR) clean
	rm -rf $(OUT_DIR)

.PHONY: blog garden