GCLI_VERSION = @PACKAGE_VERSION@

# Sort of compatibility for GNU make and BSD make
.PATH: @SRCDIR@
VPATH=@SRCDIR@

################ Enviroment and values saved by the configure script ##########################
AR                  = @AR@
CC_FOR_BUILD        = @CC_FOR_BUILD@
CC                  = @CC@
CCACHE              = @CCACHE@
CCOM_FOR_BUILD      = @CCOM_FOR_BUILD@
CCOM                = @CCOM@
OPTIMISE            = @OPTIMISE@
ENV_PKG_CONFIG      = @ENV_PKG_CONFIG@
ENV_PKG_CONFIG_PATH = @ENV_PKG_CONFIG_PATH@
INSTALL             = @INSTALL@
KYUA                = @KYUA@
RANLIB              = @RANLIB@
RM                  = @RM@
PREFIX              = @PREFIX@
SHELL               = @SHELL@
EXEEXT              = @EXEEXT@
LIBEXT              = @LIBEXT@
OBJEXT              = @OBJEXT@
EXEEXT_FOR_BUILD    = @EXEEXT_FOR_BUILD@
LIBEXT_FOR_BUILD    = @LIBEXT_FOR_BUILD@
OBJEXT_FOR_BUILD    = @OBJEXT_FOR_BUILD@
ENABLE_MAINTAINER   = @ENABLE_MAINTAINER@
################################################################################################

############## Optimiser / Warning flags #####
COPTFLAGS_gcc_debug=		-O0 -g3 -Wall -Wextra -Werror
COPTFLAGS_gcc_release=		-O2 -DNDEBUG
COPTFLAGS_clang_debug=		-O0 -g3 -Wall -Wextra -Wno-gnu-zero-variadic-macro-arguments -Werror
COPTFLAGS_clang_release=	-O2 -DNDEBUG
COPTFLAGS_xlc_debug=		-qpic -qcheck=all -qfulldebug -qnoopt -g -qhalt=w
COPTFLAGS_xlc_release=		-qpic -qoptimize=4 -DNDEBUG
COPTFLAGS_sunstudio_debug=	-xO0 -g -xvector=%none
COPTFLAGS_sunstudio_release=	-xO5 -DNDEBUG
COPTFLAGS=			$(COPTFLAGS_$(CCOM)_$(OPTIMISE))
COPTFLAGS_FOR_BUILD=		$(COPTFLAGS_$(CCOM_FOR_BUILD)_$(OPTIMISE))

CSTDFLAGS_gcc=		-std=c11 -pedantic -D_XOPEN_SOURCE=700
CSTDFLAGS_clang=	-std=c11 -pedantic -D_XOPEN_SOURCE=700
CSTDFLAGS_xlc=		-qlanglvl=stdc11 -D_XOPEN_SOURCE=700
CSTDFLAGS_sunstudio=	-std=c11 -D_XOPEN_SOURCE=600 -pedantic=yes    # Oracle Compiler on Solaris doesn't support 700
CSTDFLAGS=		$(CSTDFLAGS_$(CCOM))
CSTDFLAGS_FOR_BUILD=	$(CSTDFLAGS_$(CCOM_FOR_BUILD))

############## CFLAGS ########################
ENV_CFLAGS=		@ENV_CFLAGS@
CFLAGS_clang=		-Wno-gnu-zero-variadic-macro-arguments -fPIC -pipe
CFLAGS_gcc=		-fPIC -pipe
CFLAGS_sunstudio=	-m64 -errtags=yes -errfmt=error -errwarn=%all -xcode=pic32 -DPIC -xregs=no%appl
CFLAGS_sunstudio +=	-erroff=E_TOKENLESS_MACRO                # Causes errors with TAILQ_HEAD(, ...)
CFLAGS_sunstudio +=	-erroff=E_STATEMENT_NOT_REACHED          # Causes errors in pdjson.c
CFLAGS=			$(CSTDFLAGS) $(COPTFLAGS) $(CFLAGS_$(CCOM)) -I@SRCDIR@/include -I. $(ENV_CFLAGS)

