# Copyright (C) 2011-2013 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/libtranscript)
prefix=/usr/local

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

# The libtool executable
LIBTOOL=libtool

# Miscelaneous install paths
bindir=$(prefix)/bin
libdir=$(prefix)/lib
docdir=$(prefix)/share/doc/libtranscript
mandir=$(prefix)/share/man
includedir=$(prefix)/include
pkgconfigdir=$(libdir)/pkgconfig

all: lib linkltc ucm2ltc

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

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

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

.PHONY: all clean dist-clean distclean install install-moddev lib linkltc ucm2ltc uninstall
.IGNORE: uninstall

clean:
	@$(MAKE) -f mk/libtranscript clean
	@$(MAKE) -f mk/linkltc clean
	@$(MAKE) -f mk/ucm2ltc clean

dist-clean:
	@$(MAKE) -f mk/libtranscript dist-clean
	@$(MAKE) -f mk/linkltc dist-clean
	@$(MAKE) -f mk/ucm2ltc dist-clean
	rm -rf Makefile mk/libtranscript mk/linkltc mk/ucm2ltc config.log libtranscript.pc .Makefile* .config*

distclean: dist-clean

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

install: all
	$(INSTALL) -d $(_libdir)/transcript1
	$(LIBTOOL) --mode=install $(INSTALL) -s -m0644 src/libtranscript.la $(_libdir)
	chmod 0644 $(_libdir)/libtranscript.la
	export STRIPOPT=-s; $(INSTALL) $${STRIPOPT} -m0644 src/modules/.libs/ascii.ltc $(_libdir)/transcript1 || export STRIPOPT= ; \
		$(INSTALL) $${STRIPOPT} -m0644 src/tables/.libs/*.ltc src/modules/.libs/*.ltc $(_libdir)/transcript1
	$(INSTALL) -m0644 src/aliases.txt $(_libdir)/transcript1
	$(INSTALL) -d $(_includedir)/transcript
	$(INSTALL) -m0644 src/transcript.h src/api.h src/moduledefs.h src/utf.h src/handle.h src/bool.h $(_includedir)/transcript
	$(INSTALL) -d $(_docdir)
	$(INSTALL) -m0644 COPYING README Changelog doc/motivation.txt $(_docdir)
	$(INSTALL) -d $(_pkgconfigdir)
	$(INSTALL) -m0644 libtranscript.pc $(_pkgconfigdir)
	./install_links.sh $(_libdir)/transcript1

install-moddev: install
	$(INSTALL) -m0644 src/moduledefs.h src/api.h src/bool.h src/handle.h src/utf.h $(_includedir)/transcript
	$(INSTALL) -d $(_bindir)
	$(INSTALL) -s src.util/linkltc/linkltc $(_bindir)
	$(INSTALL) -d $(_mandir)/man1
	$(INSTALL) -m0644 man/linkltc.1 $(_mandir)/man1
	if [ -f src.util/ucm2ltc/ucm2ltc ] ; then $(INSTALL) -s src.util/ucm2ltc/ucm2ltc $(_bindir) ; $(INSTALL) -m0644 man/ucm2ltc.1 $(_mandir)/man1 ; fi

uninstall:
	$(LIBTOOL) --mode=uninstall rm $(_libdir)/libtranscript.la
	rm -rf $(_libdir)/transcript
	rm -rf $(_docdir)
	rm -f $(_bindir)/linkltc $(_bindir)/ucm2ltc $(_pkgconfigdir)/libtranscript.pc
	rm -rf $(_includedir)/transcript
	rm -f $(_mandir)/man1/linkltc.1 $(_mandir)/man1/ucm2ltc.1

# LIBVERSION=1
