# Topal: GPG/GnuPG and Alpine/Pine integration
# Copyright (C) 2001--2022  Phillip J. Brooke
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

RELEASECODE := $(shell grep '^<dt>' Changelog.html | tail -1 | sed 's/.* release //; sx</a>.*$$xx')
ifdef SOURCE_DATE_EPOCH
  BUILDDATE := $(shell date '+%Y-%m-%dT%H%M%S%Z' -d @$(SOURCE_DATE_EPOCH))
else
  # Force a rebuild each time this Makefile is run.
  .PHONY: version_id.adb versionid.tex
  BUILDDATE := $(shell date '+%Y-%m-%dT%H%M%S%Z')
endif

# If you intend to redistribute this package, you will obviously need
# to substitute your own key id here.
MYKEY := 0x47DC67A1

include Buildflags.mk

generated_ada_sources := version_id.adb help.ads help.adb
GENFILES := $(generated_ada_sources) b~topal.ads b~topal.adb
CFILES := $(wildcard *.c)
c_objects := $(CFILES:c=o)
ADAFILES := $(filter-out $(GENFILES),$(wildcard *.ad[bs]))
patches := $(wildcard pine-*.patch alpine-*.patch alpine-*.patch-*)
archive     := topal-package-$(RELEASECODE).tgz
archive_asc := topal-package-$(RELEASECODE).tgz.asc
archive_dir := topal-$(RELEASECODE)
archive_components_644 := \
  $(patches) \
  $(CFILES) \
  $(ADAFILES) \
  Buildflags.mk \
  COPYING \
  Changelog.html \
  Features.html \
  MIME-tool/GPL.txt \
  MIME-tool/Makefile \
  MIME-tool/README.Topal.txt \
  MIME-tool/README.txt \
  MIME-tool/mime-tool.man \
  MIME-tool/mime.c \
  Makefile \
  cygwin.patch \
  help.txt \
  topal.man \
  topal.pdf \
  topal.tex
archive_components_755 := \
  screens \
  mkdistrib \
  mkhelp \
  mkversionid \
  mkversionidtex

DESTDIR ?=
prefix ?= /usr
INSTALLPATH ?= $(DESTDIR)$(prefix)
INSTALLPATHBIN ?= $(INSTALLPATH)/bin
INSTALLPATHMAN ?= $(INSTALLPATH)/share/man
INSTALLPATHDOC ?= $(INSTALLPATH)/share/doc/topal
INSTALLPATHPATCHES ?= $(INSTALLPATH)/share/topal/patches

# Set this to "true" to keep debugging symbols.
strip ?= strip


.PHONY: all binary all2

# The default action.
all:	binary topal.pdf

binary: topal mime-tool

all2:	package distrib

$(c_objects): %.o: %.c
	$(CC) -c $(default_CFLAGS) $(CFLAGS) $(CPPFLAGS) $< -o $@

mime-tool:	
	$(MAKE) -C MIME-tool

# Always run gnatmake, it knows better than Make in which order Ada
# units must be recompiled.
.PHONY: topal
topal: $(c_objects) $(generated_ada_sources)
	gnatmake -j4 $(gnatmake_options) topal.adb \
	  -cargs $(default_ADAFLAGS) $(ADAFLAGS) \
	  -largs $(default_LDFLAGS) $(LDFLAGS) \

version_id.adb:	mkversionid
	./mkversionid $(RELEASECODE) $(BUILDDATE)

versionid.tex:	mkversionidtex
	./mkversionidtex $(RELEASECODE) $(BUILDDATE)

# We want this to be run only once.
# Using the hint at https://www.cmcrossroads.com/article/rules-multiple-outputs-gnu-make
%.ads %.adb: %.txt mkhelp
	./mkhelp

.PHONY: doc
doc:	topal.pdf

topal.pdf:	topal.tex versionid.tex
	pdflatex $<
	pdflatex $<

.PHONY: install
install:	all
	install -d $(INSTALLPATHBIN) $(INSTALLPATHDOC) $(INSTALLPATHMAN)/man1 $(INSTALLPATHPATCHES)
	install -m 755 -s --strip-program=$(strip) topal $(INSTALLPATHBIN)
	install -m 644 Features.html Changelog.html topal.pdf COPYING $(INSTALLPATHDOC)
	install -m 644 topal.man $(INSTALLPATHMAN)/man1/topal.1
	install -m 644 $(patches) $(INSTALLPATHPATCHES)
	install -m 755 -s --strip-program=$(strip) MIME-tool/mime-tool $(INSTALLPATHBIN)
	install -m 644 MIME-tool/mime-tool.man $(INSTALLPATHMAN)/man1/mime-tool.1

.PHONY: clean
clean:
	$(RM) *.o *.ali $(GENFILES) versionid.tex
	# Clean LaTeX stuff.
	$(RM) $(addprefix topal.,toc out log aux)
	# Clean packaging directories.
	-$(RM) -r topal-[0-9]*
	$(MAKE) -C MIME-tool $@

.PHONY: distclean realclean
distclean realclean:	clean
	-$(RM) topal README.txt *~ $(archive) $(archive_asc) topal.pdf
	-$(RM) -r www www.bak
	$(MAKE) -C MIME-tool $@


.PHONY:	package tarball fixperms

package:	$(archive) $(archive_asc)

tarball:	$(archive)

# fixperms: for manual fix before a rebuild
fixperms:	
	-chmod 644 $(archive_components_644)
	-chmod 755 $(archive_components_755)

$(archive):	$(archive_components_644) $(archive_components_755)
	chmod 644 $(archive_components_644)
	chmod 755 $(archive_components_755)
	tar cvaf $@ --transform='s|^|$(archive_dir)/|' $^

$(archive_asc):	$(archive)
	-gpg --detach-sign --armor --local-user=$(MYKEY) -o $@ $<

.PHONY: distrib
distrib:	 $(archive) $(archive_asc) mkdistrib
	-$(RM) -r www
	mkdir www
	cp $(archive) $(archive_asc) topal.pdf Changelog.html COPYING www
	cd www && ../mkdistrib $(RELEASECODE)
