#
# @configure_input@
#
# Copyright (c) 2013, Verisign, Inc., NLNet Labs
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# * Neither the names of the copyright holders nor the
#   names of its contributors may be used to endorse or promote products
#   derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package = @PACKAGE_NAME@
version = @PACKAGE_VERSION@
tarname = @PACKAGE_TARNAME@
distdir = $(tarname)-$(version)

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
INSTALL = @INSTALL@
LIBTOOL = ../../libtool

srcdir = @srcdir@

have_libevent = @have_libevent@
have_libuv = @have_libuv@
have_libev = @have_libev@
NOLIBCHECK = @NOLIBCHECK@

EXTENSION_LIBEVENT_EXT_LIBS=@EXTENSION_LIBEVENT_EXT_LIBS@
EXTENSION_LIBEVENT_LDFLAGS=@EXTENSION_LIBEVENT_LDFLAGS@
EXTENSION_LIBUV_EXT_LIBS=@EXTENSION_LIBUV_EXT_LIBS@
EXTENSION_LIBUV_LDFLAGS=@EXTENSION_LIBUV_LDFLAGS@
EXTENSION_LIBEV_EXT_LIBS=@EXTENSION_LIBEV_EXT_LIBS@
EXTENSION_LIBEV_LDFLAGS=@EXTENSION_LIBEV_LDFLAGS@

CHECK_GETDNS=@CHECK_GETDNS@
CHECK_UV_PROG=@CHECK_UV_PROG@
CHECK_EVENT_PROG=@CHECK_EVENT_PROG@
CHECK_EV_PROG=@CHECK_EV_PROG@

CC=@CC@
CFLAGS=-I$(srcdir)/.. -I$(srcdir) -I.. $(cflags) @CFLAGS@ 
LDFLAGS=-L.. @LDFLAGS@
LDLIBS=../libgetdns.la @LIBS@
CHECK_LIBS=@CHECK_LIBS@
CHECK_CFLAGS=@CHECK_CFLAGS@

CHECK_OBJS=check_getdns_common.lo check_getdns_context_set_timeout.lo \
	check_getdns.lo check_getdns_transport.lo

ALL_OBJS=$(CHECK_OBJS) check_getdns_libevent.lo check_getdns_libev.lo \
	check_getdns_selectloop.lo getdns_query.lo testmessages.lo \
	tests_dict.lo tests_list.lo tests_namespaces.lo \
	tests_stub_async.lo tests_stub_sync.lo

NON_C99_OBJS=check_getdns_libuv.lo

PROGRAMS=tests_dict tests_list tests_namespaces tests_stub_async tests_stub_sync getdns_query $(CHECK_GETDNS) $(CHECK_EV_PROG) $(CHECK_EVENT_PROG) $(CHECK_UV_PROG)


.SUFFIXES: .c .o .a .lo .h

.c.o:
	$(CC) $(CFLAGS) -c $< -o $@

.c.lo:
	$(LIBTOOL) --quiet --tag=CC --mode=compile $(CC) $(CFLAGS) -c $< -o $@

default: all

all: $(PROGRAMS)

$(ALL_OBJS):
	$(LIBTOOL) --quiet --tag=CC --mode=compile $(CC) $(CFLAGS) -c $(srcdir)/$(@:.lo=.c) -o $@

$(NON_C99_OBJS):
	$(LIBTOOL) --quiet --tag=CC --mode=compile $(CC) $(CFLAGS) -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -c $(srcdir)/$(@:.lo=.c) -o $@

tests_dict: tests_dict.lo testmessages.lo
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_dict.lo testmessages.lo

tests_list: tests_list.lo testmessages.lo
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_list.lo testmessages.lo

tests_namespaces: tests_namespaces.lo 
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_namespaces.lo 

tests_stub_async: tests_stub_async.lo testmessages.lo
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_async.lo testmessages.lo

tests_stub_sync: tests_stub_sync.lo
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_sync.lo

check_getdns_common: check_getdns_common.lo
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ check_getdns_common.lo

check_getdns: check_getdns.lo check_getdns_common.lo check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_selectloop.lo
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(CHECK_CFLAGS) $(CHECK_LIBS) -o $@ check_getdns.lo check_getdns_common.lo  check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_selectloop.lo

check_getdns_event: check_getdns.lo check_getdns_common.lo check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_libevent.lo ../libgetdns_ext_event.la
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) -o $@ check_getdns.lo check_getdns_common.lo check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_libevent.lo $(LDFLAGS) $(LDLIBS) $(CHECK_CFLAGS) $(CHECK_LIBS) ../libgetdns_ext_event.la $(EXTENSION_LIBEVENT_LDFLAGS) $(EXTENSION_LIBEVENT_EXT_LIBS)

check_getdns_uv: check_getdns.lo check_getdns_common.lo check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_libuv.lo ../libgetdns_ext_uv.la
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) -o $@ check_getdns.lo check_getdns_common.lo check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_libuv.lo $(LDFLAGS) $(LDLIBS) $(CHECK_CFLAGS) $(CHECK_LIBS) ../libgetdns_ext_uv.la $(EXTENSION_LIBUV_LDFLAGS) $(EXTENSION_LIBUV_EXT_LIBS)

