# Copyright (C) 2017 Nauta Tecnològica, SCP - All Rights Reserved.
# Unauthorized copying, publishing, commercial usage and/or distribution of this file,
# via any medium, is strictly prohibited without the express permission of the owner.

#
# Paths relatius al makefile on hi han els difetens recursos.
#

# on està el codi font
SOURCES = src

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

############
# "Destins"
############

# Carpeta de destí a on posarem tot el porcesat pel Makefile
# assets/appjs/{MODUL}/...
INSTALL_DEST = ../../../../../assets/appjs
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
APPJS_PATH = $(current_dir)

# Paths relatius a INSTALL_DEST on posar els difetens "destins"
APPJS_DEST = $(abspath ${INSTALL_DEST}/$(APPJS_PATH))
CSS_DEST = $(abspath ${INSTALL_DEST}/$(APPJS_PATH))/css
JS_DEST = $(abspath ${INSTALL_DEST}/$(APPJS_PATH))/js

###############################################
# Final de la part "configurable" del tema!!!!!
###############################################

#
# sass
#
SASS_FLAGS = --style compressed --sourcemap=auto

# Target dependant variables
release: SASS_FLAGS = --no-cache --style compressed --sourcemap=none
debug: SASS_FLAGS = --style expanded --sourcemap=auto

SASS_INCLUDES = \
    $(SASS_SRC) \
    ./bower_components

SASS_CMD = sass $(SASS_FLAGS)

#
# Javascript i CSS del tema
#
# uglify:
#   npm install uglify-js -g
#   npm install uglifycss -g
UGLIFYJS_CMD = uglifyjs
#UGLIFYJS_FLAGS =  --compress --mangle --ie8 --
UGLIFYJS_FLAGS =  --mangle --ie8 --

# Target dependant variables
debug: UGLIFYJS_FLAGS = --beautify --ie8 --

UGLIFYCSS_CMD = uglifycss
UGLIFYCSS_FLAGS =

#
# Paths absoluts
#
INSTALL_DEST_PATH = $(abspath $(INSTALL_DEST))

# Per comprovar els paths definits:
# make showvar-INSTALL_PATH

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)

# default target
all: build

build: buildsass buildcss buildjs
install: clean build
release: distclean build
debug: distclean build

buildjs: $(patsubst $(JS_SRC)/%, $(JS_DEST)/%, $(JS_FILES))
buildcss: $(patsubst $(CSS_SRC)/%, $(CSS_DEST)/%, $(CSS_FILES))

sass: sassupdate

# compile sass files into dist/css files
buildsass: $(patsubst $(SASS_SRC)/%.scss, $(CSS_DEST)/%.css, $(SASS_FILES))

# compile sass with the native sass --update syntax
sassupdate:
	$(SASS_CMD) -C $(foreach d, $(SASS_INCLUDES), -I $d) --update $(SASS_SRC):$(CSS_DEST)

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

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

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

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

distclean: clean
	@if [ -z "$$(ls -A ${APPJS_DEST})" ]; then \
	    rmdir ${APPJS_DEST}; \
	fi

clean:
	@if [ -d $(CSS_DEST) ]; then \
	    rm -rf $(CSS_DEST); \
	fi
	@if [ -d $(JS_DEST) ]; then \
	    rm -rf $(JS_DEST); \
	fi

# Per mostrar els paths de destí
showpaths:
	@echo -n "assets/appjs:" ${INSTALL_DEST_PATH}
	@test -d $(INSTALL_DEST_PATH) || echo -n " NO EXISTEIX!!!"
	@echo ""
	
	@echo -n "Destí appjs:" ${APPJS_DEST}
	@test -d $(APPJS_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""
	
	@echo -n "Destí appjs/css:" ${CSS_DEST}
	@test -d $(CSS_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""
	
	@echo -n "Destí appjs/js:" ${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 '$*=$($*)'

# 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; \
	    inotifywait -qre close_write $(SOURCES); \
	    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

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