.POSIX:


pod2man = \
	share/man/man1/x.1 \
	share/man/man1/z.1 \


lisp-images = \
	$(XDG_DATA_HOME)/lisp-cli/clozure.image \
	$(XDG_DATA_HOME)/lisp-cli/clisp.image   \
	$(XDG_DATA_HOME)/lisp-cli/sbcl.image    \

derived-assets = \
	$(pod2man)                                        \
	$(XDG_STATE_HOME)/ssh/conn                        \
	$(XDG_CONFIG_HOME)/ssh/id_rsa.pub                 \
	$(XDG_CONFIG_HOME)/ssh/config                     \
	$(XDG_CONFIG_HOME)/gnupg/gpg-agent.conf           \
	$(XDG_DATA_HOME)/common-lisp/source               \
	$(XDG_DATA_HOME)/euandreh/e.list.txt              \
	$(PRIV_STATEDIR)/postfix/relayhosts-maps.db       \
	$(PRIV_STATEDIR)/postfix/sasl-password.db         \
	$(HOME)/.ssh                                      \
	$(lisp-images)


all: $(derived-assets)


tilde:
	@cd $(PRIV_CONFIG) && $(MAKE)


share/man/man1/x.1 share/man/man1/z.1:
	pod2man bin/`basename $@ .1` > $@

share/man/man1/x.1: bin/x
share/man/man1/z.1: bin/z

$(XDG_STATE_HOME)/ssh/conn:
	mkdir -p $@

$(XDG_CONFIG_HOME)/ssh/id_rsa.pub:
	gpg --export-ssh-key eu@euandre.org > $@
	chmod 600 $@

$(XDG_CONFIG_HOME)/ssh/config: $(XDG_CONFIG_HOME)/ssh/config.tmpl
	envsubst < $(XDG_CONFIG_HOME)/ssh/config.tmpl > $@

$(XDG_CONFIG_HOME)/gnupg/gpg-agent.conf: $(XDG_CONFIG_HOME)/gnupg/gpg-agent.conf.tmpl
	cp $(XDG_CONFIG_HOME)/gnupg/gpg-agent.conf.tmpl $@
	printf 'pinentry-program ' >> $@
	command -v pinentry-gtk-2  >> $@

$(HOME)/.ssh:
	ln -s $(XDG_CONFIG_HOME)/ssh $@

$(XDG_DATA_HOME)/common-lisp/source:
	ln -s $(SRC)/libre $@

$(HOME)/Documents/txt/:
	mkdir -p $@
	touch $@/TODOs.md
	touch $@/scratch.txt

$(XDG_DATA_HOME)/euandreh/e.list.txt: $(HOME)/Documents/txt/ opt/aux/gen-e-list.sh
	sh opt/aux/gen-e-list.sh > $@

$(PRIV_STATEDIR)/postfix/relayhosts-maps.db $(PRIV_STATEDIR)/postfix/sasl-password.db:
	postmap $(@D)/`basename $@ .db`

$(PRIV_STATEDIR)/postfix/sasl-password: tilde
$(PRIV_STATEDIR)/postfix/sasl-password.db:   $(PRIV_STATEDIR)/postfix/sasl-password
$(PRIV_STATEDIR)/postfix/relayhosts-maps.db: $(PRIV_STATEDIR)/postfix/relayhosts-maps

$(lisp-images): $(XDG_CONFIG_HOME)/lisp-cli/init.lisp bin/cl bin/li
	I=`echo $@ | awk -F/ '$$0=$$(NF)' | cut -d. -f1` && \
		li -vI $$I -E "(format t \"Image for \\\"$$I\\\" created.~%\")"



check-shellcheck:
	git ls-files | \
		sor 'test -f' | \
		xargs awk '/^#!\/bin\/sh$$/ { print FILENAME } { nextfile }' | \
		xargs shellcheck -x

check-perlcritic:
	git ls-files | \
		sor 'test -f' | \
		xargs awk '/^#!\/usr\/bin\/env perl$$/ { print FILENAME } { nextfile }' | \
		xargs perlcritic --exclude=subroutine

check-nixfmt:
	git ls-files | sor 'test -f' | grep -E '\.nix$$' | xargs nixfmt -c

FIXME-excludes = \
	':(exclude)Makefile' \
	':(exclude)etc/git/ignore' \
	':(exclude)opt/aux/gen-e-list.sh'
check-fixme:
	if git grep FIXME -- $(FIXME-excludes); then \
		printf 'Leftover FIXME markers.\n' >&2; \
		exit 1; \
	fi

check-dirty-public:
	if ! git diff --quiet || ! git diff --quiet --staged; then \
		printf 'Dirty ~/.usr/ repository.\n' >&2; \
		exit 1; \
	fi
	if git status --short --branch --porcelain | \
			head -n1 | grep -E '(ahead|behind)'; then \
		printf '~/.usr/ of sync with origin.\n' >&2; \
		exit 1; \
	fi

check-dirty-private:
	if ! git -C $(PRIV_CONFIG) diff --quiet || \
			! git -C $(PRIV_CONFIG) diff --quiet --staged; then \
		printf 'Dirty private tilde repository.\n' >&2; \
		exit 1; \
	fi
	if git -C $(PRIV_CONFIG) status --short --branch --porcelain | \
			head -n1 | grep -E '(ahead|behind)'; then \
		printf 'Private of sync with origin.\n' >&2; \
		exit 1; \
	fi

check-dirty-pass:
	if ! git -C $(PASSWORD_STORE_DIR) diff --quiet || \
			! git -C $(PASSWORD_STORE_DIR) diff --quiet --staged; then \
		printf 'Dirty password store repository.\n' >&2; \
		exit 1; \
	fi
	if git -C $(PASSWORD_STORE_DIR) status --short --branch --porcelain | \
			head -n1 | grep -E '(ahead|behind)'; then \
		printf 'Password store rut of sync with origin.\n' >&2; \
		exit 1; \
	fi

check-opt:
	find opt/tests/ -name '*.sh' -exec sh {} +

check-pod:
	podchecker bin/z

check: check-shellcheck check-perlcritic check-nixfmt check-fixme              \
                check-dirty-public check-dirty-private check-dirty-pass        \
		check-opt check-pod
	@cd $(PRIV_CONFIG) && $(MAKE) $@

clean:
	rm -rf $(derived-assets)
	@cd $(PRIV_CONFIG) && $(MAKE) $@