check_getdns_ev: check_getdns.lo check_getdns_common.lo check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_libev.lo ../libgetdns_ext_ev.la
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) -o $@ check_getdns.lo check_getdns_common.lo check_getdns_context_set_timeout.lo check_getdns_transport.lo check_getdns_libev.lo $(LDFLAGS) $(LDLIBS) $(CHECK_CFLAGS) $(CHECK_LIBS) ../libgetdns_ext_ev.la $(EXTENSION_LIBEV_LDFLAGS) $(EXTENSION_LIBEV_EXT_LIBS)

getdns_query: getdns_query.lo
	$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) -o $@ getdns_query.lo $(LDFLAGS) $(LDLIBS)

install: getdns_query
	$(INSTALL) -m 755 -d $(DESTDIR)$(bindir)
	$(LIBTOOL) --mode=install cp getdns_query $(DESTDIR)$(bindir)

nolibcheck:
	@echo "***"
	@echo "*** Cannot run unit tests, because they could not be compiled,"
	@echo "*** because libcheck was not found or usable at configure time."
	@echo "*** To compile and run unit tests make sure libcheck is available"
	@echo "*** and usable during configuration"
	@echo "***"
	@false

test:	$(NOLIBCHECK) all
	./$(CHECK_GETDNS)
	if test $(have_libevent) = 1 ; then ./$(CHECK_EVENT_PROG) ; fi
	if test $(have_libev) = 1 ; then ./$(CHECK_EV_PROG) ; fi
	if test $(have_libuv) = 1 ; then ./$(CHECK_UV_PROG) ; fi
	@echo "All tests OK"

clean:
	rm -f *.o *.lo $(PROGRAMS)
	rm -rf .libs
	rm -f check_getdns.log

distclean : clean
	rm -f Makefile config.status config.log
	rm -Rf autom4te.cache

$(distdir): FORCE
	mkdir -p $(distdir)/src
	cp configure.ac $(distdir)
	cp configure $(distdir)
	cp Makefile.in $(distdir)
	cp src/Makefile.in $(distdir)/src

distcheck: $(distdir).tar.gz
	gzip -cd $(distdir).tar.gz | tar xvf -
	cd $(distdir) && ./configure
	cd $(distdir) && $(MAKE) all
	cd $(distdir) && $(MAKE) check
	cd $(distdir) && $(MAKE) DESTDIR=$${PWD}/_inst install
	cd $(distdir) && $(MAKE) DESTDIR=$${PWD}/_inst uninstall
	@remaining="`find $${PWD}/$(distdir)/_inst -type f | wc -l`"; \
	if test "$${remaining}" -ne 0; then
	  echo "@@@ $${remaining} file(s) remaining in stage directory!"; \
	  exit 1; \
	fi
	cd $(distdir) && $(MAKE) clean
	rm -rf $(distdir)
	@echo "*** Package $(distdir).tar.gz is ready for distribution"

Makefile: $(srcdir)/Makefile.in ../../config.status
	cd ../.. && ./config.status src/test/Makefile

configure.status: configure
	cd ../.. && ./config.status --recheck

depend:
	(cd $(srcdir) ; awk 'BEGIN{P=1}{if(P)print}/^# Dependencies/{P=0}' Makefile.in > Makefile.in.new )
	(cd $(srcdir) ; gcc -MM -I. -I.. *.c | \
		sed -e 's? \([a-z_-]*\)\.\([ch]\)? $$(srcdir)/\1.\2?g' \
		    -e 's? \$$(srcdir)/config\.h? ../config.h?g' \
		    -e 's? \.\./getdns/getdns_extra\.h? $$(srcdir)/../getdns/getdns_extra.h?g' \
		    -e 's? \.\./getdns/getdns_ext_libevent\.h? $$(srcdir)/../getdns/getdns_ext_libevent.h?g' \
		    -e 's? \.\./getdns/getdns_ext_libev\.h? $$(srcdir)/../getdns/getdns_ext_libev.h?g' \
		    -e 's? \.\./getdns/getdns_ext_libuv\.h? $$(srcdir)/../getdns/getdns_ext_libuv.h?g' \
		    -e 's!\(.*\)\.o[ :]*!\1.lo \1.o: !g' >> Makefile.in.new )
	(cd $(srcdir) ; diff Makefile.in.new Makefile.in && rm Makefile.in.new \
	                                                 || mv Makefile.in.new Makefile.in )



.PHONY: clean test

