# 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.72 1999/07/09 16:34:02 rgb Exp $

# public and private command directories
# Beware, other things know PRIVDIR, and define PATH settings which are
# assumed to include PUBDIR.
PUBDIR=/usr/local/sbin
PRIVDIR=/usr/local/lib/ipsec
# where manpages go
MANTREE=/usr/local/man
# where boot/shutdown scripts go (first one that exists gets it)
RCDIR1=/etc/rc.d/init.d
RCDIR2=/etc/init.d

# kernel location, and location of kernel patches in the distribution
KERNELSRC=/usr/src/linux
DIRIN22=$(KERNELSRC)/net/netlink
KERNELREL=$(shell test -d $(DIRIN22) && echo 2.2)
KCFILE=$(KERNELSRC)/.config
PATCHES=klips/patches$(KERNELREL)
# note, some of the patches know the last part of this path
KERNELKLIPS=$(KERNELSRC)/net/ipsec

# kernel make name:  zImage for 2.0.xx, bzImage for 2.2.xx
B=$(shell test -d $(DIRIN22) && echo b)
KERNEL=$(B)zImage

# 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: _patches$(KERNELREL)

# Linux-2.0.x version
_patches:
	@$(PATCHER) $(PATCHES)/Documentation.Configure.help \
		$(KERNELSRC) Documentation/Configure.help 'CONFIG_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 'CONFIG_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 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.Makefile \
		$(KERNELSRC) net/Makefile 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.ipv4.af_inet.c \
		$(KERNELSRC) net/ipv4/af_inet.c 'CONFIG_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'
	@$(PATCHER) $(PATCHES)/include.linux.socket.h \
		$(KERNELSRC) include/linux/socket.h 'PF_KEY'

# Linux-2.2.x version
_patches2.2:
	@$(PATCHER) $(PATCHES)/Documentation.Configure.help \
		$(KERNELSRC) Documentation/Configure.help 'CONFIG_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 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/include.linux.in.h \
		$(KERNELSRC) include/linux/in.h 'IPPROTO_ESP'
	@$(PATCHER) $(PATCHES)/include.linux.netlink.h \
		$(KERNELSRC) include/linux/netlink.h 'NETLINK_IPSEC'
	@$(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 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.Makefile \
		$(KERNELSRC) net/Makefile 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.ipv4.af_inet.c \
		$(KERNELSRC) net/ipv4/af_inet.c 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.ipv4.protocol.c \
		$(KERNELSRC) net/ipv4/protocol.c 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.netsyms.c \
		$(KERNELSRC) net/netsyms.c 'CONFIG_IPSEC'
	@$(PATCHER) $(PATCHES)/net.netlink.af_netlink.c \
		$(KERNELSRC) net/netlink/af_netlink.c 'netlink_open_map'
	@$(PATCHER) $(PATCHES)/net.netlink.netlink_dev.c \
		$(KERNELSRC) net/netlink/netlink_dev.c 'netlink_open_map'
	@$(PATCHER) $(PATCHES)/include.linux.socket.h \
		$(KERNELSRC) include/linux/socket.h 'define.AF_KEY'

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

ifeq ($(HOSTTYPE),)
HOSTTYPE := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
endif

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 ; \
	if test -d $(RCDIR1) ; \
	then $(MAKE) install $(SETTINGS) RCDIR=$(RCDIR1) ; \
	else $(MAKE) install $(SETTINGS) RCDIR=$(RCDIR2) ; \
	fi

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 $(KERNEL) ) 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
