LIBS = @LIBS@ @PKG_LIBS@ @PKG_CFLAGS@
CC = @CC@
prefix = @prefix@
exec_prefix = @exec_prefix@
CFLAGS = @CFLAGS@ @PKG_CFLAGS@ -DHAVE_CONFIG_H
INSTALL = @INSTALL@
bindir = @bindir@
docdir = @prefix@/share/doc/ctrlproxy
modulesdir = @libdir@/ctrlproxy
includedir = @includedir@
man1dir = @mandir@/man1
man5dir = @mandir@/man5
datadir = @datadir@/ctrlproxy

CFLAGS+=-ansi -Wall -DMODULESDIR=\"$(modulesdir)\"
OBJS = server.o line.o main.o state.o util.o transport.o hooks.o linestack.o
BINS = @BINS@

all: $(BINS)
	$(MAKE) -C mods all
@DOCBUILDE@	$(MAKE) -C doc all

ctrlproxy: $(OBJS)
	$(CC) $(LIBS) -rdynamic -o $@ $(OBJS)

printstats: printstats.o
	$(CC) -ltdb -o $@ $<

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

install: all install-dirs install-doc install-bin install-mods install-data
install-dirs:
	$(INSTALL) -d $(DESTDIR)$(bindir)
	$(INSTALL) -d $(DESTDIR)$(man1dir)
	$(INSTALL) -d $(DESTDIR)$(man5dir)
	$(INSTALL) -d $(DESTDIR)$(includedir)
	$(INSTALL) -d $(DESTDIR)$(modulesdir)
	$(INSTALL) -d $(DESTDIR)$(docdir)
	$(INSTALL) -d $(DESTDIR)$(datadir)
	$(MAKE) -C doc install 
	
install-bin:
	$(INSTALL) ctrlproxy $(DESTDIR)$(bindir)

install-doc:
	$(INSTALL) ctrlproxy.h $(DESTDIR)$(includedir)
	$(INSTALL) AUTHORS $(DESTDIR)$(docdir)
	$(INSTALL) TODO $(DESTDIR)$(docdir)

install-data:
	$(INSTALL) motd $(DESTDIR)$(datadir)

install-mods:
	$(MAKE) -C mods install

clean:
	rm -f *.o ctrlproxy printstats *~
	$(MAKE) -C mods clean
@DOCBUILDE@	$(MAKE) -C doc clean

distclean: clean
	rm -f Makefile build config.h ctrlproxy.pc ctrlproxy.spec *.log
	rm -rf autom4te.cache/ config.log config.status
	$(MAKE) -C mods distclean
	$(MAKE) -C doc distclean
