<commit_before># Unexport all make variables to prevent passing our own make variables down to
# sub-processes.
#
# Since the bulk of what we're doing is building other projects, this prevents
# odd issues from cropping up when building other projects and those picking up
# our own DESTDIR or other variables that the child projects might happen to
# use (BUILD_DIR, etc).
unexport
MAKEOVERRIDES=

STANDARD_PATH:=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
PREFIX:=/opt/api-umbrella
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
BUILD_DIR:=$(ROOT_DIR)/build
WORK_DIR:=$(BUILD_DIR)/work
DEPS_DIR:=$(WORK_DIR)/deps
STAGE_DIR:=$(WORK_DIR)/stage
STAGE_PREFIX:=$(STAGE_DIR)$(PREFIX)
STAGE_MARKERS_DIR:=$(STAGE_DIR)/.installed
VENDOR_DIR:=$(WORK_DIR)/vendor
BUNDLE_DIR:=$(VENDOR_DIR)/bundle
LUAROCKS_DIR:=$(VENDOR_DIR)/lib/luarocks/rocks
LUAROCKS_CMD:=LUA_PATH="$(STAGE_PREFIX)/embedded/openresty/luajit/share/lua/5.1/?.lua;$(STAGE_PREFIX)/embedded/openresty/luajit/share/lua/5.1/?/init.lua;;" $(STAGE_PREFIX)/embedded/bin/luarocks
LUA_SHARE_DIR:=$(VENDOR_DIR)/share/lua/5.1
LUA_LIB_DIR:=$(VENDOR_DIR)/lib/lua/5.1
TEST_PREFIX:=/opt/api-umbrella/test-env
TEST_STAGE_PREFIX:=$(STAGE_DIR)$(TEST_PREFIX)
TEST_VENDOR_DIR:=$(WORK_DIR)/test-env/vendor
TEST_LUAROCKS_DIR:=$(TEST_VENDOR_DIR)/lib/luarocks/rocks
TEST_LUA_SHARE_DIR:=$(TEST_VENDOR_DIR)/share/lua/5.1
TEST_LUA_LIB_DIR:=$(TEST_VENDOR_DIR)/lib/lua/5.1
VERSION_SEP:=-version-
RELEASE_TIMESTAMP:=$(shell date -u +%Y%m%d%H%M%S)

#
# Dependencies
#
API_UMBRELLA_STATIC_SITE_VERSION:=48d3d4379bb29ca37cd4e25900e793aafe0ca414
API_UMBRELLA_STATIC_SITE_NAME:=api-umbrella-static-site
API_UMBRELLA_STATIC_SITE:=$(API_UMBRELLA_STATIC_SITE_NAME)-$(API_UMBRELLA_STATIC_SITE_VERSION)
API_UMBRELLA_STATIC_SITE_DIGEST:=md5
API_UMBRELLA_STATIC_SITE_CHECKSUM:=9bcc73c1febd6d7a5aec3a6cc5e940e9
API_UMBRELLA_STATIC_SITE_URL:=https://github.com/NREL/api-umbrella-static-site/archive/$(API_UMBRELLA_STATIC_SITE_VERSION).tar.gz
API_UMBRELLA_STATIC_SITE_INSTALL_MARKER:=$(API_UMBRELLA_STATIC_SITE_NAME)$(VERSION_SEP)$(API_UMBRELLA_STATIC_SITE_VERSION)

BUNDLER_VERSION:=1.10.6
BUNDLER_NAME:=bundler
BUNDLER:=$(BUNDLER_NAME)-$(BUNDLER_VERSION)
BUNDLER_INSTALL_MARKER:=$(BUNDLER_NAME)$(VERSION_SEP)$(BUNDLER_VERSION)

ELASTICSEARCH_VERSION:=1.7.3
ELASTICSEARCH_NAME:=elasticsearch
ELASTICSEARCH:=$(ELASTICSEARCH_NAME)-$(ELASTICSEARCH_VERSION)
ELASTICSEARCH_DIGEST:=sha1
ELASTICSEARCH_CHECKSUM:=754b089ec0a1aae5b36b39391d5385ed7428d8f5
ELASTICSEARCH_URL:=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-$(ELASTICSEARCH_VERSION).tar.gz
ELASTICSEARCH_INSTALL_MARKER:=$(ELASTICSEARCH_NAME)$(VERSION_SEP)$(ELASTICSEARCH_VERSION)

GLIDE_VERSION:=0.6.1
GLIDE_NAME:=glide
GLIDE:=$(GLIDE_NAME)-$(GLIDE_VERSION)
GLIDE_DIGEST:=md5
GLIDE_CHECKSUM:=9067182f14b510015ffc30eae9a5329e
GLIDE_URL:=https://github.com/Masterminds/glide/archive/$(GLIDE_VERSION).tar.gz
GLIDE_INSTALL_MARKER:=$(GLIDE_NAME)$(VERSION_SEP)$(GLIDE_VERSION)

GOLANG_VERSION:=1.5.1
GOLANG_NAME:=golang
GOLANG:=$(GOLANG_NAME)-$(GOLANG_VERSION)
GOLANG_DIGEST:=sha1
GOLANG_CHECKSUM:=46eecd290d8803887dec718c691cc243f2175fe0
GOLANG_URL:=https://storage.googleapis.com/golang/go$(GOLANG_VERSION).linux-amd64.tar.gz
GOLANG_INSTALL_MARKER:=$(GOLANG_NAME)$(VERSION_SEP)$(GOLANG_VERSION)

HEKA_VERSION:=0.9.2
HEKA_VERSION_UNDERSCORE:=$(shell echo $(HEKA_VERSION) | sed -e 's/\./_/g')
HEKA_NAME:=heka
HEKA:=$(HEKA_NAME)-$(HEKA_VERSION)
HEKA_DIGEST:=md5
HEKA_CHECKSUM:=864625dff702306eba1494149ff903ee
HEKA_URL:=https://github.com/mozilla-services/heka/releases/download/v$(HEKA_VERSION)/heka-$(HEKA_VERSION_UNDERSCORE)-linux-amd64.tar.gz
HEKA_INSTALL_MARKER:=$(HEKA_NAME)$(VERSION_SEP)$(HEKA_VERSION)

LIBCIDR_VERSION:=1.2.3
LIBCIDR_NAME:=libcidr
LIBCIDR:=$(LIBCIDR_NAME)-$(LIBCIDR_VERSION)
LIBCIDR_DIGEST:=md5
LIBCIDR_CHECKSUM:=c5efcc7ae114fdaa5583f58dacecd9de
LIBCIDR_URL:=https://www.over-yonder.net/~fullermd/projects/libcidr/libcidr-$(LIBCIDR_VERSION).tar.xz
LIBCIDR_INSTALL_MARKER:=$(LIBCIDR_NAME)$(VERSION_SEP)$(LIBCIDR_VERSION)

