#################################################################
#
#	@(#) Makefile for dnssec zone key tool  (c) Mar 2005 hoz
#
#################################################################

prefix	=	@prefix@
mandir	=	@mandir@
INSTALL_DIR =	$$HOME/bin

CC	=	@CC@

PROFILE =	# -pg
OPTIM	=	# -O3 -DNDEBUG

#CFLAGS	?=	@CFLAGS@ @DEFS@ -I@top_srcdir@
CFLAGS	+=	-g @DEFS@ -I@top_srcdir@
CFLAGS	+=	-Wall #-DDBG
CFLAGS	+=	-Wmissing-prototypes
CFLAGS	+=	$(PROFILE) $(OPTIM)
LDFLAGS	+=	$(PROFILE)

PROJECT =	@PACKAGE_TARNAME@
VERSION =	@PACKAGE_VERSION@

HEADER	=	dki.h misc.h domaincmp.h zconf.h config_zkt.h \
		config.h.in strlist.h zone.h zkt.h debug.h \
		ncparse.h log.h rollover.h
SRC_ALL	=	dki.c misc.c domaincmp.c zconf.c log.c
OBJ_ALL	=	$(SRC_ALL:.c=.o)

SRC_SIG	=	dnssec-signer.c zone.c ncparse.c rollover.c
OBJ_SIG	=	$(SRC_SIG:.c=.o)
MAN_SIG	=	dnssec-signer.8
PROG_SIG=	dnssec-signer

SRC_ZKT	=	dnssec-zkt.c strlist.c zkt.c
OBJ_ZKT	=	$(SRC_ZKT:.c=.o)
MAN_ZKT	=	dnssec-zkt.8
PROG_ZKT=	dnssec-zkt

SRC_SER	=	zkt-soaserial.c
OBJ_SER	=	$(SRC_SER:.c=.o)
#MAN_SER	=	zkt-soaserial.8
PROG_SER=	zkt-soaserial

MAN_ALL	=	$(MAN_ZKT) $(MAN_SIG) #$(MAN_SER)
OTHER	=	README README.logging TODO LICENSE CHANGELOG tags Makefile.in \
		configure examples
SAVE	=	$(HEADER) $(SRC_ALL) $(SRC_SIG) $(SRC_ZKT) $(SRC_SER) man $(OTHER)
MNTSAVE	=	$(SAVE) configure.ac config.h.in doc 


all:	$(PROG_ZKT) $(PROG_SIG) $(PROG_SER)

macos:		## for MAC OS
macos:
	$(MAKE) CFLAGS="$(CFLAGS) -D HAS_UTYPES=0" all

solaris:	## for solaris
solaris:
	@$(MAKE) CFLAGS="$(CFLAGS) -D HAVE_GETOPT_LONG=0" all

linux:		## for linux (default)
linux:
	@$(MAKE) all

$(PROG_SIG):	$(OBJ_SIG) $(OBJ_ALL) Makefile
	$(CC) $(LDFLAGS) $(OBJ_SIG) $(OBJ_ALL) -o $(PROG_SIG)

$(PROG_ZKT):	$(OBJ_ZKT) $(OBJ_ALL) Makefile
	$(CC) $(LDFLAGS) $(OBJ_ZKT) $(OBJ_ALL) -o $(PROG_ZKT)

$(PROG_SER):	$(OBJ_SER) Makefile
	$(CC) $(LDFLAGS) $(OBJ_SER) -o $(PROG_SER)

install:	## install binaries in INSTALL_DIR
install:	$(PROG_ZKT) $(PROG_SIG) $(PROG_SER)
	cp $(PROG_ZKT) $(PROG_SIG) $(PROG_SER) $(INSTALL_DIR)

install-man:	## install man pages in mandir
install-man:
	test -d $(mandir)/man8/ && cp -p man/$(MAN_ZKT) man/$(MAN_SIG) $(mandir)/man8/


tags:		## create tags file
tags:	$(SRC_ALL) $(SRC_SIG) $(SRC_ZKT) $(SRC_SER)
	ctags $(SRC_ALL) $(SRC_SIG) $(SRC_ZKT) $(SRC_SER)

