MANPAGES = lacme-accountd.1 lacme.1

all: ${MANPAGES}

# upper case the headers and remove the links
%.1: %.md
	@pandoc -S -f markdown -t json "$<" | \
	jq "                                                                        \
	    def fixheaders:                                                         \
	        if .t == \"Header\" then                                            \
	            .c[2][] |= (if .t == \"Str\" then .c |= ascii_upcase else . end)\
	        else                                                                \
	            .                                                               \
	        end;                                                                \
	    def fixlinks:                                                           \
	        if type == \"object\" then                                          \
	            if .t == \"Link\" then                                          \
	                if .c[2][0][0:7] == \"mailto:\" then . else .c[1][] end     \
	            else                                                            \
	                map_values(fixlinks)                                        \
	            end                                                             \
	        else if type == \"array\" then                                      \
	                map(fixlinks)                                               \
	            else                                                            \
	                .                                                           \
	            end                                                             \
	        end;                                                                \
	    { \"pandoc-api-version\"                                                \
	    , meta                                                                  \
	    , blocks: .blocks | map(fixheaders) | map(fixlinks)                     \
	    }" | \
	pandoc -sS -f json -t man -o "$@"

install: ${MANPAGES}
	install -d $(DESTDIR)/etc/lacme
	install -d $(DESTDIR)/etc/lacme/lacme-certs.conf.d
	install -m0644 -t $(DESTDIR)/etc/lacme config/*.conf
	install -m0644 -t $(DESTDIR)/etc/lacme snippets/*.conf
	install -d $(DESTDIR)/usr/share/lacme
	install -m0644 -t $(DESTDIR)/usr/share/lacme certs/lets-encrypt-x[1-4]-cross-signed.pem
	install -d $(DESTDIR)/usr/lib/lacme
	install -m0755 -t $(DESTDIR)/usr/lib/lacme client webserver
	install -d $(DESTDIR)/usr/share/man/man1
	install -m0644 -t $(DESTDIR)/usr/share/man/man1 lacme-accountd.1 lacme.1
	install -d $(DESTDIR)/usr/bin
	install -m0644 -t $(DESTDIR)/usr/bin lacme-accountd
	install -d $(DESTDIR)/usr/sbin
	install -m0644 -t $(DESTDIR)/usr/bin lacme

clean:
	rm -vf ${MANPAGES}

.PHONY: all install clean