ENV_CFLAGS_FOR_BUILD=	@ENV_CFLAGS_FOR_BUILD@
CFLAGS_FOR_BUILD=	$(CSTDFLAGS_FOR_BUILD) $(COPTFLAGS_FOR_BUILD) $(CFLAGS_$(CCOM)) -I@SRCDIR@/include -I@SRCDIR@/thirdparty -I. $(ENV_CFLAGS_FOR_BUILD)

############## LDFLAGS  ######################
ENV_LDFLAGS=		@ENV_LDFLAGS@
LDFLAGS=		$(ENV_LDFLAGS)
ENV_LDFLAGS_FOR_BUILD=	@ENV_LDFLAGS_FOR_BUILD@
LDFLAGS_FOR_BUILD=	$(ENV_LDFLAGS_FOR_BUILD)

############## CPPFLAGS ######################
ENV_CPPFLAGS=		@ENV_CPPFLAGS@
CPPFLAGS=		-DHAVE_CONFIG_H=1 $(ENV_CPPFLAGS)

ENV_CPPFLAGS_FOR_BUILD=	@ENV_CPPFLAGS_FOR_BUILD@
CPPFLAGS_FOR_BUILD=	-DHAVE_CONFIG_H -DYY_NO_UNPUT -DYY_NO_INPUT $(ENV_CPPFLAGS_FOR_BUILD)

# CFLAGS for dependency tracking for various compilers
CCDEPFLAGS_gcc=		-MD -MF ${@:$(OBJEXT)=.d}
CCDEPFLAGS_clang=	-MD -MF ${@:$(OBJEXT)=.d}
CCDEPFLAGS_xlc=		-qmakedep=gcc -MF ${@:$(OBJEXT)=.d}
CCDEPFLAGS_sunstudio=	-xMD -xMF ${@:$(OBJEXT)=.d}
CCDEPFLAGS_unknown=
CCDEPFLAGS=		$(CCDEPFLAGS_$(CCOM))
CCDEPFLAGS_FOR_BUILD=	$(CCDEPFLAGS_$(CCOM_FOR_BUILD))

########## Static Analysis Flags #############################
CANALFLAGS_clang=	--analyze -Werror
CANALFLAGS_gcc=		-fanalyzer -c -o /dev/null
CANALFLAGS=		$(CANALFLAGS_$(CCOM))

LIBCURL_CFLAGS=		@LIBCURL_CFLAGS@
LIBCURL_LIBS=		@LIBCURL_LIBS@

LIBATFC_CFLAGS=		@LIBATFC_CFLAGS@
LIBATFC_LIBS=		@LIBATFC_LIBS@

############## LIBEDIT ##########################################
LIBEDIT_FOUND=		@LIBEDIT_FOUND@
LIBEDIT_CFLAGS_1=	@LIBEDIT_CFLAGS@
LIBEDIT_LIBS_1=		@LIBEDIT_LIBS@
LIBEDIT_CFLAGS=		$(LIBEDIT_CFLAGS_$(LIBEDIT_FOUND))
LIBEDIT_LIBS=		$(LIBEDIT_LIBS_$(LIBEDIT_FOUND))
LIBEDIT_CPPFLAGS_1=	-DHAVE_LIBEDIT=1
LIBEDIT_CPPFLAGS=	$(LIBEDIT_CPPFLAGS_$(LIBEDIT_FOUND))

############## READLINE #########################################
LIBREADLINE_FOUND=	@LIBREADLINE_FOUND@
LIBREADLINE_CFLAGS_1=	@LIBREADLINE_CFLAGS@
LIBREADLINE_LIBS_1=	@LIBREADLINE_LIBS@
LIBREADLINE_CFLAGS=	$(LIBREADLINE_CFLAGS_$(LIBREADLINE_FOUND))
LIBREADLINE_LIBS=	$(LIBREADLINE_LIBS_$(LIBREADLINE_FOUND))
LIBREADLINE_CPPFLAGS_1=	-DHAVE_LIBREADLINE=1
LIBREADLINE_CPPFLAGS=	$(LIBREADLINE_CPPFLAGS_$(LIBREADLINE_FOUND))

