PHP_EXE := $(shell which php8.4)
COMPOSER_EXE := $(shell which composer)
NODE_EXE := $(shell which node)
NPM_EXE := $(shell which npm)
SUDO_EXE := $(shell which sudo)

WWW_USER := www-data
BUILD_EXE := build.js
ENV_FILE := .env
TOOLS_DIR := tools
VENDOR_DIR := vendor

all: install migrate clear-cache build cron

install: composer_install npm_install

composer_install:
	${PHP_EXE} ${COMPOSER_EXE} install

npm_install:
	${NPM_EXE} ci

update: composer_update npm_update

composer_update:
	${PHP_EXE} ${COMPOSER_EXE} update

npm_update:
	${NPM_EXE} update --save

migrate:
	${SUDO_EXE} -u ${WWW_USER} ${PHP_EXE} ${TOOLS_DIR}/migrate

clear-cache:
	${SUDO_EXE} -u ${WWW_USER} ${PHP_EXE} ${TOOLS_DIR}/nuke-tpl-cache

cron:
	${SUDO_EXE} -u ${WWW_USER} ${PHP_EXE} ${TOOLS_DIR}/cron slow

build:
	${NODE_EXE} --env-file ${ENV_FILE} ${BUILD_EXE}

rebuild-css:
	${NODE_EXE} --env-file ${ENV_FILE} ${BUILD_EXE} css

rebuild-js:
	${NODE_EXE} --env-file ${ENV_FILE} ${BUILD_EXE} js

rebuild-twig:
	${NODE_EXE} --env-file ${ENV_FILE} ${BUILD_EXE} twig

analyse:
	${PHP_EXE} ${VENDOR_DIR}/bin/phpstan

tag: analyse
	${PHP_EXE} ${TOOLS_DIR}/create-tag

.PHONY: all update composer npm build rebuild-css rebuild-js rebuild-twig migrate clear-cache cron analyse tag