FOSWIKI_ROOT?=~/foswiki/trunk/core
FOSWIKI_LIB=$(FOSWIKI_ROOT)/lib
JQUERYPLUGIN_LIB=$(FOSWIKI_LIB)/Foswiki/Plugins/JQueryPlugin

MINIFYJS=$(shell if which uglifyjs >/dev/null; then echo $(UGLIFYJS); else echo $(NOMINIFYJS); fi)
UGLIFYJS="uglifyjs -m -c --"
NOMINIFYJS="echo 'WARNING: no javascript minifier found' >&2"

MINIFYCSS=$(shell if which cssmin >/dev/null; then echo $(CSSMIN); else echo $(NOMINIFYCSS); fi)
CSSMIN="cssmin"
NOMINIFYCSS="echo 'WARNING: no css minifier found' >&2"

JSLINT=$(shell if which eslint > /dev/null; then echo $(ESHINT); else echo $(NOJSLINT); fi)
NOJSLINT="echo 'WARNING: no javascript linter found' >&2"

SASS=$(shell if which node-sass >/dev/null; then echo $(SASS_CMD); else echo $(NOSASS); fi)
SASS_CMD="node-sass --output-style expanded"
NOSASS="echo 'WARNING: no sass preprocessor found' >&2"

ESHINT="eslint -f compact"
GZIP="gzip"
CAT="cat"
PATCH="patch"
DEBUG="false"

.SUFFIXES: .js .css .jslint
.PHONY: subdirs $(SUBDIRS)

all: subdirs $(TARGET) $(TARGET:.js=.js.gz) $(TARGET:.css=.css.gz)
	@echo -n ""

jslint: subdirs $(TARGET:.js=.jslint) 
	@echo -n ""

%.jslint: %.uncompressed.js
	@echo checking code quality of $<
	@$(JSLINT) $< && touch $@

%.jslint: %_src.js
	@echo checking code quality of $<
	@$(JSLINT) $< && touch $@

%.js: %.uncompressed.js
	@if $(DEBUG); then \
	  echo creating $@ from $<; $(CAT) $< > $@; \
	else \
	  echo minifying $@ from $<; $(MINIFYJS) $< > $@; \
	fi

%.js: %_src.js
	@if $(DEBUG); then \
	  echo creating $@ from $<; $(CAT) $< > $@; \
	else \
	  echo minifying $@ from $<; $(MINIFYJS) $< > $@; \
	fi

%.gz: %
	@echo zipping $< to $@
	@$(GZIP) -c $< > $@

%.css: %.uncompressed.css
	@echo minifying $@ from $<
	@$(MINIFYCSS) $< > $@

%.uncompressed.css: %.scss
	@echo sassing $@ from $<
	@$(SASS) $< > $@

%.css: %_src.css
	@echo minifying $@ from $<
	@$(MINIFYCSS) $< > $@


subdirs: $(SUBDIRS)

$(SUBDIRS):
	@$(MAKE) --no-print-directory -C $@

%.pm:
	@if test -f $@; then \
	   echo "found"; \
	 else \
	   echo "WARNING: no perl stub found at $@ " >&2; \
	 fi
