# $Id: Makefile,v 1.5 2002/07/22 09:51:44 mjt Exp $
# make file for tinycdb package
#
# This file is a part of tinycdb package by Michael Tokarev, mjt@corpit.ru.
# Public domain.

NAME = tinycdb
VERSION = 0.73

prefix=/usr/local
exec_prefix=$(prefix)
bindir=$(exec_prefix)/bin
libdir=$(exec_prefix)/lib
includedir=$(prefix)/include
mandir=$(prefix)/man
DESTDIR=

CC = cc
CFLAGS = -O
AR = ar
ARFLAGS = rv
RANLIB = ranlib
SED = sed

LSRCS = cdb_init.c cdb_find.c cdb_findnext.c cdb_seek.c cdb_unpack.c \
 cdb_make_add.c cdb_make_put.c cdb_make_find.c cdb_make.c cdb_hash.c

DISTFILES = Makefile cdb.h.in cdb_int.h $(LSRCS) cdb.c \
 cdb.3.in cdb.1.in cdb.5.in \
 tinycdb.spec.in tinycdb.spec tests.sh tests.ok \
 ChangeLog

MANS = cdb.1 cdb.3 cdb.5

all: libcdb.a cdb $(MANS)

LOBJS = $(LSRCS:.c=.o)

libcdb.a: $(LOBJS)
	-rm -f $@
	$(AR) $(ARFLAGS) $@ $(LOBJS)
	-$(RANLIB) $@

cdb: cdb.o libcdb.a
	$(CC) $(CFLAGS) -o $@ cdb.o libcdb.a

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

$(LOBJS) cdb.o: cdb.h
$(LOBJS): cdb_int.h

subst = sed -e 's|@VERSION@|$(VERSION)|' -e 's|@NAME@|$(NAME)|' $< > $@.tmp \
 && mv -f $@.tmp $@

cdb.h: cdb.h.in Makefile
	$(subst)
cdb.1: cdb.1.in Makefile
	$(subst)
cdb.3: cdb.3.in Makefile
	$(subst)
cdb.5: cdb.5.in Makefile
	$(subst)

spec: tinycdb.spec

tinycdb.spec: tinycdb.spec.in Makefile
	sed 's|^Version:.*|Version: $(VERSION)|' tinycdb.spec.in > $@.tmp
	mv $@.tmp $@

clean:
	-rm -f *.o core *~
realclean distclean:
	-rm -f *.o core *~ libcdb.a cdb cdb.h tinycdb.spec $(MANS)

test tests check: cdb
	sh ./tests.sh ./cdb > tests.out 2>&1
	diff tests.ok tests.out
	@echo All tests passed

install: all
	@set -- \
	 cdb.h $(includedir) \
	 libcdb.a $(libdir) \
	 cdb.3 $(mandir)/man3 \
	 cdb $(bindir) \
	 cdb.1 $(mandir)/man1 \
	 cdb.5 $(mandir)/man5 \
	 ; \
	while [ $$# -gt 0 ] ; do \
	 d="$(DESTDIR)$$2" ; echo "installing $$d/$$1"; \
	 [ -d "$$d" ] || mkdir -p "$$d" || exit 1 ; \
	 cp -p $$1 "$$d/$$1" || exit 1; \
	 shift 2; \
	done

DNAME = $(NAME)-$(VERSION)
dist: $(DNAME).tar.gz
$(DNAME).tar.gz: $(DISTFILES)
	rm -fr $(DNAME)
	mkdir $(DNAME)
	ln $(DISTFILES) $(DNAME)/
	tar cfz $@ $(DNAME)
	rm -fr $(DNAME)

.PHONY: all clean realclean test tests check install dist spec