############## LOWDOWN #########################################
LIBLOWDOWN_FOUND=	@LIBLOWDOWN_FOUND@
LIBLOWDOWN_MAJOR=	@LIBLOWDOWN_MAJOR@
LIBLOWDOWN_MINOR=	@LIBLOWDOWN_MINOR@
LIBLOWDOWN_CFLAGS_1=	@LIBLOWDOWN_CFLAGS@ \
			-DLIBLOWDOWN_MAJOR=$(LIBLOWDOWN_MAJOR) \
			-DLIBLOWDOWN_MINOR=$(LIBLOWDOWN_MINOR)
LIBLOWDOWN_LIBS_1=	@LIBLOWDOWN_LIBS@
LIBLOWDOWN_CFLAGS=	$(LIBLOWDOWN_CFLAGS_$(LIBLOWDOWN_FOUND))
LIBLOWDOWN_LIBS=	$(LIBLOWDOWN_LIBS_$(LIBLOWDOWN_FOUND))
LIBLOWDOWN_CPPFLAGS_1=	-DHAVE_LIBLOWDOWN=1
LIBLOWDOWN_CPPFLAGS=	$(LIBLOWDOWN_CPPFLAGS_$(LIBLOWDOWN_FOUND))

# Only used in maintainer mode
LOWDOWN=		@LOWDOWN@

############## LIBCRYPTO ########################################
LIBCRYPTO_CFLAGS=	@LIBCRYPTO_CFLAGS@
LIBCRYPTO_LIBS=		@LIBCRYPTO_LIBS@

############### -lrt ############################################
ENABLE_LIBRT=		@ENABLE_LIBRT@
LIBRT_LIBS_1=		-lrt
LIBRT_LIBS=		$(LIBRT_LIBS_$(ENABLE_LIBRT))

############## PDJSON ###########################################
# system version
PDJSON_CFLAGS_1=	@PDJSON_CFLAGS@
PDJSON_LIBS_1=		@PDJSON_LIBS@
PDJSON_SRCS_1=

# vendored version
PDJSON_CFLAGS_0=	-I@SRCDIR@/thirdparty/pdjson
PDJSON_LIBS_0=
PDJSON_SRCS_0=		thirdparty/pdjson/pdjson.c

# dispatch
PDJSON_FOUND=		@PDJSON_FOUND@
PDJSON_CFLAGS=		$(PDJSON_CFLAGS_$(PDJSON_FOUND))
PDJSON_LIBS=		$(PDJSON_LIBS_$(PDJSON_FOUND))
PDJSON_SRCS=		$(PDJSON_SRCS_$(PDJSON_FOUND))

#################################################################

.PHONY: all all-auto clean clean-auto check check-auto install install-auto analyse analyse-auto
all: Makefile
	@$(MAKE) all-auto

clean: Makefile
	@$(MAKE) clean-auto

check: Makefile
	@$(MAKE) check-auto

install: Makefile
	@$(MAKE) install-auto

analyse: Makefile
	@$(MAKE) analyse-auto

all-auto: gcli$(EXEEXT) manpages

