.PHONY: clean cleancache cleanall build
.DEFAULT_GOAL := build
include ../opack/options.mk
include ../opack/defines.mk

$(OPACK_PACKER_HTTP_DIR) $(OPACK_PACKER_DIR) ../$(OPACK_CACHE_DIR): 
	@mkdir -p $@

$(OPACK_PACKER_DIR)/vagrant.key: | $(OPACK_PACKER_DIR)
	@curl -s -o $@ https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant

$(OPACK_PACKER_DIR)/opack.json: | $(OPACK_PACKER_DIR)
	@printf '$(subst $(newline),\n,${OPACK_PACKER_CONTENT})' > $@

$(OPACK_PACKER_DIR)/bucket.json:
	@printf '{ "name": "$(GCE_BUCKET)", "location": "$(GCE_BUCKET_LOCATION)", "storageClass": "STANDARD", "iamConfiguration": {"uniformBucketLevelAccess": { "enabled": true }, } }' > $@

$(OPACK_PACKER_DIR)/opack-cloud.json: | $(OPACK_PACKER_DIR) $(OPACK_PACKER_DIR)/bucket.json
	@printf '$(subst $(newline),\n,${OPACK_PACKER_CLOUD_CONTENT})' > $@
	@curl -X POST -s -o /dev/null \
		--data-binary @$(OPACK_PACKER_DIR)/bucket.json \
		-H "Authorization: Bearer $(STORAGE_TOKEN)" \
		-H "Content-Type: application/json" \
		"https://storage.googleapis.com/storage/v1/b?project=$(GCE_PROJECT)"
	@curl -X DELETE -s -o /dev/null \
		-H "Authorization: Bearer $(IMAGE_TOKEN)" \
		"https://compute.googleapis.com/compute/v1/projects/$(GCE_PROJECT)/global/images/$(OPACK_TARGET)"

$(OPACK_PACKER_HTTP_DIR)/install.conf: | $(OPACK_PACKER_HTTP_DIR)
	@printf '$(subst $(newline),\n,${OPACK_INSTALL_CONTENT})' > $@

$(OPACK_PACKER_HTTP_DIR)/autodisklabel: | $(OPACK_PACKER_HTTP_DIR)
	@cp autodisklabel $@

../provision-openbsd.sh:
	@echo you need to write $@ && exit 1

../id_ed25519:
	@echo you need to generate the ssh key with 'make sshkey' && exit 1

$(OPACK_BOX_FILE): | $(OPACK_PACKER_DIR)/opack.json $(OPACK_PACKER_DIR)/vagrant.key ../provision-openbsd.sh $(OPACK_PACKER_HTTP_DIR)/install.conf $(OPACK_PACKER_HTTP_DIR)/autodisklabel
	@cd $(OPACK_PACKER_DIR) &&\
		CHECKPOINT_DISABLE=1 PACKER_CACHE_DIR=../../$(OPACK_CACHE_DIR)\
		packer build\
		-timestamp-ui opack.json &&\
		rm -rf $(OPACK_PACKER_DIR)

$(OPACK_CLOUD): | $(OPACK_PACKER_DIR)/opack-cloud.json ../id_ed25519 ../provision-openbsd.sh $(OPACK_PACKER_HTTP_DIR)/install.conf $(OPACK_PACKER_HTTP_DIR)/autodisklabel
	@cd $(OPACK_PACKER_DIR) &&\
		CHECKPOINT_DISABLE=1 PACKER_CACHE_DIR=../../$(OPACK_CACHE_DIR)\
		packer build\
		-timestamp-ui opack-cloud.json | tee -a ../$@ &&\
		rm -rf $(OPACK_PACKER_DIR)


$(OPACK_META_FILE): $(OPACK_BOX_FILE)
	@printf '$(subst $(OPACK_BOX_FILE_SHA256),$(shell sha256sum $(OPACK_BOX_FILE) | awk '{print $$1}'),$(subst $(newline),\n,${OPACK_METADATA_CONTENT}))' > $@
	@vagrant box add -f --name $(OPACK_BOX_TAG) $(OPACK_META_FILE)

build: $(OPACK_META_FILE)
	@export VAGRANT_CLOUD_TOKEN=$$(cat ../up.secret) && vagrant up 

clean: 
	@-vagrant destroy -f 
	@-cd terraform_??????? 2>/dev/null && terraform destroy -auto-approve && cd .. && rm -rf terraform_???????
	@-rm -rf opack_build_??????? *.log ssh-config .vagrant *.json

cleancache:
	@-vagrant box remove -f --all $(OPACK_BOX_TAG)
	@-rm -rf ../$(OPACK_CACHE_DIR)

cleanall: clean cleancache
