#     Copyright (C) 1998       Thomas Roessler <roessler@guug.de>
#     Copyright (C) 1999-2020  Roland Rosenfeld <roland@spinnaker.de>
# 
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
# 
#     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, write to the Free Software Foundation,
#     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,, USA.

CC=@CC@
LDFLAGS=@LDFLAGS@
CPPFLAGS=@CPPFLAGS@
CFLAGS=@CFLAGS@ $(CPPFLAGS)
DEFS=@DEFS@
INSTALL=@INSTALL@
MODULES=@MODULES@
DOTLOCKBASE=@DOTLOCKBASE@
DOTLOCK_TARGET=@DOTLOCK_TARGET@
TACTARGET=@TACTARGET@
PERL=@PERL@
XCODEBUILD=@XCODEBUILD@
PBXBUILD=@PBXBUILD@
VFORMAT=@VFORMAT@
LIBICONV=@LIBICONV@

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
srcdir=@srcdir@
libdir=@libdir@
mandir=@mandir@
sysconfdir=@sysconfdir@

SUBDIRS=tests

ALL=lbdbq lbdb_lib lbdb-fetchaddr lbdb-munge fetchaddr qpto8bit munge \
	nodelist2lbdb palm_lsaddr mutt_ldap_query lbdbq.man \
	lbdb-fetchaddr.man nodelist2lbdb.man mutt_ldap_query.man lbdb.rc \
	$(MODULES) $(DOTLOCK_TARGET) $(TACTARGET)

all: Makefile $(ALL)

test: all test_perl
	$(MAKE) -C tests check-local

testinstalled:
	$(MAKE) -C tests installcheck-local

test_perl:
	[ "$(PERL)" = "no" ] || perl -wc nodelist2lbdb
	[ "$(PERL)" = "no" ] || perl -wc palm_lsaddr
	[ "$(PERL)" = "no" ] || perl -wc mutt_ldap_query

install: all
	$(srcdir)/mkinstalldirs $(install_prefix)$(bindir)
	$(srcdir)/mkinstalldirs $(install_prefix)$(libdir)
	$(srcdir)/mkinstalldirs $(install_prefix)$(sysconfdir)
	$(srcdir)/mkinstalldirs $(install_prefix)$(mandir)
	$(srcdir)/mkinstalldirs $(install_prefix)$(mandir)/man1
	$(INSTALL) -m 755 fetchaddr $(install_prefix)$(libdir)
	$(INSTALL) -m 755 qpto8bit $(install_prefix)$(libdir)
	$(INSTALL) -m 755 lbdbq $(install_prefix)$(bindir)
	$(INSTALL) -m 755 lbdb-fetchaddr $(install_prefix)$(bindir)
	$(INSTALL) -m 755 lbdb-munge $(install_prefix)$(libdir)
	$(INSTALL) -m 755 lbdb_lib $(install_prefix)$(libdir)
	$(INSTALL) -m 755 munge $(install_prefix)$(libdir)
	$(INSTALL) -m 644 lbdb_bbdb_query.el $(install_prefix)$(libdir)
	if [ "$(PERL)" != "no" ]; then \
		$(INSTALL) -m 755 nodelist2lbdb $(install_prefix)$(bindir); \
		$(INSTALL) -m 644 nodelist2lbdb.man \
			$(install_prefix)$(mandir)/man1/nodelist2lbdb.1; \
		$(INSTALL) -m 755 palm_lsaddr $(install_prefix)$(libdir); \
		$(INSTALL) -m 755 mutt_ldap_query $(install_prefix)$(bindir); \
		$(INSTALL) -m 644 mutt_ldap_query.man \
			$(install_prefix)$(mandir)/man1/mutt_ldap_query.1; \
		$(INSTALL) -m 644 lbdb_ldap.rc \
			$(install_prefix)$(sysconfdir)/lbdb_ldap.rc; \
	fi
	if [ "$(XCODEBUILD)" != "no" -o "$(PBXBUILD)" != "no" ]; then \
		$(INSTALL) -m 755 ABQuery $(install_prefix)$(libdir); \
	fi
	if [ "$(VFORMAT)" != "no" ]; then \
		$(INSTALL) -m 755 vcquery $(install_prefix)$(libdir); \
	fi
	for i in $(MODULES); do \
		$(INSTALL) -m 755 $$i $(install_prefix)$(libdir); \
	done
	$(INSTALL) -m 644 lbdbq.man $(install_prefix)$(mandir)/man1/lbdbq.1
	$(INSTALL) -m 644 lbdb-fetchaddr.man \
		$(install_prefix)$(mandir)/man1/lbdb-fetchaddr.1
	$(INSTALL) -m 644 lbdb.rc $(install_prefix)$(sysconfdir)/lbdb.rc
	if [ "$(DOTLOCK_TARGET)" != "" ]; then \
		$(INSTALL) -m 755 lbdb_dotlock $(install_prefix)$(bindir); \
		$(INSTALL) -m 644 dotlock.man \
			$(install_prefix)$(mandir)/man1/lbdb_dotlock.1; \
	fi
	if [ "$(TACTARGET)" != "" ]; then \
		$(INSTALL) -m 755 tac $(install_prefix)$(libdir)/tac; \
	fi

