# 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 = .

# 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
IMG_SRC = $(SOURCES)/img

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

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

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

###############################################
# 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)

#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 =

# 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 copyto
release: distclean build copyto
debug: distclean build copyto

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) $< >$@

copyto:
	@test -d $(ASSETS_DEST) || mkdir -p $(ASSETS_DEST)
	@if ls $(IMG_SRC)/* &> /dev/null; then \
	    echo "Copiar $(IMG_SRC)"; \
	    if [ ! -d $(IMG_DEST) ]; then \
		mkdir -p $(IMG_DEST); \
	    fi; \
	    cp -ra $(IMG_SRC)/* $(IMG_DEST)/; \
	fi

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

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

# Per mostrar els paths de destí
showpaths:
	@echo -n "Destí assets:" ${ASSETS_DEST}
	@test -d $(ASSETS_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""
	
	@echo -n "Destí assets/js:" ${JS_DEST}
	@test -d $(JS_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""
	
	@echo -n "Destí assets/css:" ${CSS_DEST}
	@test -d $(CSS_DEST) || echo -n " NO EXISTEIX!!!"
	@echo ""
	
	@echo -n "Destí assets/img:" ${IMG_DEST}
	@test -d $(IMG_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