LIBMAXMINDDB_VERSION:=1.1.1
LIBMAXMINDDB_NAME:=libmaxminddb
LIBMAXMINDDB:=$(LIBMAXMINDDB_NAME)-$(LIBMAXMINDDB_VERSION)
LIBMAXMINDDB_DIGEST:=md5
LIBMAXMINDDB_CHECKSUM:=36c31f0814dbf71b210ee57c2b9ef98c
LIBMAXMINDDB_URL:=https://github.com/maxmind/libmaxminddb/releases/download/$(LIBMAXMINDDB_VERSION)/libmaxminddb-$(LIBMAXMINDDB_VERSION).tar.gz
LIBMAXMINDDB_INSTALL_MARKER:=$(LIBMAXMINDDB_NAME)$(VERSION_SEP)$(LIBMAXMINDDB_VERSION)

LUA_RESTY_DNS_CACHE_VERSION:=691613739a32f8405e56e56547270b9f72e77c34
LUA_RESTY_DNS_CACHE_NAME:=lua-resty-dns-cache
LUA_RESTY_DNS_CACHE:=$(LUA_RESTY_DNS_CACHE_NAME)-$(LUA_RESTY_DNS_CACHE_VERSION)
LUA_RESTY_DNS_CACHE_DIGEST:=md5
LUA_RESTY_DNS_CACHE_CHECKSUM:=c7304c1f434ac251246904db51423d5e
LUA_RESTY_DNS_CACHE_URL:=https://github.com/hamishforbes/lua-resty-dns-cache/archive/$(LUA_RESTY_DNS_CACHE_VERSION).tar.gz
LUA_RESTY_DNS_CACHE_INSTALL_MARKER:=$(LUA_RESTY_DNS_CACHE_NAME)$(VERSION_SEP)$(LUA_RESTY_DNS_CACHE_VERSION)

LUA_RESTY_HTTP_VERSION:=0.06
LUA_RESTY_HTTP_NAME:=lua-resty-http
LUA_RESTY_HTTP:=$(LUA_RESTY_HTTP_NAME)-$(LUA_RESTY_HTTP_VERSION)
LUA_RESTY_HTTP_DIGEST:=md5
LUA_RESTY_HTTP_CHECKSUM:=d828ba0da7bc8f39e0ce0565912aa597
LUA_RESTY_HTTP_URL:=https://github.com/pintsized/lua-resty-http/archive/v$(LUA_RESTY_HTTP_VERSION).tar.gz
LUA_RESTY_HTTP_INSTALL_MARKER:=$(LUA_RESTY_HTTP_NAME)$(VERSION_SEP)$(LUA_RESTY_HTTP_VERSION)

LUA_RESTY_LOGGER_SOCKET_VERSION:=d435ea6052c0d252cf7f89fe4b7cb9c69306de93
LUA_RESTY_LOGGER_SOCKET_NAME:=lua-resty-logger-socket
LUA_RESTY_LOGGER_SOCKET:=$(LUA_RESTY_LOGGER_SOCKET_NAME)-$(LUA_RESTY_LOGGER_SOCKET_VERSION)
LUA_RESTY_LOGGER_SOCKET_DIGEST:=md5
LUA_RESTY_LOGGER_SOCKET_CHECKSUM:=6d7273438100ddcdfa57bdbf1a8c3a01
LUA_RESTY_LOGGER_SOCKET_URL:=https://github.com/cloudflare/lua-resty-logger-socket/archive/$(LUA_RESTY_LOGGER_SOCKET_VERSION).tar.gz
LUA_RESTY_LOGGER_SOCKET_INSTALL_MARKER:=$(LUA_RESTY_LOGGER_SOCKET_NAME)$(VERSION_SEP)$(LUA_RESTY_LOGGER_SOCKET_VERSION)

LUA_RESTY_SHCACHE_VERSION:=fb2e275c2cdca08eaa34a7b73375e41ac3eff200
LUA_RESTY_SHCACHE_NAME:=lua-resty-shcache
LUA_RESTY_SHCACHE:=$(LUA_RESTY_SHCACHE_NAME)-$(LUA_RESTY_SHCACHE_VERSION)
LUA_RESTY_SHCACHE_DIGEST:=md5
LUA_RESTY_SHCACHE_CHECKSUM:=5d3cbcf8fbad1954cdcb3826afa41afe
LUA_RESTY_SHCACHE_URL:=https://github.com/cloudflare/lua-resty-shcache/archive/$(LUA_RESTY_SHCACHE_VERSION).tar.gz
LUA_RESTY_SHCACHE_INSTALL_MARKER:=$(LUA_RESTY_SHCACHE_NAME)$(VERSION_SEP)$(LUA_RESTY_SHCACHE_VERSION)

LUA_RESTY_UUID_VERSION:=834e69c1603b796ffb3ec2921f720f3059cb62c0
LUA_RESTY_UUID_NAME:=lua-resty-uuid
LUA_RESTY_UUID:=$(LUA_RESTY_UUID_NAME)-$(LUA_RESTY_UUID_VERSION)
LUA_RESTY_UUID_DIGEST:=md5
LUA_RESTY_UUID_CHECKSUM:=044bb66af54bbf11f2f53eb62535e4ff
LUA_RESTY_UUID_URL:=https://github.com/bungle/lua-resty-uuid/archive/$(LUA_RESTY_UUID_VERSION).tar.gz
LUA_RESTY_UUID_INSTALL_MARKER:=$(LUA_RESTY_UUID_NAME)$(VERSION_SEP)$(LUA_RESTY_UUID_VERSION)

LUAROCKS_VERSION:=2.2.2
LUAROCKS_NAME:=luarocks
LUAROCKS:=$(LUAROCKS_NAME)-$(LUAROCKS_VERSION)
LUAROCKS_DIGEST:=md5
LUAROCKS_CHECKSUM:=5a830953d27715cc955119609f8096e6
LUAROCKS_URL:=http://luarocks.org/releases/luarocks-$(LUAROCKS_VERSION).tar.gz
LUAROCKS_INSTALL_MARKER:=$(LUAROCKS_NAME)$(VERSION_SEP)$(LUAROCKS_VERSION)

LUSTACHE_VERSION:=241b3a16f358035887c2c05c6e151c1f48401a42
LUSTACHE_NAME:=lustache
LUSTACHE:=$(LUSTACHE_NAME)-$(LUSTACHE_VERSION)
LUSTACHE_DIGEST:=md5
LUSTACHE_CHECKSUM:=7c64dd36bbb02e71a0e60e847b70d561
LUSTACHE_URL:=https://github.com/Olivine-Labs/lustache/archive/$(LUSTACHE_VERSION).tar.gz
LUSTACHE_INSTALL_MARKER:=$(LUSTACHE_NAME)$(VERSION_SEP)$(LUSTACHE_VERSION)

MONGODB_VERSION:=3.0.7
MONGODB_NAME:=mongodb
MONGODB:=$(MONGODB_NAME)-$(MONGODB_VERSION)
MONGODB_DIGEST:=md5
MONGODB_CHECKSUM:=e3894b76089fa9d38901c96bc5516a14
MONGODB_URL:=https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-$(MONGODB_VERSION).tgz
MONGODB_INSTALL_MARKER:=$(MONGODB_NAME)$(VERSION_SEP)$(MONGODB_VERSION)