LIBGCLI_SRCS = \
	src/bugzilla/api.c \
	src/bugzilla/attachments.c \
	src/bugzilla/bugs-parser.c \
	src/bugzilla/bugs.c \
	src/bugzilla/comment.c \
	src/bugzilla/config.c \
	src/gitea/comments.c \
	src/gitea/config.c \
	src/gitea/forks.c \
	src/gitea/issues.c \
	src/gitea/labels.c \
	src/gitea/milestones.c \
	src/gitea/pulls.c \
	src/gitea/releases.c \
	src/gitea/repos.c \
	src/gitea/sshkeys.c \
	src/gitea/status.c \
	src/github/api.c \
	src/github/checks.c \
	src/github/checkout.c \
	src/github/comments.c \
	src/github/config.c \
	src/github/forks.c \
	src/github/gists.c \
	src/github/issues.c \
	src/github/labels.c \
	src/github/milestones.c \
	src/github/path.c \
	src/github/pulls.c \
	src/github/releases.c \
	src/github/repos.c \
	src/github/sshkeys.c \
	src/github/status.c \
	src/gitlab/api.c \
	src/gitlab/checkout.c \
	src/gitlab/comments.c \
	src/gitlab/config.c \
	src/gitlab/forks.c \
	src/gitlab/issues.c \
	src/gitlab/labels.c \
	src/gitlab/merge_requests.c \
	src/gitlab/milestones.c \
	src/gitlab/pipelines.c \
	src/gitlab/releases.c \
	src/gitlab/repos.c \
	src/gitlab/snippets.c \
	src/gitlab/sshkeys.c \
	src/gitlab/status.c \
	src/attachments.c \
	src/base64.c \
	src/comments.c \
	src/ctx.c \
	src/curl.c \
	src/date_time.c \
	src/diffutil.c \
	src/forges.c \
	src/forks.c \
	src/gcli.c \
	src/issues.c \
	src/json_gen.c \
	src/json_util.c \
	src/labels.c \
	src/milestones.c \
	src/nvlist.c \
	src/path.c \
	src/pulls.c \
	src/releases.c \
	src/repos.c \
	src/sshkeys.c \
	src/status.c \
	src/url.c \
	src/waitproc.c \
	src/port/err.c \
	src/port/string.c \
	src/port/sv.c \
	src/port/util.c \
	$(PDJSON_SRCS)

GCLI_SRCS= \
	src/cmd/actions.c \
	src/cmd/api.c \
	src/cmd/attachments.c \
	src/cmd/ci.c \
	src/cmd/cmd.c \
	src/cmd/cmdconfig.c \
	src/cmd/colour.c \
	src/cmd/comment.c \
	src/cmd/config.c \
	src/cmd/editor.c \
	src/cmd/forks.c \
	src/cmd/gcli.c \
	src/cmd/gists.c \
	src/cmd/gitconfig.c \
	src/cmd/interactive.c \
	src/cmd/issues.c \
	src/cmd/labels.c \
	src/cmd/milestones.c \
	src/cmd/open.c \
	src/cmd/pipelines.c \
	src/cmd/pull_reviews.c \
	src/cmd/pulls.c \
	src/cmd/releases.c \
	src/cmd/repos.c \
	src/cmd/snippets.c \
	src/cmd/status.c \
	src/cmd/status_interactive.c \
	src/cmd/table.c

TEMPLATES= \
	templates/bugzilla/api.t \
	templates/bugzilla/bugs.t \
	templates/gitea/milestones.t \
	templates/gitea/status.t \
	templates/github/api.t \
	templates/github/checks.t \
	templates/github/comments.t \
	templates/github/forks.t \
	templates/github/gists.t \
	templates/github/issues.t \
	templates/github/labels.t \
	templates/github/milestones.t \
	templates/github/pulls.t \
	templates/github/releases.t \
	templates/github/repos.t \
	templates/github/status.t \
	templates/gitlab/api.t \
	templates/gitlab/comments.t \
	templates/gitlab/forks.t \
	templates/gitlab/issues.t \
	templates/gitlab/labels.t \
	templates/gitlab/merge_requests.t \
	templates/gitlab/milestones.t \
	templates/gitlab/pipelines.t \
	templates/gitlab/releases.t \
	templates/gitlab/repos.t \
	templates/gitlab/snippets.t \
	templates/gitlab/sshkeys.t \
	templates/gitlab/status.t

PGEN_SRCS= \
	src/port/err.c \
	src/pgen/dump_c.c \
	src/pgen/dump_h.c \
	src/pgen/dump_plain.c \
	lex.yy.c \
	y.tab.c

TEMPLATE_HEADERS=	${TEMPLATES:.t=.h}
TEMPLATE_SRCS=		${TEMPLATES:.t=.c}

