SHELL:=/usr/bin/env bash

FASTLANE ?= bundle exec fastlane

.PHONY: test release-secrets deploy-production

.PHONY: bump-release-dev
bump-release-dev: ## Bump GitHub version
	./scripts/bumpver update --tag=dev --push

.PHONY: bump-release-production
bump-release-production: ## Bump Google Play version
	./scripts/bumpver update --tag=final

.PHONY: changelog
changelog: ## Prep next release notes
	./scripts/changelog

.PHONY: review-opml
review-opml: ## Save Play review account subscriptions, e.g. OPML=~/Desktop/subscriptions.xml
	./scripts/review-opml $(OPML)

test: ## Run all tests
	./gradlew test -x :app:testGplayDebugUnitTest --build-cache

deploy-production: release-secrets
	$(FASTLANE) production

deploy-github-release: shared-release-secrets
	$(FASTLANE) github_release

deploy-free-nightly: shared-release-secrets
	$(FASTLANE) free_nightly

.SILENT:
release-secrets: shared-release-secrets
	echo ${ENCODED_GOOGLE_PLAY_CREDENTIALS} | base64 --decode > ./google-play-service-account.json

.SILENT:
shared-release-secrets:
	echo ${ENCODED_GOOGLE_SERVICES} | base64 --decode > ./app/google-services.json
	echo ${ENCODED_RELEASE_KEYSTORE} | base64 --decode > ./release.keystore
	: > ./secrets.properties
	for name in $$(compgen -e | grep '^PROP_'); do \
		key=$$(echo "$${name#PROP_}" | tr '[:upper:]' '[:lower:]'); \
		echo "$$key=$${!name}" >> ./secrets.properties; \
	done

.PHONY: bench-profile
bench-profile: ## Run refresh profile benchmark
	./gradlew :bench:run --args="refresh-profile"

.PHONY: bench-reset
bench-reset: ## Delete bench data
	./gradlew :bench:run --args="reset"

.PHONY: install-tailscale
install-tailscale: ## Install Tailscale on emulator
	./scripts/install-tailscale

.PHONY: help
help:
	@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