MORA_VERSION:=0c409c9cbb283708e92cc69a50281ac536f97874
MORA_NAME:=mora
MORA:=$(MORA_NAME)-$(MORA_VERSION)
MORA_DIGEST:=md5
MORA_CHECKSUM:=563945c899b30099543254df84b487d7
MORA_URL:=https://github.com/emicklei/mora/archive/$(MORA_VERSION).tar.gz
MORA_DEPENDENCIES_CHECKSUM:=$(shell openssl md5 $(BUILD_DIR)/mora_glide.yaml | sed 's/^.* //')
MORA_INSTALL_MARKER:=$(MORA_NAME)$(VERSION_SEP)$(MORA_VERSION)-$(MORA_DEPENDENCIES_CHECKSUM)

NGX_TXID_VERSION:=f1c197cb9c42e364a87fbb28d5508e486592ca42
NGX_TXID_NAME:=ngx_txid
NGX_TXID:=$(NGX_TXID_NAME)-$(NGX_TXID_VERSION)
NGX_TXID_DIGEST:=md5
NGX_TXID_CHECKSUM:=408ee86eb6e42e27a51514f711c41d6b
NGX_TXID_URL:=https://github.com/streadway/ngx_txid/archive/$(NGX_TXID_VERSION).tar.gz
NGX_TXID_INSTALL_MARKER:=$(NGX_TXID_NAME)$(VERSION_SEP)$(NGX_TXID_VERSION)

NGX_DYUPS_VERSION:=0.2.8
NGX_DYUPS_NAME:=ngx_http_dyups_module
NGX_DYUPS:=$(NGX_DYUPS_NAME)-$(NGX_DYUPS_VERSION)
NGX_DYUPS_DIGEST:=md5
NGX_DYUPS_CHECKSUM:=295b7cb202de069b313f4da50d6952e0
NGX_DYUPS_URL:=https://github.com/yzprofile/ngx_http_dyups_module/archive/v$(NGX_DYUPS_VERSION).tar.gz
NGX_DYUPS_INSTALL_MARKER:=$(NGX_DYUPS_NAME)$(VERSION_SEP)$(NGX_DYUPS_VERSION)

NGX_GEOIP2_VERSION:=1.0
NGX_GEOIP2_NAME:=ngx_http_geoip2_module
NGX_GEOIP2:=$(NGX_GEOIP2_NAME)-$(NGX_GEOIP2_VERSION)
NGX_GEOIP2_DIGEST:=md5
NGX_GEOIP2_CHECKSUM:=4c89eea53ce8318f940c03adfe0b502b
NGX_GEOIP2_URL:=https://github.com/leev/ngx_http_geoip2_module/archive/1.0.tar.gz
NGX_GEOIP2_INSTALL_MARKER:=$(NGX_GEOIP2_NAME)$(VERSION_SEP)$(NGX_GEOIP2_VERSION)

OPENRESTY_VERSION:=1.9.3.1
OPENRESTY_NAME:=openresty
OPENRESTY:=$(OPENRESTY_NAME)-$(OPENRESTY_VERSION)
OPENRESTY_DIGEST:=md5
OPENRESTY_CHECKSUM:=cde1f7127f6ba413ee257003e49d6d0a
OPENRESTY_URL:=http://openresty.org/download/ngx_openresty-$(OPENRESTY_VERSION).tar.gz
OPENRESTY_INSTALL_MARKER:=$(OPENRESTY_NAME)$(VERSION_SEP)$(OPENRESTY_VERSION)

PCRE_VERSION:=8.37
PCRE_NAME:=pcre
PCRE:=$(PCRE_NAME)-$(PCRE_VERSION)
PCRE_DIGEST:=md5
PCRE_CHECKSUM:=ed91be292cb01d21bc7e526816c26981
PCRE_URL:=http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-$(PCRE_VERSION).tar.bz2
PCRE_INSTALL_MARKER:=$(PCRE_NAME)$(VERSION_SEP)$(PCRE_VERSION)

PERP_VERSION:=2.07
PERP_NAME:=perp
PERP:=$(PERP_NAME)-$(PERP_VERSION)
PERP_DIGEST:=md5
PERP_CHECKSUM:=a2acc7425d556d9635a25addcee9edb5
PERP_URL:=http://b0llix.net/perp/distfiles/perp-$(PERP_VERSION).tar.gz
PERP_INSTALL_MARKER:=$(PERP_NAME)$(VERSION_SEP)$(PERP_VERSION)

RUBY_VERSION:=2.2.3
RUBY_NAME:=ruby
RUBY:=$(RUBY_NAME)-$(RUBY_VERSION)
RUBY_DIGEST:=sha256
RUBY_CHECKSUM:=df795f2f99860745a416092a4004b016ccf77e8b82dec956b120f18bdc71edce
RUBY_URL:=https://cache.ruby-lang.org/pub/ruby/2.2/ruby-$(RUBY_VERSION).tar.gz
RUBY_INSTALL_MARKER:=$(RUBY_NAME)$(VERSION_SEP)$(RUBY_VERSION)

RUNIT_VERSION:=2.1.2
RUNIT_NAME:=runit
RUNIT:=$(RUNIT_NAME)-$(RUNIT_VERSION)
RUNIT_DIGEST:=md5
RUNIT_CHECKSUM:=6c985fbfe3a34608eb3c53dc719172c4
RUNIT_URL:=http://smarden.org/runit/runit-$(RUNIT_VERSION).tar.gz
RUNIT_INSTALL_MARKER:=$(RUNIT_NAME)$(VERSION_SEP)$(RUNIT_VERSION)

# Don't move to 6.0.0 quite yet until we have a better sense of this issue:
# http://mail-archives.apache.org/mod_mbox/trafficserver-users/201510.mbox/%3c1443975393.1364867.400869481.2BFF6EEF@webmail.messagingengine.com%3e
TRAFFICSERVER_VERSION:=5.3.1
TRAFFICSERVER_NAME:=trafficserver
TRAFFICSERVER:=$(TRAFFICSERVER_NAME)-$(TRAFFICSERVER_VERSION)
TRAFFICSERVER_DIGEST:=md5
TRAFFICSERVER_CHECKSUM:=9c0e2450b1dd1bbdd63ebcc344b5a813
TRAFFICSERVER_URL:=http://mirror.olnevhost.net/pub/apache/trafficserver/trafficserver-$(TRAFFICSERVER_VERSION).tar.bz2
TRAFFICSERVER_INSTALL_MARKER:=$(TRAFFICSERVER_NAME)$(VERSION_SEP)$(TRAFFICSERVER_VERSION)

