GCLI_VERSION = @PACKAGE_VERSION@

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

# Don't print directory messages when reinvoking make
GNUMAKEFLAGS+=   --no-print-directory

################ 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@
ENV_PKG_CONFIG_LIBDIR      = @ENV_PKG_CONFIG_LIBDIR@
ENV_PKG_CONFIG_SYSROOT_DIR = @ENV_PKG_CONFIG_SYSROOT_DIR@
INSTALL                    = @INSTALL@
RANLIB                     = @RANLIB@
RM                         = @RM@
PREFIX                     = @PREFIX@
PERL                       = @PERL@
GZIP                       = @GZIP@
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@
ENABLE_TESTS               = @ENABLE_TESTS@
ENABLE_SANITISERS          = @ENABLE_SANITISERS@
################################################################################################

############## 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

CSANFLAGS_clang_1=		-fsanitize=address
CSANFLAGS_gcc_1=		-fsanitize=address

COPTFLAGS=			$(COPTFLAGS_$(CCOM)_$(OPTIMISE)) $(CSANFLAGS_$(CCOM)_$(ENABLE_SANITISERS))
COPTFLAGS_FOR_BUILD=		$(COPTFLAGS_$(CCOM_FOR_BUILD)_$(OPTIMISE)) $(CSANFLAGS_$(CCOM_FOR_BUILD)_$(ENABLE_SANITISERS))

CSTDFLAGS_gcc=		-std=c11 -pedantic
CSTDFLAGS_clang=	-std=c11 -pedantic
CSTDFLAGS_xlc=		-qlanglvl=stdc11
CSTDFLAGS_sunstudio=	-std=c11 -pedantic=yes
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)

# Disable -Werror on generated code
CFLAGS_DISABLE_WERROR_clang=		-Wno-error
CFLAGS_DISABLE_WERROR_gcc=		-Wno-error
CFLAGS_DISABLE_WERROR_FOR_BUILD=	$(CFLAGS_DISABLE_WERROR_$(CCOM_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@

############## 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

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/pipelines.c \
	src/port/err.c \
	src/port/string.c \
	src/port/sv.c \
	src/port/util.c \
	src/pulls.c \
	src/releases.c \
	src/repos.c \
	src/sshkeys.c \
	src/status.c \
	src/url.c \
	src/waitproc.c \
	$(PDJSON_SRCS)

GCLI_SRCS= \
	src/cmd/actions.c \
	src/cmd/api.c \
	src/cmd/attachments.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/interactive.c \
	src/cmd/issues.c \
	src/cmd/labels.c \
	src/cmd/milestones.c \
	src/cmd/open.c \
	src/cmd/pipelines.c \
	src/cmd/pipelines_watch.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 \
	src/cmd/vcs.c \
	src/cmd/vcs/git.c \
	src/cmd/vcs/gitconf_parser.c \
	src/cmd/vcs/got.c \
	src/cmd/vcs/gotconf_parser.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}

.PRECIOUS: Makefile
Makefile: @SRCDIR@/configure @SRCDIR@/Makefile.in
	@echo "Reconfiguring Makefile"
	env CC="${CC}" YACC="${YACC}" LEX="${LEX}" RM="${RM}" RANLIB="${RANLIB}" \
		ENABLE_TESTS="${ENABLE_TESTS}" ENABLE_SANITISERS="${ENABLE_SANITISERS}" PERL="${PERL}" 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}" \
		$$(test -n "$(ENV_PKG_CONFIG_LIBDIR)" && echo 'PKG_CONFIG_LIBDIR="${ENV_PKG_CONFIG_LIBDIR}"') \
		PKG_CONFIG_SYSROOT_DIR="${ENV_PKG_CONFIG_SYSROOT_DIR}" \
		CCACHE="${CCACHE}" GZIP="$(GZIP)" \
		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@

TEST_OBJS=	${TEST_PROGRAMS:$(EXEEXT)=.tests$(OBJEXT)}
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} ${TEST_OBJS:$(OBJEXT)=.d}
# Include of dependencies at the very bottom

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

# lex.yy.c is generated by flex and may trigger warnings we don't control.
# Suppress -Werror for it to avoid breaking debug builds.
lex.yy.pgen$(OBJEXT_FOR_BUILD): lex.yy.c
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(CFLAGS_DISABLE_WERROR_FOR_BUILD) \
		$(CCDEPFLAGS_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) \
		-c -o lex.yy.pgen$(OBJEXT_FOR_BUILD) lex.yy.c

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) \
		$(MANPAGES) $(MANPAGES:=.gz)
	sh -c 'rm -f gcli_*.srv.log gcli_*srv.out.* gcli_*.console.log'

.PHONY: distclean
distclean: clean
	@rm -f Makefile config.h

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

TEST_PROGRAMS = \
	$(SIMPLE_TEST_PROGRAMS) \
	tests/unit/gitconf-parser$(EXEEXT) \
	tests/unit/gotconf-parser$(EXEEXT)

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