fetchaddr: $(srcdir)/fetchaddr.o $(srcdir)/rfc822.o $(srcdir)/helpers.o  \
	$(srcdir)/rfc2047.o
	$(CC) $(CFLAGS) $(LDFLAGS) $? -o $@ $(LIBICONV)

qpto8bit: $(srcdir)/qpto8bit.o $(srcdir)/rfc822.o $(srcdir)/helpers.o  \
		$(srcdir)/rfc2047.o
	$(CC) $(CFLAGS) $(LDFLAGS) $? -o $@ $(LIBICONV)

fetch: $(srcdir)/fetch.sh

query: $(srcdir)/query.sh

lbdb-query: $(srcdir)/lbdb-query.sh

nodelist2lbdb: $(srcdir)/nodelist2lbdb.pl
	cat nodelist2lbdb.pl > $@

palm_lsaddr: $(srcdir)/palm_lsaddr.pl
	cat palm_lsaddr.pl > $@

mutt_ldap_query: $(srcdir)/mutt_ldap_query.pl
	cat mutt_ldap_query.pl > $@

lbdb_dotlock: $(srcdir)/dotlock.o $(srcdir)/helpers.o
	$(CC) $(CFLAGS) $(LDFLAGS) $? -o $@

dotlock.o: $(srcdir)/dotlock.h $(srcdir)/dotlock.c

tac: $(srcdir)/tac.awk
	cat tac.awk > $@
	chmod a+x $@

munge: $(srcdir)/munge.awk
	cat munge.awk > $@
	chmod a+x $@

mutt_ldap_query.man: $(srcdir)/mutt_ldap_query.pl
	if [ "$(PERL)" != "no" ]; then \
		pod2man mutt_ldap_query.pl > $@; \
	else \
		touch mutt_ldap_query.man; \
	fi

m_vcf: vcquery

vcquery: vcquery.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $? -lvformat

m_osx_addressbook: ABQuery

ABQuery: ABQuery.m
	$(CC) $(CFLAGS) $(DEFS) -framework Foundation -framework AddressBook -o $@ $^

update-version:
	-rm -f configure tests/package.m4 tests/testsuite
	autoconf
	./configure
	$(MAKE) test
	$(MAKE) distclean

clean:
	-rm -f *.o *~ $(ALL) munge.awk tac.awk ABQuery \
		nodelist2lbdb.pl palm_lsaddr.pl mutt_ldap_query.pl \
		lbdb_dotlock lbdb.spec lbdb-pkg.el
	-rm -rf ABQuery.dSYM autom4te.cache
	$(MAKE) -C tests clean

distclean: clean
	-rm -f config.status config.cache config.log Makefile
	-rm -f *.sh
	-rm -f vcquery
	$(MAKE) -C tests distclean

Makefile: Makefile.in
	./config.status

%.sh: %.sh.in
	./config.status

%.pl: %.pl.in
	./config.status

%.awk: %.awk.in
	./config.status

%.man: %.man.in
	sed -e 's!@''libdir@!$(libdir)!' \
	    -e 's!@''sysconfdir@!$(sysconfdir)!' \
	    -e 's!@''dotlock@!$(DOTLOCKBASE)!' $@.in > $@

%.o: %.c
	$(CC) $(CFLAGS) $(DEFS) -c $<

.PHONY: test test_perl update-version
