CC=gcc

## The tray icon needs Ayatana AppIndicator3 (StatusNotifierItem). Fail early
## with a useful message instead of a wall of "unknown type name" errors.
PKGS := gtk+-3.0 ayatana-appindicator3-0.1
ifneq ($(shell pkg-config --exists $(PKGS) && echo ok),ok)
$(error missing development packages, need: $(PKGS) \
  (Debian: libgtk-3-dev libayatana-appindicator3-dev, \
   Fedora: gtk3-devel libayatana-appindicator-gtk3-devel, \
   Arch: gtk3 libayatana-appindicator))
endif

CFLAGS := $(shell pkg-config --cflags $(PKGS))
LIBS   := $(shell pkg-config --libs $(PKGS))
BUILDFLAGS = -std=c99 -O3 -Wall -Werror

## Build time customization, both are include files in src/ and both are set
## per target below, so a value from the environment cannot leak into another
## build. Passing them on the command line still works, for example:
##   make ministart BUTTONS_FILE=some-buttons.c
##
## BUTTONS_FILE     : the buttons compiled into the binary
## CONFIG_LOGIC_FILE: the build specific configuration logic, see main.c
BUTTONS_SRC      = src/$(if $(BUTTONS_FILE),$(BUTTONS_FILE),default-buttons.c)
CONFIG_LOGIC_SRC = src/$(if $(CONFIG_LOGIC_FILE),$(CONFIG_LOGIC_FILE),default-config-logic.c)

BUILD_DEFS = $(if $(BUTTONS_FILE),-DBUTTONS_FILE='"$(BUTTONS_FILE)"') \
	$(if $(CONFIG_LOGIC_FILE),-DCONFIG_LOGIC_FILE='"$(CONFIG_LOGIC_FILE)"')

## "make install" destinations. TARGET picks which binary gets installed
## ("ministart" or "gnupg/ministart"), the *DIR variables where it and its
## icons/config go. Override on the command line, for example:
##   make TARGET=gnupg/ministart BINDIR=$(APPDIR)/usr/bin \
##        ICONSDIR=$(APPDIR)/usr/share/icons CONFIGDIR=$(APPDIR)/usr/etc install
TARGET    ?= ministart
PREFIX    ?= /usr/local
BINDIR    ?= $(DESTDIR)$(PREFIX)/bin
ICONSDIR  ?= $(DESTDIR)$(PREFIX)/share/icons
CONFIGDIR ?= $(DESTDIR)$(PREFIX)/etc

.PHONY: all clean archive gnupg appdir appimage test-appimage-help help install vars

.DEFAULT_GOAL := help

help:
	@echo "Available targets:"
	@echo "  help                show this help message"
	@echo "  ministart           build the standard Linux binary"
	@echo "  gnupg               build the GnuPG binary (shorthand for gnupg/ministart)"
	@echo "  all                 build both ministart and gnupg"
	@echo "  test-appimage       build+run gnupg/ministart with APPDIR and VERSION file"
	@echo "  test-appimage-help  build+run gnupg/ministart and show --help"
	@echo "  archive             create a source tarball via git archive"
	@echo "  clean               remove build artifacts"
	@echo "  install             install binary, icons and config."
	@echo ""
	@echo "Example Installation:"
	@echo "      make DESTDIR=/usr/local/ install"
	@echo "  or"
	@echo "      make TARGET=gnupg/ministart BINDIR=/usr/local/bin \\"
	@echo "           ICONSDIR=/usr/local/share/icons \\"
	@echo "           CONFIGDIR=/usr/local/etc  install"

## -- build targets --

## "ministart" is the standard Linux build, "gnupg/ministart" the build for
## the GnuPG AppImages. One binary serves all GnuPG flavors: VS-Desktop,
## Desktop and Gpg4Linux are told apart at runtime, see src/gnupg-config.c.

ministart: BUTTONS_FILE      =
ministart: CONFIG_LOGIC_FILE =

gnupg/ministart: BUTTONS_FILE      = gpd-buttons.c
gnupg/ministart: CONFIG_LOGIC_FILE = gnupg-config.c

src/version.h: VERSION
	echo "#define VERSION \"`cat $<`\"" > $@

## The include files are prerequisites too, so a change to them rebuilds.
## They depend on the target specific variables above, hence the second
## expansion.
.SECONDEXPANSION:
ministart gnupg/ministart: src/main.c src/version.h $$(BUTTONS_SRC) $$(CONFIG_LOGIC_SRC) vars
	@mkdir -p $(@D)
	$(CC) $(BUILDFLAGS) -o $@ src/main.c $(CFLAGS) $(LIBS) $(BUILD_DEFS)

## Shorthand, so "make gnupg" works instead of "make gnupg/ministart".
gnupg: gnupg/ministart
	@:

all: ministart gnupg

## --- helper targets --

vars:
	@echo "** Build Variables **"
	@echo "CFLAGS           : $(CFLAGS)"
	@echo "LIBS             : $(LIBS)"
	@echo "BUILDFLAGS       : $(BUILDFLAGS)"
	@echo "BUTTONS_FILE     : $(BUTTONS_FILE)"
	@echo "CONFIG_LOGIC_FILE: $(CONFIG_LOGIC_FILE)"
	@echo "TARGET           : $(TARGET)"
	@echo "PREFIX           : $(PREFIX)"
	@echo "BINDIR           : $(BINDIR)"
	@echo "ICONSDIR         : $(ICONSDIR)"
	@echo "CONFIGDIR        : $(CONFIGDIR)"
	@echo "TARGET           : $(TARGET)"

test-appimage: gnupg/ministart
	@echo "** with APPDIR=/tmp/ministart-appdir and GnuPG-VS-Desktop-VERSION file"
	touch /tmp/ministart-appdir/GnuPG-VS-Desktop-VERSION
	mkdir -p /tmp/ministart-appdir
	APPDIR=/tmp/ministart-appdir ./$< -v -v --verbose

test-appimage-help: gnupg/ministart
	@echo "** with APPDIR=/tmp/ministart-appdir"
	mkdir -p /tmp/ministart-appdir
	APPDIR=/tmp/ministart-appdir ./$< --help
	@echo "** with GnuPG-VS-Desktop-VERSION file:"
	touch /tmp/ministart-appdir/GnuPG-VS-Desktop-VERSION
	APPDIR=/tmp/ministart-appdir ./$< --help
	@echo "** without APPDIR **"
	./$< --help
#	APPDIR=/tmp/ministart-appdir ./$< --help | \
#	grep -E 'Current icons path:[[:space:]]+/tmp/ministart-appdir/usr/share/icons'

archive: all
	git archive --format=tar.gz -o ../ministart-$(shell cat VERSION).tar.gz --prefix=ministart/ HEAD

clean:
	rm -f ministart src/version.h
	rm -rf gnupg

install: $(TARGET)
	mkdir -p $(BINDIR) $(ICONSDIR) $(CONFIGDIR)
	cp $(TARGET) $(BINDIR)/ministart
	cp -r config_template/icons/* $(ICONSDIR)/
	cp config_template/ministart.config_template $(CONFIGDIR)/ministart.config

# EOF
