# Makefile for copying files downloaded using composer
# into the proper location for use with WebCalendar.
# We only copy the min number of files required from the vendor directory.
# Also, compute the SHA hash to use with the integrity tag.
# We don't want WebCalendar releases to bundle
# every single file in the vendor directory.
#
# Also, composer dependency management sucks at asset management.
#
# NOTE: This Makefile does not work on macos, just linux.
#       This is because there is no sha384sum command on macos.
#       If you're on a Mac, you can use docker to setup
#       a linux container where (after installing xdd),
#       you can run make to generate the sha files.

PHPMAILER_DIR = includes/classes/phpmailer
PHPMAILER_VENDOR_DIR = vendor/phpmailer/phpmailer/src

BOOTSTRAP_ICON_DIR = images/bootstrap-icons
BOOTSTRAP_ICON_VENDOR_DIR = vendor/twbs/bootstrap-icons/icons

TINYMCE_VENDOR_DIR = vendor/tinymce

SHA384SUM = /usr/bin/sha384sum

_DEFAULT: _phpmailer includes/load_assets.php \
	_ICONS pub/tinymce/CHANGELOG.md

_phpmailer: $(PHPMAILER_DIR)/PHPMailer.php \
	$(PHPMAILER_DIR)/Exception.php \
	$(PHPMAILER_DIR)/OAuth.php \
	$(PHPMAILER_DIR)/POP3.php \
	$(PHPMAILER_DIR)/SMTP.php

$(PHPMAILER_DIR)/PHPMailer.php: $(PHPMAILER_VENDOR_DIR)/PHPMailer.php
	cp $< $@

$(PHPMAILER_DIR)/Exception.php: $(PHPMAILER_VENDOR_DIR)/Exception.php
	cp $< $@

$(PHPMAILER_DIR)/OAuth.php: $(PHPMAILER_VENDOR_DIR)/OAuth.php
	cp $< $@

$(PHPMAILER_DIR)/POP3.php: $(PHPMAILER_VENDOR_DIR)/POP3.php
	cp $< $@

$(PHPMAILER_DIR)/SMTP.php: $(PHPMAILER_VENDOR_DIR)/SMTP.php
	cp $< $@

_ICONS: \
	$(BOOTSTRAP_ICON_DIR)/printer.svg \
	$(BOOTSTRAP_ICON_DIR)/search.svg \
	$(BOOTSTRAP_ICON_DIR)/arrow-left.svg \
	$(BOOTSTRAP_ICON_DIR)/arrow-right-circle.svg \
	$(BOOTSTRAP_ICON_DIR)/arrow-left-circle.svg \
	$(BOOTSTRAP_ICON_DIR)/arrow-up-short.svg \
	$(BOOTSTRAP_ICON_DIR)/arrow-down-short.svg \
	$(BOOTSTRAP_ICON_DIR)/plus-circle.svg \
	$(BOOTSTRAP_ICON_DIR)/rss-fill.svg \
	$(BOOTSTRAP_ICON_DIR)/circle-fill.svg \
	$(BOOTSTRAP_ICON_DIR)/exclamation-triangle-fill.svg \
	$(BOOTSTRAP_ICON_DIR)/question-circle-fill.svg \
	$(BOOTSTRAP_ICON_DIR)/arrow-90deg-up.svg \
	$(BOOTSTRAP_ICON_DIR)/dash-circle.svg \
	$(BOOTSTRAP_ICON_DIR)/check-circle.svg \
	$(BOOTSTRAP_ICON_DIR)/trash.svg \
	$(BOOTSTRAP_ICON_DIR)/key-fill.svg \
	$(BOOTSTRAP_ICON_DIR)/info-circle-fill.svg \
	$(BOOTSTRAP_ICON_DIR)/circle.svg

$(BOOTSTRAP_ICON_DIR)/printer.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/printer.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/search.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/search.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/arrow-right-circle.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/arrow-right-circle.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/arrow-left.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/arrow-left.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/arrow-left-circle.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/arrow-left-circle.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/arrow-up-short.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/arrow-up-short.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/arrow-down-short.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/arrow-down-short.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/plus-circle.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/plus-circle.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/rss-fill.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/rss-fill.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/circle-fill.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/circle-fill.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/exclamation-triangle-fill.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/exclamation-triangle-fill.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/question-circle-fill.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/question-circle-fill.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/arrow-90deg-up.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/arrow-90deg-up.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/dash-circle.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/dash-circle.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/check-circle.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/check-circle.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/trash.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/trash.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/key-fill.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/key-fill.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/info-circle-fill.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/info-circle-fill.svg
	cp $< $@

$(BOOTSTRAP_ICON_DIR)/circle.svg: $(BOOTSTRAP_ICON_VENDOR_DIR)/circle.svg
	cp $< $@

