#  Copyright (c) 2012 Antti-Juhani Kaijanaho
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#  2. 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.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.

SHELL = /bin/sh

PACKAGE_TARNAME = @PACKAGE_TARNAME@
PACKAGE_VERSION = @PACKAGE_VERSION@

srcdir          = @srcdir@

CC              = @CC@
CPPFLAGS        = @CPPFLAGS@
CFLAGS          = @CFLAGS@
MDMF            = @MDMF@
LDFLAGS         = @LDFLAGS@
INSTALL         = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA    = @INSTALL_DATA@
INSTALL_DIR     = ${INSTALL} -d
MKDIR_P         = @MKDIR_P@
RANLIB		= @RANLIB@ 

AR		= ar
LN		= ln

echoexec        = sh $(srcdir)/echoexec.sh
link_alternative_names = sh $(srcdir)/link-alternative-names

prefix          = @prefix@
exec_prefix     = @exec_prefix@
datarootdir     = @datarootdir@

includedir      = @includedir@
libdir          = @libdir@
mandir          = @mandir@
man3dir         = ${mandir}/man3
man3ext	        = .3

objs =  alloc/memdup.o          \
	alloc/xmalloc.o         \
	alloc/xrealloc.o        \
	alloc/xfree.o           \
	alloc/xstrdup.o         \
	alloc/xmemdup.o         \
	alloc/dynarr.o          \
	base64/base64.o         \
	bitarr/ba_and_ba.o      \
	bitarr/ba_clear.o       \
	bitarr/ba_clear_all.o   \
	bitarr/ba_copy.o        \
	bitarr/ba_create.o      \
	bitarr/ba_destroy.o     \
	bitarr/ba_not.o         \
	bitarr/ba_or_ba.o       \
	bitarr/ba_or_not_ba.o   \
	bitarr/ba_query.o       \
	bitarr/ba_resize.o      \
	bitarr/ba_set.o         \
	bitarr/ba_xor_ba.o      \
	cmp/cmp.o               \
	errormsg/errormsg.o     \
	files/xgetaline.o       \
	files/xfseek.o          \
	files/xfopen.o          \
	files/xfclose.o         \
	files/getaline.o        \
	files/fassert.o         \
	files/file_io.o         \
	fname/fnbase.o          \
	fname/fndelbeg.o        \
	fname/fndelend.o        \
	fname/fndelsuf.o        \
	fname/fndir.o           \
	fname/fnhome.o          \
	fname/fnjoin.o          \
	fname/fnlastsuf.o       \
	fname/fnpathfind.o      \
	fname/fnqualify.o       \
	fname/fnsetsuf.o        \
	hash/hash_create.o      \
	hash/hash_destroy.o     \
	hash/hash_install.o     \
	hash/hash_lookup.o      \
	hash/hash_uninstall.o   \
	hash/hash_iter.o        \
	iset/iset_add.o         \
	iset/iset_add_rng.o     \
	iset/iset_clear.o       \
	iset/iset_contains.o    \
	iset/iset_copy.o        \
	iset/iset_create.o      \
	iset/iset_destroy.o     \
	iset/iset_diff.o        \
	iset/iset_is_empty.o    \
	iset/iset_isect.o       \
	iset/iset_nth_rng.o     \
	iset/iset_range.o       \
	iset/iset_remove.o      \
	iset/iset_rm_rng.o      \
	iset/iset_union.o       \
	lockfile/lockfile.o     \
	log/log.o               \
	main/main_filter.o      \
	nntp/nntp.o             \
	nntp/nntpart.o          \
	priq/priq.o             \
	queue/queue.o           \
	sbuf/sbuf.o             \
	sbuf/sbuf_aux.o         \
	sbuf/sbuf_srch.o        \
	sbuf/sbuf_io.o          \
	stack/stack_copy.o      \
	stack/stack_create.o    \
	stack/stack_destroy.o   \
	stack/stack_emptyp.o    \
	stack/stack_pop.o       \
	stack/stack_push.o      \
	strutil/memarrfill.o    \
	strutil/memdel.o        \
	strutil/memfill.o       \
	strutil/memins.o        \
	strutil/memisort.o      \
	strutil/memmem.o        \
	strutil/memoverlap.o    \
	strutil/memrchr.o       \
	strutil/memrev.o        \
	strutil/memrmem.o       \
	strutil/memshuffle.o    \
	strutil/memswap.o       \
	strutil/stracat.o       \
	strutil/stranaxfrm.o    \
	strutil/strchange.o     \
	strutil/strcins.o       \
	strutil/strcstr.o       \
	strutil/strdel.o        \
	strutil/strdiff.o       \
	strutil/strend.o        \
	strutil/strendzap.o     \
	strutil/strgsub.o       \
	strutil/strhash.o       \
	strutil/strinit.o       \
	strutil/strins.o        \
	strutil/strltrim.o      \
	strutil/strmaxcpy.o     \
	strutil/strmove.o       \
	strutil/strmtrim.o      \
	strutil/strnins.o       \
	strutil/strnlen.o       \
	strutil/stroverlap.o    \
	strutil/strrev.o        \
	strutil/strright.o      \
	strutil/strrot13.o      \
	strutil/strrstr.o       \
	strutil/strrtrim.o      \
	strutil/strset.o        \
	strutil/strshuffle.o    \
	strutil/strsplit.o      \
	strutil/strsub.o        \
	strutil/strtabify.o     \
	strutil/strtrexpand.o   \
	strutil/strtrim.o       \
	strutil/struncstr.o     \
	strutil/struntabify.o   \
	strutil/strvars.o       \
	strutil/strzap.o        \
	tbuf/tbuf.o