#
# LuaRocks Dependencies
#
INSPECT:=inspect
INSPECT_VERSION:=3.0-1
LIBCIDR_FFI:=libcidr-ffi
LIBCIDR_FFI_VERSION:=0.1.0-1
LIBCIDR_FFI_URL:=https://raw.githubusercontent.com/GUI/lua-libcidr-ffi/master/libcidr-ffi-git-1.rockspec
LUA_CMSGPACK:=lua-cmsgpack
LUA_CMSGPACK_VERSION:=0.4.0-0
LUAPOSIX:=luaposix
LUAPOSIX_VERSION:=33.3.1-1
LUASOCKET:=luasocket
LUASOCKET_VERSION:=2.0.2-6
LYAML:=lyaml
LYAML_VERSION:=6.0-1
PENLIGHT:=penlight
PENLIGHT_VERSION:=1.3.2-2

#
# Test Dependencies
#
UNBOUND_VERSION:=1.5.6
UNBOUND_NAME:=unbound
UNBOUND:=$(UNBOUND_NAME)-$(UNBOUND_VERSION)
UNBOUND_DIGEST:=sha256
UNBOUND_CHECKSUM:=ad3823f5895f59da9e408ea273fcf81d8a76914c18864fba256d7f140b83e404
UNBOUND_URL:=https://www.unbound.net/downloads/unbound-$(UNBOUND_VERSION).tar.gz
UNBOUND_INSTALL_MARKER:=$(UNBOUND_NAME)$(VERSION_SEP)$(UNBOUND_VERSION)

#
# LuaRocks Test Dependencies
#
LUACHECK:=luacheck
LUACHECK_VERSION:=0.11.1-1

# Define non-file/folder targets
.PHONY: \
	all \
	clean \
	dependencies \
	install \
	local_work_dir \
	stage \
	stage_dependencies \
	test_dependencies \
	lint \
	test

define download
	$(eval DOWNLOAD_PATH:=$@)
	$(eval DOWNLOAD_URL:=$($(1)_URL))
	curl -L -o $(DOWNLOAD_PATH) $(DOWNLOAD_URL)
	touch $(DOWNLOAD_PATH)
endef

define decompress
	$(eval DOWNLOAD_PATH:=$<)
	$(eval DIR:=$@)
	$(eval CHECKSUM_TYPE:=$($(1)_DIGEST))
	$(eval CHECKSUM:=$($(1)_CHECKSUM))
	$(eval ARCHIVE_DEPTH:=$(if $(2),$(2),1))
	openssl $(CHECKSUM_TYPE) $(DOWNLOAD_PATH) | grep $(CHECKSUM) || (echo "checksum mismatch $(DOWNLOAD_PATH)" && exit 1)
	mkdir -p $(DIR)
	tar --strip-components $(ARCHIVE_DEPTH) -C $(DIR) -xf $(DOWNLOAD_PATH)
	touch $(DIR)
endef

define luarocks_install
	$(eval PACKAGE:=$($(1)))
	$(eval PACKAGE_VERSION:=$($(1)_VERSION))
	$(LUAROCKS_CMD) --tree=$(VENDOR_DIR) install $(PACKAGE) $(PACKAGE_VERSION)
	touch $@
endef

define test_luarocks_install
	$(eval PACKAGE:=$($(1)))
	$(eval PACKAGE_VERSION:=$($(1)_VERSION))
	$(LUAROCKS_CMD) --tree=$(TEST_VENDOR_DIR) install $(PACKAGE) $(PACKAGE_VERSION)
	touch $@
endef

all: stage

$(DEPS_DIR):
	mkdir -p $@
	touch $@

$(STAGE_PREFIX)/embedded/bin:
	mkdir -p $@
	touch $@

$(STAGE_PREFIX)/embedded/sbin:
	mkdir -p $@
	touch $@

$(STAGE_MARKERS_DIR):
	mkdir -p $@
	touch $@

# api-umbrella-core
$(STAGE_MARKERS_DIR)/api-umbrella-core-web-bundled: $(ROOT_DIR)/src/api-umbrella/web-app/Gemfile $(ROOT_DIR)/src/api-umbrella/web-app/Gemfile.lock | $(VENDOR_DIR) $(STAGE_MARKERS_DIR) $(STAGE_MARKERS_DIR)/$(BUNDLER_INSTALL_MARKER)
	rm -rf $(ROOT_DIR)/src/api-umbrella/web-app/.bundle
	cd $(ROOT_DIR)/src/api-umbrella/web-app && PATH=$(STAGE_PREFIX)/embedded/bin:$(PATH) bundle install --path=$(BUNDLE_DIR)
	touch $@

# Rebuild when the contents of any of the asset files change. We base this on a
# checksum of all the files, rather than timestamps, so that in our CI
# environment we skip precompiling (it's slow), if none of the files changed
# (but since the CI does a fresh checkout, all the timestamps on the files
# change, so make's normal checking would trigger changes).
WEB_ASSETS_CHECKSUM:=$(shell find $(ROOT_DIR)/src/api-umbrella/web-app/app/assets $(ROOT_DIR)/src/api-umbrella/web-app/Gemfile.lock -type f -exec cksum {} \; | sort | openssl md5 | sed 's/^.* //')
$(STAGE_MARKERS_DIR)/api-umbrella-core-web-assets-$(WEB_ASSETS_CHECKSUM): $(STAGE_MARKERS_DIR)/api-umbrella-core-web-bundled | $(STAGE_MARKERS_DIR)
	# Compile the assets, but then move them to a temporary build directory so
	# they aren't used when working in development mode.
	cd $(ROOT_DIR)/src/api-umbrella/web-app && PATH=$(STAGE_PREFIX)/embedded/bin:$(PATH) DEVISE_SECRET_KEY=temp RAILS_SECRET_TOKEN=temp bundle exec rake assets:precompile
	mkdir -p $(WORK_DIR)/tmp/web-assets
	cd $(ROOT_DIR)/src/api-umbrella/web-app && rsync -a --delete-after public/web-assets/ $(WORK_DIR)/tmp/web-assets/
	rm -rf $(ROOT_DIR)/src/api-umbrella/web-app/public/web-assets
	touch $@

$(STAGE_MARKERS_DIR)/api-umbrella-core: $(STAGE_MARKERS_DIR)/api-umbrella-core-dependencies | $(STAGE_MARKERS_DIR)
	# Create a new release directory, copying the relevant source code from the
	# current repo checkout into the release (but excluding tests, etc).
	rm -rf $(STAGE_PREFIX)/embedded/apps/core/releases
	mkdir -p $(STAGE_PREFIX)/embedded/apps/core/releases/$(RELEASE_TIMESTAMP)
	rsync -a \
		--filter=":- $(ROOT_DIR)/.gitignore" \
		--include="/templates/etc/perp/.boot" \
		--exclude=".*" \
		--exclude="/src/api-umbrella/web-app/spec" \
		--exclude="/src/api-umbrella/web-app/app/assets" \<commit_msg>Exclude test environment config files from package release.<commit_after># Unexport all make variables to prevent passing our own make variables down to