Makefile: @SRCDIR@/configure @SRCDIR@/Makefile.in
	@echo "Reconfiguring Makefile"
	env CC="${CC}" YACC="${YACC}" LEX="${LEX}" RM="${RM}" RANLIB="${RANLIB}" \
		KYUA="${KYUA}" LOWDOWN="${LOWDOWN}" SHELL="${SHELL}" CFLAGS="${ENV_CFLAGS}" CPPFLAGS="${ENV_CPPFLAGS}" \
		INSTALL="${INSTALL}" CC_FOR_BUILD="${CC_FOR_BUILD}" ENABLE_MAINTAINER="${ENABLE_MAINTAINER}" \
		LDFLAGS="${ENV_LDFLAGS}" LDFLAGS_FOR_BUILD="${LDFLAGS_FOR_BUILD}" \
		CFLAGS_FOR_BUILD="${ENV_CFLAGS_FOR_BUILD}" CPPFLAGS_FOR_BUILD="${ENV_CPPFLAGS_FOR_BUILD}" \
		PKG_CONFIG="${ENV_PKG_CONFIG}" PKG_CONFIG_PATH="${ENV_PKG_CONFIG_PATH}" CCACHE="${CCACHE}" \
		EXEEXT="${EXEEXT}" OBJEXT="${OBJEXT}" LIBEXT="${LIBEXT}" \
		EXEEXT_FOR_BUILD="${EXEEXT_FOR_BUILD}" OBJEXT_FOR_BUILD="${OBJEXT_FOR_BUILD}" LIBEXT_FOR_BUILD="${LIBEXT_FOR_BUILD}" \
		sh @SRCDIR@/configure @CONFIGURE_CMD_ARGS@

LIBGCLI_OBJS=	${LIBGCLI_SRCS:.c=.libgcli$(OBJEXT)} ${TEMPLATE_SRCS:.c=.libgcli$(OBJEXT)}
GCLI_OBJS=	${GCLI_SRCS:.c=.gcli$(OBJEXT)}
PGEN_OBJS=	${PGEN_SRCS:.c=.pgen$(OBJEXT_FOR_BUILD)}

# Dependencies
DEPS=	${LIBGCLI_OBJS:$(OBJEXT)=.d} ${GCLI_OBJS:$(OBJEXT)=.d} ${PGEN_OBJS:$(OBJEXT_FOR_BUILD)=.d}
-include $(DEPS)

pgen$(EXEEXT_FOR_BUILD): $(PGEN_OBJS)
	$(CCACHE) $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) \
		$(LDFLAGS_FOR_BUILD) -o pgen$(EXEEXT_FOR_BUILD) $(PGEN_OBJS)

y.tab.c y.tab.h: @SRCDIR@/src/pgen/parser.y
	$(YACC) -d @SRCDIR@/src/pgen/parser.y

lex.yy.c: y.tab.h @SRCDIR@/src/pgen/lexer.l
	$(LEX) @SRCDIR@/src/pgen/lexer.l

.SUFFIXES: .h .t .c

# Hack to make GNU make shut up
$(TEMPLATE_HEADERS) $(TEMPLATE_SRCS): pgen$(EXEEXT_FOR_BUILD)
.t.h:
	@mkdir -p $$(dirname $@)
	./pgen$(EXEEXT_FOR_BUILD) -th -o $@ $<

.t.c:
	@mkdir -p $$(dirname $@)
	./pgen$(EXEEXT_FOR_BUILD) -tc -o $@ $<

gcli$(EXEEXT): libgcli$(LIBEXT) $(GCLI_OBJS)
	$(CCACHE) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o gcli$(EXEEXT) \
		$(GCLI_OBJS) libgcli$(LIBEXT) $(LIBCURL_LIBS) $(LIBEDIT_LIBS) \
		$(LIBREADLINE_LIBS) $(LIBCRYPTO_LIBS) \
		$(LIBLOWDOWN_LIBS) $(PDJSON_LIBS) \
		$(LIBRT_LIBS)

libgcli$(LIBEXT): $(LIBGCLI_OBJS)
	$(CCACHE) $(AR) -rc libgcli$(LIBEXT) $(LIBGCLI_OBJS)
	$(CCACHE) $(RANLIB) libgcli$(LIBEXT)

$(LIBGCLI_OBJS): $(TEMPLATE_HEADERS) $(TEMPLATE_SRCS)

