ifeq ($(ENV),)
$(error ENV variable is not defined. Please set it to one of development|staging|production)
endif

.PHONY: flux

namespaces:
	./scripts/k8s-namespaces.sh

secrets:
	dotenvx run -f .env.${ENV} -- ./scripts/k8s-secrets.sh

flux:
	./scripts/flux-bootstrap.sh

cluster: echoenv kind namespaces secrets velero chisel flux metrics

metrics: 
	kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

echoenv:
	echo "[echoenv] Using ${ENV} environment. If this is not what you want, export ENV=development|staging|production"

scrap: namespaces secrets velero chisel flux

velero:
	./scripts/velero-create.sh

tilt:
	kind get kubeconfig > ~/.kube/kind.yaml
	KUBECONFIG=~/.kube/kind.yaml tilt up -f ./Tiltfile

exoscale:
	kubectl apply -f https://raw.githubusercontent.com/exoscale/cert-manager-webhook-exoscale/master/deploy/exoscale-webhook-kustomize/deploy.yaml

kind:
	./scripts/kind-with-local-registry.sh

kindload:
	./scripts/kind-load.sh

chisel:
	./scripts/k8s-chisel-operator.sh

clean:
	kind delete cluster
	node ./packages/infra/vultr-delete-orphaned-resources.js

deps:
	echo "Some of the install methods for these dependencies are not cross-platform compatible. Some of the install methods are not tested. Expect this to fail. Please consult the Makefile for URLs to project sources."
	sudo pamac install make entr nvm kubectl docker helm expect
	curl -fsSL https://raw.githubusercontent.com/tilt-dev/tilt/master/scripts/install.sh | bash
	curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
	echo "go to https://github.com/txn2/kubefwd/releases/latest to get kubefwd"
	echo "go to https://github.com/tilt-dev/ctlptl/releases/latest to get ctlptl"
	sudo systemctl enable docker
	sudo systemctl start docker
	usermod -aG docker cj
	newgrp docker
	npm i -g pnpm
	pnpm install -g @dotenvx/dotenvx
	curl -OL 'https://github.com/vmware-tanzu/velero/releases/download/v1.13.2/velero-v1.13.2-linux-amd64.tar.gz'
	OS=$(go env GOOS); ARCH=$(go env GOARCH); curl -fsSL -o cmctl https://github.com/cert-manager/cmctl/releases/latest/download/cmctl_${OS}_${ARCH}
	chmod +x cmctl
	sudo mv cmctl /usr/local/bin

# A gitea act runner which runs locally
# https://docs.gitea.com/next/usage/actions/overview
# this doesnt work because of missing docker in docker
# I'm running this using systemd instead
#runner:
#	docker run -d --rm -e GITEA_INSTANCE_URL=https://gitea.futureporn.net -e GITEA_RUNNER_REGISTRATION_TOKEN=${GITEA_RUNNER_REGISTRATION_TOKEN} -v /var/run/docker.sock:/var/run/docker.sock -v $$HOME/.local/share/applications/fp/act-runner-data:/data --privileged --name fp-gitea-act-runner gitea/act_runner