# sub-processes.
#
# Since the bulk of what we're doing is building other projects, this prevents
# odd issues from cropping up when building other projects and those picking up
# our own DESTDIR or other variables that the child projects might happen to
# use (BUILD_DIR, etc).
unexport
MAKEOVERRIDES=

STANDARD_PATH:=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
PREFIX:=/opt/api-umbrella
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
BUILD_DIR:=$(ROOT_DIR)/build
WORK_DIR:=$(BUILD_DIR)/work
DEPS_DIR:=$(WORK_DIR)/deps
STAGE_DIR:=$(WORK_DIR)/stage
STAGE_PREFIX:=$(STAGE_DIR)$(PREFIX)
STAGE_MARKERS_DIR:=$(STAGE_DIR)/.installed
VENDOR_DIR:=$(WORK_DIR)/vendor
BUNDLE_DIR:=$(VENDOR_DIR)/bundle
LUAROCKS_DIR:=$(VENDOR_DIR)/lib/luarocks/rocks
LUAROCKS_CMD:=LUA_PATH="$(STAGE_PREFIX)/embedded/openresty/luajit/share/lua/5.1/?.lua;$(STAGE_PREFIX)/embedded/openresty/luajit/share/lua/5.1/?/init.lua;;" $(STAGE_PREFIX)/embedded/bin/luarocks
LUA_SHARE_DIR:=$(VENDOR_DIR)/share/lua/5.1
LUA_LIB_DIR:=$(VENDOR_DIR)/lib/lua/5.1
TEST_PREFIX:=/opt/api-umbrella/test-env
TEST_STAGE_PREFIX:=$(STAGE_DIR)$(TEST_PREFIX)
TEST_VENDOR_DIR:=$(WORK_DIR)/test-env/vendor
TEST_LUAROCKS_DIR:=$(TEST_VENDOR_DIR)/lib/luarocks/rocks
TEST_LUA_SHARE_DIR:=$(TEST_VENDOR_DIR)/share/lua/5.1
TEST_LUA_LIB_DIR:=$(TEST_VENDOR_DIR)/lib/lua/5.1
VERSION_SEP:=-version-
RELEASE_TIMESTAMP:=$(shell date -u +%Y%m%d%H%M%S)

#
# Dependencies
#
API_UMBRELLA_STATIC_SITE_VERSION:=48d3d4379bb29ca37cd4e25900e793aafe0ca414
API_UMBRELLA_STATIC_SITE_NAME:=api-umbrella-static-site
API_UMBRELLA_STATIC_SITE:=$(API_UMBRELLA_STATIC_SITE_NAME)-$(API_UMBRELLA_STATIC_SITE_VERSION)
API_UMBRELLA_STATIC_SITE_DIGEST:=md5
API_UMBRELLA_STATIC_SITE_CHECKSUM:=9bcc73c1febd6d7a5aec3a6cc5e940e9
API_UMBRELLA_STATIC_SITE_URL:=https://github.com/NREL/api-umbrella-static-site/archive/$(API_UMBRELLA_STATIC_SITE_VERSION).tar.gz
API_UMBRELLA_STATIC_SITE_INSTALL_MARKER:=$(API_UMBRELLA_STATIC_SITE_NAME)$(VERSION_SEP)$(API_UMBRELLA_STATIC_SITE_VERSION)

BUNDLER_VERSION:=1.10.6
BUNDLER_NAME:=bundler
BUNDLER:=$(BUNDLER_NAME)-$(BUNDLER_VERSION)
BUNDLER_INSTALL_MARKER:=$(BUNDLER_NAME)$(VERSION_SEP)$(BUNDLER_VERSION)

ELASTICSEARCH_VERSION:=1.7.3
ELASTICSEARCH_NAME:=elasticsearch
ELASTICSEARCH:=$(ELASTICSEARCH_NAME)-$(ELASTICSEARCH_VERSION)
ELASTICSEARCH_DIGEST:=sha1
ELASTICSEARCH_CHECKSUM:=754b089ec0a1aae5b36b39391d5385ed7428d8f5
ELASTICSEARCH_URL:=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-$(ELASTICSEARCH_VERSION).tar.gz
ELASTICSEARCH_INSTALL_MARKER:=$(ELASTICSEARCH_NAME)$(VERSION_SEP)$(ELASTICSEARCH_VERSION)

GLIDE_VERSION:=0.6.1
GLIDE_NAME:=glide
GLIDE:=$(GLIDE_NAME)-$(GLIDE_VERSION)
GLIDE_DIGEST:=md5
GLIDE_CHECKSUM:=9067182f14b510015ffc30eae9a5329e
GLIDE_URL:=https://github.com/Masterminds/glide/archive/$(GLIDE_VERSION).tar.gz
GLIDE_INSTALL_MARKER:=$(GLIDE_NAME)$(VERSION_SEP)$(GLIDE_VERSION)

GOLANG_VERSION:=1.5.1
GOLANG_NAME:=golang
GOLANG:=$(GOLANG_NAME)-$(GOLANG_VERSION)
GOLANG_DIGEST:=sha1
GOLANG_CHECKSUM:=46eecd290d8803887dec718c691cc243f2175fe0
GOLANG_URL:=https://storage.googleapis.com/golang/go$(GOLANG_VERSION).linux-amd64.tar.gz
GOLANG_INSTALL_MARKER:=$(GOLANG_NAME)$(VERSION_SEP)$(GOLANG_VERSION)

HEKA_VERSION:=0.9.2
HEKA_VERSION_UNDERSCORE:=$(shell echo $(HEKA_VERSION) | sed -e 's/\./_/g')
HEKA_NAME:=heka
HEKA:=$(HEKA_NAME)-$(HEKA_VERSION)
HEKA_DIGEST:=md5
HEKA_CHECKSUM:=864625dff702306eba1494149ff903ee
HEKA_URL:=https://github.com/mozilla-services/heka/releases/download/v$(HEKA_VERSION)/heka-$(HEKA_VERSION_UNDERSCORE)-linux-amd64.tar.gz
HEKA_INSTALL_MARKER:=$(HEKA_NAME)$(VERSION_SEP)$(HEKA_VERSION)

LIBCIDR_VERSION:=1.2.3
LIBCIDR_NAME:=libcidr
LIBCIDR:=$(LIBCIDR_NAME)-$(LIBCIDR_VERSION)
LIBCIDR_DIGEST:=md5
LIBCIDR_CHECKSUM:=c5efcc7ae114fdaa5583f58dacecd9de
LIBCIDR_URL:=https://www.over-yonder.net/~fullermd/projects/libcidr/libcidr-$(LIBCIDR_VERSION).tar.xz
LIBCIDR_INSTALL_MARKER:=$(LIBCIDR_NAME)$(VERSION_SEP)$(LIBCIDR_VERSION)