srcs = $(objs:.o=.c)

# excepting publib.h (special case!)
includes =                          \
	includes/publib/alloc.h     \
	includes/publib/base64.h    \
	includes/publib/bitarr.h    \
	includes/publib/cmp.h       \
	includes/publib/errormsg.h  \
	includes/publib/files.h     \
	includes/publib/fname.h     \
	includes/publib/hash.h      \
	includes/publib/iset.h      \
	includes/publib/lockfile.h  \
	includes/publib/log.h       \
	includes/publib/main.h      \
	includes/publib/nntp.h      \
	includes/publib/priq.h      \
	includes/publib/queue.h     \
	includes/publib/sbuf.h      \
	includes/publib/stack.h     \
	includes/publib/strutil.h   \
	includes/publib/tbuf.h

manpages =                          \
	man/bitarr.3                \
	man/cmp.3                   \
	man/dynarr.3                \
	man/errormsg.3              \
	man/files.3                 \
	man/fname.3                 \
	man/fnpathfind.3            \
	man/fnqualify.3             \
	man/hash.3                  \
	man/iset.3                  \
	man/log.3                   \
	man/memarrfill.3            \
	man/memdel.3                \
	man/memfill.3               \
	man/memins.3                \
	man/memisort.3              \
	man/memmem.3                \
	man/memoverlap.3            \
	man/memrchr.3               \
	man/memrev.3                \
	man/memrmem.3               \
	man/memshuffle.3            \
	man/memswap.3               \
	man/publib.3                \
	man/__set_liberror.3        \
	man/stack.3                 \
	man/stracat.3               \
	man/stranaxfrm.3            \
	man/strchange.3             \
	man/strcins.3               \
	man/strcstr.3               \
	man/strdel.3                \
	man/strdiff.3               \
	man/strend.3                \
	man/strendzap.3             \
	man/strgsub.3               \
	man/strhash.3               \
	man/strinit.3               \
	man/strins.3                \
	man/strltrim.3              \
	man/strmaxcpy.3             \
	man/strmove.3               \
	man/strmtrim.3              \
	man/strnins.3               \
	man/stroverlap.3            \
	man/strrev.3                \
	man/strright.3              \
	man/strrot13.3              \
	man/strrstr.3               \
	man/strrtrim.3              \
	man/strset.3                \
	man/strshuffle.3            \
	man/strsplit.3              \
	man/strsub.3                \
	man/strtabify.3             \
	man/strtrexpand.3           \
	man/strtrim.3               \
	man/struncstr.3             \
	man/struntabify.3           \
	man/strvars.3               \
	man/strzap.3                \
	man/tbuf.3                  \
	man/xmalloc.3

tests = iset/test_add.c             \
	main/test-main-filter.c     \
	log/test-log.c              \
	tbuf/test_tbuf_with_stats.c \
	tbuf/test_tbuf.c            \
	queue/test-queue.c          \
	sbuf/test-colmark.c         \
	sbuf/test-sbuf-undo.c       \
	sbuf/test-sbuf.c            \
	priq/test-priq.c            \
	fname/test_fnqualify.c

distfiles = $(srcs) $(includes) $(manpages) $(tests) \
	aclocal.m4 ChangeLog.old configure configure.ac distcheck.mk \
	echoexec.sh INSTALL includes/publib.h \
	install-sh link-alternative-names Makefile.in NEWS README TODO

all :	libpub.a

clean mostlyclean :
	$(RM) libpub.a */*.o */*.d

distclean maintainer-clean : clean
	$(RM) config.log config.status Makefile

