# The following condition is used to determinate if the sed version is BSD or
# GNU. It is useful because `sed -i` has a different implementation (and this
# command is needed by $(CSS_DEST). It uses the fact that `sed --version`
# doesn't exist for the BSD version.

.PHONY: css check_sass clean-style

check_sed:
ifneq ($(shell sed --version 2> /dev/null),)
SED = sed -i
else
SED = sed -i.css
endif

css: $(CSS_DEST)

define ERROR_SASS

Error: SASS not found.

Ocsigen Start gives the choice to use SASS (a CSS preprocessor) to write
stylesheets. We encourage you to use SASS and the template needs it by default.
See https://sass-lang.com for more information.
If you don't really want to use it, you can change the value of the variable
USE_SASS to "no" in Makefile.options to use CSS.

endef


check_sass:
ifeq ($(strip $(USE_SASS)),yes)
ifeq ($(shell which sassc),)
ifeq ($(shell which sass),)
$(error $(ERROR_SASS))
endif
endif
endif

ifeq ($(USE_NPM), yes)
PACKAGE_JSON = package.json
NPM_POSTCSS = node_modules/postcss-cli
NPM_AUTOPREFIXER = node_modules/autoprefixer
POSTCSS = node_modules/.bin/postcss

$(NPM_POSTCSS): $(PACKAGE_JSON)
	npm install postcss-cli@7.1.2

$(NPM_AUTOPREFIXER): $(PACKAGE_JSON)
	npm install autoprefixer@9.8.6

$(PACKAGE_JSON):
	npm init --yes
else
PACKAGE_JSON =
NPM_POSTCSS =
NPM_AUTOPREFIXER =
POSTCSS = postcss
endif

##----------------------------------------------------------------------
## SASS rules

# If $(USE_SASS) (see Makefile.options) is set to yes, it will compile and
# compress all SASS files and save it in $(LOCAL_CSS).
# If SASS is not activated, it will concatenate all CSS files (listed in
# $(CSS_FILES)) in $(LOCAL_CSS).
# In both cases, external CSS files ($(EXTERNAL_CSS_FILES)) are copied.
$(LOCAL_CSS): $(PACKAGE_JSON) $(NPM_POSTCSS) $(NPM_AUTOPREFIXER) $(LOCAL_STATIC_CSS)/.import-external-css | check_sass
ifeq "$(USE_SASS)" "yes"
ifeq ($(shell which sassc),)
	[ -d $(SASSDIR) ] && \
	SASS_PATH=$(SASS_PATH) sass --style compressed $(SASS_SRC) $@
else
	[ -d $(SASSDIR) ] && \
	sassc -t compressed $(addprefix -I ,$(subst :, ,$(SASS_PATH))) $(SASS_SRC) $@
endif
	$(POSTCSS) --use autoprefixer --replace $@
else
	cat $(CSS_FILES) > $@
endif

##----------------------------------------------------------------------
## CSS rules

$(CSSDIR):
	mkdir -p $@

# Copy the CSS file $(LOCAL_CSS) in $(CSS_DEST) after adding a hash in the name
# and make a symlink for $(PROJECT_NAME).css which is used in index.html.
# FIXME: md5 is not by default on Mac OSX: it must be installed with brew.
# Instead of md5, md5 is present but the output is different.
$(CSS_DEST): $(LOCAL_CSS) | $(CSSDIR) check_sed
	HASH=`cat $< | md5 | cut -d ' ' -f 1` && \
	cp $< $(CSS_PREFIX)_$$HASH.css && \
	$(SED) '1s/^/@charset "UTF-8";/' $(CSS_PREFIX)_$$HASH.css && \
	ln -sf $(PROJECT_NAME)_$$HASH.css $@
# Charset is necessary for iOS.
# Including it in scss does not work because sass removes it.

##----------------------------------------------------------------------
## External CSS

# Copy files from other projects into the working directory.
# By default, it imports all CSS files from ocsigen-toolkit because the template
# needs it.
# See EXTERNAL_CSS_FILES definition in Makefile.options for more information.
# It is executed with every run of make to be sure external CSS files are
# up-to-date and it allows to add other external CSS files between two
# compilation processes.

$(LOCAL_STATIC_CSS)/.import-external-css: $(EXTERNAL_CSS_FILES)
ifneq "$(EXTERNAL_CSS_FILES)" ""
	cp $(EXTERNAL_CSS_FILES) $(LOCAL_STATIC_CSS)
endif
	touch $(LOCAL_STATIC_CSS)/.import-external-css

clean-style:
	$(RM) $(LOCAL_CSS) $(LOCAL_STATIC_CSS)/$(PROJECT_NAME).css.map
	$(RM) -r .sass-cache