LIBMAXMINDDB_VERSION:=1.1.1
LIBMAXMINDDB_NAME:=libmaxminddb
LIBMAXMINDDB:=$(LIBMAXMINDDB_NAME)-$(LIBMAXMINDDB_VERSION)
LIBMAXMINDDB_DIGEST:=md5
LIBMAXMINDDB_CHECKSUM:=36c31f0814dbf71b210ee57c2b9ef98c
LIBMAXMINDDB_URL:=https://github.com/maxmind/libmaxminddb/releases/download/$(LIBMAXMINDDB_VERSION)/libmaxminddb-$(LIBMAXMINDDB_VERSION).tar.gz
LIBMAXMINDDB_INSTALL_MARKER:=$(LIBMAXMINDDB_NAME)$(VERSION_SEP)$(LIBMAXMINDDB_VERSION)

LUA_RESTY_DNS_CACHE_VERSION:=691613739a32f8405e56e56547270b9f72e77c34
LUA_RESTY_DNS_CACHE_NAME:=lua-resty-dns-cache
LUA_RESTY_DNS_CACHE:=$(LUA_RESTY_DNS_CACHE_NAME)-$(LUA_RESTY_DNS_CACHE_VERSION)
LUA_RESTY_DNS_CACHE_DIGEST:=md5
LUA_RESTY_DNS_CACHE_CHECKSUM:=c7304c1f434ac251246904db51423d5e
LUA_RESTY_DNS_CACHE_URL:=https://github.com/hamishforbes/lua-resty-dns-cache/archive/$(LUA_RESTY_DNS_CACHE_VERSION).tar.gz
LUA_RESTY_DNS_CACHE_INSTALL_MARKER:=$(LUA_RESTY_DNS_CACHE_NAME)$(VERSION_SEP)$(LUA_RESTY_DNS_CACHE_VERSION)

LUA_RESTY_HTTP_VERSION:=0.06
LUA_RESTY_HTTP_NAME:=lua-resty-http
LUA_RESTY_HTTP:=$(LUA_RESTY_HTTP_NAME)-$(LUA_RESTY_HTTP_VERSION)
LUA_RESTY_HTTP_DIGEST:=md5
LUA_RESTY_HTTP_CHECKSUM:=d828ba0da7bc8f39e0ce0565912aa597
LUA_RESTY_HTTP_URL:=https://github.com/pintsized/lua-resty-http/archive/v$(LUA_RESTY_HTTP_VERSION).tar.gz
LUA_RESTY_HTTP_INSTALL_MARKER:=$(LUA_RESTY_HTTP_NAME)$(VERSION_SEP)$(LUA_RESTY_HTTP_VERSION)

LUA_RESTY_LOGGER_SOCKET_VERSION:=d435ea6052c0d252cf7f89fe4b7cb9c69306de93
LUA_RESTY_LOGGER_SOCKET_NAME:=lua-resty-logger-socket
LUA_RESTY_LOGGER_SOCKET:=$(LUA_RESTY_LOGGER_SOCKET_NAME)-$(LUA_RESTY_LOGGER_SOCKET_VERSION)
LUA_RESTY_LOGGER_SOCKET_DIGEST:=md5
LUA_RESTY_LOGGER_SOCKET_CHECKSUM:=6d7273438100ddcdfa57bdbf1a8c3a01
LUA_RESTY_LOGGER_SOCKET_URL:=https://github.com/cloudflare/lua-resty-logger-socket/archive/$(LUA_RESTY_LOGGER_SOCKET_VERSION).tar.gz
LUA_RESTY_LOGGER_SOCKET_INSTALL_MARKER:=$(LUA_RESTY_LOGGER_SOCKET_NAME)$(VERSION_SEP)$(LUA_RESTY_LOGGER_SOCKET_VERSION)

LUA_RESTY_SHCACHE_VERSION:=fb2e275c2cdca08eaa34a7b73375e41ac3eff200
LUA_RESTY_SHCACHE_NAME:=lua-resty-shcache
LUA_RESTY_SHCACHE:=$(LUA_RESTY_SHCACHE_NAME)-$(LUA_RESTY_SHCACHE_VERSION)
LUA_RESTY_SHCACHE_DIGEST:=md5
LUA_RESTY_SHCACHE_CHECKSUM:=5d3cbcf8fbad1954cdcb3826afa41afe
LUA_RESTY_SHCACHE_URL:=https://github.com/cloudflare/lua-resty-shcache/archive/$(LUA_RESTY_SHCACHE_VERSION).tar.gz
LUA_RESTY_SHCACHE_INSTALL_MARKER:=$(LUA_RESTY_SHCACHE_NAME)$(VERSION_SEP)$(LUA_RESTY_SHCACHE_VERSION)

LUA_RESTY_UUID_VERSION:=834e69c1603b796ffb3ec2921f720f3059cb62c0
LUA_RESTY_UUID_NAME:=lua-resty-uuid
LUA_RESTY_UUID:=$(LUA_RESTY_UUID_NAME)-$(LUA_RESTY_UUID_VERSION)
LUA_RESTY_UUID_DIGEST:=md5
LUA_RESTY_UUID_CHECKSUM:=044bb66af54bbf11f2f53eb62535e4ff
LUA_RESTY_UUID_URL:=https://github.com/bungle/lua-resty-uuid/archive/$(LUA_RESTY_UUID_VERSION).tar.gz
LUA_RESTY_UUID_INSTALL_MARKER:=$(LUA_RESTY_UUID_NAME)$(VERSION_SEP)$(LUA_RESTY_UUID_VERSION)

LUAROCKS_VERSION:=2.2.2
LUAROCKS_NAME:=luarocks
LUAROCKS:=$(LUAROCKS_NAME)-$(LUAROCKS_VERSION)
LUAROCKS_DIGEST:=md5
LUAROCKS_CHECKSUM:=5a830953d27715cc955119609f8096e6
LUAROCKS_URL:=http://luarocks.org/releases/luarocks-$(LUAROCKS_VERSION).tar.gz
LUAROCKS_INSTALL_MARKER:=$(LUAROCKS_NAME)$(VERSION_SEP)$(LUAROCKS_VERSION)

LUSTACHE_VERSION:=241b3a16f358035887c2c05c6e151c1f48401a42
LUSTACHE_NAME:=lustache
LUSTACHE:=$(LUSTACHE_NAME)-$(LUSTACHE_VERSION)
LUSTACHE_DIGEST:=md5
LUSTACHE_CHECKSUM:=7c64dd36bbb02e71a0e60e847b70d561
LUSTACHE_URL:=https://github.com/Olivine-Labs/lustache/archive/$(LUSTACHE_VERSION).tar.gz
LUSTACHE_INSTALL_MARKER:=$(LUSTACHE_NAME)$(VERSION_SEP)$(LUSTACHE_VERSION)

MONGODB_VERSION:=3.0.7
MONGODB_NAME:=mongodb
MONGODB:=$(MONGODB_NAME)-$(MONGODB_VERSION)
MONGODB_DIGEST:=md5
MONGODB_CHECKSUM:=e3894b76089fa9d38901c96bc5516a14
MONGODB_URL:=https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-$(MONGODB_VERSION).tgz
MONGODB_INSTALL_MARKER:=$(MONGODB_NAME)$(VERSION_SEP)$(MONGODB_VERSION)

