# Mustache is distributed for compat reasons

basedir = $(pkgdatadir)/base1
nodist_base_DATA = \
	dist/base1/cockpit.min.css.gz \
	dist/base1/patternfly.min.css.gz \
	dist/base1/mustache.min.js.gz \
	dist/base1/jquery.min.js.gz \
	dist/base1/cockpit.min.js.gz \
	$(NULL)
base_DATA = \
	dist/base1/manifest.json \
	$(NULL)

basedebugdir = $(debugdir)$(basedir)
basedebug_DATA = \
	dist/base1/cockpit.min.css.map \
	dist/base1/patternfly.min.css.map \
	dist/base1/mustache.min.js.map \
	dist/base1/jquery.min.js.map \
	dist/base1/cockpit.min.js.map \
	$(NULL)

AM_TESTS_ENVIRONMENT = export G_DEBUG=fatal-criticals,fatal-warnings ; export XDG_CACHE_HOME=$$(mktemp -d) ; export XDG_RUNTIME_DIR=$$XDG_CACHE_HOME ;

dist/base1/cockpit.min.css: src/base1/cockpit.css
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	$(srcdir)/tools/missing $(CLEAN_CSS) --keep-line-breaks --output=$@ --inline none --source-map --source-map-inline-sources $^
dist/base1/patternfly.min.css: dist/base1/patternfly-base.css
	$(MIN_CSS_RULE)
dist/base1/jquery.js:
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	cat $(srcdir)/node_modules/jquery/dist/jquery.js $(srcdir)/node_modules/bootstrap/dist/js/bootstrap.js $(srcdir)/node_modules/patternfly/dist/js/patternfly.js > $@.tmp && $(MV) $@.tmp $@
dist/base1/jquery.min.js: dist/base1/jquery.js
	$(MIN_JS_RULE)
dist/base1/cockpit.js: src/base1/cockpit.js
	$(ESLINT_RULE)
	$(COPY_RULE)
dist/base1/cockpit.min.js: dist/base1/cockpit.js
	$(MIN_JS_RULE)
dist/base1/mustache.js: src/base1/deprecated.js
	$(V_COPY) $(MKDIR_P) $(dir $@) && \
	cat $(srcdir)/src/base1/deprecated.js $(srcdir)/node_modules/mustache/mustache.js > $@.tmp && $(MV) $@.tmp $@
dist/base1/mustache.min.js: dist/base1/mustache.js
	$(MIN_JS_RULE)
dist/base1/manifest.json: src/base1/manifest.json
	$(COPY_RULE)


dist/base1/cockpit.min.js.map: dist/base1/cockpit.min.js

dist/base1/jquery.min.js.map: dist/base1/jquery.min.js

dist/base1/mustache.min.js.map: dist/base1/mustache.min.js

dist/base1/cockpit.min.css.map: dist/base1/cockpit.min.css

dist/base1/patternfly.min.css.map: dist/base1/patternfly.min.css

# Modify the patternfly files appropriately
#
# PatternFly 4 base.css: remove external font declarations, re-using the ones from our PatternFly 3 patternfly.min.css above for now
# once that goes away, use the above seddery instead
dist/base1/react-core-base.css:
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	$(AM_V_GEN) sed '/^@font-face/,/^$$/d' $(srcdir)/node_modules/@patternfly/react-core/dist/styles/base.css > $@.tmp && mv $@.tmp $@

# PatternFly 3 with hacked CSS to look like PF4
dist/base1/patternfly.css.map: dist/base1/patternfly-base.css
dist/base1/patternfly-base.css: src/base1/patternfly-overrides.less dist/base1/react-core-base.css
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	F=$(srcdir)/node_modules/patternfly/dist/less/patternfly.less && \
	sed '/import "variables.less"/d' $$F > $$F.patched && \
	F_EXTRA=$(srcdir)/node_modules/patternfly/dist/less/patternfly-additions.less && \
	cat $$F.patched $$F_EXTRA > $$F.tmp && \
	echo -e '@import "../../../../src/base1/patternfly-overrides.less";\n@import "../../../../src/base1/patternfly-overrides-variables.less";' >> $$F.tmp && \
	$(LESSC) --source-map=dist/base1/patternfly.css.map --source-map-include-source $$F.tmp $@.tmp && \
	sed -i -f $(srcdir)/tools/patternfly.sed $@.tmp && \
	rm $$F.tmp $$F.patched && \
	$(MV) $@.tmp $@

