GIT_VERSION  := $(shell git describe --tags --dirty --always 2>/dev/null)
VERSION      := $(if $(GIT_VERSION),$(GIT_VERSION),$(shell cat VERSION 2>/dev/null))

SRC= src/sigtool.c src/monocypher.c src/hex.c src/base64.c src/parse.c src/util.c

BLDDATE ?= $(shell date "+%Y-%m-%d")
CFLAGS += -DBLDDATE='"$(BLDDATE)"'
CFLAGS += -DSIGTOOL_DEBUG=1
CFLAGS += -DSIGTOOL_VERSION='"$(VERSION)"'

CFLAGS += -Wall -Wextra -Wpedantic -ggdb -Og -std=gnu99 -Wshadow \
			-Wformat=2 -Wundef -Werror=return-type \
			-Wdouble-promotion -Wmissing-prototypes \
			-Wstrict-prototypes -Wconversion

all: sigtool

install:
	@echo -e "\n\t\033[1;31mThis is not finished, you do not want to use this yet.\033[0m\n"

check: test all
	make -C test
	sh script/apicheck.sh

manpage: sigtool.1

sigtool: $(SRC)
	$(CC) $(CFLAGS) $(SRC) -o sigtool

sigtool.1: docs/sigtool.scd
	scdoc < $< | sed "s/@VERSION@/$(VERSION)/g" > $@

CLEANFILES = *.o *.tar.gz *.1 sigtool *amalgamation.c *.json hextest mctest testrun
ifneq ($(GIT_VERSION),)
	CLEANFILES += VERSION
endif

monocypher:
	sh script/vendor_monocypher.sh
	git apply script/monocypher_4.0.2.patch

clean:
	rm -fr $(CLEANFILES)
	make -C test clean

release:
	sh script/dist.sh

.PHONY: clean monocypher install uninstall release help manpage sigtool.1 amalgamation
