###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# 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 3 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, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

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

include Config

PATCHLEVEL = .17
VER        = 2.6.25.17

THISAPP    = linux-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
CFLAGS     =
CXXFLAGS   =

# Normal build or SMP build.
#
ifeq "$(SMP)" "1"
	TARGET = $(DIR_INFO)/linux-$(VER)-smp
else
	TARGET = $(DIR_INFO)/linux-$(VER)
endif


###############################################################################
# Top-level Rules
###############################################################################
objects =$(DL_FILE) \
	mISDN-1_1_8.tar.gz \
	squashfs3.4.tar.gz \
	netfilter-layer7-v2.20.tar.gz \
	patch-2.6.16-nath323-1.3.bz2 \
	reiser4-for-2.6.25.patch.bz2
	
$(DL_FILE)				= $(URL_IPFIRE)/$(DL_FILE)
netfilter-layer7-v2.20.tar.gz		= $(URL_IPFIRE)/netfilter-layer7-v2.20.tar.gz
patch-2.6.16-nath323-1.3.bz2		= $(URL_IPFIRE)/patch-2.6.16-nath323-1.3.bz2
squashfs3.4.tar.gz			= $(URL_IPFIRE)/squashfs3.4.tar.gz
mISDN-1_1_8.tar.gz			= $(URL_IPFIRE)/mISDN-1_1_8.tar.gz
reiser4-for-2.6.25.patch.bz2		= $(URL_IPFIRE)/reiser4-for-2.6.25.patch.bz2

$(DL_FILE)_MD5				= 30618bff93fd4fd048e20a9a6aab8e5d
netfilter-layer7-v2.20.tar.gz_MD5	= 525a821c973b730c8feaf9eedb57d1e4
patch-2.6.16-nath323-1.3.bz2_MD5	= f926409ff703a307baf54b57ab75d138
squashfs3.4.tar.gz_MD5			= 2a4d2995ad5aa6840c95a95ffa6b1da6
mISDN-1_1_8.tar.gz_MD5	          	= 29c55ffe0b35668a41eaed3b1c3ddfe2
reiser4-for-2.6.25.patch.bz2_MD5	= 4294bbcb39e5ee859b94b09ff4fb1e0c

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) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) $(DIR_SRC)/linux $(DIR_SRC)/xen-* && cd $(DIR_SRC) && tar jxf $(DIR_DL)/$(DL_FILE)
	ln -s linux-$(VER) /usr/src/linux

	# Fix crash on Twinhead H12Y notebooks
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/linux-h12y-mmio-quirks.patch

	# Openswan
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/openswan-2.4.x.kernel-2.6.23-natt.patch

	# Reiser4
	cd $(DIR_APP) && bzip2 -d -c $(DIR_DL)/reiser4-for-2.6.25.patch.bz2 | patch -Np1
	
	# SquashFS
	cd $(DIR_SRC) && rm -rf squashfs*
	cd $(DIR_SRC) && tar xfz $(DIR_DL)/squashfs3.4.tar.gz
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/squashfs3.4/kernel-patches/linux-2.6.26/squashfs3.4-patch

	# ip_conntrack permissions from 440 to 444
#	cd $(DIR_APP) && patch -Np0 < $(DIR_SRC)/src/patches/ip_conntrack_standalone-patch-for-ipfire.patch
	
	# Layer7-patch
	cd $(DIR_SRC) && rm -rf $(DIR_SRC)/netfilter-layer7-v2.20
	cd $(DIR_SRC) && tar xzf $(DIR_DL)/netfilter-layer7-v2.20.tar.gz
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch

	# Linux Intermediate Queueing Device
ifeq "$(XEN)" ""
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/linux-2.6.25-imq5.diff
endif

	# mISDN
	cd $(DIR_SRC) && rm -rf mISDN-*
	cd $(DIR_SRC) && tar xfz $(DIR_DL)/mISDN-1_1_8.tar.gz
	cd $(DIR_SRC)/mISDN-1_1_8 && yes 'yes' | ./std2kern -k /usr/src/linux
	
	cd $(DIR_APP)/drivers/isdn/hardware/mISDN && patch -Np0 < $(DIR_SRC)/src/patches/mISDN-avmfritz.patch

	# Cleanup kernel source
	cd $(DIR_APP) && make mrproper

ifeq "$(SMP)" "1"
	cp $(DIR_SRC)/config/kernel/kernel.config.$(MACHINE).smp $(DIR_APP)/.config
else
	cp $(DIR_SRC)/config/kernel/kernel.config.$(MACHINE) $(DIR_APP)/.config
endif

	cd $(DIR_APP) && make CC="$(KGCC)" oldconfig
	cd $(DIR_APP) && make CC="$(KGCC)" clean

ifeq "$(SMP)" "1"
	cd $(DIR_APP) && sed -i -e 's/EXTRAVERSION\ =.*/EXTRAVERSION\ =\ $(PATCHLEVEL)-ipfire-smp/' Makefile
	cd $(DIR_APP) && make $(MAKETUNING) CC="$(KGCC)" bzImage
	cd $(DIR_APP) && cp -v arch/i386/boot/bzImage /boot/vmlinuz-$(VER)-ipfire-smp
	cd $(DIR_APP) && cp -v System.map /boot/System.map-$(VER)-ipfire-smp
	ln -sf vmlinuz-$(VER)-ipfire-smp /boot/vmlinuz-ipfire-smp
	cd $(DIR_APP) && make CC="$(KGCC)" $(MAKETUNING) modules
	cd $(DIR_APP) && make CC="$(KGCC)" $(MAKETUNING) modules_install
else
	cd $(DIR_APP) && sed -i -e 's/EXTRAVERSION\ =.*/EXTRAVERSION\ =\ $(PATCHLEVEL)-ipfire/' Makefile
	cd $(DIR_APP) && make $(MAKETUNING) CC="$(KGCC)" bzImage
	cd $(DIR_APP) && cp -v arch/i386/boot/bzImage /boot/vmlinuz-$(VER)-ipfire
	cd $(DIR_APP) && cp -v System.map /boot/System.map-$(VER)-ipfire
	cd $(DIR_APP) && cp -v .config /boot/config-$(VER)-ipfire
	ln -sf vmlinuz-$(VER)-ipfire /boot/vmlinuz-ipfire
	ln -sf System.map-$(VER)-ipfire /boot/System.map-ipfire
	cd $(DIR_APP) && make CC="$(KGCC)" $(MAKETUNING) modules
	cd $(DIR_APP) && make CC="$(KGCC)" $(MAKETUNING) modules_install
endif

ifeq "$(SMP)" ""
	# Only do this once on the non-SMP pass	
	cd $(DIR_APP) && install -m 755 usr/gen_init_cpio /sbin/
endif 

	@rm -rf $(DIR_SRC)/patch-o-matic* $(DIR_SRC)/iptables* $(DIR_SRC)/squashfs* $(DIR_SRC)/mISDN-* $(DIR_SRC)/netfilter-layer7-*
	@$(POSTBUILD)