dist/base1/fonts/fontawesome.woff:
	$(V_COPY) $(MKDIR_P) $(dir $@) && \
	cp $(srcdir)/node_modules/patternfly/dist/fonts/fontawesome-webfont.woff $@.tmp && $(MV) $@.tmp $@
dist/base1/fonts/glyphicons.woff:
	$(V_COPY) $(MKDIR_P) $(dir $@) && \
	cp $(srcdir)/node_modules/patternfly/dist/fonts/glyphicons-halflings-regular.woff $@.tmp && $(MV) $@.tmp $@
dist/base1/fonts/patternfly.woff:
	$(V_COPY) $(MKDIR_P) $(dir $@) && \
	cp $(srcdir)/node_modules/patternfly/dist/fonts/PatternFlyIcons-webfont.woff $@.tmp && $(MV) $@.tmp $@

basefontsdir = $(pkgdatadir)/base1/fonts
basefonts_DATA = \
	dist/base1/fonts/fontawesome.woff \
	dist/base1/fonts/patternfly.woff \
	dist/base1/fonts/glyphicons.woff \
	$(NULL)

# Tests run with QUnit

base_QUNIT_TESTS = \
	dist/base1/test-base64.html \
	dist/base1/test-utf8.html \
	dist/base1/test-events.html \
	dist/base1/test-chan.html \
	dist/base1/test-dbus.html \
	dist/base1/test-dbus-address.html \
	dist/base1/test-echo.html \
	dist/base1/test-spawn.html \
	dist/base1/test-spawn-proc.html \
	dist/base1/test-http.html \
	dist/base1/test-external.html \
	dist/base1/test-location.html \
	dist/base1/test-locale.html \
	dist/base1/test-format.html \
	dist/base1/test-file.html \
	dist/base1/test-metrics.html \
	dist/base1/test-user.html \
	dist/base1/test-machines.html \
	dist/base1/test-permissions.html \
	dist/base1/test-series.html \
	dist/base1/test-cache.html \
	dist/base1/test-websocket.html \
	dist/base1/test-browser-storage.html \
	dist/base1/test-promise.html \
	dist/base1/test-protocol.html \
	$(NULL)

base_QUNIT_DEPS = \
	dist/base1/qunit.css \
	dist/base1/qunit.js \
	dist/base1/test-dbus-common.js \
	src/base1/qunit-template.html \
	src/base1/test-dbus-common.js \
	$(NULL)

dist/base1/qunit.js:
	$(V_COPY) $(MKDIR_P) $(dir $@) && \
	cat $(srcdir)/node_modules/qunit/qunit/qunit.js \
	    $(srcdir)/node_modules/qunit-tap/lib/qunit-tap.js \
            $(srcdir)/pkg/lib/qunit-config.js > $@.tmp && $(MV) $@.tmp $@

dist/base1/qunit.css:
	$(V_COPY) $(MKDIR_P) $(dir $@) && \
	cp $(srcdir)/node_modules/qunit/qunit/qunit.css $@.tmp && $(MV) $@.tmp $@

dist/base1/test-dbus-common.js: src/base1/test-dbus-common.js
	$(ESLINT_RULE)
	$(COPY_RULE)

$(base_QUNIT_TESTS): dist/base1/test-%.html: src/base1/test-%.js $(base_QUNIT_DEPS)
	$(ESLINT_RULE)
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	cp -L $< $@.tmp.js && $(MV) $@.tmp.js $(dir $@)/$(notdir $<) && \
	$(SED_TEST_TEMPLATE) $(srcdir)/src/base1/qunit-template.html > $@.tmp && $(MV) $@.tmp $@

# Simple tests run without QUnit

base_SIMPLE_TESTS = \
	dist/base1/test-no-jquery.html \
	dist/base1/test-framed-cache.html \
	dist/base1/test-framed.html \
	dist/base1/test-dbus-framed.html \
	$(NULL)