clean:		## remove objectfiles and binaries
clean:
	rm -f $(OBJ_SIG) $(OBJ_ZKT) $(OBJ_SER) $(OBJ_ALL)

dist:		## create tar file for distribution
dist:	$(PROJECT)-$(VERSION).tar.gz
tar:		## create tar file for distribution
tar:	$(PROJECT)-$(VERSION).tar.gz

maintain:	## create configure script
maintain:	configure

mainttar:	## create tar file for maintenance
mainttar:	$(PROJECT)-maint-$(VERSION).tar.gz

configure:	configure.ac
	autoconf && autoheader

man:	man/$(MAN_ZKT).html man/$(MAN_ZKT).pdf man/$(MAN_SIG).html man/$(MAN_SIG).pdf

man/$(MAN_ZKT).html: man/$(MAN_ZKT)
	groff -Thtml -man -mhtml man/$(MAN_ZKT) > man/$(MAN_ZKT).html
man/$(MAN_ZKT).pdf: man/$(MAN_ZKT)
	groff -Tps -man man/$(MAN_ZKT) | ps2pdf - man/$(MAN_ZKT).pdf
man/$(MAN_SIG).html: man/$(MAN_SIG)
	groff -Thtml -man -mhtml man/$(MAN_SIG) > man/$(MAN_SIG).html
man/$(MAN_SIG).pdf: man/$(MAN_SIG)
	groff -Tps -man man/$(MAN_SIG) | ps2pdf - man/$(MAN_SIG).pdf
	
	
$(PROJECT)-$(VERSION).tar.gz:	$(SAVE)
	rm -f examples/hierarchical/log/zkt-*
	(	\
		distfiles=`ls -d $(SAVE) | sed 's|^|$(PROJECT)-$(VERSION)/|'` ;\
		cd .. && tar czvf $(PROJECT)-$(VERSION)/$(PROJECT)-$(VERSION).tar.gz $$distfiles ;\
	)

$(PROJECT)-maint-$(VERSION).tar.gz:	$(MNTSAVE)
	(	\
		distfiles=`ls -d $(MNTSAVE) | sed 's|^|$(PROJECT)-$(VERSION)/|'` ;\
		cd .. && tar czvf $(PROJECT)-$(VERSION)/$(PROJECT)-maint-$(VERSION).tar.gz $$distfiles ;\
	)

depend:
	$(CC) -MM $(SRC_SIG) $(SRC_ZKT) $(SRC_SER) $(SRC_ALL)

help:
	@grep "^.*:[ 	]*##" Makefile

## all dependicies
#:r !make depend
#gcc -MM dnssec-signer.c zone.c ncparse.c rollover.c dnssec-zkt.c strlist.c zkt.c zkt-soaserial.c dki.c misc.c domaincmp.c zconf.c log.c
dnssec-signer.o: dnssec-signer.c config_zkt.h zconf.h debug.h misc.h \
  ncparse.h zone.h dki.h rollover.h log.h
zone.o: zone.c config_zkt.h debug.h domaincmp.h misc.h zconf.h dki.h \
  zone.h
ncparse.o: ncparse.c debug.h misc.h zconf.h log.h ncparse.h
rollover.o: rollover.c config_zkt.h zconf.h debug.h misc.h zone.h dki.h \
  log.h rollover.h
dnssec-zkt.o: dnssec-zkt.c config_zkt.h debug.h misc.h zconf.h strlist.h \
  dki.h zkt.h
strlist.o: strlist.c strlist.h
zkt.o: zkt.c config_zkt.h dki.h misc.h zconf.h strlist.h zkt.h
zkt-soaserial.o: zkt-soaserial.c config_zkt.h
dki.o: dki.c config_zkt.h debug.h domaincmp.h misc.h zconf.h dki.h
misc.o: misc.c config_zkt.h zconf.h log.h debug.h misc.h
domaincmp.o: domaincmp.c domaincmp.h
zconf.o: zconf.c config_zkt.h debug.h misc.h zconf.h dki.h
log.o: log.c config_zkt.h misc.h zconf.h debug.h log.h
