.PHONY: all installdirs install clean check dist

PACKAGE=zeichensalat
VERSION=$(shell ./zs --version|cut -f 3 -d " ")

prefix ?= /usr/local
exec_prefix ?= $(prefix)
bindir ?= $(exec_prefix)/bin
mandir ?= $(prefix)/man/man1

INSTALL ?= /usr/bin/install
INSTALL_DATA = $(INSTALL) -m 644

all: zs.1

zs.1: zs
	LC_ALL=C.UTF8 help2man -L C.UTF8 -N -n "Zeichensalat, compress, sign and encode or decode, verify, decompress and run executables" ./zs > $@.tmp
	mv $@.tmp $@

# man2html output wasn't that nice
# use mandoc for now
# (I still have to do some manual editing)
zs.html: zs.1
	mandoc -T html zs.1 > $@.tmp
	mv $@.tmp $@
	type w3m && w3m -dump $@ || true

installdirs:
	$(INSTALL) -d $(DESTDIR)$(bindir) $(DESTDIR)$(mandir)

# todo: run mandb after installation?
install: installdirs
	$(INSTALL) zs $(DESTDIR)$(bindir)
	$(INSTALL_DATA) zs.1 $(DESTDIR)$(mandir)

clean:
	-rm -vf zs.1 *.tmp zs.bin zs.zs zs.sig zs.html zs.info zs.texi

check: all
	unset GPG_AGENT_INFO; ./test.exp

dist: zs.bin
	if ! [ "x$$(git describe --tags)" = "x$(VERSION)" ]; then echo "Missing git tag?"; exit 1; fi
	if test -e $(PACKAGE)-$(VERSION).tar.gz; then echo "Release tarball already exists"; exit 1; fi
	mkdir $(PACKAGE)-$(VERSION)
	cp -v AUTHORS COPYING example example2 Makefile README test.sh test.exp zs zs.bin \
		build-binary-in-schroot.sh build-binary.sh binary-install.sh binary-update.sh \
		$(PACKAGE)-$(VERSION)
	tar czvf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION)
	rm -rf $(PACKAGE)-$(VERSION)
	gpg --detach-sign $(PACKAGE)-$(VERSION).tar.gz

# for now we only build for linux/amd64
zs.bin: zs
	./build-binary-in-schroot.sh "$(VERSION)" > zs.tmp
	chmod +x zs.tmp
	mv zs.tmp zs.bin

zs.zs: zs.bin
	echo $$DISPLAY $$GPG_AGENT_INFO
	./zs -e < zs.bin > $@.tmp
	mv $@.tmp $@

zs.sig: zs.bin
	gpg --detach-sign < zs.bin > zs.sig.tmp
	mv zs.sig.tmp $@

# not that great / overkill anyway
zs.texi: zs.1
	pandoc -f man zs.1 -t texinfo -o zs.texi.tmp
	mv zs.texi.tmp zs.texi

zs.info: zs.texi
	makeinfo zs.texi

# doesn't work
#zs.pdf: zs.texi
#	makeinfo --pdf zs.texi
