CSS_FILES += ${IHP}/static/vendor/bootstrap.min.css
CSS_FILES += ${IHP}/static/vendor/flatpickr.min.css
CSS_FILES += static/app.css

JS_FILES += ${IHP}/static/vendor/jquery-3.6.0.slim.min.js
JS_FILES += ${IHP}/static/vendor/timeago.js
JS_FILES += ${IHP}/static/vendor/popper.min.js
JS_FILES += ${IHP}/static/vendor/bootstrap.min.js
JS_FILES += ${IHP}/static/vendor/flatpickr.js
JS_FILES += ${IHP}/static/helpers.js
JS_FILES += ${IHP}/static/vendor/morphdom-umd.min.js
JS_FILES += ${IHP}/static/vendor/turbolinks.js
JS_FILES += ${IHP}/static/vendor/turbolinksInstantClick.js
JS_FILES += ${IHP}/static/vendor/turbolinksMorphdom.js

.DEFAULT_GOAL := help
BOOTSTRAP_GOALS := help install install-nix doctor ui recovery-drill

ifneq ($(strip $(IHP)),)
include ${IHP}/Makefile.dist
else ifeq ($(strip $(MAKECMDGOALS)),)
else ifneq ($(filter-out $(BOOTSTRAP_GOALS),$(MAKECMDGOALS)),)
$(error IHP is not set. Run `make` to list setup targets, `make install` to prepare local tooling, or enter `devenv shell` before using IHP make targets)
endif

.PHONY: help install install-nix doctor ui recovery-drill
help:
	@printf '%s\n' 'inter-hub targets:'
	@printf '  %-17s %s\n' 'make install' 'Prepare local tooling; installs devenv when Nix is available.'
	@printf '  %-17s %s\n' 'make install-nix' 'Show the Nix installer command required before make install.'
	@printf '  %-17s %s\n' 'make doctor' 'Check whether devenv, nix, and direnv are visible.'
	@printf '  %-17s %s\n' 'make ui' 'Start the local Inter-Hub UI at http://localhost:8000.'
	@printf '  %-17s %s\n' 'make recovery-drill' 'Verify custody-backed SOPS recovery for the runtime Secret.'
	@printf '%s\n' ''
	@printf '%s\n' 'IHP targets are also available after entering the dev environment with devenv shell.'

install:
	@set -eu; \
	nix_bin=""; \
	if command -v devenv >/dev/null 2>&1; then \
		echo "devenv is already installed: $$(command -v devenv)"; \
		exit 0; \
	fi; \
	if command -v nix >/dev/null 2>&1; then \
		nix_bin="$$(command -v nix)"; \
	elif [ -x "$$HOME/.nix-profile/bin/nix" ]; then \
		nix_bin="$$HOME/.nix-profile/bin/nix"; \
	elif [ -x /nix/var/nix/profiles/default/bin/nix ]; then \
		nix_bin="/nix/var/nix/profiles/default/bin/nix"; \
	fi; \
	if [ -n "$$nix_bin" ]; then \
		if [ -d /nix/var/nix/daemon-socket ] && [ ! -x /nix/var/nix/daemon-socket ]; then \
			echo "Nix is installed at $$nix_bin, but this user cannot access the Nix daemon socket." >&2; \
			echo 'Typical fix: sudo usermod -aG nix-users "$$USER"' >&2; \
			echo "Then restart WSL or open a new login shell before rerunning: make install" >&2; \
			exit 126; \
		fi; \
		echo "Installing devenv with $$nix_bin"; \
		"$$nix_bin" --extra-experimental-features 'nix-command flakes' profile install github:cachix/devenv; \
		echo "Done. If your shell still cannot find devenv, open a new shell or add the Nix profile bin directory to PATH."; \
	else \
		echo "Nix is not available, so this target cannot install devenv yet." >&2; \
		echo "Run: make install-nix" >&2; \
		echo "Then rerun: make install" >&2; \
		echo "After Nix is available, this target installs devenv for you." >&2; \
		exit 127; \
	fi

