export REGISTRO=harbor.reymota.es\/reymota
#export REGISTRO=registry.reymota.es
export NAMESPACE = finanzas
VERSION_FILE = .version

# Get current version or default to alpine-1
IMG_VERSION := $(shell cat $(VERSION_FILE) 2>/dev/null || echo "alpine-1")
# Get built version for deployment targets
BUILT_VERSION := $(shell cat .current_version 2>/dev/null || echo "$(IMG_VERSION)")
export IMG_VERSION

help:
	@echo ""
	@echo "Comandos disponibles:"
	@echo ""
	@grep "^[[:lower:]]" Makefile | grep -v "^\." | grep "##" | awk -F':.*## ' '{printf "  \033[1m%-20s\033[0m %s\n", $$1, $$2}'
	@echo ""

# limpia todo
all: imagen clean install

imagen: ## Crea la imagen
	@echo "Building image with version: $(IMG_VERSION)"
	@# Store current version for other targets to use
	@echo "$(IMG_VERSION)" > .current_version
	cd ../; make
	@# Increment version for next build
	@echo "$(IMG_VERSION)" | sed 's/alpine-\([0-9]*\)/alpine-\1/' | awk -F'-' '{print "alpine-" $$2+1}' > $(VERSION_FILE)
	@echo "Next version will be: $$(cat $(VERSION_FILE))"

ns: ## Crea el namespace
	-cat namespace.yaml | sed -e "s/<NS>/${NAMESPACE}/" |kubectl create -f -

install: ## Instala todo (meno las claims)
	-cat reg-secret.yaml| sed -e "s/<NS>/${NAMESPACE}/" | kubectl create -f -
	-cat env-prod-configmap.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl create -f -
	-cat secreto-db.yaml| sed -e "s/<NS>/${NAMESPACE}/" | kubectl create -f -

	-cat finanzas-deployment.yaml | sed -e "s/<NS>/${NAMESPACE}/" | sed -e "s/<TAG>/${BUILT_VERSION}/" | sed -e "s/<REGISTRO>/${REGISTRO}/" | kubectl create -f -
	-cat finanzas-service.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl create -f -
	-cat finanzas-ingress.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl create -f -

clean: ## Borra todo (meno las claims)

	-cat finanzas-ingress.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl delete -f -

	-cat finanzas-deployment.yaml | sed -e "s/<NS>/${NAMESPACE}/" | sed -e "s/<TAG>/${BUILT_VERSION}/" | sed -e "s/<REGISTRO>/${REGISTRO}/" | kubectl delete -f -

	-cat finanzas-service.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl delete -f -

	-cat secreto-db.yaml| sed -e "s/<NS>/${NAMESPACE}/" | kubectl delete -f -
	-cat env-prod-configmap.yaml  | sed -e "s/<NS>/${NAMESPACE}/" | kubectl delete -f -
	-cat reg-secret.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl delete -f -

borraclaims: ## Borra las claims
	-cat pvc-finanzas.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl delete -f -
	-cat pvc-static.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl delete -f -

claims: ## Crea las claims
	-cat pvc-finanzas.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl create -f -
	-cat pvc-static.yaml | sed -e "s/<NS>/${NAMESPACE}/" | kubectl create -f -

app: ## Borra y vuelve a crear el deployment
	-cat finanzas-deployment.yaml | sed -e "s/<NS>/${NAMESPACE}/" | sed -e "s/<TAG>/${BUILT_VERSION}/" | sed -e "s/<REGISTRO>/${REGISTRO}/" | kubectl delete -f -
	-cat finanzas-deployment.yaml | sed -e "s/<NS>/${NAMESPACE}/" | sed -e "s/<TAG>/${BUILT_VERSION}/" | sed -e "s/<REGISTRO>/${REGISTRO}/" | kubectl create -f -

db: ## Crea la base de datos en el cluster
	-kubectl create -f finanzas-db.yaml
