APP_NAME=songbirdcli

.PHONY: env
env:
# check ENV env var has been set
ifndef ENV
	$(error Must set ENV variable!)
endif
# load env vars from .env file if present
ifneq ("$(wildcard $(ENV).env)", "")
	@echo "Loading configuration from $(ENV).env"
# include cannot be indented
include $(ENV).env
else
	@echo "Continuing without .env file."
	@echo "Creating template $(ENV).env file"
	echo 'RUN_LOCAL=false' > $(ENV).env
	echo 'ITUNES_ENABLED=true' >> $(ENV).env
	echo 'GDRIVE_ENABLED=true' >> $(ENV).env
endif

# variables as a list, required for pytest targets
# in this makefile
ENV_VARS = $(shell cat $(ENV).env | xargs)

.PHONY: setup
setup:
	uv sync --extra dev
	uv run playwright install

.PHONY: upgrade
upgrade:
	uv lock --upgrade
	uv sync --extra dev

# /app/data folder is legacy for backwards compatability
# from the times before songbird was split across
# core, cli and api.
.PHONY: volumesinit
volumesinit:
	mkdir -p ./$(APP_NAME)/data/dump
	mkdir -p ./$(APP_NAME)/data/local_chromium
	mkdir -p ./$(APP_NAME)/data/gdrive

.PHONY: volumesclean
volumesclean:
	rm -rf ./$(APP_NAME)/data/dump
	rm -rf ./$(APP_NAME)/data/local_chromium
	rm -rf ./$(APP_NAME)/data/gdrive


# documentation targets
.PHONY: docs-lint
docs-lint:
	@echo linting files at docs/**/*.md
	markdownlint docs/**/*.md

.PHONY: docs-serve
docs-serve:
	@echo serving the site on http://localhost:8000
	mkdocs serve

.PHONY: docs-build
docs-build:
	@echo building the site
	mkdocs build --strict --verbose --site-dir public

.PHONY: build
build:
	DOCKER_BUILDKIT=1 docker build -t $(APP_NAME):latest .

.PHONY: run-itunes
run-itunes:
	docker run -it --env-file docker.env \
        -v "${HOME}"/songbirdcli/data/dump:/app/data/dump \
        -v "${HOME}"/songbirdcli/data/local_chromium:/root/.local/share/pyppeteer/local-chromium \
        -v "${HOME}"/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ Music.localized:/app/data/itunesauto \
        -v "${HOME}"/Music/Itunes/Itunes\ Media/Music:/app/data/ituneslib \
		$(APP_NAME):latest

.PHONY: run-gdrive
run-gdrive:
	docker run -it --env-file docker.env \
		-v "${PWD}"/songbirdcli/data/dump:/app/data/dump \
		-v "${PWD}"/songbirdcli/data/local_chromium:/root/.local/share/pyppeteer/local-chromium \
		-v "${PWD}"/songbirdcli/data/gdrive:/app/data/gdrive \
		-p 8080:8080 \
		--hostname songbird \
		$(APP_NAME):latest

.PHONY: run-default
run-default:
	docker run -it --env-file docker.env \
		-v "${PWD}"/songbirdcli/data/dump:/app/data/dump \
		$(APP_NAME):latest 

.PHONY: clean
clean:
	docker rm $(APP_NAME) || true

.PHONY: stop
stop:
	docker kill $(APP_NAME)

.PHONY: dev
dev: clean build run

lint:
	uv run black $(APP_NAME)/.
	uv run black tests

.PHONY: test-stdout
test-stdout:
	uv run pytest --cov=songbirdcli tests/unit -v

.PHONY: test
test:
	uv run pytest --doctest-modules --junitxml=junit/test-results.xml --cov=songbirdcli --cov-report=xml --cov-report=html tests/unit -v

.PHONY: test-env
test-env:
	$(ENV_VARS) uv run pytest --doctest-modules --junitxml=junit/test-results.xml --cov=songbirdcli --cov-report=xml --cov-report=html tests/unit -v
