.POSIX:


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


derived-assets = \
	var/respiro                                       \
	var/result-home                                   \
	var/result-system                                 \
	$(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_CONFIG_HOME)/xmobar/xmobarrc                \
	$(XDG_DATA_HOME)/euandreh/e.list.txt              \
	$(HOME)/.ssh                                      \


all: $(derived-assets) tilde

mnt: $(HOME)/mnt/suyin/enc $(HOME)/mnt/crypt/.keep

umnt:
	cryfs-unmount $(HOME)/mnt/crypt
	fusermount -u $(HOME)/mnt/suyin

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


var/respiro:
	fallocate -l 8G $@

var/result-home:
	guix build -r $@ -v3 -f etc/guix/home.scm

var/result-system:
	guix build -r $@ -v3 -f etc/guix/system.scm

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  >> $@

$(XDG_CONFIG_HOME)/xmobar/xmobarrc:
	cd $(@D) && ln -s `hostname` xmobarrc

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

$(HOME)/mnt/suyin/enc:
	if [ ! -e $(@D) ]; then mkdir -p $(@D); fi
	sshfs suyin: $(@D)

$(HOME)/mnt/crypt/.keep: $(HOME)/mnt/suyin/enc
	if [ ! -e $(@D) ]; then mkdir -p $(@D); fi
	if [ ! -e $@    ]; then cryfs $? $(@D); fi
	touch $@

$(HOME)/txt/TODOs.adoc:
	mkdir -p $(@D)
	touch $@

$(HOME)/txt/scratch.txt:
	mkdir -p $(@D)
	touch $@

$(XDG_DATA_HOME)/euandreh/e.list.txt: opt/aux/gen-e-list.sh \
		$(HOME)/txt/TODOs.adoc $(HOME)/txt/scratch.txt
	sh opt/aux/gen-e-list.sh > $@



check-shellcheck:
	git ls-files | \
		grep -v '^share/aux/' | \
		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 ||:

FIXME-excludes = \
	':(exclude)Makefile' \
	':(exclude)bin/aux' \
	':(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-aliases:
	for alias in abuse admin hostmaster postmaster; do \
		uuid | email -s "\"$$alias\" alias test from $$(id -un)@$$(hostname)" "$$alias@euandre.org"; \
	done
	uuid | email -s "toph->lawtech" -f eu@euandre.org    andre@pilotis.net
	uuid | email -s "lawtech->papo" -f andre@pilotis.net andre@papo.im
	uuid | email -s "papo->toph"    -f andre@papo.im     eu@euandre.org

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

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

VM-DIR = /srv/www/s/vm
upload:
	rsync \
		--rsync-path='sudo -u deployer rsync' \
		-avP \
		--exclude mint-x86_64-cinnamon.qcow2 \
		 $(XDG_STATE_HOME)/vm/qemu/ euandre.org:$(VM-DIR)/qemu/
	ssh euandre.org find $(VM-DIR)/qemu/ -type f | \
		sed 's|^$(VM-DIR)/qemu/||' | \
		sed 's|\.qcow2$$||'        | \
		ssh euandre.org sudo -u deployer dd of=$(VM-DIR)/vms.txt
