###############################################################################
# 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 3800 2009-11-13 13:21:01Z owes $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = linux
VER        = 2.6.27
PATCHLEVEL = 2.6.27.39
IPCOPKRELEASE = 2
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)-$(IPCOPKRELEASE)
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				= 9ce07344e2d9e5fe77ca474e8f5bd83a
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)/$(THISAPP) && cd $(DIR_SRC) && tar --no-same-owner --no-same-permissions -jxf $(DIR_DL)/$(DL_FILE)
	mv $(DIR_SRC)/$(THISAPP) $(DIR_APP)
	cd $(DIR_APP) && bzcat $(DIR_DL)/patch-$(PATCHLEVEL).bz2 | patch -p1
	# Remove patch level in EXTRAVERSION.
	cd $(DIR_APP) && sed -i -e 's/EXTRAVERSION\ =.*/EXTRAVERSION\ =/' Makefile
	# And add our own versioning
	cd $(DIR_APP) && echo "-$(IPCOPKRELEASE)" > localversion

	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
	# add lzma compression (x86 only, patch 4 is for arm only)
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_lzma1.patch
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_lzma2.patch
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_lzma3.patch
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_lzma5.patch
endif

	# Add USB ID 0x0fe6:0x8101 to dm9601 driver
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_dm9601.patch

	# 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-$(KVER)
	cd $(DIR_APP) && cp -f System.map /boot/System.map-$(KVER)
	ln -sf vmlinuz-$(KVER) /boot/vmlinuz
	ln -sf System.map-$(KVER) /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-$(KVER)
	cd $(DIR_APP) && cp -f System.map /boot/System.map-$(KVER)
	ln -sf vmlinuz-$(KVER) /boot/vmlinuz
	ln -sf System.map-$(KVER) /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-$(KVER)
	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-$(KVER)
	ln -sf vmlinuz-$(KVER) /boot/vmlinuz
	ln -sf System.map-$(KVER) /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-$(KVER)
	cd $(DIR_APP) && cp -f System.map /boot/System.map-$(KVER)
	ln -sf vmlinuz-$(KVER) /boot/vmlinuz
	ln -sf System.map-$(KVER) /boot/System.map
	cd $(DIR_APP) && make CROSS_COMPILE=$(MACHINE_REAL)-linux- modules_install
endif

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

	# compress modules to save space on disk
	find /lib/modules/$(KVER)/ -name '*.ko' -a -type f -exec gzip -nf9 {} \;

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

	@$(POSTBUILD)
