# FreeS/WAN master makefile
# Copyright (C) 1998, 1999  Henry Spencer.
# 
# 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
# 
# 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.
#
# RCSID $Id: Makefile,v 1.65 1999/04/11 01:50:48 henry Exp $

# public and private command directories
PUBDIR=/usr/local/sbin
PRIVDIR=/usr/local/lib/ipsec
MANTREE=/usr/local/man

# kernel location, and location of kernel patches in the distribution
KERNELSRC=/usr/src/linux
KCFILE=$(KERNELSRC)/.config
PATCHES=klips/patches
# note, some of the patches know the last part of this path
KERNELKLIPS=$(KERNELSRC)/net/ipsec

# install stuff
INSTALL=install

SHELL=/bin/sh



def:
	@echo "Please read INSTALL before running make"
	@false



# everything that's necessary to put Klips into the kernel
insert:	patches klink device klipsdefaults

# For this ln -s to work with the Linux kernel build stuff, the last 
# components in the local path of the Klips source MUST be the same as
# KERNELKLIPS less the KERNELSRC prefix.  Which is why the Klips source
# now has to be in klips/net/ipsec rather than klips/src.  How annoying.
klink:
	rm -rf $(KERNELKLIPS)
	ln -s `pwd`/klips/net/ipsec $(KERNELKLIPS)

PATCHER=utils/patcher
patches:
	@$(PATCHER) $(PATCHES)/Documentation.Configure.help \
		$(KERNELSRC) Documentation/Configure.help 'IPSEC'
	@$(PATCHER) $(PATCHES)/drivers.isdn.isdn_net.c \
		$(KERNELSRC) drivers/isdn/isdn_net.c 'IPPROTO_ESP'
	@$(PATCHER) $(PATCHES)/drivers.net.Space.c \
		$(KERNELSRC) drivers/net/Space.c 'ipsec'
	@$(PATCHER) $(PATCHES)/include.linux.in.h \
		$(KERNELSRC) include/linux/in.h 'IPPROTO_ESP'
	@$(PATCHER) $(PATCHES)/include.linux.proc_fs.h \
		$(KERNELSRC) include/linux/proc_fs.h 'PROC_NET_IPSEC'
	@$(PATCHER) $(PATCHES)/net.Config.in \
		$(KERNELSRC) net/Config.in 'ipsec'
	@$(PATCHER) $(PATCHES)/net.Makefile \
		$(KERNELSRC) net/Makefile 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.ipv4.af_inet.c \
		$(KERNELSRC) net/ipv4/af_inet.c 'ipsec'
	@$(PATCHER) $(PATCHES)/net.ipv4.protocol.c \
		$(KERNELSRC) net/ipv4/protocol.c 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.netlink.c \
		$(KERNELSRC) net/netlink.c 'open_map&=~.1<<unit.'
	@$(PATCHER) $(PATCHES)/net.netsyms.c \
		$(KERNELSRC) net/netsyms.c 'ip_fragment'

device:
	@if test ! -e /dev/ipsec ; \
	then set -x ; \
	  mknod -m 0200 /dev/ipsec c 36 10 ; \
	fi

klipsdefaults:
	@KERNELDEFCONFIG=$(KERNELSRC)/arch/$(HOSTTYPE)/defconfig ; \
	if ! egrep -q 'CONFIG_IPSEC' $$KERNELDEFCONFIG ; \
	then set -x ; \
	  cp -a $$KERNELDEFCONFIG $$KERNELDEFCONFIG.orig ; \
	  cat $(KERNELKLIPS)/defconfig >>$$KERNELDEFCONFIG ; \
	fi ; \
	if test -w $(KCFILE) ; \
	then KERNELCONFIG=$(KCFILE) ; \
	  if ! egrep -q 'CONFIG_IPSEC' $$KERNELCONFIG ; \
	  then set -x ; \
	    cp -a $$KERNELCONFIG $$KERNELCONFIG.orig ; \
	    cat $(KERNELKLIPS)/defconfig >>$$KERNELCONFIG ; \
	  fi ; \
	fi



# programs

programs:
	cd lib ; $(MAKE)
	cd pluto ; $(MAKE)
	cd klips/utils ; $(MAKE)
	cd utils ; $(MAKE)

SETTINGS=BINDIR=$(PRIVDIR) PUBDIR=$(PUBDIR) PRIVDIR=$(PRIVDIR) \
	MANTREE=$(MANTREE) INSTALL="$(INSTALL)"
install:
	mkdir -p $(PRIVDIR) $(PUBDIR)
	cd lib ; $(MAKE) install $(SETTINGS)
	cd klips/utils ; $(MAKE) install $(SETTINGS)
	cd pluto ; $(MAKE) install $(SETTINGS)
	cd utils ; $(MAKE) install $(SETTINGS)

clean:
	cd lib ; $(MAKE) clean		# looks after gmp and libdes too
	cd klips/utils ; $(MAKE) clean
	cd pluto ; $(MAKE) clean
	cd utils ; $(MAKE) clean



# proxies for major kernel make operations

# do-everything entries
ogo:	insert config     confcheck programs install kernel
menugo:	insert menuconfig confcheck programs install kernel
xgo:	insert xconfig    confcheck programs install kernel

# configuring
xconfig:
	cd $(KERNELSRC) ; $(MAKE) xconfig
menuconfig:
	cd $(KERNELSRC) ; $(MAKE) menuconfig
config:
	cd $(KERNELSRC) ; $(MAKE) config
confcheck:
	@if test ! -f $(KCFILE) ; \
	then echo '*** no kernel configuration file written!!' ; exit 1 ; \
	fi
	@if ! egrep -q '^CONFIG_IPSEC=[my]' $(KCFILE) ; \
	then echo '*** IPSEC not in kernel configuration!!' ; exit 1 ; \
	fi
	@if egrep -q '^CONFIG_IPSEC=m' $(KCFILE) && \
		! egrep -q '^CONFIG_MODULES=y' $(KCFILE) ; \
	then echo '*** IPSEC configured as module in kernel with no module support!!' ; exit 1 ; \
	fi

# kernel building, with error checks
kernel:
	rm -f out.kbuild out.kinstall
	( cd $(KERNELSRC) ; $(MAKE) dep clean zImage ) 2>&1 | tee out.kbuild
	@if egrep -q '^CONFIG_MODULES=y' $(KCFILE) ; \
	then set -x ; \
		( cd $(KERNELSRC) ; \
		$(MAKE) modules 2>&1 ) | tee -a out.kbuild ; \
	fi
	utils/errcheck out.kbuild

# kernel install, with error checks
kinstall:
	rm -f out.kinstall
	( cd $(KERNELSRC) ; $(MAKE) install ) 2>&1 | tee out.kinstall
	@if egrep -q '^CONFIG_MODULES=y' $(KCFILE) ; \
	then set -x ; \
		( cd $(KERNELSRC) ; \
		$(MAKE) modules_install 2>&1 ) | tee -a out.kinstall ; \
	fi
	utils/errcheck out.kinstall



# some oddities meant for the developers, probably of no use to users

# make developer version of symlinks
dlinks:
	cd .. ; top/dtrmklinks -d