MORA_VERSION:=0c409c9cbb283708e92cc69a50281ac536f97874
MORA_NAME:=mora
MORA:=$(MORA_NAME)-$(MORA_VERSION)
MORA_DIGEST:=md5
MORA_CHECKSUM:=563945c899b30099543254df84b487d7
MORA_URL:=https://github.com/emicklei/mora/archive/$(MORA_VERSION).tar.gz
MORA_DEPENDENCIES_CHECKSUM:=$(shell openssl md5 $(BUILD_DIR)/mora_glide.yaml | sed 's/^.* //')
MORA_INSTALL_MARKER:=$(MORA_NAME)$(VERSION_SEP)$(MORA_VERSION)-$(MORA_DEPENDENCIES_CHECKSUM)

NGX_TXID_VERSION:=f1c197cb9c42e364a87fbb28d5508e486592ca42
NGX_TXID_NAME:=ngx_txid
NGX_TXID:=$(NGX_TXID_NAME)-$(NGX_TXID_VERSION)
NGX_TXID_DIGEST:=md5
NGX_TXID_CHECKSUM:=408ee86eb6e42e27a51514f711c41d6b
NGX_TXID_URL:=https://github.com/streadway/ngx_txid/archive/$(NGX_TXID_VERSION).tar.gz
NGX_TXID_INSTALL_MARKER:=$(NGX_TXID_NAME)$(VERSION_SEP)$(NGX_TXID_VERSION)

NGX_DYUPS_VERSION:=0.2.8
NGX_DYUPS_NAME:=ngx_http_dyups_module
NGX_DYUPS:=$(NGX_DYUPS_NAME)-$(NGX_DYUPS_VERSION)
NGX_DYUPS_DIGEST:=md5
NGX_DYUPS_CHECKSUM:=295b7cb202de069b313f4da50d6952e0
NGX_DYUPS_URL:=https://github.com/yzprofile/ngx_http_dyups_module/archive/v$(NGX_DYUPS_VERSION).tar.gz
NGX_DYUPS_INSTALL_MARKER:=$(NGX_DYUPS_NAME)$(VERSION_SEP)$(NGX_DYUPS_VERSION)

NGX_GEOIP2_VERSION:=1.0
NGX_GEOIP2_NAME:=ngx_http_geoip2_module
NGX_GEOIP2:=$(NGX_GEOIP2_NAME)-$(NGX_GEOIP2_VERSION)
NGX_GEOIP2_DIGEST:=md5
NGX_GEOIP2_CHECKSUM:=4c89eea53ce8318f940c03adfe0b502b
NGX_GEOIP2_URL:=https://github.com/leev/ngx_http_geoip2_module/archive/1.0.tar.gz
NGX_GEOIP2_INSTALL_MARKER:=$(NGX_GEOIP2_NAME)$(VERSION_SEP)$(NGX_GEOIP2_VERSION)

OPENRESTY_VERSION:=1.9.3.1
OPENRESTY_NAME:=openresty
OPENRESTY:=$(OPENRESTY_NAME)-$(OPENRESTY_VERSION)
OPENRESTY_DIGEST:=md5
OPENRESTY_CHECKSUM:=cde1f7127f6ba413ee257003e49d6d0a
OPENRESTY_URL:=http://openresty.org/download/ngx_openresty-$(OPENRESTY_VERSION).tar.gz
OPENRESTY_INSTALL_MARKER:=$(OPENRESTY_NAME)$(VERSION_SEP)$(OPENRESTY_VERSION)

PCRE_VERSION:=8.37
PCRE_NAME:=pcre
PCRE:=$(PCRE_NAME)-$(PCRE_VERSION)
PCRE_DIGEST:=md5
PCRE_CHECKSUM:=ed91be292cb01d21bc7e526816c26981
PCRE_URL:=http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-$(PCRE_VERSION).tar.bz2
PCRE_INSTALL_MARKER:=$(PCRE_NAME)$(VERSION_SEP)$(PCRE_VERSION)

PERP_VERSION:=2.07
PERP_NAME:=perp
PERP:=$(PERP_NAME)-$(PERP_VERSION)
PERP_DIGEST:=md5
PERP_CHECKSUM:=a2acc7425d556d9635a25addcee9edb5
PERP_URL:=http://b0llix.net/perp/distfiles/perp-$(PERP_VERSION).tar.gz
PERP_INSTALL_MARKER:=$(PERP_NAME)$(VERSION_SEP)$(PERP_VERSION)

RUBY_VERSION:=2.2.3
RUBY_NAME:=ruby
RUBY:=$(RUBY_NAME)-$(RUBY_VERSION)
RUBY_DIGEST:=sha256
RUBY_CHECKSUM:=df795f2f99860745a416092a4004b016ccf77e8b82dec956b120f18bdc71edce
RUBY_URL:=https://cache.ruby-lang.org/pub/ruby/2.2/ruby-$(RUBY_VERSION).tar.gz
RUBY_INSTALL_MARKER:=$(RUBY_NAME)$(VERSION_SEP)$(RUBY_VERSION)

RUNIT_VERSION:=2.1.2
RUNIT_NAME:=runit
RUNIT:=$(RUNIT_NAME)-$(RUNIT_VERSION)
RUNIT_DIGEST:=md5
RUNIT_CHECKSUM:=6c985fbfe3a34608eb3c53dc719172c4
RUNIT_URL:=http://smarden.org/runit/runit-$(RUNIT_VERSION).tar.gz
RUNIT_INSTALL_MARKER:=$(RUNIT_NAME)$(VERSION_SEP)$(RUNIT_VERSION)

# Don't move to 6.0.0 quite yet until we have a better sense of this issue:
# http://mail-archives.apache.org/mod_mbox/trafficserver-users/201510.mbox/%3c1443975393.1364867.400869481.2BFF6EEF@webmail.messagingengine.com%3e
TRAFFICSERVER_VERSION:=5.3.1
TRAFFICSERVER_NAME:=trafficserver
TRAFFICSERVER:=$(TRAFFICSERVER_NAME)-$(TRAFFICSERVER_VERSION)
TRAFFICSERVER_DIGEST:=md5
TRAFFICSERVER_CHECKSUM:=9c0e2450b1dd1bbdd63ebcc344b5a813
TRAFFICSERVER_URL:=http://mirror.olnevhost.net/pub/apache/trafficserver/trafficserver-$(TRAFFICSERVER_VERSION).tar.bz2
TRAFFICSERVER_INSTALL_MARKER:=$(TRAFFICSERVER_NAME)$(VERSION_SEP)$(TRAFFICSERVER_VERSION)

