# ipvs: IP Virtual Server Module for the NetFilter framework

# uncomment the following line for debugging
DEBUGFLAGS	= -DCONFIG_IP_VS_DEBUG -g

# need to specify the path of kernel source
KERNELSOURCE	= /usr/src/linux-2.5.50


KERNELRELEASE	= $(shell echo -e "\#include <linux/version.h>\nUTS_RELEASE" \
		    > conftest.c &&					     \
		    gcc -E -I$(KERNELSOURCE)/include conftest.c | tail -1    \
		    | cut -d '"' -f 2 && rm -f conftest.c)

KERNELSYMS	= /boot/System.map-$(KERNELRELEASE)

ARCH		= $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \
		  -e s/arm.*/arm/ -e s/sa110/arm/)

i386_CFLAGS	= -I$(KERNELSOURCE)/arch/i386/mach-generic
ARCH_CFLAGS	= $($(ARCH)_CFLAGS)


comma		:= ,
MODNAME		= $(subst $(comma),_,$(subst -,_,$(*F)))
BASENAME_FLAGS	= -DKBUILD_BASENAME=$(subst $(comma),_,$(subst -,_,$(*F)))


# As per the Linux Kernel's Makefile:
# INSTALL_MOD_PATH specifies a prefix to MODLIB for module directory
# relocations required by build roots.  This is not defined in the
# makefile but the arguement can be passed to make if needed.

MODLIB		= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)/kernel/

CC		= gcc
CFLAGS		= -D__KERNEL__ -DMODULE -DEXPORT_SYMTAB -DMODVERSIONS	\
		  $(DEBUGFLAGS) -O2 -Wall -Wstrict-prototypes		\
		  -I$(KERNELSOURCE)/include $(ARCH_CFLAGS) $(BASENAME_FLAGS)

CORE		= ip_vs.o
CORE_OBJS	= ip_vs_conn.o ip_vs_core.o ip_vs_ctl.o ip_vs_sched.o	   \
		  ip_vs_xmit.o ip_vs_timer.o ip_vs_app.o ip_vs_sync.o	   \
		  ip_vs_est.o ip_vs_proto.o ip_vs_proto_tcp.o		   \
		  ip_vs_proto_udp.o ip_vs_proto_icmp.o ip_vs_proto_ahesp.o
SCHEDULERS	= ip_vs_lc.o ip_vs_rr.o ip_vs_wlc.o ip_vs_wrr.o		\
		  ip_vs_lblc.o ip_vs_lblcr.o ip_vs_dh.o ip_vs_sh.o
APPMODS		= ip_vs_ftp.o


all:		$(CORE) $(SCHEDULERS) $(APPMODS)

ip_vs.o:	$(CORE_OBJS)
		$(LD) -r $^ -o $@

$(CORE_OBJS):	%.o : %.c
		$(CC) $(CFLAGS) -DKBUILD_MODNAME=ip_vs -c -o $@ $<

$(SCHEDULERS):	%.o : %.c
		$(CC) $(CFLAGS) -DKBUILD_MODNAME=$(MODNAME) -c -o $@ $<

$(APPMODS):	%.o : %.c
		$(CC) $(CFLAGS) -DKBUILD_MODNAME=$(MODNAME) -c -o $@ $<

install:	ip_vs.o
		-rmmod ip_vs
		insmod ip_vs.o
		lsmod

modules_install:
		if [ ! -d "$(MODLIB)" ]; then mkdir -p "$(MODLIB)"; fi
		install -m 600 -c $(CORE) $(SCHEDULERS) $(APPMODS) "$(MODLIB)"
		depmod -ae -F $(KERNELSYMS) -b $(INSTALL_MOD_PATH) $(KERNELRELEASE)

modules_uninstall:
		(cd "$(MODLIB)"; rm -f $(CORE) $(SCHEDULERS) $(APPMODS))
		rmdir "$(MODLIB)"

clean:
		rm -f *.o *~ *.bak *.orig *.rej

distclean:	clean
		make -C ipvsadm distclean
		rm -f TAGS tags
