#!make
SHELL := /bin/bash

include .env
-include .env.local
export

.PHONY: help build start stop lint

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

format: format-api format-ui ## Format code

psql: ## Connect to the PSQL DB
	@docker exec -it aviation-postgres psql -U ${POSTGRES_USER} -P pager=off

#################
# API Commands  #
#################

format-api: ## Format code
	@cd api && cargo fmt

build-api: ## Build the API project
	@cd api && cargo build

run-api: ## Run the API project
	@cd api && cargo run -p api

##################
# ADS-B Commands #
##################

build-adsb: ## Build the ADS-B project
	@cd adsb && cargo build

run-sim: ## Run the ADS-B Simulator
	@cd adsb/adsb_sim && cargo run -p adsb_sim

run-recv: ## Run the ADS-B Receiver
	@cd adsb/adsb_recv && cargo run -p adsb_recv -- --sim

#################
#  UI Commands  #
#################

lint-ui:  ## Run the linter
	@cd ui && npm run lint

format-ui: ## Run the formatter
	@cd ui && npm run format

build-ui:  ## Build the UI app
	@cd ui && npm install && npm run build

clean-ui:  ## Remove UI build files
	@cd ui && rm -rf node_modules dist stats.html

run-ui:  ## Run the UI app
	@cd ui && npm install && npm run dev

###################
# Docker Commands #
###################

backend-up: ## Start Docker containers
	@docker compose --profile backend up -d

up-backend: backend-up

backend-down: ## Stop Docker containers
	@docker compose --profile backend down

down-backend: backend-down

run: ## Run the api
	@cd api && cargo run

frontend-up: ## Start Docker containers
	@docker compose --profile frontend up -d

up-frontend: frontend-up

frontend-down: ## Stop Docker containers
	@docker compose --profile frontend down

down-frontend: frontend-down

docker-prune: ## Prune the docker system
	@docker system prune -a

docker-clean: ## Stop the docker containers and remove volumes
	@docker compose --profile frontend --profile api --profile backend down -v

docker-down: ## Stop the docker container
	@docker compose --profile frontend --profile api --profile backend down

docker-up: ## Start the docker container
	@docker compose --profile backend --profile api up -d

docker-refresh: docker-clean up-backend ## Refresh the database

refresh: docker-refresh

build: version=$(if $(v),$(v),latest)
build: folder=$(if $(f),$(f),nginx)
build: registry=$(if $(r),$(r),gitea.bensherriff.com/bsherriff)
build: image=${registry}/aviation-${folder}:${version}
build: ## Build a specific docker image (`make build f=httpd`)
	docker buildx build \
	-f ${folder}/Dockerfile \
	-t ${image} \
	--load \
	--build-arg BUILD_DATE=$$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
	--build-arg BUILD_VERSION=${version} \
	--build-arg VCS_REF=$$(git rev-parse HEAD) \
	.

push: version=$(if $(v),$(v),latest)
push: folder=$(if $(f),$(f),nginx)
push: registry=$(if $(r),$(r),gitea.bensherriff.com/bsherriff)
push: platform=$(if $(p),$(p),linux/amd64,linux/arm64)
push: image=${registry}/aviation-${folder}:${version}
push: ## Build and push a specific docker image (`make push f=httpd`)
	docker buildx build \
	-f ${folder}/Dockerfile \
	--platform ${platform} \
	-t ${image} \
	--push \
	--build-arg BUILD_DATE=$$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
	--build-arg BUILD_VERSION=${version} \
	--build-arg VCS_REF=$$(git rev-parse HEAD) \
	.

docker-pull:
	@docker compose --profile api --profile backend pull

cert: domain=$(if $(d),$(d),${NGINX_HOST})
cert: ## Generate a cert for the given domain
	./scripts/generate_cert.sh ${domain}