.SUFFIXES: .c .libgcli$(OBJEXT) .gcli$(OBJEXT) .pgen$(OBJEXT_FOR_BUILD) .tests$(OBJEXT)
.c.libgcli$(OBJEXT):
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC) $(CFLAGS) $(CCDEPFLAGS) $(CPPFLAGS) \
		$(LIBCURL_CFLAGS) $(LIBCRYPTO_CFLAGS) \
		$(LIBLOWDOWN_CFLAGS) $(LIBLOWDOWN_CPPFLAGS) \
		$(PDJSON_CFLAGS) \
		-DIN_LIBGCLI=1 -c -o $@ $<

.c.gcli$(OBJEXT):
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC) $(CFLAGS) $(CCDEPFLAGS) $(CPPFLAGS) \
		$(LIBEDIT_CPPFLAGS) $(LIBREADLINE_CPPFLAGS) \
		$(LIBCURL_CFLAGS) $(LIBEDIT_CFLAGS) \
		$(LIBREADLINE_CFLAGS) $(LIBCRYPTO_CFLAGS) \
		$(LIBLOWDOWN_CFLAGS) $(LIBLOWDOWN_CPPFLAGS) \
		$(PDJSON_CFLAGS) \
		-c -o $@ $<

.c.pgen$(OBJEXT_FOR_BUILD):
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
		$(CCDEPFLAGS_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) \
		-c -o $@ $<

clean-auto:
	$(RM) -f pgen$(EXEEXT_FOR_BUILD) gcli$(EXEEXT) \
		libgcli$(LIBEXT) \
		$(LIBGCLI_OBJS) $(GCLI_OBJS) \
		$(TEMPLATE_HEADERS) $(TEMPLATE_SRCS) \
		$(PGEN_OBJS) $(DEPS) \
		lex.yy.c y.tab.c y.tab.h \
		$(TEST_PROGRAMS:=.tests$(OBJEXT)) \
		$(TEST_PROGRAMS) \
		tests/Kyuafile \
		$(MANPAGES)

###### TEST SUITE #####################################
TEST_PROGRAMS = \
	tests/append-url$(EXEEXT) \
	tests/base64$(EXEEXT) \
	tests/bugzilla-parse$(EXEEXT) \
	tests/difftests$(EXEEXT) \
	tests/gitea-parse$(EXEEXT) \
	tests/github-parse$(EXEEXT) \
	tests/gitlab-parse$(EXEEXT) \
	tests/json-escape$(EXEEXT) \
	tests/jsongen$(EXEEXT) \
	tests/parse-url$(EXEEXT) \
	tests/url-encode$(EXEEXT)

tests/json-escape: tests/json-escape.tests$(OBJEXT)
tests/github-parse: tests/github-parse.tests$(OBJEXT)
tests/gitlab-parse: tests/gitlab-parse.tests$(OBJEXT)
tests/gitea-parse: tests/gitea-parse.tests$(OBJEXT)
tests/bugzilla-parse: tests/bugzilla-parse.tests$(OBJEXT)
tests/url-encode: tests/url-encode.tests$(OBJEXT)
tests/jsongen: tests/jsongen.tests$(OBJEXT)
tests/base64: tests/base64.tests$(OBJEXT)
tests/difftests: tests/difftests.tests$(OBJEXT)
tests/parse-url: tests/parse-url.tests$(OBJEXT)
tests/append-url: tests/append-url.tests$(OBJEXT)

$(TEST_PROGRAMS): libgcli$(LIBEXT)
	$(CCACHE) $(CC) $(CFLAGS) $(CCDEPFLAGS) $(CPPFLAGS) \
		$(LIBATFC_CFLAGS) $(LIBCURL_CFLAGS) $(LIBCRYPTO_CFLAGS) \
		$(LDFLAGS) -o $@ $(@:=.tests$(OBJEXT)) libgcli$(LIBEXT) \
		$(LIBATFC_LIBS) $(LIBCURL_LIBS) $(LIBCRYPTO_LIBS) \
		$(PDJSON_LIBS)

