all:
	make -s -j8 website

debug:
	make website

# Extensions in src/
# th - template with html code
# ph - page with html code
# pm - page with markdown code
# ps - page with shell code
# pa - page with asciidoctor code
# pt - page with plain text
# everything will be converted to html

PH_FILES !=	find src -type f -name "*.ph" | sed 's|src/||g'
PM_FILES !=	find src -type f -name "*.pm" | sed 's|src/||g'
PS_FILES !=	find src -type f -name "*.ps" | sed 's|src/||g'
PA_FILES !=	find src -type f -name "*.pa" | sed 's|src/||g'
PT_FILES !=	find src -type f -name "*.pt" | sed 's|src/||g'

COMMIT_MSG !=	echo "Update $$(date +"%Y-%m-%d %H:%M:%S")"

# put static files and dirs into .control/static-files. Globbing is supported.
STATIC_FILES !=	cat .control/static-files

# put a local directory or a remote server with path (ssh notation) in here.
REMOTE !=	cat .control/remote-ssh-target

$(PH_FILES):
	echo "Create: src/$@ => www/${@:S/ph/html/}"
	mkdir -p "$$(dirname www/${@})"
	cat src/header.th > www/${@:S/ph/html/}
	cat src/$@ >> www/${@:S/ph/html/}
	cat src/footer.th >> www/${@:S/ph/html/}

$(PT_FILES):
	echo "Create: src/$@ => www/${@:S/pt/html/}"
	mkdir -p "$$(dirname www/${@})"
	cat src/header.th > www/${@:S/pt/html/}
	echo "<pre>" >> www/${@:S/pt/html/}
	cat src/$@ >> www/${@:S/pt/html/}
	echo "</pre>" >> www/${@:S/pt/html/}
	cat src/footer.th >> www/${@:S/pt/html/}

$(PM_FILES):
	echo "Create: src/$@ => www/${@:S/pm/html/}"
	mkdir -p "$$(dirname www/${@})"
	cat src/header.th > www/${@:S/pm/html/}
	discount -f fencedcode src/$@ >> www/${@:S/pm/html/}
	cat src/footer.th >> www/${@:S/pm/html/}

$(PS_FILES):
	echo "Create: src/$@ => www/${@:S/ps/html/}"
	mkdir -p "$$(dirname www/${@})"
	cat src/header.th > www/${@:S/ps/html/}
	ksh src/$@ >> www/${@:S/ps/html/}
	cat src/footer.th >> www/${@:S/ps/html/}

$(PA_FILES):
	echo "Create: src/$@ => www/${@:S/pa/html/}"
	mkdir -p "$$(dirname www/${@})"
	cat src/header.th > www/${@:S/pa/html/}
	asciidoctor -e \
		-r asciidoctor-diagram \
		-a imagesdir=/tmp/adoc-tmp \
		-a stylesheet! \
		-a data-uri \
		-o - src/$@ >> www/${@:S/pa/html/}
	cat src/footer.th >> www/${@:S/pa/html/}

website: prepare copy-static-files $(PH_FILES) $(PM_FILES) $(PS_FILES) $(PA_FILES) $(PT_FILES)

prepare:
	echo "Mkdir: www"
	mkdir -p www

git-commit:
	git pull --autostash
	git add src Makefile
	git commit -m "$(COMMIT_MSG)" && git push || exit 0

copy-static-files: prepare
	rsync -a --partial --delete --no-p --no-o --no-g --out-format="Copy: %f => www/" $(STATIC_FILES) www/

clean:
	make -s _clean
_clean:
	echo "Delete: www"
	rm -rf www

install:
	make -s _install
	make -s git-commit
_install: website
	echo "Fixing Permissions in www/"
	doas chown -R sdk:www www
	doas chmod -R ugo+Xrw www
	echo "Deploy: www/ => /var/www/htdocs/"
	cd www && doas rsync -a -o -g -p --inplace --partial --delete --exclude "*/cache/*" --out-format="Deploy: %f => /var/www/htdocs/%f" * $(REMOTE)


#### HELPER TARGETS ####

css:
	@vim src/assets/style.css
	@echo rebuild?
	@read
	@make install
	@make css