includes/load_assets.php: \
	pub/bootstrap.min.css \
	pub/bootstrap.min.css.sha \
	pub/bootstrap.bundle.min.js \
	pub/bootstrap.bundle.min.js.sha \
	pub/jquery.min.js \
	pub/jquery.min.js.sha
	echo '<?php' > $@
	echo '// Auto-generated by make. Do not hand-edit.' >> $@
	echo '// See Makefile in source for details..' >> $@
	echo '// Last updated: ' | tr -d '\012' >> $@
	date >> $@
	echo "XASSETS =" | tr X '\044' >> $@
	echo '  _<link rel="stylesheet" href="pub/bootstrap.min.css" integrity="sha384-' | tr -d '\012' | tr _ '\047' >> $@
	cat pub/bootstrap.min.css.sha | tr -d '\012' >> $@
	echo '">_ .' | tr _ '\047' >> $@
	echo '  _<script src="pub/jquery.min.js" integrity="sha384-' | tr -d '\012' | tr _ '\047' >> $@
	cat pub/jquery.min.js.sha | tr -d '\012' >> $@
	echo '"></script>_ .' | tr _ '\047' >> $@
	echo '  _<script src="pub/bootstrap.bundle.min.js" integrity="sha384-' | tr -d '\012' | tr _ '\047' >> $@
	cat pub/bootstrap.bundle.min.js.sha | tr -d '\012' >> $@
	echo '"></script>_ .' | tr _ '\047' >> $@
	echo '  "\\n";' >> $@
	echo '?>' >> $@

pub/bootstrap.min.css: vendor/twbs/bootstrap/dist/css/bootstrap.min.css
	cp $< $@

pub/bootstrap.min.css.sha: pub/bootstrap.min.css $(SHA384SUM)
	$(SHA384SUM) $< | head -c 96 | xxd -r -p | base64 > $@

pub/bootstrap.bundle.min.js: vendor/twbs/bootstrap/dist/js/bootstrap.bundle.min.js
	cp $< $@

pub/bootstrap.bundle.min.js.sha: pub/bootstrap.bundle.min.js $(SHA384SUM)
	$(SHA384SUM) $< | head -c 96 | xxd -r -p | base64 > $@

pub/jquery.min.js: vendor/components/jquery/jquery.min.js
	cp $< $@

pub/jquery.min.js.sha: pub/jquery.min.js $(SHA384SUM)
	$(SHA384SUM) $< | head -c 96 | xxd -r -p | base64 > $@

