INSTALL="install"
TAR="tar"
GREP="grep"
NODE="node"
NPM="npm"
DESTDIR="./dist"
PKG_VERSION := $( $(GREP) -Po '(?<="version": ")[^"]*' )
TMPDIR := $(shell mktemp -d)

all: builddirs npm_dependencies swig htmlmin min-css min-js copy-php

swig:
	$(NODE) node_modules/swig/bin/swig.js render -j dist.json templates/upload_logged.swig > $(CURDIR)/build/upload_logged.html
	$(NODE) node_modules/swig/bin/swig.js render -j dist.json templates/upload_unlogged.swig > $(CURDIR)/build/upload_unlogged.html
	$(NODE) node_modules/swig/bin/swig.js render -j dist.json templates/404.swig > $(CURDIR)/build/404.html

htmlmin:
	$(NODE) node_modules/htmlmin/bin/htmlmin $(CURDIR)/build/upload_logged.html -o $(CURDIR)/build/upload_logged.html
	$(NODE) node_modules/htmlmin/bin/htmlmin $(CURDIR)/build/upload_unlogged.html -o $(CURDIR)/build/upload_unlogged.html
	$(NODE) node_modules/htmlmin/bin/htmlmin $(CURDIR)/build/404.html -o $(CURDIR)/build/404.html

installdirs:
	mkdir -p $(DESTDIR)/classes $(DESTDIR)/includes

min-css:
	$(NODE) $(CURDIR)/node_modules/.bin/cleancss --s0 $(CURDIR)/static/css/pomf.css > $(CURDIR)/build/pomf.min.css

min-js:
	echo "// @source https://github.com/pomf/pomf/tree/master/static/js" > $(CURDIR)/build/pomf.min.js
	echo "// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat" >> $(CURDIR)/build/pomf.min.js
	$(NODE) $(CURDIR)/node_modules/.bin/uglifyjs  --screw-ie8 ./static/js/app.js >> $(CURDIR)/build/pomf.min.js
	echo "// @license-end" >> $(CURDIR)/build/pomf.min.js

copy-php:
	cp -rv $(CURDIR)/static/php/* $(CURDIR)/build/

install: installdirs
	cp -rv $(CURDIR)/build/* $(DESTDIR)/

dist:
	DESTDIR=$(TMPDIR)/pomf-$(PKGVERSION)
	export DESTDIR
	install
	$(TAR) cJf pomf-$(PKG_VERSION).tar.xz $(DESTDIR)
	rm -rf $(TMPDIR)

clean:
	rm -rvf $(CURDIR)/node_modules
	rm -rvf $(CURDIR)/build

uninstall:
	rm -rvf $(DESTDIR)/

npm_dependencies:
	$(NPM) install

builddirs:
	mkdir -p $(CURDIR)/build/classes $(CURDIR)/build/includes