base_SIMPLE_DEPS = \
	dist/base1/simple-tap.js \
	src/base1/simple-template.html \
	src/base1/simple-tap.js \
	$(NULL)

dist/base1/simple-tap.js: src/base1/simple-tap.js
	$(ESLINT_RULE)
	$(COPY_RULE)

$(base_SIMPLE_TESTS): dist/base1/test-%.html: src/base1/test-%.js $(base_SIMPLE_DEPS)
	$(ESLINT_RULE)
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	cp -L $< $@.tmp.js && $(MV) $@.tmp.js $(dir $@)/$(notdir $<) && \
	$(SED_TEST_TEMPLATE) $(srcdir)/src/base1/simple-template.html > $@.tmp && $(MV) $@.tmp $@

dist/base1/test-%.js: dist/base1/test-%.html
	@true

# A html-webpack-plugin workalike for tests not built by webpack

SED_TEST_TEMPLATE = sed -e 's,[@]TEST[@],$(notdir $<),g'

base_TESTS = $(base_QUNIT_TESTS) $(base_SIMPLE_TESTS)
base_TESTS_DEPS = $(base_QUNIT_DEPS) $(base_SIMPLE_DEPS)
base_TESTS_JAVASCRIPT = $(base_TESTS:.html=.js)
base_TESTS_SOURCE = $(subst dist/,src/,$(base_TESTS_JAVASCRIPT))

TESTS += $(base_TESTS)
noinst_SCRIPTS += $(base_TESTS)

EXTRA_DIST += \
	dist/base1/cockpit.js \
	dist/base1/cockpit.min.css \
	dist/base1/cockpit.min.css.map \
	dist/base1/cockpit.min.js \
	dist/base1/cockpit.min.js.map \
	dist/base1/jquery.js \
	dist/base1/jquery.min.js \
	dist/base1/jquery.min.js.map \
	dist/base1/mustache.js \
	dist/base1/mustache.min.js \
	dist/base1/mustache.min.js.map \
	dist/base1/patternfly-base.css \
	dist/base1/patternfly.min.css \
	dist/base1/patternfly.min.css.map \
	dist/base1/patternfly.css.map \
	dist/base1/react-core-base.css \
	dist/base1/simple-tap.js \
	src/base1/cockpit.css \
	src/base1/cockpit.js \
	src/base1/deprecated.js \
	src/base1/manifest.json \
	src/base1/patternfly-overrides.less \
	src/base1/simple-tap.js \
	src/base1/test-dbus-common.js \
	$(base_DATA) \
	$(basefonts_DATA) \
	$(base_TESTS) \
	$(base_TESTS_DEPS) \
	$(base_TESTS_SOURCE) \
	$(base_TESTS_JAVASCRIPT) \
	$(NULL)

MAINTAINERCLEANFILES += \
	dist/base1/cockpit.js \
	dist/base1/cockpit.min.css \
	dist/base1/cockpit.min.css.gz \
	dist/base1/cockpit.min.css.map \
	dist/base1/cockpit.min.js \
	dist/base1/cockpit.min.js.gz \
	dist/base1/cockpit.min.js.map \
	dist/base1/fonts/fontawesome.woff \
	dist/base1/fonts/glyphicons.woff \
	dist/base1/fonts/patternfly.woff \
	dist/base1/jquery.js \
	dist/base1/jquery.min.js \
	dist/base1/jquery.min.js.gz \
	dist/base1/jquery.min.js.map \
	dist/base1/manifest.json \
	dist/base1/mustache.js \
	dist/base1/mustache.min.js \
	dist/base1/mustache.min.js.gz \
	dist/base1/mustache.min.js.map \
	dist/base1/patternfly-base.css \
	dist/base1/patternfly.min.css \
	dist/base1/patternfly.min.css.gz \
	dist/base1/patternfly.min.css.map \
	dist/base1/patternfly.css.map \
	dist/base1/react-core-base.css \
	dist/base1/qunit.css \
	dist/base1/qunit.js \
	dist/base1/simple-tap.js \
	dist/base1/test-dbus-common.js \
	$(base_TESTS) \
	$(base_TESTS_JAVASCRIPT) \
	$(NULL)
