# Carpeta de destí a on posarem els fitxers.
INSTALL_PATH = ../../assets

# Carpeta local on deixarem els fitxers finals.
SRCDIR = .

# Directoris relatius a SOURCES on està el codi del tema segons el tipus d'asset
SASS_SRC = $(SRCDIR)/scss
CSS_SRC = $(SRCDIR)/css
JS_SRC = $(SRCDIR)/js

# Directoris per copiar "sencers", eg: COPYDIRS = img fonts
COPYDIRS = img

# Passem els fiferents paths relatius a INSTALL_PATH a paths absoluts
INSTALL_DEST = $(abspath ${INSTALL_PATH})
CSS_DEST = $(abspath ${INSTALL_PATH})/css
JS_DEST = $(abspath ${INSTALL_PATH})/js

# YUI Compressor
# --------------
#COMPRESSOR = java -jar yuicompressor-2.4.6.jar
#COMPRESSOR = yuicompressor
#COMPRESSOR_FLAGS = --charset utf-8 --verbose

# uglify
# ------
#   npm install uglify-js -g
#   npm install uglifycss -g
UGLIFYJS_CMD = uglifyjs
UGLIFYCSS_CMD = uglifycss

#UGLIFYJS_FLAGS =  --compress --mangle --ie8 --
UGLIFYJS_FLAGS =  --mangle --ie8 --
UGLIFYCSS_FLAGS =

# Configuracions segons target
debug: UGLIFYJS_FLAGS = --beautify --ie8 --

# SASS
# ----
# --style NAME
#	Output style. Can be nested (default), compact, compressed, or expanded.
#
# --sourcemap=TYPE
#	How to link generated output to the source files.
#	auto (default): relative paths where possible, file URIs elsewhere
#	file: always absolute file URIs
#	inline: include the source text in the sourcemap
#	none: no sourcemaps
#
# --debug-info
#	Emit output that can be used by the FireSass Firebug plugin.
#
# --no-cache
#	 Don't cache parsed Sass files.
SASS = sass
#SASS_FLAGS = --style expanded --sourcemap=auto
SASS_FLAGS = --style compressed --sourcemap=none

# Configuracions segons target
release: SASS_FLAGS = -f --no-cache --style compressed --sourcemap=none
debug: SASS_FLAGS = --style expanded --sourcemap=auto

# SASS_INCLUDES = \
#     $(SASS_SRC) \
#     ./bower_components
SASS_INCLUDES = \
    $(SASS_SRC)

SASS_CMD = sass $(SASS_FLAGS)

###########################

MKDIR = mkdir -p

SASS_FILES = $(shell find $(SASS_SRC) -name '*.scss' | grep -v '/_')
CSS_FILES  = $(wildcard $(CSS_SRC)/*.css $(CSS_SRC)/**/*.css)
JS_FILES   = $(wildcard $(JS_SRC)/*.js $(JS_SRC)/**/*.js)

SASS_BUILD := $(patsubst $(SASS_SRC)/%.scss, $(CSS_DEST)/%.css, $(SASS_FILES))
CSS_BUILD := $(patsubst $(CSS_SRC)/%, $(CSS_DEST)/%, $(CSS_FILES))
JS_BUILD := $(patsubst $(JS_SRC)/%, $(JS_DEST)/%, $(JS_FILES))

build: build-scss build-css build-js

all: clean build-scss build-css build-js
release: clean build-scss build-css build-js install
debug: clean build-scss build-css build-js

build-css: $(CSS_BUILD)
build-js: $(JS_BUILD)
build-scss: $(SASS_BUILD)

sassupdate:
	$(SASS_CMD) -E utf-8 -C $(foreach d, $(SASS_INCLUDES), -I $d) --update $(SASS_SRC):$(CSS_DEST)

sasswatch:
	$(SASS_CMD) -E utf-8 $(foreach d, $(SASS_INCLUDES), -I $d) --watch $(SASS_SRC):$(CSS_DEST)

$(CSS_DEST)/%.css: $(SASS_SRC)/%.scss
	@echo "SASS:" $<
	@test -d $(@D) || $(MKDIR) $(@D)
	@$(SASS_CMD) -E utf-8 -C $(foreach d, $(SASS_INCLUDES), -I $d) $? $@

$(CSS_DEST)/%.css: $(CSS_SRC)/%.css
	@echo "CSS:" $<
	@test -d $(@D) || $(MKDIR) $(@D)
	@$(UGLIFYCSS_CMD) $(UGLIFYCSS_FLAGS) $< >$@

$(JS_DEST)/%.js: $(JS_SRC)/%.js
	@echo "JS:" $<
	@test -d $(@D) || $(MKDIR) $(@D)
	@$(UGLIFYJS_CMD) $(UGLIFYJS_FLAGS) $< >$@

# Copiar directoris addicionals al directori destí. Son els definits a COPYDIRS (img, fonts, etz...)
copydirs: $(COPYDIRS)
$(COPYDIRS):
	@test -d $(INSTALL_PATH) || $(MKDIR) $(INSTALL_PATH)
	@if [ -d $(SRCDIR)/$@ ]; then \
		echo '==> Copiant $@ '; \
		rsync -rupE $(SRCDIR)/$@ $(INSTALL_PATH); \
	fi

# Install: packer -S inotify-tools
# Si dona error el make:
#   echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
watch:
	@while true; do \
	    make debug; \
	    inotifywait -qre close_write $(SRCDIR); \
	    if [ $$? -ne 0 ]; then \
		echo 'NOTA: Si dona error max_user_watches executar make watchsetup per solventar-ho.'; \
		exit; \
	    fi; \
	done

watchsetup:
	@echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

# Copiem els fitxers que estan al directori css (que no son els generats via scss...)
install: all copydirs
	@test -d $(INSTALL_PATH) || $(MKDIR) $(INSTALL_PATH)

clean:
	@for file in $(SASS_BUILD); do \
	    if [ -e $$file ]; then \
		rm $$file; \
	    fi; \
	    if [ -e $$file.map ]; then \
		rm $$file.map; \
	    fi; \
	done
	@for file in $(CSS_BUILD); do \
	    if [ -e $$file ]; then \
		rm $$file; \
	    fi; \
	done
	@for file in $(JS_BUILD); do \
	    if [ -e $$file ]; then \
		rm $$file; \
	    fi; \
	done

distclean: clean
	# Eliminar sols si està buit
	rmdir --ignore-fail-on-non-empty $(CSS_DEST)
	rmdir --ignore-fail-on-non-empty $(JS_DEST)
	@for f in $(COPYDIRS); do \
	    if ls $(INSTALL_PATH)/$$f &> /dev/null; then \
			echo "    " $${f##%%/*}; \
			rm -rf $(INSTALL_PATH)/$${f##%%/*}; \
	    fi; \
	    # Eliminar tota la subcarpeta sols si està buida \
	    if [ -d $(INSTALL_PATH)/$${f%%/*} ]; then \
			rmdir --ignore-fail-on-non-empty $(INSTALL_PATH)/$${f%%/*}; \
	    fi; \
	done

# Per mostrar els paths de destí
showpaths:
	@echo -n "Arrel:" ${INSTALL_DEST}
	@test -d $(INSTALL_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""

	@echo -n "CSS:" ${CSS_DEST}
	@test -d $(CSS_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""

	@echo -n "Javascript:" ${JS_DEST}
	@test -d $(JS_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""

# Per mostrar el contigut de qualsevol variable.
# eg: make showvar-CSS_FILES
showvar-%:
	@echo '$*=$($*)'

.PHONY : sass all release debug install clean distclean showpaths watch watchsetup
