# Copyright (C) 2011-2012,2018 G.P. Halkes
# 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/>.
.POSIX:

# Installation prefix (also defined in mk/libt3key)
prefix=/usr/local

# Install program to use (should be BSD install compatible)
INSTALL=install

# The libtool executable
LIBTOOL=libtool

# Miscelaneous install paths. Note that datadir and LOCALEDIR are also defined
# in mk/libt3key
bindir=$(prefix)/bin
libdir=$(prefix)/lib
datadir=$(prefix)/share
docdir=$(prefix)/share/doc/libt3key
mandir=$(prefix)/share/man
includedir=$(prefix)/include
pkgconfigdir=$(libdir)/pkgconfig
LOCALEDIR=$(prefix)/share/locale

all: lib t3keyc t3learnkeys

lib:
	@$(MAKE) -f mk/libt3key

t3keyc: lib
	@$(MAKE) -f mk/t3keyc

t3learnkeys: lib
	@$(MAKE) -f mk/t3learnkeys

.PHONY: all clean dist-clean distclean install install-moddev lib t3keyc t3learnkeys uninstall
.IGNORE: uninstall

clean:
	@$(MAKE) -f mk/libt3key clean
	@$(MAKE) -f mk/t3keyc clean
	@$(MAKE) -f mk/t3learnkeys clean

dist-clean:
	@$(MAKE) -f mk/libt3key dist-clean
	@$(MAKE) -f mk/t3keyc dist-clean
	@$(MAKE) -f mk/t3learnkeys dist-clean
	rm -rf Makefile mk/libt3key mk/t3keyc mk/t3learnkeys config.log libt3key.pc .Makefile* .config*

distclean: dist-clean

# Macros to make DESTDIR support more readable
_bindir=$(DESTDIR)$(bindir)
_libdir=$(DESTDIR)$(libdir)
_datadir=$(DESTDIR)$(datadir)
_docdir=$(DESTDIR)$(docdir)
_mandir=$(DESTDIR)$(mandir)
_includedir=$(DESTDIR)$(includedir)
_pkgconfigdir=$(DESTDIR)$(pkgconfigdir)
_LOCALEDIR=$(DESTDIR)$(LOCALEDIR)

install: all
	$(INSTALL) -d $(_libdir)
	$(LIBTOOL) --mode=install $(INSTALL) -s -m0644 src/libt3key.la $(_libdir)
	chmod 0644 $(_libdir)/libt3key.la
	$(INSTALL) -d $(_includedir)/t3key
	$(INSTALL) -m0644 src/key.h src/key_api.h src/key_errors.h $(_includedir)/t3key
	$(INSTALL) -d $(_docdir)
	$(INSTALL) -m0644 COPYING README Changelog doc/format.txt doc/format.html doc/supplemental.kmap $(_docdir)
	$(INSTALL) -d $(_pkgconfigdir)
	$(INSTALL) -m0644 libt3key.pc $(_pkgconfigdir)
	$(INSTALL) -d $(_bindir)
	$(INSTALL) -s src.util/t3keyc/t3keyc $(_bindir)
	$(INSTALL) -d $(_datadir)/libt3key1
	find src/database -type f | while read FILE ; do install -m0644 "$$FILE" $(_datadir)/libt3key1 ; \
		$(_bindir)/t3keyc -l  $(_datadir)/libt3key1/"$${FILE##*/}" ; done
	$(INSTALL) -d $(_mandir)/man1
	$(INSTALL) -m0644 man/t3keyc.1 $(_mandir)/man1
	if [ -f src.util/t3learnkeys/t3learnkeys ] ; then $(INSTALL) -s src.util/t3learnkeys/t3learnkeys $(_bindir) ; \
		$(INSTALL) -m0644 man/t3learnkeys.1 $(_mandir)/man1 ; fi

uninstall:
	$(LIBTOOL) --mode=uninstall rm $(_libdir)/libt3key.la
	rm -rf $(_docdir) $(_datadir)
	rm -f $(_bindir)/t3keyc $(_bindir)/t3learnkeys $(_pkgconfigdir)/libt3key.pc
	rm -rf $(_includedir)/t3key
	rm -f $(_mandir)/man1/t3keyc.1 $(_mandir)/man1/t3learnkeys.1

# LIBVERSION=1
