######################################################################
# Makefile                                                 August 2005
#
# ASYM: An implementation of Asymetric Cryptography in the Linux Kernel
# 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.
#
######################################################################

include ../Makefile.base

SUBDIRS =

ASYM_KCFLAGS	= $(KCFLAGS) -I../include/kssl

KOBJ_DIR	= .kobj
UOBJ_DIR	= .uobj

COMMON_SRCS	= base64.c base64.h \
		  elgamal.c elgamal.h \
		  pk.c pk.h \
		  pk_test.c pk_test.h\
		  rsa.c rsa.h \
		  unsx.c unsx.h unsx_e.c unsx_e.h \
		  test_util.c test_util.h \
		  base64_test.c base64_test.h

.PHONY: all install modules_install unintall modules_uninstall clean dist

ASYM		= asym.o
ASYM_OBJS	= $(KOBJ_DIR)/base64.o $(KOBJ_DIR)/elgamal.o \
		  $(KOBJ_DIR)/pk.o $(KOBJ_DIR)/rsa.o $(KOBJ_DIR)/unsx.o \
		  $(KOBJ_DIR)/core.o $(KOBJ_DIR)/intmath.o \
		  $(KOBJ_DIR)/test_util.o
ASYM_SRCS	= core.c intmath.c

ASYM_K_TEST	 = asym_k_test.o
ASYM_K_TEST_OBJS = $(KOBJ_DIR)/asym_k_test.o $(KOBJ_DIR)/base64_test.o \
                   $(KOBJ_DIR)/pk_test.o
ASYM_K_TEST_SRCS = asym_k_test.c

ASYM_U		= asym_u
ASYM_U_OBJS	= $(UOBJ_DIR)/base64.o $(UOBJ_DIR)/elgamal.o \
		  $(UOBJ_DIR)/pk.o $(UOBJ_DIR)/rsa.o $(UOBJ_DIR)/unsx.o \
                  $(UOBJ_DIR)/pk_test.o \
		  $(UOBJ_DIR)/asym_u.o $(UOBJ_DIR)/test_util.o \
		  $(UOBJ_DIR)/base64_test.o
ASYM_U_SRCS	= asym_u.c compat.h

DISTFILE	= Makefile $(COMMON_SRCS) $(ASYM_SRCS) $(ASYM_K_TEST_SRCS) \
		  $(ASYM_U_SRCS)

all:		$(ASYM_U) $(ASYM) $(ASYM_K_TEST)
		@for i in $(SUBDIRS); do $(MAKE) -C $$i all || exit 1; done

$(ASYM):	$(KOBJ_DIR)/ $(ASYM_OBJS)
		$(LD) -r $(ASYM_OBJS) -o $@
		@echo "#########################################"
		@echo "# Kernel Module rebuilt: asym.o"
		@echo "#########################################"

$(ASYM_K_TEST):	$(UOBJ_DIR)/ $(ASYM_K_TEST_OBJS)
		$(LD) -r $(ASYM_K_TEST_OBJS) -o $@
		@echo "#########################################"
		@echo "# Kernel Module rebuilt: asym_k_test.o"
		@echo "#########################################"

$(ASYM_U):	$(UOBJ_DIR)/ $(ASYM_U_OBJS)
		$(LINK.o) $(ASYM_U_OBJS) -o $@
		@echo "#########################################"
		@echo "# User Space programme rebuilt: asym_u"
		@echo "#########################################"

$(UOBJ_DIR)/:	
		mkdir $(UOBJ_DIR)

$(UOBJ_DIR)/%.o: %.c
		$(COMPILE.c) -DVERSION=\"$(VERSION)\"  $^ -o $@

$(KOBJ_DIR)/:	
		mkdir $(KOBJ_DIR)

$(KOBJ_DIR)/%.o: %.c
		$(COMPILE.c) $(ASYM_KCFLAGS) -DVERSION=\"$(VERSION)\"  $^ -o $@

install:	modules_install
		@for i in $(SUBDIRS); do $(MAKE) -C $$i install || exit 1; done

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

uninstall:	modules_uninstall
		@for i in $(SUBDIRS); do $(MAKE) -C $$i uninstall || exit 1; \
			done

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

clean:
		rm -f *.o *~ *.bak *.orig *.rej $(ASYM_U)
		rm -rf $(KOBJ_DIR) $(UOBJ_DIR)
		@for i in $(SUBDIRS); do $(MAKE) -C $$i clean || exit 1; done

distclean:	clean

dist:
		mkdir $(DISTDIR)/
		@for i in $(SUBDIRS); do \
			$(MAKE) DISTDIR="../$(DISTDIR)/$$i" -C $$i dist \
				|| exit 1; done
		install -m 644 $(DISTFILE) $(DISTDIR)	