tests/unit/gotconf-parser$(EXEEXT): libgcli$(LIBEXT) tests/unit/gotconf-parser.tests$(OBJEXT) src/cmd/vcs/gotconf_parser.tests$(OBJEXT)
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC) $(CFLAGS) $(CCDEPFLAGS) $(CPPFLAGS) \
		$(LIBCURL_CFLAGS) $(LIBCRYPTO_CFLAGS) $(PDJSON_CFLAGS) \
		$(LDFLAGS) -o $@ $(@:=.tests$(OBJEXT)) src/cmd/vcs/gotconf_parser.tests$(OBJEXT) \
		libgcli$(LIBEXT) $(LIBCURL_LIBS) $(LIBCRYPTO_LIBS) $(PDJSON_LIBS) $(LIBRT_LIBS)

# extra clean hook
clean-auto: clean-src/cmd/vcs/gotconf_parser.tests$(OBJEXT)
clean-src/cmd/vcs/gotconf_parser.tests$(OBJEXT):
	rm -f src/cmd/vcs/gotconf_parser.tests$(OBJEXT)

tests/unit/gitconf-parser$(EXEEXT): libgcli$(LIBEXT) tests/unit/gitconf-parser.tests$(OBJEXT) src/cmd/vcs/gitconf_parser.tests$(OBJEXT)
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC) $(CFLAGS) $(CCDEPFLAGS) $(CPPFLAGS) \
		$(LIBCURL_CFLAGS) $(LIBCRYPTO_CFLAGS) $(PDJSON_CFLAGS) \
		$(LDFLAGS) -o $@ $(@:=.tests$(OBJEXT)) src/cmd/vcs/gitconf_parser.tests$(OBJEXT) \
		libgcli$(LIBEXT) $(LIBCURL_LIBS) $(LIBCRYPTO_LIBS) $(PDJSON_LIBS) $(LIBRT_LIBS)

# extra clean hook
clean-auto: clean-src/cmd/vcs/gitconf_parser.tests$(OBJEXT)
clean-src/cmd/vcs/gitconf_parser.tests$(OBJEXT):
	rm -f src/cmd/vcs/gitconf_parser.tests$(OBJEXT)

$(SIMPLE_TEST_PROGRAMS): libgcli$(LIBEXT)
	@mkdir -p $$(dirname $@)
	$(CCACHE) $(CC) $(CFLAGS) $(CCDEPFLAGS) $(CPPFLAGS) \
		$(LIBCURL_CFLAGS) $(LIBCRYPTO_CFLAGS) $(PDJSON_CFLAGS) \
		$(LDFLAGS) -o $@ $(@:=.tests$(OBJEXT)) libgcli$(LIBEXT) \
		$(LIBCURL_LIBS) $(LIBCRYPTO_LIBS) $(PDJSON_LIBS) $(LIBRT_LIBS)

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

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

.PHONY: check-auto-1 check-auto-0

# HACK: BSD Make exposes the number of jobs in .MAKE.JOBS, define
# it to one for makes that don't expose it (effectively unchanged
# behaviour). This makes the tests run in parallel when -j X has
# been specified on the command line
.MAKE.JOBS ?=   1
check-auto-1: $(TEST_PROGRAMS) gcli$(EXEEXT)
	$(PERL) @SRCDIR@/tests/run.pl -j $(.MAKE.JOBS)

check-auto-0 check-auto-:
	@echo "The test suite is disabled because I couldn't find Perl or the"
	@echo "TAP::Harness Module which is distributed with Perl is too old."
	@echo ""
	@echo "To enable tests, please either install a suitable Perl and make it"
	@echo "available in the PATH or alternatively set the PERL env variable"
	@echo "to a valid Perl executable. Then reconfigure the build directory."
	@echo ""
	@echo "For build requirements please check on the README.md."
	@echo ""
	@echo "HINT: If you know what you're doing you may override this"
	@echo "sanity check and invoke make with ENABLE_TESTS=1 as an"
	@echo "argument; expect the spanish inquisition. And bugs."

check-auto: check-auto-$(ENABLE_TESTS)

check: Makefile
	@$(MAKE) check-auto

#################### 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

COMP_MANPAGES=	$(MANPAGES:=.gz)

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:=.gz)

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' \
		< ${<} > $@

# Compression
.SUFFIXES: .1.gz .5.gz .1.gz-install .5.gz-install
.1.1.gz .5.5.gz:
	$(GZIP) < $< > $@

# Install targets
.PHONY: .1.gz-install .5.gz-install
.1.gz.1.gz-install:
	$(INSTALL) -d $(MANDIR)
	$(INSTALL) -d $(MANDIR)/man1
	$(INSTALL) -m 644 $< $(MANDIR)/man1

.5.gz.5.gz-install:
	$(INSTALL) -d $(MANDIR)
	$(INSTALL) -d $(MANDIR)/man5
	$(INSTALL) -m 644 $< $(MANDIR)/man5

install-auto: gcli $(COMP_MANPAGES:=-install)
	$(INSTALL) -d $(BINDIR)
	$(INSTALL) -m 0755 gcli $(BINDIR)

###############################################################
# 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 && $(MAKE) clean)
	@if find dist/.build-gcli-$(GCLI_VERSION) -type f -name '*.o' | grep '\.o'; then echo "DISTCHECK ERROR: clean target doesn't clean up the files listed above" >&2; exit 1; fi
	(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"

-include $(DEPS)
