prefix=@prefix@
exec_prefix=@exec_prefix@
sbindir=@sbindir@
mandir=@mandir@
sysconfdir=@sysconfdir@
varrun=/var/run/bld

CC= @CC@
PROGNAME= bld
READERNAME= $(PROGNAME)read
SUBMITNAME= $(PROGNAME)submit
QUERYNAME= $(PROGNAME)query
INSERTNAME= $(PROGNAME)insert
DECRNAME= $(PROGNAME)decr
POLICYNAME= $(PROGNAME)-pf_policy
CFLAGS= @CFLAGS@ -DPROGNAME=\"$(PROGNAME)\" -DVARRUN=\"$(varrun)\" -DETC_LOCATION=\"$(sysconfdir)\"
OBJS= main.o net.o cmd.o iptree.o daemon.o netlist.o utils.o parse_args.o \
      parse_config.o options.o client.o
LDFLAGS= @LDFLAGS@
LIBS= @LIBS@
GZIP= gzip -c -9

all: $(PROGNAME) $(READERNAME) $(SUBMITNAME) $(POLICYNAME)

$(PROGNAME): $(OBJS)
	$(CC) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
$(READERNAME): dumpread.o
	$(CC) -o $@ dumpread.o
$(SUBMITNAME): submit.o net.o client.o utils.o
	$(CC) -o $@ $(LDFLAGS) submit.o net.o client.o utils.o $(LIBS)
$(POLICYNAME): postfix_policy.o net.o client.o
	$(CC) -o $@ $(LDFLAGS) postfix_policy.o net.o client.o $(LIBS)

links: $(SUBMITNAME)
	-ln -sf $(SUBMITNAME) $(QUERYNAME)
	-ln -sf $(SUBMITNAME) $(INSERTNAME)
	-ln -sf $(SUBMITNAME) $(DECRNAME)

submit.o: submit.c net.h

install: all
	[ -d $(DESTDIR)$(sbindir) ] || mkdir -p $(DESTDIR)$(sbindir)
	cp $(PROGNAME) $(READERNAME) $(POLICYNAME) $(DESTDIR)$(sbindir)
	strip $(DESTDIR)$(sbindir)/$(PROGNAME)
	strip $(DESTDIR)$(sbindir)/$(READERNAME)
	strip $(DESTDIR)$(sbindir)/$(POLICYNAME)
	cp $(SUBMITNAME) $(DESTDIR)$(sbindir) \
	    && (cd $(DESTDIR)$(sbindir) && ln -s $(SUBMITNAME) $(QUERYNAME)) \
	    && (cd $(DESTDIR)$(sbindir) && ln -s $(SUBMITNAME) $(INSERTNAME)) \
	    && (cd $(DESTDIR)$(sbindir) && ln -s $(SUBMITNAME) $(DECRNAME))
	strip $(DESTDIR)$(sbindir)/$(SUBMITNAME)
	[ -d $(DESTDIR)$(mandir) ] || mkdir -p $(DESTDIR)$(mandir)/man8
	$(GZIP) -c bld.8 > $(DESTDIR)$(mandir)/man8/bld.8.gz
	$(GZIP) -c bldread.8 > $(DESTDIR)$(mandir)/man8/bldread.8.gz
	$(GZIP) -c bldquery.8 > $(DESTDIR)$(mandir)/man8/bldquery.8.gz
	$(GZIP) -c bldsubmit.8 > $(DESTDIR)$(mandir)/man8/bldsubmit.8.gz
	$(GZIP) -c bldinsert.8 > $(DESTDIR)$(mandir)/man8/bldinsert.8.gz
	$(GZIP) -c blddecr.8 > $(DESTDIR)$(mandir)/man8/blddecr.8.gz
	$(GZIP) -c bld-pf_policy.8 > $(DESTDIR)$(mandir)/man8/bld-pf_policy.8.gz
	[ -d $(DESTDIR)$(mandir) ] || mkdir -p $(DESTDIR)$(mandir)/man5
	$(GZIP) -c bld.conf.5 > $(DESTDIR)$(mandir)/man5/bld.conf.5.gz
	$(GZIP) -c bld_acl.conf.5 > $(DESTDIR)$(mandir)/man5/bld_acl.conf.5.gz
	$(GZIP) -c bld_whitelist.conf.5 > $(DESTDIR)$(mandir)/man5/bld_whitelist.conf.5.gz


client.o: client.c options.h netlist.h client.h cmd.h iptree.h utils.h \
 net.h
cmd.o: cmd.c options.h netlist.h cmd.h iptree.h ipinfo.h utils.h \
 client.h
daemon.o: daemon.c options.h netlist.h daemon.h
dumpread.o: dumpread.c ipinfo.h
iptree.o: iptree.c ipinfo.h iptree.h options.h netlist.h
main.o: main.c options.h netlist.h parse_args.h parse_config.h net.h \
 cmd.h iptree.h daemon.h utils.h
net.o: net.c net.h
netlist.o: netlist.c netlist.h utils.h
options.o: options.c options.h netlist.h
parse_args.o: parse_args.c options.h netlist.h utils.h parse_args.h
parse_config.o: parse_config.c utils.h options.h netlist.h
postfix_policy.o: postfix_policy.c client.h cmd.h iptree.h options.h \
 netlist.h
submit.o: submit.c options.h netlist.h net.h client.h cmd.h iptree.h
utils.o: utils.c utils.h


clean:
	-rm -f $(PROGNAME) $(READERNAME) $(SUBMITNAME) $(DECRNAME) \
	$(POLICYNAME) $(OBJS) dumpread.o submit.o postfix_policy.o core

distclean: clean
	-rm -f *~ Makefile config.status config.log config.cache config.h \
	$(READERNAME) $(SUBMITNAME) $(QUERYNAME) $(INSERTNAME) $(DECRNAME) \
	$(POLICYNAME) bld.8 bld_acl.conf.5 bld_whitelist.conf.5 bld.conf.5
	-rm -rf autom4te.cache bld.8.html
	-rm -rf mktest/Makefile mktest/config.h mktest/mktest.log \
		mktest/bld_iplist.txt mktest/bld_blacklist.txt \
		mktest/bld_iplist.dump mktest/bld_blacklist.dump