install-nix:
	@printf '%s\n' 'Nix is the machine-level prerequisite for this repo.'
	@printf '%s\n' ''
	@printf '%s\n' 'Recommended next step: review and run the Determinate Nix installer:'
	@printf '%s\n' '  curl -fsSL https://install.determinate.systems/nix | sh -s -- install'
	@printf '%s\n' ''
	@printf '%s\n' 'If Nix is already installed but the daemon socket is not accessible:'
	@printf '%s\n' '  sudo usermod -aG nix-users "$$USER"'
	@printf '%s\n' '  # then restart WSL or open a new login shell'
	@printf '%s\n' ''
	@printf '%s\n' 'After installation, open a new shell and run:'
	@printf '%s\n' '  make install'
	@printf '%s\n' '  make ui'

doctor:
	@devenv_path="$$(command -v devenv || true)"; printf '%-8s %s\n' 'devenv:' "$${devenv_path:-not found}"
	@nix_path="$$(command -v nix || true)"; printf '%-8s %s\n' 'nix:' "$${nix_path:-not found}"
	@direnv_path="$$(command -v direnv || true)"; printf '%-8s %s\n' 'direnv:' "$${direnv_path:-not found}"
	@if [ -d /nix/var/nix/daemon-socket ]; then \
		if [ -x /nix/var/nix/daemon-socket ]; then \
			echo "nix daemon: accessible"; \
		else \
			echo "nix daemon: not accessible; current user may need the nix-users group"; \
		fi; \
	fi
	@if [ -x "$$HOME/.nix-profile/bin/nix" ]; then echo "nix profile: $$HOME/.nix-profile/bin/nix"; fi
	@if [ -x "$$HOME/.nix-profile/bin/devenv" ]; then echo "devenv profile: $$HOME/.nix-profile/bin/devenv"; fi
	@if [ -x /nix/var/nix/profiles/default/bin/nix ]; then echo "nix default profile: /nix/var/nix/profiles/default/bin/nix"; fi
	@if [ -x /nix/var/nix/profiles/default/bin/devenv ]; then echo "devenv default profile: /nix/var/nix/profiles/default/bin/devenv"; fi

recovery-drill:
	@deploy/railiance/recovery-drill.sh

ui:
	@echo "Starting inter-hub UI at http://localhost:8000"
	@if [ -d /nix/var/nix/daemon-socket ] && [ ! -x /nix/var/nix/daemon-socket ]; then \
		echo "Nix is installed, but this user cannot access the Nix daemon socket." >&2; \
		echo 'Typical fix: sudo usermod -aG nix-users "$$USER"' >&2; \
		echo "Then restart WSL or open a new login shell before rerunning: make ui" >&2; \
		exit 126; \
	fi; \
	if command -v devenv >/dev/null 2>&1; then \
		exec devenv up; \
	elif [ -x "$$HOME/.nix-profile/bin/devenv" ]; then \
		exec "$$HOME/.nix-profile/bin/devenv" up; \
	elif [ -x /nix/var/nix/profiles/default/bin/devenv ]; then \
		exec /nix/var/nix/profiles/default/bin/devenv up; \
	elif command -v nix >/dev/null 2>&1; then \
		echo "devenv is not on PATH; using nix run github:cachix/devenv fallback"; \
		exec nix --extra-experimental-features 'nix-command flakes' run github:cachix/devenv -- up; \
	elif [ -x "$$HOME/.nix-profile/bin/nix" ]; then \
		echo "devenv is not on PATH; using $$HOME/.nix-profile/bin/nix run github:cachix/devenv fallback"; \
		exec "$$HOME/.nix-profile/bin/nix" --extra-experimental-features 'nix-command flakes' run github:cachix/devenv -- up; \
	elif [ -x /nix/var/nix/profiles/default/bin/nix ]; then \
		echo "devenv is not on PATH; using /nix/var/nix/profiles/default/bin/nix run github:cachix/devenv fallback"; \
		exec /nix/var/nix/profiles/default/bin/nix --extra-experimental-features 'nix-command flakes' run github:cachix/devenv -- up; \
	elif command -v direnv >/dev/null 2>&1; then \
		echo "devenv is not on PATH; trying direnv exec . devenv up"; \
		exec direnv exec . devenv up; \
	else \
		echo "Could not find devenv or nix." >&2; \
		echo "Run make doctor to inspect the local tool path." >&2; \
		echo "Run make install after Nix is available to install devenv." >&2; \
		exit 127; \
	fi
