#!/bin/sh

  RPM_SOURCE_DIR="/home/shashank/rpmbuild/SOURCES"
  RPM_BUILD_DIR="/home/shashank/rpmbuild/BUILD"
  RPM_OPT_FLAGS="-O2 -g -pipe -m32 -march=i686 -mtune=pentium4"
  RPM_ARCH="i386"
  RPM_OS="linux"
  export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS
  RPM_DOC_DIR="/usr/share/doc"
  export RPM_DOC_DIR
  RPM_PACKAGE_NAME="kernel"
  RPM_PACKAGE_VERSION="2.6.10"
  RPM_PACKAGE_RELEASE="1.1121_FC4.shashank"
  export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
  RPM_BUILD_ROOT="/var/tmp/kernel-2.6.10-1.1121_FC4.shashank-root"
  export RPM_BUILD_ROOT
  
  
  set -x
  umask 022
  cd /home/shashank/rpmbuild/BUILD
LANG=C
export LANG
unset DISPLAY


cd /home/shashank/rpmbuild/BUILD
rm -rf kernel-2.6.10
/bin/mkdir -p kernel-2.6.10
cd kernel-2.6.10
/usr/bin/bzip2 -dc /home/shashank/rpmbuild/SOURCES/linux-2.6.10.tar.bz2 | tar -xf -
STATUS=$?
if [ $STATUS -ne 0 ]; then
  exit $STATUS
