all:
.PHONY: all

.NOTINTERMEDIATE:
.DELETE_ON_ERROR:

.PHONY: sync-from-webflow

# HTML
sync-from-webflow: static/index.html
#sync-from-webflow: static/about/index.html
#sync-from-webflow: static/home-copy/index.html
#sync-from-webflow: static/contact/index.html
#sync-from-webflow: static/team/index.html
#sync-from-webflow: static/blog/index.html
#sync-from-webflow: static/styleguide/index.html
#sync-from-webflow: static/press/index.html

# CSS & JS
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/js/webflow.fbf9e890f.js
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/css/umorpha.webflow.a8989709b.min.css
sync-from-webflow: static/other-assets/typed.min.js
sync-from-webflow: static/other-assets/jquery-3.5.1.min.dc5e7f18c8.js

# Images
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/650a2b4cc0091d7b7c21ae3d_portfolio-2---wide.svg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/650a2b4cc0091d7b7c21ae42_portfolio-1---wide.svg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/650a2b4cc0091d7b7c21ae43_portfolio-3---wide.svg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/65137e999ce90eda6bc2c5f1_placeholder_background.jpg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/65139fa5e0b8553d4c913fe5_logo_horizontal_2.svg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/6514f53226dcd604df7bb02c_oshwa.png
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/65150cff2a5918f4659245bb_eeba-new.svg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/65151c532305724bd7375cc3_colorado_green_building_guild.svg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/6515ffbd1e90dc6f533c7a85_umorpha_favcon.jpg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/651603b40788b95807194509_umorpha_opengraph.jpg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/6544458754dbcf28bbba8c70_umorpha_square_logo_black.svg
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21adaa/65c2e2af7a1dadffe6300e57_umorpha_logo_square_noborder_blue_256.png
sync-from-webflow: static/webflow-assets/650a2b4cc0091d7b7c21ae2e/6515e92ca9da14dcb48bcb9e_html_image_full.jpg

# Uploads
sync-from-webflow: static/webflow-uploads/650a2b4cc0091d7b7c21adaa/650a2db03a157b2c46cdd565_Futura-Std-Medium.ttf

# TODO: There aught to be a way to automate this:
#
# Open Firefox DevTools to the "Network" tab.  Check "Disable Cache"
# and "Persist Logs".  Visit the following pages:
#
#  - https://umorpha.webflow.io/
#  - https://umorpha.webflow.io/about
#  - https://umorpha.webflow.io/home-copy
#  - https://umorpha.webflow.io/contact
#  - https://umorpha.webflow.io/team
#  - https://umorpha.webflow.io/blog
#  - https://umorpha.webflow.io/styleguide
#  - https://umorpha.webflow.io/press
#  - https://assets-global.website-files.com/650a2b4cc0091d7b7c21adaa/650a2b4cc0091d7b7c21ae3d_portfolio%202%20-%20wide.svg
#  - https://assets-global.website-files.com/650a2b4cc0091d7b7c21adaa/650a2b4cc0091d7b7c21ae42_portfolio%201%20-%20wide.svg
#  - https://assets-global.website-files.com/650a2b4cc0091d7b7c21adaa/650a2b4cc0091d7b7c21ae43_portfolio%203%20-%20wide.svg
#  - https://assets-global.website-files.com/650a2b4cc0091d7b7c21adaa/651603b40788b95807194509_umorpha_opengraph.jpg
#
# "Save All As HAR"
#umorpha.webflow.io.har:
#	ffscript ...
mirror: umorpha.webflow.io.har unhar.go
	rm -rf $@
	go run unhar.go $@ <$<
mirror/%: mirror
	touch --no-create $@

rewrite = sed
rewrite += -e 's,https://assets-global\.website-files\.com/,/webflow-assets/,g'
rewrite += -e 's,https://uploads-ssl\.webflow\.com/,/webflow-uploads/,g'
rewrite += -e 's,https://cdn\.jsdelivr\.net/npm/typed\.js@2\.0\.9,/other-assets/typed.min.js,g'
rewrite += -e 's,https://cdnjs\.cloudflare\.com/ajax/libs/typed\.js/2\.0\.10/typed\.min\.js,/other-assets/typed.min.js,g'
rewrite += -e 's,https://d3e54v103j8qbb\.cloudfront\.net/js/jquery-3\.5\.1\.min\.dc5e7f18c8\.js?site=650a2b4cc0091d7b7c21adaa,/other-assets/jquery-3.5.1.min.dc5e7f18c8.js,g'

rewrite.css = sed
rewrite.css += -e 's/%20/-/g'
rewrite.css += -e '/background-image\.svg/d'

# TODO: Figure out what to do with the following URLs in
# webflow.fbf9e890f.js:
#
#  - https://(formdata\.)?webflow\.com/api/v1/form/650a2b4cc0091d7b7c21adaa(/signFile\?.*)?
#  - list-manage[0-9]?.com

rewrite.js1 = $(rewrite)
rewrite.js1 += -e 's@var ve=(t,f)=>()=>(f||t((f={exports:{}})\.exports,f),f\.exports);@var ve = function(body, module) { return function() { if (!module) { module = { exports: {} }; body(module.exports, module); } return module.exports; }; }@'

