###############################################################################
# This file is part of the IPCop Firewall.                                    #
#                                                                             #
# IPCop 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.                                         #
#                                                                             #
# IPCop 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 IPCop; if not, write to the Free Software                        #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA    #
#                                                                             #
# Makefiles are based on LFSMake, which is                                    #
# Copyright (C) 2002 Rod Roard <rod@sunsetsystems.com>                        #
#                                                                             #
# Modifications by:                                                           #
# ??-12-2003 Mark Wormgoor < mark@wormgoor.com>                               #
#          - Modified Makefile for IPCop build                                #
#                                                                             #
# $Id: linux 2875 2009-05-20 09:22:32Z owes $
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

PKG_NAME   = linux
VER        = 2.6.27
PATCHLEVEL = 2.6.27.24
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = linux-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_KERNEL)/linux/kernel/v2.6
DIR_APP    = $(DIR_SRC)/$(THISAPP)
CFLAGS     =
CXXFLAGS   =
# compilation will display VER when target will write PATCHLEVEL
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(PKG_NAME)-$(PATCHLEVEL)

###############################################################################
# Top-level Rules
###############################################################################
objects =$(DL_FILE) \
	patch-$(PATCHLEVEL).bz2

	# grsecurity-2.1.10-2.6.19.2-200701222307.patch.gz 

$(DL_FILE)						= $(DL_FROM)/$(DL_FILE)
patch-$(PATCHLEVEL).bz2					= $(DL_FROM)/patch-$(PATCHLEVEL).bz2
grsecurity-2.1.10-2.6.19.2-200701222307.patch.gz	= http://www.grsecurity.net/grsecurity-2.1.10-2.6.19.2-200701222307.patch.gz

$(DL_FILE)_MD5						= b3e78977aa79d3754cb7f8143d7ddabd
patch-$(PATCHLEVEL).bz2_MD5				= e80fcf73166ec34e54ab7c720cadc925
grsecurity-2.1.10-2.6.19.2-200701222307.patch.gz_MD5	= 718ccce29ce12064673dbe8eb4752696

install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

md5 : $(subst %,%_MD5,$(objects))

###############################################################################
# Downloading, checking, md5sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_MD5,$(objects)) :
	@$(MD5)

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(firstword $(MAKEFILE_LIST)) $(patsubst %,$(DIR_DL)/%,$(objects)) $(DIR_SRC)/config/kernel/kernel.config.$(MACHINE)
	@$(PREBUILD)
	@rm -rf $(DIR_APP) $(DIR_SRC)/linux && cd $(DIR_SRC) && tar --no-same-owner --no-same-permissions -jxf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && bzcat $(DIR_DL)/patch-$(PATCHLEVEL).bz2 | patch -p1
	# Remove patch level in EXTRAVERSION.
	# We want to avoid the need to supply a full kernel
	# (installed in a different place) if only one part could be updated
	cd $(DIR_APP) && sed -i -e 's/EXTRAVERSION\ =.*/EXTRAVERSION\ =/' Makefile

	cd $(DIR_APP) && sed -i -e 's/gettext//' scripts/kconfig/lkc.h

	# allow to build pmac driver with CONFIG_IDE=m && CONFIG_BLK_DEV_IDE=m
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_pmac-module.patch

ifeq "$(MACHINE)" "i486"
	# LEDs driver for PCEngines ALIX 2/3 series
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)-leds-alix.patch
endif

	# Openswan nat-t no more require patching the kernel

	# ATM Header fix
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/linux-2.6-atm-headers.patch

	# Grsecurity -currently disabled as we need to setup PaX
	#cd $(DIR_APP) && gzip -dc $(DIR_DL)/grsecurity-2.1.10-2.6.19.2-200701222307.patch.gz | patch -Np1
	#cd $(DIR_APP) && sed -i "s/\-grsec//" Makefile

	# Cleanup kernel source
	cd $(DIR_APP) && make mrproper
	cd $(DIR_APP) && sed -i -e 's/-O2/-Os/g' Makefile
	cp $(DIR_SRC)/config/kernel/kernel.config.$(MACHINE) $(DIR_APP)/.config
	cd $(DIR_APP) && make oldconfig
	cd $(DIR_APP) && make clean

ifeq "$(MACHINE)" "i486"
	cd $(DIR_APP) && make  -j $(PARALLELISM) all
	cd $(DIR_APP) && cp -f arch/i386/boot/bzImage /boot/vmlinuz-$(VER)
	cd $(DIR_APP) && cp -f System.map /boot/System.map-$(VER)
	ln -sf vmlinuz-$(VER) /boot/vmlinuz
	ln -sf System.map-$(VER) /boot/System.map
	cd $(DIR_APP) && make modules_install
endif

ifeq "$(MACHINE)" "alpha"
	cd $(DIR_APP) && make -j $(PARALLELISM) vmlinux
	cd $(DIR_APP) && gzip -9c vmlinux > /boot/vmlinuz-$(VER)
	cd $(DIR_APP) && cp -f System.map /boot/System.map-$(VER)
	ln -sf vmlinuz-$(VER) /boot/vmlinuz
	ln -sf System.map-$(VER) /boot/System.map
	cd $(DIR_APP) && make -j $(PARALLELISM) modules
	cd $(DIR_APP) && make modules_install
endif

ifeq "$(MACHINE)" "ppc"
	cd $(DIR_APP) && make -j $(PARALLELISM) all
	cd $(DIR_APP) && cp -f vmlinux /boot/vmlinuz-$(VER)
	cd $(DIR_APP) && cp -f vmlinux.strip.gz /boot/vmlinuz.strip.gz
	cd $(DIR_APP) && cp -f arch/powerpc/boot/zImage.coff /boot/vmlinuz.coff
	cd $(DIR_APP) && cp -f System.map /boot/System.map-$(VER)
	ln -sf vmlinuz-$(VER) /boot/vmlinuz
	ln -sf System.map-$(VER) /boot/System.map
	cd $(DIR_APP) && make modules_install
endif

ifeq "$(MACHINE)" "sparc"
	cd $(DIR_APP) && make CROSS_COMPILE=$(MACHINE_REAL)-linux- -j $(PARALLELISM) all
	cd $(DIR_APP) && cp -f vmlinux /boot/vmlinuz-$(VER)
	cd $(DIR_APP) && cp -f System.map /boot/System.map-$(VER)
	ln -sf vmlinuz-$(VER) /boot/vmlinuz
	ln -sf System.map-$(VER) /boot/System.map
	cd $(DIR_APP) && make CROSS_COMPILE=$(MACHINE_REAL)-linux- modules_install
endif

	# remove symlinked pcmcia directory
	rm -rf /lib/modules/$(VER)/pcmcia

	# Finally, force the rebuilding of the extra modules
	rm -f $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/{linux-$(KVER)-*,openswan-*}

	@$(POSTBUILD)
