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

############
# "Origens"
############

# Carpetes i/o fitxers a copiar desde la carpeta indicada a la carpetà destí "js"
COPYTO_JS =

# Carpetes i/o fitxers a copiar desde la carpeta indicada a la carpetà destí "css"
COPYTO_CSS =

# Carpetes i/o fitxers a copiar desde FONTS a la carpetà destí corresponent
COPYTO_FONTS =

# Carpetes i/o fitxers a copiar desde la carpeta indicada a la mateixa carpetà destí però a INSTALL
COPYTO_SELFPATH =

COPYTO_COMPONENTS = \
    bootstrap-select/dist/* \
    ajax-bootstrap-select/dist/* \
    devbridge-autocomplete/dist/* \
    bootstrap-datepicker/dist/* \
    bootstrap-timepicker/js/*

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

# Path on estan les "llibreries", "mòduls externs", etz
COPYTO_SRC = ./
COPYFONTS_SRC =
COPYTO_COMPONENTS_SRC = bower_components

# on està el codi font del tama
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
IMG_SRC = $(SOURCES)/img

# Directori on posar les "propostes" de codi generades per les utiltizats que en generin.
FIX_DIR = $(SOURCES)/../src_fixed/

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

# Carpeta de destí a on posarem tot el porcesat pel Makefile (amb slash final!!!)
INSTALL_DEST = ../../../assets/appjs

# Path relatiu a INSTALL_DEST on posar totes els COPYTO_XXX
COPYTO_DEST =

# Paths relatius a INSTALL_DEST on posar els difetens "destins"
CSS_DEST = $(abspath ${INSTALL_DEST})/css
JS_DEST = $(abspath ${INSTALL_DEST})/js
IMG_DEST = $(abspath ${INSTALL_DEST})/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) \
    ./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))
COPYCSS_DEST_PATH = $(abspath ${INSTALL_DEST_PATH})/css
COPYJS_DEST_PATH = $(abspath ${INSTALL_DEST_PATH})/js
COPYFONTS_DEST_PATH = $(abspath ${INSTALL_DEST_PATH})/fonts
COPYTO_DEST_PATH = $(abspath ${INSTALL_DEST_PATH}/${COPYTO_DEST})

# Per comprovar els paths definits:
# make showvar-INSTALL_PATH
# Si no dona res es que està malament el path o la carpeta no existeix.
SOURCES_REALPATH = $(realpath $(SOURCES))
INSTALL_REALPATH = $(realpath $(INSTALL_DEST))

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

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

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

copyto:
	@test -d $(INSTALL_DEST_PATH) || mkdir -p $(INSTALL_DEST_PATH)
	@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
	@echo "Copiar a CSS:"
	@test -d $(COPYTO_DEST_PATH) || mkdir -p $(COPYTO_DEST_PATH)
	@for f in $(COPYTO_CSS); do \
	    if [ -f $(COPYTO_SRC)/$$f ]; then \
		echo "    $$f"; \
		if [ ! -d $(COPYCSS_DEST_PATH) ]; then \
		    mkdir -p $(COPYCSS_DEST_PATH); \
		fi; \
		cp $(COPYTO_SRC)/$$f $(COPYCSS_DEST_PATH)/$${f##*/}; \
	    fi; \
	done
	@echo "Copiar a JS:"
	@for f in $(COPYTO_JS); do \
	    if [ -f $(COPYTO_SRC)/$$f ]; then \
		echo "    $$f"; \
		if [ ! -d $(COPYJS_DEST_PATH) ]; then \
		    mkdir -p $(COPYJS_DEST_PATH); \
		fi; \
		cp $(COPYTO_SRC)/$$f $(COPYJS_DEST_PATH)/$${f##*/}; \
	    fi; \
	done
	@echo "Copiar a FONTS:"
	@for f in $(COPYTO_FONTS); do \
	    # Per processar els wildcards ** \
	    shopt -s globstar; \
	    for s in $(COPYFONTS_SRC)/$$f; do \
		echo "    " $$s; \
		v=$${s%*/*}; \
		if [ ! -d $(COPYFONTS_DEST_PATH)/$${v#*/*} ]; then \
		    mkdir -p $(COPYFONTS_DEST_PATH)/$${v#*/*}; \
		fi; \
		cp -r $$s $(COPYFONTS_DEST_PATH)/$${v#*/*}; \
	    done; \
	done
	@echo "Copiar al seu pròpi destí:"
	@for f in $(COPYTO_SELFPATH); do \
	    if ls $(COPYTO_SRC)/$$f &> /dev/null; then \
		echo "    " $${f##%%/*}; \
		if [ ! -d $(COPYTO_DEST_PATH)/$${f%%/*} ]; then \
		    mkdir -p $(COPYTO_DEST_PATH)/$${f%%/*}; \
		fi; \
		cp -r $(COPYTO_SRC)/$$f $(COPYTO_DEST_PATH)/$${f##%%/*}; \
	    fi; \
	done
	@echo "Copiar components:"
	@for f in $(COPYTO_COMPONENTS); do \
	    d=$${f%%/*}; \
	    echo "    " $$d; \
	    # Per processar els wildcards ** \
	    shopt -s globstar; \
	    if ls $(COPYTO_COMPONENTS_SRC)/$$f &> /dev/null; then \
		for s in $(COPYTO_COMPONENTS_SRC)/$$f; do \
		    if [ ! -d $(COPYTO_DEST_PATH)/$$d ]; then \
			mkdir -p $(COPYTO_DEST_PATH)/$$d; \
		    fi; \
		    cp -r $$s $(COPYTO_DEST_PATH)/$$d; \
	       done; \
	    fi; \
	done

cleancopyto:
	@if [ -d $(COPYCSS_DEST_PATH) ]; then \
	    echo "Eliminar de CSS:"; \
	    for f in $(COPYTO_CSS); do \
		if [ -f $(COPYCSS_DEST_PATH)/$${f##*/} ]; then \
		    echo "    $$f"; \
		    rm $(COPYCSS_DEST_PATH)/$${f##*/}; \
		fi; \
	    done; \
	fi
	@if [ -d $(COPYJS_DEST_PATH) ]; then \
	    echo "Eliminar de JS:"; \
	    for f in $(COPYTO_JS); do \
		if [ -f $(COPYJS_DEST_PATH)/$${f##*/} ]; then \
		    echo "    $$f"; \
		    rm $(COPYJS_DEST_PATH)/$${f##*/}; \
		fi; \
	    done; \
	fi
	@if [ -d $(COPYFONTS_DEST_PATH) ]; then \
	    echo "Eliminar de FONTS:"; \
	    for f in $(COPYTO_FONTS); do \
		# Per processar els wildcards ** \
		shopt -s globstar; \
		for s in $(COPYFONTS_DEST_PATH)/$$f; do \
		    if [ -f $$s ]; then \
			echo "    " $${s#${COPYFONTS_DEST_PATH}/}; \
			rm $$s; \
		    fi; \
		    # Eliminar les subcarpetes sols si estan buides \
		    # Path de la carpeta on està el fitxer \
		    if [ -d $${s%/*} ]; then \
			rmdir --ignore-fail-on-non-empty $${s%/*}; \
		    fi; \
		    # Path "base" de "tot el conjunt" \
		    v=$${s#${COPYFONTS_DEST_PATH}/}; \
		    if [ -d ${COPYFONTS_DEST_PATH}/$${v%%/*} ]; then \
			rmdir --ignore-fail-on-non-empty ${COPYFONTS_DEST_PATH}/$${v%%/*}; \
		    fi; \
		done; \
	    done; \
	fi
	@if [ -d $(COPYTO_DEST_PATH) ]; then \
	    echo "Eliminar Components:"; \
	    for f in $(COPYTO_COMPONENTS); do \
		# Per processar els wildcards ** \
		shopt -s globstar; \
		for s in $(COPYTO_DEST_PATH)/$$f; do \
		    # Path "base" de "tot el conjunt" \
		    v=$${s#${COPYTO_DEST_PATH}/}; \
		    if [ -d ${COPYTO_DEST_PATH}/$${v%%/*} ]; then \
			echo "    " $${v%%/*}; \
			rm -rf ${COPYTO_DEST_PATH}/$${v%%/*}; \
			if [ -d ${COPYTO_DEST_PATH}/$${v%%/*} ]; then \
			    rmdir --ignore-fail-on-non-empty ${COPYTO_DEST_PATH}/$${v%%/*}; \
			fi; \
		    fi; \
		done; \
	    done; \
	    echo "Eliminar del seu pròpi destí:"; \
	    for f in $(COPYTO_SELFPATH); do \
		if ls $(COPYTO_DEST_PATH)/$$f &> /dev/null; then \
		    echo "    " $${f##%%/*}; \
		    rm -rf $(COPYTO_DEST_PATH)/$${f##%%/*}; \
		fi; \
		# Eliminar tota la subcarpeta sols si està buida \
		if [ -d $(COPYTO_DEST_PATH)/$${f%%/*} ]; then \
		    rmdir --ignore-fail-on-non-empty $(COPYTO_DEST_PATH)/$${f%%/*}; \
		fi; \
	    done; \
	fi

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

	@echo -n "Copiar a base/[?]:" ${COPYTO_DEST_PATH}
	@test -d $(COPYTO_DEST_PATH) || echo -n " NO EXISTEIX!!!"
	@echo ""

	@echo -n "Copiar a base/CSS:" ${COPYCSS_DEST_PATH}
	@test -d $(COPYCSS_DEST_PATH) || echo -n " NO EXISTEIX!!!"
	@echo ""

	@echo -n "Copiar a base/JS:" ${COPYJS_DEST_PATH}
	@test -d $(COPYJS_DEST_PATH) || 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
###
### npm
###
# properly get npm-bin in cygwin (Eg. CYGWIN_NT-10.0)
platform = $(shell uname -s)
ifeq ($(findstring _NT,$(platform)),_NT)
	npm-bin = $(shell cygpath -u $(shell npm bin))
else
	npm-bin = $(shell npm bin)
endif

# Si no tenim npm local (./node_modules) agafarem el global...
ifeq ($(wildcard $(npm-bin)),)
	npm-path =
else
	npm-path = $(npm-bin)/
endif

# root: npm install -g uncs
.PHONY: uncss
uncss:
	@echo uncss http://website.localnet/ -u .uncssrc > uncss.css

cssrelease: cleancss uncss
.PHONY: cleancss
cleancss:
#	$(npm-path)cleancss --source-map $@/fine-uploader.css -o $@/fine-uploader.min.css
#	$(npm-path)cleancss --source-map $@/fine-uploader-gallery.css -o $@/fine-uploader-gallery.min.css
#	$(npm-path)cleancss --source-map $@/fine-uploader-new.css -o $@/fine-uploader-new.min.css

# root: npm install eslint -g
.PHONY: lint
lint:
	@$(npm-path)eslint $(JS_SRC)/*

.PHONY: lintfix
lintfix: fixjs

.PHONY: fixjs
fixjs:
	# NOTA: Les versions entigues no porten --fix-dry-run per tant, per evitar
	# que modifiqui el codi font copiem aquest a un altre carpeta per poder
	# executar el parser sense que modifiqui el codi original.
	# Si la versió instalada accepta --fix-dry-run això ja no caldria...
	@if [ -d $(FIX_DIR) ]; then \
	    rm -rf $(FIX_DIR); \
	fi
	@test -d $(FIX_DIR) || mkdir -p $(FIX_DIR)
	@cp -a $(JS_SRC) $(FIX_DIR)/
	@$(npm-path)eslint --fix --format codeframe $(FIX_DIR)/*

# these are targets that don't produce a file of the same name
.PHONY: all release debug build sass buildsass buildjs install clean distclean copyto cleancopyto showpaths watch watchsetup
