# Makefile for libdhcp
#
# Copyright (C) 2006  Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2.  This program is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
# including the implied warranties of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
# trademarks that are incorporated in the source code or documentation are
# not subject to the GNU General Public License and may only be used or
# replicated with the express permission of Red Hat, Inc.
#
# Red Hat Author(s): Jason Vas Dias
#                    David Cantrell
#

SRC = libdhcp.c ip_addr.c dhcp4_lease.c dhcp4_nic.c dhcp6_lease.c dhcp6_nic.c \
      nic.c dhcp_nic.c pump.c
HDR = $(SRC:%.c=%.h)
OBJ = $(SRC:%.c=%.o)
TXT = Makefile libdhcp.spec libdhcp.pc test_dhcp4.c test_dhcp6.c test_dhcp.c \
      test_pump_dhcp.c test_pump_static.c test_dhcp_fork.c \
      Makefile.examples README COPYING

export CFLAGS := $(CFLAGS) -fPIC -I. -Wall -Werror -g2 -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -D_GNU_SOURCE

DHC4_CFLAGS     = $(shell pkg-config --cflags libdhcp4client)
DHC4_ISC_CFLAGS = $(shell pkg-config --variable=ISC.Cflags libdhcp4client)
DHC4_LIBS       = $(shell pkg-config --libs libdhcp4client)
DHC6_CFLAGS     = $(shell pkg-config --cflags libdhcp6client)
DHCPV6_CFLAGS   = $(shell pkg-config --variable=DHCPv6.Cflags libdhcp6client)
DHC6_LIBS       = $(shell pkg-config --libs libdhcp6client)
LIBNL_LIBS      = $(shell pkg-config --libs libnl-1)

LIBDHCP_VERSION=\
  $(shell \
    sed -n '/^#define[\ \	]*LIBDHCP[\ \	]/{s/^.*LIBDHCP[\ \	]*//;s/[\ \	]//g;p}' \
    libdhcp.h \
   )

prefix     ?= $(DESTDIR)/usr
libdir     ?= $(prefix)/lib
includedir ?= $(prefix)/include
pkgcfgdir  ?= $(libdir)/pkgconfig

LN       ?= /bin/ln
INSTALL  ?= /usr/bin/install
TAR      ?= /bin/tar
GZIP     ?= /usr/bin/gzip
SED      ?= /bin/sed
CP       ?= /bin/cp
RM       ?= /bin/rm
MKDIR    ?= /bin/mkdir
VERSION  = $(shell awk '/Version:/ { print $$2 }' libdhcp.spec)
RELEASE  = $(shell awk '/Release:/ { print $$2 }' libdhcp.spec | sed -e 's|%.*$$||g')
ARCH     = $(shell /bin/uname -i)
RPMBUILD ?= /usr/bin/rpmbuild
DOXYGEN  ?= /usr/bin/doxygen

all: libdhcp.so.1 libdhcp.a

include $(SRC:.c=.depends)

$(SRC:.c=.depends):	$(@:.depends=.c)
	/usr/bin/cpp -I. $(DHC4_CFLAGS) $(DHC4_ISC_CFLAGS) $(DHC6_CFLAGS) $(DHCPV6_CFLAGS) -M $(@:.depends=.c) > $@

$(OBJ): %.o: %.c %.h

pump.o: override CFLAGS += $(DHC4_ISC_CFLAGS)

dhcp4_lease.o: override CFLAGS += $(DHC4_CFLAGS) $(DHC4_ISC_CFLAGS)  

dhcp4_nic.o: override CFLAGS += $(DHC4_CFLAGS) $(DHC4_ISC_CFLAGS)  

dhcp6_lease.o: override CFLAGS += $(DHC6_CFLAGS) $(DHCPV6_CFLAGS)

libdhcp.a: $(OBJ)
	$(AR) crus $@ $(OBJ)

libdhcp.so.1: $(OBJ)
	$(CC) -shared -o $@ -Wl,-soname,$@ $(OBJ) $(LIBNL_LIBS) $(DHC6_LIBS) $(DHC4_LIBS) $(EXTRA_LIBS)
	$(LN) -sf $@ libdhcp.so

clean: clean-depends
	rm -f *.[ao] *.so.* *.so *.list core.[0-9]*
	rm -rf libdhcp-$(VERSION) libdhcp-$(VERSION).tar.gz
	rm -rf libdhcp-$(VERSION)-$(RELEASE).$(ARCH).rpm

clean-depends:
	rm -f *.depends

install: libdhcp.so.1 libdhcp.a
	mkdir -p $(libdir)
	$(INSTALL) -D -m 0755 libdhcp.so.1 $(libdir)
	$(INSTALL) -D -m 0644 libdhcp.a $(libdir)
	$(LN) -sf libdhcp.so.1 $(libdir)/libdhcp.so
	mkdir -p $(includedir)/libdhcp
	chmod 0755 $(includedir)/libdhcp
	$(INSTALL) -m 0644 $(HDR) $(includedir)/libdhcp/
	mkdir -p $(pkgcfgdir)
	$(SED) 's/@LIBDHCP_VERSION@/'$(VERSION)'/' < libdhcp.pc > $(pkgcfgdir)/libdhcp.pc
	chmod 644 $(pkgcfgdir)/libdhcp.pc

CVSTAG = libdhcp-$(subst .,_,$(VERSION)-$(RELEASE))
tag:
	@cvs tag -cRF $(CVSTAG)
	@echo "Tagged as $(CVSTAG)"

tar: tag
	@rm -rf /tmp/libdhcp
	@rm -rf /tmp/libdhcp-$(VERSION)
	@tag=`cvs status Makefile|awk ' /Sticky Tag/ { print $$3 } '` 2>/dev/null;\
	[ x"$$tag" = x"(none)" ] && tag=HEAD; \
	[ x"$$TAG" != x ] && tag=$$TAG; \
	cvsroot=`cat CVS/Root` 2>/dev/null; \
	echo "*** Pulling off $$tag from $$cvsroot!"; \
	cd /tmp ; cvs -z3 -Q -d $$cvsroot export -r $$tag libdhcp || echo "Um... export aborted."
	@mv /tmp/libdhcp /tmp/libdhcp-$(VERSION)
	@cd /tmp ; tar --bzip2 -cSpf libdhcp-$(VERSION).tar.bz2 libdhcp-$(VERSION)
	@rm -rf /tmp/libdhcp-$(VERSION)
	@cp /tmp/libdhcp-$(VERSION).tar.bz2 .
	@rm -f /tmp/libdhcp-$(VERSION).tar.bz2
	@echo
	@echo "The final archive is in libdhcp-$(VERSION).tar.bz2"

.PHONY: all depends clean clean-depends install tar