install : libpub.a
	$(PRE_INSTALL)
	$(NORMAL_INSTALL)
	$(INSTALL_DIR) $(DESTDIR)$(includedir)/publib
	$(INSTALL_DIR) $(DESTDIR)$(libdir)
	$(INSTALL_DIR) $(DESTDIR)$(man3dir)	
	$(INSTALL_DATA) libpub.a $(DESTDIR)$(libdir)
	$(INSTALL_DATA) includes/publib.h $(DESTDIR)$(includedir)
	@for f in $(includes) ; do \
	  $(echoexec) $(INSTALL_DATA) $$f $(DESTDIR)$(includedir)/publib/ ;\
	done
	@for f in $(manpages) ; do\
	  if test -f $$f; then \
	    t="$(DESTDIR)$(man3dir)/`basename $$f .3`$(man3ext)"; \
	    $(echoexec) $(INSTALL_DATA) $$f "$$t" ;\
	    $(echoexec) gzip -9f "$$t" ;\
	    $(link_alternative_names) $$t $(DESTDIR)$(man3dir) $(man3ext) ;\
	  fi ;\
	done
	$(POST_INSTALL)

install-strip: install
#	No point in stripping a static library ...

uninstall :
	$(PRE_UNINSTALL)
	$(NORMAL_UNINSTALL)
	$(RM) $(DESTDIR)$(libdir)/libpub.a
	$(RM) $(DESTDIR)$(includedir)publib.h
	@for f in $(includes) ; do \
	  $(echoexec) $(RM) $(DESTDIR)$(includedir)/publib/`basename $$f` ;\
	done
	@for f in $(manpages) ; do\
	  t="$(DESTDIR)$(man3dir)/`basename $$f .3`$(man3ext)"; \
	  $(link_alternative_names) -r $$t $(DESTDIR)$(man3dir) $(man3ext) ;\
	  $(echoexec) $(RM) "$$t".gz ;\
	done
	$(POST_UNINSTALL)

libpub.a : $(objs)
	$(AR) cr $@ $^
	$(RANLIB) $@

check : libpub.a
	@set -e; for f in $(tests) ; do\
	  $(echoexec) $(CC) -Iincludes $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $$f libpub.a ;\
	  echo './a.out </dev/null >/dev/null' ;\
	  ./a.out </dev/null >/dev/null ;\
	  $(echoexec) $(RM) a.out ;\
	  if test -r test.log; then rm test.log ; fi ;\
	done

dist_base = $(PACKAGE_TARNAME)-$(PACKAGE_VERSION)
dist_tar = $(dist_base).tar
dist_tgz = $(dist_tar).gz

dist : $(dist_tgz)

distsign : $(dist_tgz).asc

$(dist_tgz) : $(distfiles)
	@if test -e $(DESTDIR)$(dist_base) ; then \
	  echo "$(DESTDIR)$(dist_base) exists; cannot continue" ; \
	  exit 1 ;\
	fi
	$(MKDIR_P) $(DESTDIR)$(dist_base)
	@set -e ; for f in $(distfiles) ; do \
	  dir=$(DESTDIR)$(dist_base)/`dirname $$f` ; \
	  if ! test -d $$dir ; then \
	    $(echoexec) $(MKDIR_P) $$dir ;\
	  fi ;\
	  $(echoexec) $(LN) $$f $(DESTDIR)$(dist_base)/$$f ;\
	done
	cd $(DESTDIR). && tar cf $(dist_tar) $(dist_base)
	gzip -9f $(DESTDIR)$(dist_tar)
	$(RM) -r $(DESTDIR)$(dist_base)

$(dist_tgz).asc : $(dist_tgz)
	gpg --output $@ --clearsign --detach-sig $<

distcheck_stage = distcheck_stage
distcheck : $(dist_tgz)
	@if test -e $(DESTDIR)$(distcheck_stage) ; then \
	  echo "$(DESTDIR)$(distcheck_stage) exists; cannot continue" ; \
	  exit 1 ;\
	fi
	$(MKDIR_P) $(DESTDIR)$(distcheck_stage)
	cp $(dist_tgz) $(DESTDIR)$(distcheck_stage)
	cp distcheck.mk $(DESTDIR)$(distcheck_stage)/Makefile
	cp echoexec.sh $(DESTDIR)$(distcheck_stage)
	$(MAKE) -C $(DESTDIR)$(distcheck_stage) \
	  base=$(dist_base) tgz=$(dist_tgz) \
	  includes="$(includes)" manpages="$(manpages)" \
	  run_distcheck
	$(RM) -r $(DESTDIR)$(distcheck_stage)
.c.o:
	$(CC) -Iincludes -D__publib__ $(CPPFLAGS) $(CFLAGS) $(MDMF) -c $< -o $@

Makefile : Makefile.in config.status
	./config.status

config.status : configure
	./configure

configure : configure.ac aclocal.m4
	autoconf

aclocal.m4 :
	aclocal

.SUFFIXES:
.SUFFIXES: .c .o

-include */*.d

