-include .envrc

## help: print this help message
.PHONY: help
help:
	@echo 'Usage:'
	@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'

.PHONY: confirm
confirm:
	@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]

current_time = $(shell date -Iseconds)
git_description = $(shell git describe --always --dirty)
linker_flags = '-s -X main.buildTime=${current_time} -X main.version=${git_description}'

production_host = 152.53.236.243
production_user = root
production_src  = /srv/party

## build/api: build for the current OS and cross-compile for linux/amd64
.PHONY: build/api
build/api:
	@echo 'Building cmd/party...'
	go build -ldflags=${linker_flags} -o=./bin/party ./cmd/party
	GOOS=linux GOARCH=amd64 go build -ldflags=${linker_flags} -o=./bin/linux_amd64/party ./cmd/party

## build/package: assemble .deb — runs on the production server, not locally
.PHONY: build/package
build/package:
	@echo 'Building binary...'
	go build -ldflags=${linker_flags} -o=./bin/party ./cmd/party
	@echo 'Assembling .deb...'
	rm -rf /tmp/party-pkg
	mkdir -p /tmp/party-pkg/DEBIAN
	mkdir -p /tmp/party-pkg/usr/bin
	mkdir -p /tmp/party-pkg/usr/share/party
	mkdir -p /tmp/party-pkg/lib/systemd/system
	cp ./bin/party              /tmp/party-pkg/usr/bin/party
	cp -r ./web                 /tmp/party-pkg/usr/share/party/
	cp -r ./migrations          /tmp/party-pkg/usr/share/party/
	cp ./deploy/party.service   /tmp/party-pkg/lib/systemd/system/
	cp ./deploy/DEBIAN/postinst /tmp/party-pkg/DEBIAN/postinst
	cp ./deploy/DEBIAN/prerm    /tmp/party-pkg/DEBIAN/prerm
	cp ./deploy/DEBIAN/postrm   /tmp/party-pkg/DEBIAN/postrm
	chmod 755 /tmp/party-pkg/DEBIAN/postinst /tmp/party-pkg/DEBIAN/prerm /tmp/party-pkg/DEBIAN/postrm
	printf 'Package: party\nVersion: %s\nArchitecture: amd64\nMaintainer: Vicente Ferrari Smith <vicenteferrarismith@gmail.com>\nDescription: Party\n' \
		'${git_description}' > /tmp/party-pkg/DEBIAN/control
	dpkg-deb --build --root-owner-group /tmp/party-pkg ./party.deb
	@echo 'Package ready: ./party.deb'

## deploy/configure: one-time production server setup
.PHONY: deploy/configure
deploy/configure:
	cat deploy/setup.sh | ssh ${production_user}@${production_host} bash

## deploy/teardown: remove everything from the production server
.PHONY: deploy/teardown
deploy/teardown: confirm
	cat deploy/teardown.sh | ssh ${production_user}@${production_host} bash

## deploy/migrate: run database migrations on production
.PHONY: deploy/migrate
deploy/migrate: confirm
	ssh -t ${production_user}@${production_host} \
		'set -a; . /etc/party/environment; set +a; migrate -path /usr/share/party/migrations -database $$PARTY_DB_DSN up'

## deploy: pull latest, build .deb on server, and install it
.PHONY: deploy
deploy: confirm
	ssh -t ${production_user}@${production_host} \
		'cd ${production_src} && git pull origin main && make build/package && dpkg -i ./party.deb'

## run/api: run the cmd/api application
.PHONY: run/api
run/api:
	@echo "Running the API"
	go run ./cmd/party -db-dsn=${PARTY_DB_DSN}

.PHONY: db/psql
db/psql:
	psql ${PARTY_DB_DSN}

.PHONY: db/migrations/new
db/migrations/new:
	@echo 'Creating migration files for ${name}...'
	migrate create -seq -ext=.sql -dir=./migrations ${name}

.PHONY: db/migrations/up
db/migrations/up: confirm
	@echo 'Running up migrations...'
	migrate -path ./migrations -database ${PARTY_DB_DSN} up

.PHONY: vendor
vendor:
	@echo 'Tidying and verifying module dependencies...'
	go mod tidy
	go mod verify
	@echo 'Vendoring dependencies...'
	go mod vendor