.c.tests$(OBJEXT):
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC) $(CFLAGS) $(CCDEPFLAGS) $(CPPFLAGS) \
		$(LIBATFC_CFLAGS) $(LIBCURL_CFLAGS) $(PDJSON_CFLAGS) \
		-DTESTSRCDIR="\"@SRCDIR@/tests/\"" -c -o $@ $<

$(TEST_PROGRAMS:$(EXEEXT)=.c): $(TEMPLATE_HEADERS)

tests/Kyuafile: Makefile
	@mkdir -p tests
	echo "syntax(2)" > tests/Kyuafile
	echo "test_suite('gcli')" >> tests/Kyuafile
	for t in $(TEST_PROGRAMS); do printf "atf_test_program{name='%s', timeout = 1}\n" "$${t##tests/}" >> tests/Kyuafile; done

check-auto: tests/Kyuafile $(TEST_PROGRAMS)
	$(KYUA) test --build-root ./tests --kyuafile tests/Kyuafile

#################### static analysis #######################################
LIBGCLI_ANALYSE=	${LIBGCLI_SRCS:.c=.libgcli.analyse}
GCLI_ANALYSE=		${GCLI_SRCS:.c=.gcli.analyse}
ANALYSE_TARGETS=	$(LIBGCLI_ANALYSE) $(GCLI_ANALYSE)

analyse-auto: $(ANALYSE_TARGETS)

$(ANALYSE_TARGETS): $(TEMPLATE_HEADERS)

.SUFFIXES: .c .libgcli.analyse .gcli.analyse
.c.libgcli.analyse:
	@mkdir -p $$(dirname $@)
	$(CC) $(CFLAGS) $(CPPFLAGS) \
		$(LIBCURL_CFLAGS) $(LIBCRYPTO_CFLAGS) \
		$(LIBLOWDOWN_CFLAGS) $(LIBLOWDOWN_CPPFLAGS) \
		$(PDJSON_CFLAGS) \
		-DIN_LIBGCLI=1 $(CANALFLAGS) $<

.c.gcli.analyse:
	@mkdir -p $$(dirname $@)
	$(CC) $(CFLAGS) $(CPPFLAGS) \
		$(LIBEDIT_CPPFLAGS) $(LIBREADLINE_CPPFLAGS) \
		$(LIBCURL_CFLAGS) $(LIBEDIT_CFLAGS) \
		$(LIBREADLINE_CFLAGS) $(LIBCRYPTO_CFLAGS) \
		$(LIBLOWDOWN_CFLAGS) $(LIBLOWDOWN_CPPFLAGS) \
		$(PDJSON_CFLAGS) $(CANALFLAGS) $<


#################### install target ########################################
MANPAGES= \
	docs/gcli-api.1 \
	docs/gcli-comment.1 \
	docs/gcli-config.1 \
	docs/gcli-forks.1 \
	docs/gcli-gists.1 \
	docs/gcli-issues.1 \
	docs/gcli-labels.1 \
	docs/gcli-milestones.1 \
	docs/gcli-pipelines.1 \
	docs/gcli-pulls.1 \
	docs/gcli-pulls-review.1 \
	docs/gcli-releases.1 \
	docs/gcli-repos.1 \
	docs/gcli-snippets.1 \
	docs/gcli-status.1 \
	docs/gcli.1 \
	docs/gcli.5 \
	docs/gcli-tutorial.1

bindir=	/bin
mandir=	/share/man

BINDIR=	$(DESTDIR)$(PREFIX)$(bindir)
MANDIR=	$(DESTDIR)$(PREFIX)$(mandir)

.PHONY: manpages manpages-auto

# phony target to ensure the makefile is updated with the correct
# sed substitions.
#
# The -auto target here makes make not print "foo is up-to-date"
# when they are up-to-date.
manpages-auto: Makefile @SRCDIR@/configure $(MANPAGES)

manpages: Makefile @SRCDIR@/configure
	@$(MAKE) manpages-auto

$(MANPAGES): @SRCDIR@/configure Makefile