#
# LuaRocks Dependencies
#
INSPECT:=inspect
INSPECT_VERSION:=3.0-1
LIBCIDR_FFI:=libcidr-ffi
LIBCIDR_FFI_VERSION:=0.1.0-1
LIBCIDR_FFI_URL:=https://raw.githubusercontent.com/GUI/lua-libcidr-ffi/master/libcidr-ffi-git-1.rockspec
LUA_CMSGPACK:=lua-cmsgpack
LUA_CMSGPACK_VERSION:=0.4.0-0
LUAPOSIX:=luaposix
LUAPOSIX_VERSION:=33.3.1-1
LUASOCKET:=luasocket
LUASOCKET_VERSION:=2.0.2-6
LYAML:=lyaml
LYAML_VERSION:=6.0-1
PENLIGHT:=penlight
PENLIGHT_VERSION:=1.3.2-2

#
# Test Dependencies
#
UNBOUND_VERSION:=1.5.6
UNBOUND_NAME:=unbound
UNBOUND:=$(UNBOUND_NAME)-$(UNBOUND_VERSION)
UNBOUND_DIGEST:=sha256
UNBOUND_CHECKSUM:=ad3823f5895f59da9e408ea273fcf81d8a76914c18864fba256d7f140b83e404
UNBOUND_URL:=https://www.unbound.net/downloads/unbound-$(UNBOUND_VERSION).tar.gz
UNBOUND_INSTALL_MARKER:=$(UNBOUND_NAME)$(VERSION_SEP)$(UNBOUND_VERSION)

#
# LuaRocks Test Dependencies
#
LUACHECK:=luacheck
LUACHECK_VERSION:=0.11.1-1

# Define non-file/folder targets
.PHONY: \
	all \
	clean \
	dependencies \
	install \
	local_work_dir \
	stage \
	stage_dependencies \
	test_dependencies \
	lint \
	test

define download
	$(eval DOWNLOAD_PATH:=$@)
	$(eval DOWNLOAD_URL:=$($(1)_URL))
	curl -L -o $(DOWNLOAD_PATH) $(DOWNLOAD_URL)
	touch $(DOWNLOAD_PATH)
endef

define decompress
	$(eval DOWNLOAD_PATH:=$<)
	$(eval DIR:=$@)
	$(eval CHECKSUM_TYPE:=$($(1)_DIGEST))
	$(eval CHECKSUM:=$($(1)_CHECKSUM))
	$(eval ARCHIVE_DEPTH:=$(if $(2),$(2),1))
	openssl $(CHECKSUM_TYPE) $(DOWNLOAD_PATH) | grep $(CHECKSUM) || (echo "checksum mismatch $(DOWNLOAD_PATH)" && exit 1)
	mkdir -p $(DIR)
	tar --strip-components $(ARCHIVE_DEPTH) -C $(DIR) -xf $(DOWNLOAD_PATH)
	touch $(DIR)
endef

define luarocks_install
	$(eval PACKAGE:=$($(1)))
	$(eval PACKAGE_VERSION:=$($(1)_VERSION))
	$(LUAROCKS_CMD) --tree=$(VENDOR_DIR) install $(PACKAGE) $(PACKAGE_VERSION)
	touch $@
endef

define test_luarocks_install
	$(eval PACKAGE:=$($(1)))
	$(eval PACKAGE_VERSION:=$($(1)_VERSION))
	$(LUAROCKS_CMD) --tree=$(TEST_VENDOR_DIR) install $(PACKAGE) $(PACKAGE_VERSION)
	touch $@
endef

all: stage

$(DEPS_DIR):
	mkdir -p $@
	touch $@

$(STAGE_PREFIX)/embedded/bin:
	mkdir -p $@
	touch $@

$(STAGE_PREFIX)/embedded/sbin:
	mkdir -p $@
	touch $@

$(STAGE_MARKERS_DIR):
	mkdir -p $@
	touch $@

# api-umbrella-core
$(STAGE_MARKERS_DIR)/api-umbrella-core-web-bundled: $(ROOT_DIR)/src/api-umbrella/web-app/Gemfile $(ROOT_DIR)/src/api-umbrella/web-app/Gemfile.lock | $(VENDOR_DIR) $(STAGE_MARKERS_DIR) $(STAGE_MARKERS_DIR)/$(BUNDLER_INSTALL_MARKER)
	rm -rf $(ROOT_DIR)/src/api-umbrella/web-app/.bundle
	cd $(ROOT_DIR)/src/api-umbrella/web-app && PATH=$(STAGE_PREFIX)/embedded/bin:$(PATH) bundle install --path=$(BUNDLE_DIR)
	touch $@

# Rebuild when the contents of any of the asset files change. We base this on a
# checksum of all the files, rather than timestamps, so that in our CI
# environment we skip precompiling (it's slow), if none of the files changed
# (but since the CI does a fresh checkout, all the timestamps on the files
# change, so make's normal checking would trigger changes).
WEB_ASSETS_CHECKSUM:=$(shell find $(ROOT_DIR)/src/api-umbrella/web-app/app/assets $(ROOT_DIR)/src/api-umbrella/web-app/Gemfile.lock -type f -exec cksum {} \; | sort | openssl md5 | sed 's/^.* //')
$(STAGE_MARKERS_DIR)/api-umbrella-core-web-assets-$(WEB_ASSETS_CHECKSUM): $(STAGE_MARKERS_DIR)/api-umbrella-core-web-bundled | $(STAGE_MARKERS_DIR)
	# Compile the assets, but then move them to a temporary build directory so
	# they aren't used when working in development mode.
	cd $(ROOT_DIR)/src/api-umbrella/web-app && PATH=$(STAGE_PREFIX)/embedded/bin:$(PATH) DEVISE_SECRET_KEY=temp RAILS_SECRET_TOKEN=temp bundle exec rake assets:precompile
	mkdir -p $(WORK_DIR)/tmp/web-assets
	cd $(ROOT_DIR)/src/api-umbrella/web-app && rsync -a --delete-after public/web-assets/ $(WORK_DIR)/tmp/web-assets/
	rm -rf $(ROOT_DIR)/src/api-umbrella/web-app/public/web-assets
	touch $@

$(STAGE_MARKERS_DIR)/api-umbrella-core: $(STAGE_MARKERS_DIR)/api-umbrella-core-dependencies | $(STAGE_MARKERS_DIR)
	# Create a new release directory, copying the relevant source code from the
	# current repo checkout into the release (but excluding tests, etc).
	rm -rf $(STAGE_PREFIX)/embedded/apps/core/releases
	mkdir -p $(STAGE_PREFIX)/embedded/apps/core/releases/$(RELEASE_TIMESTAMP)
	rsync -a \
		--filter=":- $(ROOT_DIR)/.gitignore" \
		--include="/templates/etc/perp/.boot" \
		--exclude=".*" \
		--exclude="/templates/etc/test-env*" \
		--exclude="/templates/etc/perp/test-env*" \
		--exclude="/src/api-umbrella/web-app/spec" \
		--exclude="/src/api-umbrella/web-app/app/assets" \
{'fraction_non_alphanumeric': 0.13834614316140384, 'fraction_numerical': 0.060931567737753496, 'mean_word_length': 9.692409240924093, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 42, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6548631', 'n_tokens_mistral': 15977, 'n_tokens_neox': 14199, 'n_words': 1243}