rewrite.js2 = sed -E
# Base
rewrite.js2 += -e 's/\bve\b/defineModule/g'
# Module names
rewrite.js2 += -e 's/\bVe\b/requireTramJS/g'
rewrite.js2 += -e 's/\brt\b/requireWebflowUnderscore/g'
rewrite.js2 += -e 's/\bLe\b/requireWebflow/g'
rewrite.js2 += -e 's/\bvt\b/requireWebflowBadge/g'
rewrite.js2 += -e 's/\bmt\b/requireWebflowEdit/g'
rewrite.js2 += -e 's/\bwt\b/requireWebflowFocusVisible/g'
rewrite.js2 += -e 's/\bEt\b/requireWebflowFocus/g'
rewrite.js2 += -e 's/\b_t\b/requireWebflowLinks/g'
rewrite.js2 += -e 's/\bOt\b/requireWebflowScroll/g'
rewrite.js2 += -e 's/\bSt\b/requireWebflowTouch/g'
rewrite.js2 += -e 's/\bAt\b/requireWebflowForms_helper/g'
rewrite.js2 += -e 's/\bCt\b/requireWebflowForms/g'
rewrite.js2 += -e 's/\bIt\b/requireWebflowNavbar_helper_helper/g'
rewrite.js2 += -e 's/\bPt\b/requireWebflowNavbar_helper/g'
rewrite.js2 += -e 's/\bNt\b/requireWebflowNavbar/g'
# Module args
rewrite.js2 += -e 's/\b(cn|ln|dn|hn|vn|pn|mn|gn|Je|bn|yn|En|xn)\b/exports/g'
rewrite.js2 += -e 's/\(fn\b/(exports/g'
rewrite.js2 += -e 's/\b(nt|lt|ht|gt|yt|xt|kt|Lt|Tt|Mt|Dt|qt)\b/module/g'
rewrite.js2 += -e 's/\bpt([^|])/module\1/g'
# Variables
rewrite.js2 += -e 's/\bie\b/Webflow/g'
rewrite.js2 += -e 's/\bWe\b/webflowModules/g'
rewrite.js2 += -e 's/\bat\b/activate/g'
rewrite.js2 += -e 's/\bst\b/deactivate/g'
rewrite.js2 += -e 's/\b(Fe|et|nn|Bt)\b/jQuery/g' # can't use '$' because other vars/funcs got minified to '$'
rewrite.js2 += -e 's/\bxe\b/underscore/g' # can't use '_' because other vars/funcs got minified to '_'
rewrite.js2 += -e 's/\b(Qe|Jt|bt|Pe|Ue|en|Be|Me)\b/Webflow/g'
rewrite.js2 += -e 's/(\bot|\$$t)\b/tram/g'
rewrite.js2 += -e 's/\bOe=[^.]*\.isFunction,//' -e 's/\bOe\(/jQuery.isFunction(/g'
rewrite.js2 += -e 's/\bXt=[^.]*\(document\),//' -e 's/\bXt\b/jQuery(document)/g'
rewrite.js2 += -e 's/\b_e=[^.]*\(window\),//' -e 's/\b_e\b/jQuery(window)/g'
rewrite.js2 += -e 's/\bSe\b/exports/g'
# Un-golf
rewrite.js2 += -e 's/!0\b/true/g'
rewrite.js2 += -e 's/!1\b/false/g'

rewrite.js3 = sed -E
# Module args
rewrite.js3 += -e '/^        Webflow\.define\("[^"]+", module\.exports = function\(t, f\)/,/^        }\)/{ s/\bt\b/jQuery/g; s/\bf\b/underscore/g; }'
rewrite.js3 += -e '/^        Webflow\.define\("[^"]+", module\.exports = function\(t\)/,/^        }\)/{ s/\bt\b/jQuery/g; }'
# Functional changes
rewrite.js3 += -e '/requireWebflowBadge\(\);/d' -e '/var requireWebflowBadge =/,/^    }\);/d'
rewrite.js3 += -e '/requireWebflowEdit\(\);/d' -e '/var requireWebflowEdit =/,/^    }\);/d'

static/%.html: mirror/umorpha.webflow.io/%.html patches/%.html.patch Makefile
	mkdir -p $(@D)
	<$< $(rewrite) | (tidy -quiet -indent -wrap 0||true) >$@
	patch -i patches/$*.html.patch $@
static/%.html: mirror/umorpha.webflow.io/%.html Makefile
	mkdir -p $(@D)
	<$< $(rewrite) | (tidy -quiet -indent -wrap 0||true) >$@
	! test -f patches/$*.html.patch
static/webflow-assets/%.js: mirror/assets-global.website-files.com/%.js Makefile
	mkdir -p $(@D)
	<$< $(rewrite.js1) | $(rewrite.js2) | js-beautify --stdin --end-with-newline | $(rewrite.js3) >$@
static/webflow-assets/%.css: mirror/assets-global.website-files.com/%.css Makefile
	mkdir -p $(@D)
	<$< $(rewrite) | css-beautify --stdin --end-with-newline | $(rewrite.css) >$@
static/webflow-assets/%: mirror/assets-global.website-files.com/% Makefile
	mkdir -p $(@D)
	$(rewrite) <$< >$@
static/webflow-uploads/%: mirror/uploads-ssl.webflow.com/% Makefile
	mkdir -p $(@D)
	$(rewrite) <$< >$@
static/other-assets/typed.min.js: mirror/cdnjs.cloudflare.com/ajax/libs/typed.js/2.0.10/typed.min.js
	mkdir -p $(@D)
	cp $< $@
static/other-assets/jquery-3.5.1.min.dc5e7f18c8.js: mirror/d3e54v103j8qbb.cloudfront.net/js/jquery-3.5.1.min.dc5e7f18c8.js
	mkdir -p $(@D)
	cp $< $@
