#<: 'Makefile' v0.013 @ 2012-10-03 13:00:26 UTC (Jonas@Ubbe-III) "Jonas Eckerman" <jonas@truls.org> :>#

# What the DNS?
# make DNS server

OFLAGS=-O3
SFLAGS=-s
DBGDEF=0
DBGUMW=0
ifneq ($(wildcard packaged.not),)
	DBGDEF=1
endif
ifneq ($(wildcard debug),)
	DBGDEF=1
	DBGUMW=1
endif
ifdef DEBUG
	DBGDEF=1
	DBGUMW=1
endif
ifdef debug
	DBGDEF=1
	DBGUMW=1
endif

ifneq ($(DBGDEF),0)
	OFLAGS=-O0 -g
	SFLAGS=
endif
ifeq ($(wildcard memwatch.c),)
	DBGUMW=0
endif
ifeq ($(wildcard memwatch.h),)
	DBGUMW=0
endif

CFLAGS=-Wall $(OFLAGS) -fstack-protector -std=gnu99 -D_GNU_SOURCE -pedantic
LFLAGS=$(CFLAGS) $(SFLAGS)

XOBJS=
ifneq ($(DBGUMW),0)
	XOBJS=memwatch.o
endif

DOBJS=wtdnsd.o global.o strutil.o log.o sockutil.o dir.o dnsdata.o dnssec.o cdecode.o $(XOBJS)
HOBJS=wtdnshash.o global.o strutil.o dir.o log.o dnsdata.o $(XOBJS)
TOBJS=test.o global.o strutil.o log.o dnsdata.o

SRCS=$(wildcard *.c b64/*.c)
DBGS=tst run ctl mem

OBJECTS=$(SRCS:.c=.o)

IFO=-p -m u=rwx,go=rx,a-s

default: all

all: wtdnsd wtdnshash

.SUFFIXES:
.SUFFIXES: .c .o

stamps:
	for f in $(DBGS); do if [ -f $$f ]; then chmod -f a+x $$f; fi; done
	if [ -f run ]; then chmod -f a+x run; fi
	if [ -f ctl ]; then chmod -f a+x ctl; fi
	date -u +'#define MAKEDATE "%F %T %Z"' > makestamp.h
	echo "#define MAKEDBGD $(DBGDEF)"     >> makestamp.h
	echo "#define MAKEMEMW $(DBGUMW)"     >> makestamp.h

test: stamps $(OBJECTS)
	gcc $(LFLAGS) -o test $(TOBJS)

wtdnsd: stamps $(OBJECTS)
	gcc $(LFLAGS) -o wtdnsd $(DOBJS)

wtdnshash: stamps $(OBJECTS)
	gcc $(LFLAGS) -o wtdnshash $(HOBJS)

.c.o: $(SRCS)
	gcc $(CFLAGS) -E $< > $*.i
	gcc $(CFLAGS) -c $*.i

clean:
	rm -f wtdnsd wtdnshash test *.o *.i b64/*.i core log dbg pid mtr memwatch.log

distclean: clean
	rm -f *~ *.diss test.* t.* makestamp.h packaged.not debug [a-z][a-z][a-z] memwatch.*

preinstall:

postinstall:

prepackage:

postpackage:

install: all
	install $(IFO) wtdnshash /usr/bin/
	set -e; \
	if ! install $(IFO) wtdnsd /usr/sbin/; then \
	  if pidof /usr/sbin/wtdnsd > /dev/null; then \
	    echo "\\n\\nwtdnsd is running.\\n"; \
	    if (which initctl > /dev/null && /sbin/initctl version > /dev/null && \
	        which service > /dev/null && service wtdnsd status > /dev/null && \
	        [ -d /etc/init ] && [ -f /etc/init/wtdnsd.conf ]); then \
	      echo Trying to stop wtdnsd.; \
	      service wtdnsd stop; \
	      install $(IFO) wtdnsd /usr/sbin/; \
	      echo Trying to start wtdnsd.; \
	      service wtdnsd start; \
	    fi; \
	  fi; \
	fi