.SUFFIXES: .1.in .1 .5.in .5 .md
#################################################
# Maintainer target for regenerating the tutorial
TUTORIAL_PAGES:sh=	find @SRCDIR@/docs/website/tutorial/ -type f -name \*.md | grep -v old_index.md | sort
TUTORIAL_PAGES!=	find @SRCDIR@/docs/website/tutorial/ -type f -name \*.md | grep -v old_index.md | sort

# Dispatch between maintainer/non-maintainer mode
TUTORIAL_SRC_MAINTAINER_0=
TUTORIAL_SRC_MAINTAINER_1=	@SRCDIR@/docs/gcli-tutorial.1.in

$(TUTORIAL_SRC_MAINTAINER_$(ENABLE_MAINTAINER)): Makefile $(TUTORIAL_PAGES)
	cat $(TUTORIAL_PAGES) \
	| $(LOWDOWN) -s -tman \
		-m section=1 \
		-m title=gcli-tutorial \
		-m manheader="Tutorial of GCLI - Generated Offline Version of https://herrhotzenplotz.de/gcli/tutorial" \
		-m date="@PACKAGE_DATE@" \
	> @SRCDIR@/docs/gcli-tutorial.1.in

docs/gcli-tutorial.1: $(TUTORIAL_SRC_MAINTAINER_$(ENABLE_MAINTAINER))

#################################################
# Manual page preprocessing rules
.1.in.1 .5.in.5:
	@mkdir -p $$(dirname $@)
	sed \
		-e 's|\@PACKAGE_DATE\@|@PACKAGE_DATE@|g' \
		-e 's|\@PACKAGE_STRING\@|@PACKAGE_STRING@|g' \
		-e 's|\@PACKAGE_BUGREPORT\@|@PACKAGE_BUGREPORT@|g' \
		-e 's|\@PACKAGE_URL\@|@PACKAGE_URL@|g' \
		-e 's|\@PACKAGE_VERSION\@|@PACKAGE_VERSION@|g' \
		< ${<} > $@

install-auto: manpages gcli
	$(INSTALL) -d $(BINDIR)
	$(INSTALL) -m 0755 gcli $(BINDIR)
	$(INSTALL) -d $(MANDIR)
	$(INSTALL) -d $(MANDIR)/man5
	$(INSTALL) -d $(MANDIR)/man1
	# TODO: compress manual pages
	for PAGE in $(MANPAGES); do                               \
		case $$PAGE in                                    \
		*.1)                                              \
			$(INSTALL) -m 644 $$PAGE $(MANDIR)/man1   \
			;;                                        \
		*.5)                                              \
			$(INSTALL) -m 644 $$PAGE $(MANDIR)/man5   \
			;;                                        \
		*)                                                \
			echo error installing man page $$PAGE >&2 \
			;;                                        \
		esac                                              \
	done

###############################################################
# Distcheck target
distcheck: Makefile @SRCDIR@/configure
	@$(MAKE) distcheck-auto

distcheck-auto:
	(cd @SRCDIR@ && @SRCDIR@/tools/gentarball.sh)
	mkdir -p dist/.build-gcli-$(GCLI_VERSION)
	(cd dist && tar -x -f @SRCDIR@/dist/gcli-$(GCLI_VERSION)/gcli-$(GCLI_VERSION).tar.gz && chmod -R a-w gcli-$(GCLI_VERSION))
	(cd dist/.build-gcli-$(GCLI_VERSION) && ../gcli-$(GCLI_VERSION)/configure && $(MAKE) && $(MAKE) check)
	(chmod -R a+w dist/gcli-$(GCLI_VERSION) && rm -fr dist/.build-gcli-$(GCLI_VERSION) dist/gcli-$(GCLI_VERSION))

###############################################################
# Help Target
.PHONY: help help-auto
help: Makefile
	@$(MAKE) help-auto

help-auto:
	@echo "Available targets:"
	@echo "        all - Do a full build (this is the default target)"
	@echo "      check - Run the test suite"
	@echo "       dist - Build distribution tarballs"
	@echo "  distcheck - Run the test suite on the built distribution tarball."
	@echo "      clean - Clean the build directory"
	@echo "    analyse - Run static code analysis"
	@echo "    install - Install build artifacts"
	@echo "   manpages - Only build manual pages"