# Dependencies for the unit tests
check_getdns.lo check_getdns.o: $(srcdir)/check_getdns.c ../getdns/getdns.h $(srcdir)/check_getdns_common.h \
 $(srcdir)/check_getdns_general.h $(srcdir)/check_getdns_general_sync.h \
 $(srcdir)/check_getdns_address.h $(srcdir)/check_getdns_address_sync.h \
 $(srcdir)/check_getdns_hostname.h $(srcdir)/check_getdns_hostname_sync.h \
 $(srcdir)/check_getdns_context_create.h $(srcdir)/check_getdns_context_destroy.h \
 $(srcdir)/check_getdns_cancel_callback.h $(srcdir)/check_getdns_list_get_length.h \
 $(srcdir)/check_getdns_list_get_data_type.h $(srcdir)/check_getdns_list_get_dict.h \
 $(srcdir)/check_getdns_list_get_list.h $(srcdir)/check_getdns_list_get_int.h \
 $(srcdir)/check_getdns_list_get_bindata.h $(srcdir)/check_getdns_dict_get_names.h \
 $(srcdir)/check_getdns_dict_get_data_type.h $(srcdir)/check_getdns_dict_get_dict.h \
 $(srcdir)/check_getdns_dict_get_list.h $(srcdir)/check_getdns_dict_get_bindata.h \
 $(srcdir)/check_getdns_dict_get_int.h $(srcdir)/check_getdns_dict_destroy.h \
 $(srcdir)/check_getdns_dict_set_dict.h $(srcdir)/check_getdns_dict_set_list.h \
 $(srcdir)/check_getdns_dict_set_bindata.h $(srcdir)/check_getdns_dict_set_int.h \
 $(srcdir)/check_getdns_convert_ulabel_to_alabel.h \
 $(srcdir)/check_getdns_convert_alabel_to_ulabel.h $(srcdir)/check_getdns_pretty_print_dict.h \
 $(srcdir)/check_getdns_display_ip_address.h \
 $(srcdir)/check_getdns_context_set_context_update_callback.h \
 $(srcdir)/check_getdns_context_set_timeout.h \
 $(srcdir)/check_getdns_context_set_upstream_recursive_servers.h \
 $(srcdir)/check_getdns_service.h $(srcdir)/check_getdns_service_sync.h \
 $(srcdir)/check_getdns_transport.h
check_getdns_common.lo check_getdns_common.o: $(srcdir)/check_getdns_common.c ../getdns/getdns.h \
 ../config.h $(srcdir)/check_getdns_common.h $(srcdir)/check_getdns_eventloop.h
check_getdns_context_set_timeout.lo check_getdns_context_set_timeout.o: $(srcdir)/check_getdns_context_set_timeout.c \
 $(srcdir)/check_getdns_context_set_timeout.h $(srcdir)/check_getdns_common.h \
 ../getdns/getdns.h
check_getdns_libev.lo check_getdns_libev.o: $(srcdir)/check_getdns_libev.c $(srcdir)/check_getdns_eventloop.h \
 ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libev.h \
 $(srcdir)/../getdns/getdns_extra.h $(srcdir)/check_getdns_common.h
check_getdns_libevent.lo check_getdns_libevent.o: $(srcdir)/check_getdns_libevent.c $(srcdir)/check_getdns_eventloop.h \
 ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libevent.h \
 $(srcdir)/../getdns/getdns_extra.h $(srcdir)/check_getdns_libevent.h $(srcdir)/check_getdns_common.h
check_getdns_libuv.lo check_getdns_libuv.o: $(srcdir)/check_getdns_libuv.c $(srcdir)/check_getdns_eventloop.h \
 ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libuv.h \
 $(srcdir)/../getdns/getdns_extra.h $(srcdir)/check_getdns_common.h
check_getdns_selectloop.lo check_getdns_selectloop.o: $(srcdir)/check_getdns_selectloop.c \
 $(srcdir)/check_getdns_eventloop.h ../config.h ../getdns/getdns.h \
 $(srcdir)/../getdns/getdns_extra.h
check_getdns_transport.lo check_getdns_transport.o: $(srcdir)/check_getdns_transport.c \
 $(srcdir)/check_getdns_transport.h $(srcdir)/check_getdns_common.h ../getdns/getdns.h
getdns_query.lo getdns_query.o: $(srcdir)/getdns_query.c ../config.h ../getdns/getdns.h \
 $(srcdir)/../getdns/getdns_extra.h
testmessages.lo testmessages.o: $(srcdir)/testmessages.c $(srcdir)/testmessages.h
tests_dict.lo tests_dict.o: $(srcdir)/tests_dict.c $(srcdir)/testmessages.h ../getdns/getdns.h
tests_list.lo tests_list.o: $(srcdir)/tests_list.c $(srcdir)/testmessages.h ../getdns/getdns.h
tests_namespaces.lo tests_namespaces.o: $(srcdir)/tests_namespaces.c $(srcdir)/testmessages.h ../getdns/getdns.h
tests_stub_async.lo tests_stub_async.o: $(srcdir)/tests_stub_async.c ../config.h $(srcdir)/testmessages.h \
 ../getdns/getdns.h $(srcdir)/../getdns/getdns_extra.h
tests_stub_sync.lo tests_stub_sync.o: $(srcdir)/tests_stub_sync.c $(srcdir)/testmessages.h ../getdns/getdns.h
