# 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.

# 
# Default target
#

default:: driver
exes:: driver

# Get the variables definition
include $(ZK_ROOT)/freedom/client/makefiles/Makefile.vars

# Override and define new variables
DRV_CC = $(CC)
DRV_CFLAGS = $(ZK_DEFS) $(ZK_INCS) 
DRV_BINDIR= ../drv_binaries

#uname -r | sed -e 's/\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)-\?\([0-9]\+\)\?//'

ifneq (,$(findstring linux,$(ZK_ARCH)))
   ZK_ARCHSRCDIR = linux
   DRV_LD  = $(LD)
   DRV_OUT = clientshim.o
   DRV_OSR = $(shell uname -s)$(shell uname -r | sed -e 's/-.*//' )# -e 's/\.\(.*\)\..*$$/.\1/' )
   DRV_CPU = $(shell uname -m | sed 's/i[456]86/i386/' )
   DRV_PATCH_LEVEL = $(shell  uname -r | sed -e 's/[0-9\.]*-\?\([0-9]\)*.*/\1/' )
#   DRV_CFLAGS += -fno-strength-reduce -fomit-frame-pointer
   DRV_CFLAGS += -DCLIENT_SHIM -DKERNEL -D__KERNEL__ -DMODULE -DEXPORT_SYMTAB -I/usr/src/linux/include 
#-DCACHE !!! THIS IS BROKEN DO NOT TURN ON !!!
ifneq ($(strip $(DRV_PATCH_LEVEL)),)
   DRV_CFLAGS += -DKERNEL_PATCH_LEVEL=$(DRV_PATCH_LEVEL)
endif
   ZK_SRC_ALLFILES_linux := zkshim_drv.c
   PKG_CONF :=
endif

ifneq (,$(findstring gcc,$(DRV_CC)))
   DRV_CFLAGS +=  -O2 -g -fno-strict-aliasing -fno-omit-frame-pointer -malign-loops=2 -malign-functions=2 -malign-functions=2 
else
   DRV_CFLAGS += -v -xO3
endif

ZK_INCS += -I$(ZK_ARCHSRCDIR)

DRV_OUT_ARCH = $(DRV_OUT)-$(DRV_OSR)-$(DRV_CPU)

ifneq ($(strip $(DEBUG_LEVEL)),)
   DRV_CFLAGS += -DZKSHIM_DEBUG=$(DEBUG_LEVEL)
endif

ALL_SRCFILES := \
	$(ZK_SRC_ALLFILES_$(ZK_ARCHSRCDIR)) \
	zkshim_dgram_cache.c \
	zkshim_frag.c \
	zkshim_rules.c \
	zkshim_tcp_cache.c \
	$(END_OF_LIST)

DRV_INCS = $(wildcard *.h $(ZK_ARCHSRCDIR)/*.h ../include/*.h)

ZK_PUBLIC_INCLUDES := \
	zkshim.h \
	$(END_OF_LIST)

DRV_OBJS := $(addprefix $(ZK_BUILDDIR_PROJ)/drv/,$(ALL_SRCFILES:.c=.o))

PKG_MAN= zkshim.7d zkshim-internals.7d

# Get the target rules
include $(ZK_ROOT)/freedom/client/makefiles/Makefile.targs


# Add new targets or complement default target using :: rules
driver: $(ZK_BUILDDIR_PUBBIN)/$(DRV_OUT)

#ksyms.csum:
#	cat /proc/ksyms | grep -v '\[' | sum | awk -- '{print $$1}' > $(ZK_BUILDDIR_PUBBIN)/ksyms.csum

package:: default 
	@if [ ! -d "$(ZK_PKG_CLIENT_ROOT)/drv" ]; then mkdir -p "$(ZK_PKG_CLIENT_ROOT)/drv"; fi
	@if [ ! -d "$(ZK_ROOT)/kernel" ]; then $(ZK_SCRIPTDIR)/promoteFiles.pl $(ZK_BUILDDIR_PUBBIN) $(ZK_PKG_CLIENT_ROOT)/usr/share/freedom/kernel $(DRV_OUT); fi
#ifeq (,$(FREEDOM_PRODUCTION_BUILD))
#ifneq (,$(DRV_BINDIR))
#	#[ -d $(DRV_BINDIR) ] && ( cd $(DRV_BINDIR) && find . -type f -print | grep -v '^\.\/CVS' | cpio -pu $(ZK_PKG_CLIENT_ROOT)/drv )
#endif
#endif
#	@$(ZK_SCRIPTDIR)/promoteFiles.pl $(ZK_BUILDDIR_PUBBIN) $(ZK_PKG_CLIENT_ROOT)/usr/share/freedom $(DRV_OUT)
#	@$(ZK_SCRIPTDIR)/promoteFiles.pl $(ZK_BUILDDIR_PUBBIN) $(ZK_PKG_CLIENT_ROOT)/usr/share/freedom ksyms.csum

ifneq (,$(PKG_CONF))
	#@$(ZK_SCRIPTDIR)/promoteFiles.pl . $(ZK_PKG_CLIENT_ROOT)/drv $(PKG_CONF)
endif
ifneq (,$(FREEDOM_PRODUCTION_BUILD))
ifneq (,$(DRV_BINDIR))
	[ -d $(DRV_BINDIR) ] && ( cd $(DRV_BINDIR) && find . -type f -print | grep -v '^\.\/CVS' | cpio -pu $(ZK_PKG_CLIENT_ROOT)/drv )
endif
endif

$(ZK_BUILDDIR_PUBBIN)/$(DRV_OUT): $(DRV_OBJS)
	$(DRV_LD) -r -o $@ $^
#	rm -f $(ZK_BUILDDIR_PUBBIN)/ksyms.csum

$(DRV_OBJS): $(ZK_BUILDDIR_PROJ)/drv/%.o: %.c $(DRV_INCS)
	@-if [ ! -d "$(@D)" ]; then mkdir -p "$(@D)"; fi
	$(DRV_CC) $(DRV_CFLAGS) -c $< -o $@

install: uninstall install_modules
	mknod --mode=777 /dev/clientshim c 10 194

install_modules:
	insmod  $(ZK_BUILDDIR_PUBBIN)/clientshim.o

uninstall:
	modprobe -r  $(ZK_BUILDDIR_PUBBIN)/clientshim
	rm -vf /dev/clientshim

clean::
	-rm -f $(ZK_BUILDDIR_PUBBIN)/$(DRV_OUT)
	-rm -f $(ZK_BUILDDIR_PROJ)/drv/*.o

.PHONY: driver ksyms.csum uninstall install_modules