fi
[ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .
[ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .
/bin/chmod -Rf a+rX,u+w,g-w,o-w .
cd linux-2.6.10

#
# Patches 0 through 100 are meant for core subsystem upgrades
# 
echo "Patch #1 (patch-2.6.11-rc2.bz2):"
/usr/bin/bzip2 -d < /home/shashank/rpmbuild/SOURCES/patch-2.6.11-rc2.bz2 | patch -p1  -s
STATUS=$?
if [ $STATUS -ne 0 ]; then
  exit $STATUS
fi
echo "Patch #2 (patch-2.6.11-rc2-bk10.bz2):"
/usr/bin/bzip2 -d < /home/shashank/rpmbuild/SOURCES/patch-2.6.11-rc2-bk10.bz2 | patch -p1  -s
STATUS=$?
if [ $STATUS -ne 0 ]; then
  exit $STATUS
fi

#
# Patches to back out
#

#
# Architecture patches
#

#
# x86(-64)
#
# Flexible mmap patch for x86-64
#%patch210 -p1
# Fix wrong TASK_SIZE on x86_64 for 32bit processes
#%patch211 -p1
# Compile 686 kernels tuned for Pentium4.
echo "Patch #212 (linux-2.6.10-x86-tune-p4.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.10-x86-tune-p4.patch


# 
# ppc64
#

# Patch for Kconfig and Makefile build issues
echo "Patch #300 (linux-2.6.2-ppc64-build.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.2-ppc64-build.patch
echo "Patch #301 (linux-2.6.8-ppc64-netboot.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.8-ppc64-netboot.patch

#
# ia64
#

# Basic build fixes
echo "Patch #330 (linux-2.6.3-ia64-build.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.3-ia64-build.patch


#
# s390
#

# Basic build fixes
echo "Patch #350 (linux-2.6.1-s390-compile.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.1-s390-compile.patch
# Auto raidstart for S390
echo "Patch #351 (linux-2.6.9-s390-autoraid.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-s390-autoraid.patch
# Recover after aborted nameserver request.
echo "Patch #353 (linux-2.6.9-s390-zfcp_port-fix.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-s390-zfcp_port-fix.patch
# Support broadcast on z800/z900 HiperSockets
echo "Patch #360 (linux-2.6.9-s390-qeth_hipersocket-fix.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-s390-qeth_hipersocket-fix.patch

#
# sparc/sparc64
#
echo "Patch #400 (linux-2.6.3-sparc-addbzimage.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.3-sparc-addbzimage.patch

#
# Xen
#
echo "Patch #450 (linux-2.6.10-xen.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.10-xen.patch
echo "Patch #452 (linux-2.6.9-xen-compile.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-xen-compile.patch
echo "Patch #453 (linux-2.6.9-xen-agpgart.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-xen-agpgart.patch

#
# Patches 500 through 1000 are reserved for bugfixes to the core system
# and patches related to how RPMs are build
#


# This patch adds a "make nonint_oldconfig" which is non-interactive and
# also gives a list of missing options at the end. Useful for automated
# builds (as used in the buildsystem).
echo "Patch #500 (linux-2.4.0-nonintconfig.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.4.0-nonintconfig.patch


#
# The execshield patch series, broken into smaller pieces
#
# 1) Exec shield core
echo "Patch #511 (linux-2.6.0-exec-shield.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.0-exec-shield.patch

# 2) Option to printk fatal signals, useful for debugging
echo "Patch #512 (linux-2.6.8-print-fatal-signals.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.8-print-fatal-signals.patch

# 3) The Execshield VA rearrangements
echo "Patch #513 (linux-2.6.8-execshield-vaspace.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.8-execshield-vaspace.patch

# 4) Fixes to make Xen work with Execshield
echo "Patch #514 (linux-2.6.8-execshield-xen.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.8-execshield-xen.patch

#
# Patch that adds a __must_check attribute for functions for which checking
# the return value is mantadory (eg copy_from_user)
#
echo "Patch #530 (linux-2.6.0-must_check.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.0-must_check.patch

#
# GPG signed kernel modules
#
echo "Patch #600 (linux-2.6.7-modsign-core.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.7-modsign-core.patch
echo "Patch #601 (linux-2.6.7-modsign-crypto.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.7-modsign-crypto.patch
echo "Patch #602 (linux-2.6.7-modsign-ksign.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.7-modsign-ksign.patch
echo "Patch #603 (linux-2.6.7-modsign-mpilib.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.7-modsign-mpilib.patch
echo "Patch #604 (linux-2.6.7-modsign-script.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.7-modsign-script.patch
echo "Patch #605 (linux-2.6.7-modsign-include.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.7-modsign-include.patch
echo "Patch #606 (linux-2.6.9-sha1.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-sha1.patch


#
# Patches 1000 to 5000 are reserved for bugfixes to drivers and filesystems
#


# add vidfail capability; 
# without this patch specifying a framebuffer on the kernel prompt would
# make the boot stop if there's no supported framebuffer device; this is bad
# for the installer cd that wants to automatically fall back to textmode
# in that case
echo "Patch #1000 (linux-2.4.0-test11-vidfail.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.4.0-test11-vidfail.patch

# Periodically scan slab caches for corruption.
echo "Patch #1010 (linux-2.6.10-periodic-slab-debug.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.10-periodic-slab-debug.patch

#
# Fix the extreme stack usage in some kernel functions
#
echo "Patch #1020 (linux-2.6.4-stackusage.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.4-stackusage.patch

#
# Make /dev/mem a need-to-know function 
#
echo "Patch #1050 (linux-2.6.7-devmem.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.7-devmem.patch
echo "Patch #1051 (linux-2.6.0-devmem.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.0-devmem.patch
echo "Patch #1052 (linux-2.6.8-devmem-xen.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.8-devmem-xen.patch


#
# /dev/crash driver for the crashdump analysis tool
#
echo "Patch #1060 (linux-2.6.3-crash-driver.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.3-crash-driver.patch

#
# Most^WAll users of sleep_on are broken; fix a bunch
#
echo "Patch #1070 (linux-2.6.0-sleepon.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.0-sleepon.patch

#
# SCSI Bits.
#
# Additions to the SCSI whitelist to make card readers work.
echo "Patch #1120 (linux-2.6.10-scsi-whitelist.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.10-scsi-whitelist.patch
# Enable both new and old megaraid drivers.
echo "Patch #1136 (linux-2.6.9-scsi-megaraid-legacy.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-scsi-megaraid-legacy.patch

#
# Various upstream NFS/NFSD fixes.
#
echo "Patch #1201 (linux-2.6.9-NFSD-non-null-getxattr.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-NFSD-non-null-getxattr.patch
echo "Patch #1207 (linux-2.6.8-lockd-racewarn2.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.8-lockd-racewarn2.patch
echo "Patch #1209 (linux-2.6.9-lockd-block-nosleep.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-lockd-block-nosleep.patch
echo "Patch #1210 (linux-2.6.9-lockd-reclaims.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-lockd-reclaims.patch
#%patch1211 -p1

# NIC driver fixes.
# Use correct spinlock functions in token ring net code
echo "Patch #1351 (linux-2.6.9-net-tr-irqlock-fix.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-net-tr-irqlock-fix.patch
# Workaround for the E1000 erratum 23
#%patch1358 -p1
# Workaround E1000 post-maturely writing back to TX descriptors
#%patch1359 -p1
# E1000 64k-alignment fix
#%patch1361 -p1
# NULL out ptrs in airo after kfree'ing them.
echo "Patch #1362 (linux-2.6.9-net-airo-nullptr.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-net-airo-nullptr.patch

# netdump bits
echo "Patch #1500 (linux-2.6.10-crashdump-common.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.10-crashdump-common.patch
echo "Patch #1501 (linux-2.6.10-netdump.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.10-netdump.patch
echo "Patch #1502 (linux-2.6.8-netconsole.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.8-netconsole.patch
echo "Patch #1503 (linux-2.6.10-diskdump.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.10-diskdump.patch

#
# Various SELinux fixes from 2.6.10rc
#

# Misc fixes
# Add missing MODULE_VERSION tags to some modules.
echo "Patch #1760 (linux-2.6.9-module_version.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-module_version.patch
# Make spinlock debugging panic instead of continue.
echo "Patch #1910 (linux-2.6.9-spinlock-debug-panic.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-spinlock-debug-panic.patch

#
# VM related fixes.
#
# Extra debugging on OOM Kill.
echo "Patch #2003 (linux-2.6.9-vm-oomkiller-debugging.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-vm-oomkiller-debugging.patch

# IDE bits.
# Handle early EOF on CD's.
echo "Patch #2101 (linux-2.6.9-ide-cd-early-EOF.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-ide-cd-early-EOF.patch

#
# Local hack (off for any shipped kernels) to printk all files opened 
# the first 180 seconds after boot for debugging userspace startup 
# speeds
#
# %patch2999 -p1

#
# External drivers that are about to get accepted upstream
#

# Intel wireless
echo "Patch #3020 (linux-2.6.9-ipw2100.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-ipw2100.patch
echo "Patch #3021 (linux-2.6.9-ipw2200.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-ipw2200.patch
echo "Patch #3022 (linux-2.6.9-ieee80211.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.9-ieee80211.patch

#
# Patches 5000 to 6000 are reserved for new drivers that are about to
# be merged upstream
#


#
# final stuff
#

#
# misc small stuff to make things compile or otherwise improve performance
#
echo "Patch #10000 (linux-2.6.0-compile.patch):"
patch -p1  -s < /home/shashank/rpmbuild/SOURCES/linux-2.6.0-compile.patch


# END OF PATCH APPLICATIONS

cp /home/shashank/rpmbuild/SOURCES/COPYING.modules Documentation/

mkdir configs

cp -fv $RPM_SOURCE_DIR/kernel-2.6.10-i?86*.config .


# now run oldconfig over all the config files
for i in *.config 
do 
	mv $i .config 
	Arch=`head -1 .config | cut -b 3-`
	make ARCH=$Arch nonint_oldconfig > /dev/null
	echo "# $Arch" > configs/$i
	cat .config >> configs/$i 
done

# make sure the kernel has the sublevel we know it has. This looks weird
# but for -pre and -rc versions we need it since we only want to use
# the higher version when the final kernel is released.
perl -p -i -e "s/^SUBLEVEL.*/SUBLEVEL = 10/" Makefile
perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -prep/" Makefile

# get rid of unwanted files resulting from patch fuzz
find . -name "*.orig" -exec rm -fv {} \;
find . -name "*~" -exec rm -fv {} \;

###
### build
###

exit 0