######################################################################
# Makefile                                                 August 2005
#
# TCPS: TCP Splicing Module
# Copyright (C) 2005  NTT COMWARE Corporation.
#
# This file based in part on code from LVS www.linuxvirtualserver.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty 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.
#
#
# Changes:
#     Hirotaka Sasaki <hiro1967@mti.biglobe.ne.jp>:
#         - Supported Linux-2.6.
#
#####################################################################

CC = gcc
LD = ld
#EXTRA_CFLAGS = -DTCPS_INFO_USE_JHASH
EXTRA_CFLAGS =
PREFIX =

KERNELDIR = /lib/modules/$(shell uname -r)/build
KERNELVER = $(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')
DISTDIR = $(PREFIX)/lib/modules/$(shell uname -r)/kernel/net/ipv4/tcps
DEB_VERFILE = /usr/src/kernel-headers-$(shell expr `uname -r` : '\(2\.[46]\.[0-9]\+-[0-9]\+\)-.*')/kernel-headers.revision

MODNAME = tcps
MODSRCS = conn.c core.c info.c
MODOBJS = $(MODSRCS:.c=.o)
MODINCS = tcps.h tcps_compat.h

targets =

# Check if linux-2.4.x or linux-2.6.x or other
ifeq ($(KERNELVER), 2.4)
	MODFILE = $(MODNAME).o
	targets += $(MODFILE)
else
	MODFILE = $(MODNAME).ko
	targets += $(MODFILE)
endif

# Check if debian3.1 kernel or other
ifneq ("$(shell cat $(DEB_VERFILE) 2>/dev/null | grep sarge)", "")
	EXTRA_CFLAGS += -DDEBIAN_3_1_LINUX_KERNEL=1
endif

ifdef DEBUG
	ifeq ("$(origin DEBUG)", "command line")
		EXTRA_CFLAGS += -DTCPS_DEBUG
	endif
endif

all: $(targets)

######################
# Make tcps module
######################
ifeq ($(KERNELVER), 2.4)
# make for linux-2.4.x

CFLAGS= -D__KERNEL__ -DMODULE -DEXPORT_SYMTAB -DMODVERSIONS \
	$(EXTRA_CFLAGS) -O -Wall \
	-Wstrict-prototypes -I$(KERNELDIR)/include \
	-include $(KERNELDIR)/include/linux/modversions.h

$(MODNAME).o: $(MODOBJS)
	$(LD) -r -o $@ $(MODOBJS)

%.o: %.c
	$(CC) $(CFLAGS) -c $<

$(MODOBJ): $(MODINCS)

else
# make for linux-2.6.x

obj-m := $(MODNAME).o
$(MODNAME)-objs := $(MODOBJS)

$(MODNAME).ko: $(MODSRCS) $(MODINCS)
	make -C $(KERNELDIR) M=$(shell pwd) V=1 modules

endif

######################
# Install tcps module
######################
install: $(MODFILE)
	@if [ ! -d $(DISTDIR) ]; then mkdir -p $(DISTDIR); fi
	install -m644 $(MODFILE) $(DISTDIR)
	@if [ "$(PREFIX)x" = "x" ]; then depmod -a; fi

######################
# Make test tools
######################
tcpstest: tcpstest.o
	$(CC) -o $@ tcpstest.o

tcpstest.o: tcpstest.c
	$(CC) -c tcpstest.c

nosplicetest: nosplicetest.o
	$(CC) -o $@ nosplicetest.o

nosplicetest.o: nosplicetest.c
	$(CC) -c nosplicetest.c

######################
# Other targets
######################
TAGS:
	etags $(MODSRCS) $(MODINCS)

clean:
	-rm -rf $(targets) *.o *.ko .*.d .*.cmd *.mod.c .tmp* *~
	-rm -f TAGS

.PHONEY:
	all clean install TAGS