# TINYMCE files
# NOTE: This Makefile is assuming that the 'CHANGELOG.md' file gets
# updated with each TinyMCE update (seems like a safe assumption)
TINYMCE_FILES = \
	pub/tinymce/models/dom/model.js \
	pub/tinymce/models/dom/model.min.js \
	pub/tinymce/models/dom/index.js \
	pub/tinymce/icons/default/icons.js \
	pub/tinymce/icons/default/icons.min.js \
	pub/tinymce/icons/default/index.js \
	pub/tinymce/plugins/autoresize/plugin.min.js \
	pub/tinymce/plugins/autoresize/plugin.js \
	pub/tinymce/plugins/autoresize/index.js \
	pub/tinymce/plugins/link/plugin.min.js \
	pub/tinymce/plugins/link/plugin.js \
	pub/tinymce/plugins/link/index.js \
	pub/tinymce/plugins/advlist/plugin.min.js \
	pub/tinymce/plugins/advlist/plugin.js \
	pub/tinymce/plugins/advlist/index.js \
	pub/tinymce/plugins/autolink/plugin.min.js \
	pub/tinymce/plugins/autolink/plugin.js \
	pub/tinymce/plugins/autolink/index.js \
	pub/tinymce/plugins/code/plugin.min.js \
	pub/tinymce/plugins/code/plugin.js \
	pub/tinymce/plugins/code/index.js \
	pub/tinymce/plugins/lists/plugin.min.js \
	pub/tinymce/plugins/lists/plugin.js \
	pub/tinymce/plugins/lists/index.js \
	pub/tinymce/plugins/table/plugin.min.js \
	pub/tinymce/plugins/table/plugin.js \
	pub/tinymce/plugins/table/index.js \
	pub/tinymce/skins/ui/oxide-dark/skin.shadowdom.js \
	pub/tinymce/skins/ui/oxide-dark/content.inline.css \
	pub/tinymce/skins/ui/oxide-dark/content.css \
	pub/tinymce/skins/ui/oxide-dark/content.inline.js \
	pub/tinymce/skins/ui/oxide-dark/content.min.css \
	pub/tinymce/skins/ui/oxide-dark/content.js \
	pub/tinymce/skins/ui/oxide-dark/skin.shadowdom.css \
	pub/tinymce/skins/ui/oxide-dark/skin.min.css \
	pub/tinymce/skins/ui/oxide-dark/skin.shadowdom.min.css \
	pub/tinymce/skins/ui/oxide-dark/content.inline.min.css \
	pub/tinymce/skins/ui/oxide-dark/skin.js \
	pub/tinymce/skins/ui/oxide-dark/skin.css \
	pub/tinymce/skins/ui/tinymce-5-dark/skin.shadowdom.js \
	pub/tinymce/skins/ui/tinymce-5-dark/content.inline.css \
	pub/tinymce/skins/ui/tinymce-5-dark/content.css \
	pub/tinymce/skins/ui/tinymce-5-dark/content.inline.js \
	pub/tinymce/skins/ui/tinymce-5-dark/content.min.css \
	pub/tinymce/skins/ui/tinymce-5-dark/content.js \
	pub/tinymce/skins/ui/tinymce-5-dark/skin.shadowdom.css \
	pub/tinymce/skins/ui/tinymce-5-dark/skin.min.css \
	pub/tinymce/skins/ui/tinymce-5-dark/skin.shadowdom.min.css \
	pub/tinymce/skins/ui/tinymce-5-dark/content.inline.min.css \
	pub/tinymce/skins/ui/tinymce-5-dark/skin.js \
	pub/tinymce/skins/ui/tinymce-5-dark/skin.css \
	pub/tinymce/skins/ui/oxide/skin.shadowdom.js \
	pub/tinymce/skins/ui/oxide/content.inline.css \
	pub/tinymce/skins/ui/oxide/content.css \
	pub/tinymce/skins/ui/oxide/content.inline.js \
	pub/tinymce/skins/ui/oxide/content.min.css \
	pub/tinymce/skins/ui/oxide/content.js \
	pub/tinymce/skins/ui/oxide/skin.shadowdom.css \
	pub/tinymce/skins/ui/oxide/skin.min.css \
	pub/tinymce/skins/ui/oxide/skin.shadowdom.min.css \
	pub/tinymce/skins/ui/oxide/content.inline.min.css \
	pub/tinymce/skins/ui/oxide/skin.js \
	pub/tinymce/skins/ui/oxide/skin.css \
	pub/tinymce/skins/ui/tinymce-5/skin.shadowdom.js \
	pub/tinymce/skins/ui/tinymce-5/content.inline.css \
	pub/tinymce/skins/ui/tinymce-5/content.css \
	pub/tinymce/skins/ui/tinymce-5/content.inline.js \
	pub/tinymce/skins/ui/tinymce-5/content.min.css \
	pub/tinymce/skins/ui/tinymce-5/content.js \
	pub/tinymce/skins/ui/tinymce-5/skin.shadowdom.css \
	pub/tinymce/skins/ui/tinymce-5/skin.min.css \
	pub/tinymce/skins/ui/tinymce-5/skin.shadowdom.min.css \
	pub/tinymce/skins/ui/tinymce-5/content.inline.min.css \
	pub/tinymce/skins/ui/tinymce-5/skin.js \
	pub/tinymce/skins/ui/tinymce-5/skin.css \
	pub/tinymce/skins/content/tinymce-5-dark/content.css \
	pub/tinymce/skins/content/tinymce-5-dark/content.min.css \
	pub/tinymce/skins/content/tinymce-5-dark/content.js \
	pub/tinymce/skins/content/document/content.css \
	pub/tinymce/skins/content/document/content.min.css \
	pub/tinymce/skins/content/document/content.js \
	pub/tinymce/skins/content/default/content.css \
	pub/tinymce/skins/content/default/content.min.css \
	pub/tinymce/skins/content/default/content.js \
	pub/tinymce/skins/content/dark/content.css \
	pub/tinymce/skins/content/dark/content.min.css \
	pub/tinymce/skins/content/dark/content.js \
	pub/tinymce/skins/content/writer/content.css \
	pub/tinymce/skins/content/writer/content.min.css \
	pub/tinymce/skins/content/writer/content.js \
	pub/tinymce/skins/content/tinymce-5/content.css \
	pub/tinymce/skins/content/tinymce-5/content.min.css \
	pub/tinymce/skins/content/tinymce-5/content.js \
	pub/tinymce/tinymce.min.js \
	pub/tinymce/themes/silver/theme.min.js \
	pub/tinymce/themes/silver/theme.js \
	pub/tinymce/themes/silver/index.js

# TINYMCE
pub/tinymce/CHANGELOG.md: $(TINYMCE_VENDOR_DIR)/tinymce/CHANGELOG.md
	(cd pub/tinymce; mkdir -p models models/dom icons icons/default plugins plugins/autoresize \
	plugins/link plugins/advlist plugins/autolink plugins/code plugins/lists plugins/table \
	skins skins/ui skins/ui/oxide-dark skins/ui/tinymce-5-dark skins/ui/oxide \
	skins/ui/tinymce-5 skins/content skins/content/tinymce-5-dark skins/content/document \
	skins/content/default skins/content/dark skins/content/writer skins/content/tinymce-5 \
	themes themes/silver)
	cp $(TINYMCE_VENDOR_DIR)/tinymce/CHANGELOG.md pub/tinymce/CHANGELOG.md
	for f in $(TINYMCE_FILES); do\
	  a="$(TINYMCE_VENDOR_DIR)/"`echo $${f} | sed 's?pub/??'`; \
	  echo "Copying file: $${f}"; \
	  cp $${a} $${f}; \
	done
