#!/bin/sh
#
#     Filename: build_linux26.sh
#      Created: 08-Aug-2010
#       Author: Sedat Dilek <sedat.dilek@gmail.com>
# Last Changed: 11-Aug-2010
#
# URL: http://wiki.debian.org/HowToRebuildAnOfficialDebianKernelPackage
# Details see "Building only a single kernel variant"
#

##### IMPORTANT NOTES #####

# [1] linux-kbuild-2.6
# You need linux-kbuild-2.6 fitting to your Linux-kernel (linux-headers dependency).
# HELP:
# http://wiki.debian.org/HowToRebuildAnOfficialDebianKernelPackage -> "The story of linux-kbuild-2.6"

# [2] Debian Official Kernel Configuration files
# http://merkel.debian.org/~jurij/

##### VARIABLES #####

# Set language environment variables.
export LANG=C
export LC_ALL=C

# Set ccache via kernel-buildsystem from debian-kernel team.
#export CC="ccache gcc"

# Speed-Up build by using ccache.
USE_CCACHE="yes"

# Set number of parallel-make-jobs.
# HINT:
# "Sane" (risk of build or even hardware failure) settings calculated via ((Nr_Of_Cores * 2) + 1).
# For a single-core CPU like Intel Pentium-M "Banias" that would be "3".
# "5" as value was sucessfully tested here (no stagnant system).
PARALLEL_MAKE_JOBS="5"

# Set one single kernel variant explicitly.
# NOTE:
# The build of flavours like 486 or featuresets like xen are not wanted.
ARCH="i386"
FEATURESET="none"
FLAVOUR="686"
SINGLE_KERNEL_VARIANT="${ARCH}_${FEATURESET}_${FLAVOUR}"

# Version of Linux-kernel.
VERSION="2.6.35"

##### PREPARATION #####

# Download the pruned means DFSG-ified Linux tarball (Thanks benh).
wget http://people.debian.org/~benh/linux-2.6_${VERSION}.orig.tar.gz

# Unpack the Linux tarball.
tar -xf linux-2.6_${VERSION}.orig.tar.gz

# Enter build-directory.
cd linux-2.6-${VERSION}

# Checkout debian-directory from linux-2.6 SVN trunk repository.
svn co svn://svn.debian.org/svn/kernel/dists/trunk/linux-2.6/debian

# Create missing control and rules.gen files.
fakeroot make -f debian/rules debian/control-real

# Check if kernel-buildsystem works fine.
# EXAMPLES:
# 1. Do all patches apply? 2. Is ccache used? 3. Name of uploader changed (dch)? etc.
# HINT:
# As i386 has diverse flavours/featuresets, you can check their setups via 'fakeroot debian/rules setup'.
fakeroot make -f debian/rules.gen setup_${SINGLE_KERNEL_VARIANT}

##### START BUILD #####

# Start build: Set number of parallel-make-jobs and/or ccache to speed-up build.
# NOTE:
# Append to line DEBIAN_KERNEL_JOBS=${PARALLEL_MAKE_JOBS} and/or DEBIAN_KERNEL_USE_CCACHE=${USE_CCACHE}.
# ALTERNATIVE:
# Set values permanently into debian/rules.defs (preferred by author).
echo "DEBIAN_KERNEL_JOBS=5" >> debian/rules.defs
# It looks like ccache slows down the build here, so disable the usage.
#echo "DEBIAN_KERNEL_USE_CCACHE=yes" >> debian/rules.defs
# PATCH:
# Create-missing-linux-headers-common-package.patch is required.
fakeroot make -f debian/rules.gen binary-arch_${SINGLE_KERNEL_VARIANT}

##### MISSING PACKAGES #####

# Create linux-base Debian package (required by linux-image).
fakeroot make -f debian/rules.real install-linux-base

# Create firmware-linux-free Debian package.
fakeroot make -f debian/rules.real install-firmware
