#!/bin/bash

##########################################################################
# Script : kernel-2.6.SlackBuild
# Purpose: Natively build and package a Linux 2.6 Kernel for ARMedslack
# Author : Stuart Winter <mozes@armedslack.org>
# Date...: 20-Sep-2005
##########################################################################
# Changelog
############
# 20-Sep-2005 
#       * First version for Linux 2.6.13.1 on the StrongARM RiscPC
# 31-Dec-2005 
#       * Linux 2.6.14.5
##########################################################################

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract the kernel source:
tar jxf $CWD/sources/linux-2.6/linux-$VERSION.tar.bz2
cd linux-$VERSION
slackhousekeeping

# Apply RiscPC patches:
# RapIDE (IDE expansion/podule card) patch to add in driver:
patch -p1 --verbose < $CWD/sources/linux-2.6/patches/rapide.patch || failpatch
# initrd patch for RiscPC:
patch --verbose -lp1 < $CWD/sources/linux-2.6/patches/linux-2.6.13.1-arch-rpc-zImage.patch || failpatch
# Frame buffer:
patch --verbose -lp1 < $CWD/sources/linux-2.6/patches/acornfb3.diff || failpatch
# 2.6.18 header problem:
# http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=3608/1
patch --verbose -lp0 < $CWD/sources/linux-2.6/patches/2.6.18.headers || failpatch

# Install the appropriate Kernel config file:
install -m644 $CWD/configs/config-$SLKARCH-linux-2.6 .config

# Bring the config file uptodate:
make oldconfig

# Build Kernel and modules:
make clean
make zImage  || failmake
make modules || failmake

# Archive the compiled kernel source for future reference:
# This is handy for when we need the kernel source in /usr/src -- some packages
# look for it.  So on a new build box, I just extract it into /usr/src and 
# build the package.
echo "Archiving compiled source for future reference"
( cd .. && tar jcf $CWD/compiled-sources/$SLKARCH-kernel-$VERSION-compiled.tar.bz2 linux-$VERSION )

# Archive the Kernel headers in order to build the d/kernel-headers package.
echo "Archiving the Kernel includes for d/kernel-headers"
( cd include && tar jcf $CWD/../d/kernel-headers/sources/kernel-headers-$VERSION.tar.bz2 asm-generic asm asm-$PORTARCH linux )

# Install modules into the package:
make modules_install INSTALL_MOD_PATH=$PKG
# Install the modules into the a/kernel-modules source directory for use afterwards.
echo "Archiving Kernel modules for the a/kernel-modules package "
( cd $PKG && tar jcvvf $CWD/../a/kernel-modules/sources/$SLKARCH-kernel-modules-$VERSION.tar.bz2 lib/ )

# Install the kernel image and system map:
mkdir -p $PKG/{install,boot}
install -m644 System.map $PKG/boot/System.map-$SLKARCH-$VERSION
install -m644 arch/arm/boot/zImage $PKG/boot/zImage-$SLKARCH-$VERSION
# Make symlinks:
( cd $PKG/boot
  ln -fs System.map-$SLKARCH-$VERSION System.map
  ln -fs zImage-$SLKARCH-$VERSION zImage )

# The Kernel config file used (following the Slackware standard):
install -m644 .config $PKG/boot/config-$SLKARCH-$VERSION

# The package description:
install -m644 $CWD/slack-descs/$SLKARCH-slack-desc $PKG/install/slack-desc

# Build the base Kernel package (without libraries):
cd $PKG
chown -R root:root .
chmod -R og-w .
# Move the libs out of the way -- they go in a/kernel-modules package
# mv lib /tmp/$$lib
rm -rf lib # don't need these anymore since they are archived in the a/kernel-modules source directory
makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
# Now put the libs back (we need them for the a/kernel-modules package):
# mv /tmp/$$lib lib

cat << EOF
Next steps:
-----------

 [1] If we're upgrading the Kernel headers, run the 
     build script in d/kernel-headers
     and modify l/glibc's build script to use the newer
     headers package and rebuild glibc.
     ** Normally I will not do this step **

 [2] # cd ~/tmp/build-kernel_xxx
     # cd linux*/arch/arm/boot
     # cp -fv zImage /mnt/somepath
     Copy that Kernel into a working !SlackBt and test both
     the installer and booting an installed OS.
      
 [3] If that checks out:
      cd bootware/riscos/grub.  
      - modify the Kernel version number of appropriate
      - Run build script.
      - Then follow the instructions to test the new stuff.

 [4] Kernel source package:
        - Rebuild the k/ kernel-source package.
        ./kernel-2.6-sourcepackage.SlackBuild     

***  [5] Update Kernel modules: ***********
cd ../../a/kernel-modules
Update Kernel version in 'arm/build'
./arm/build
./versatile

EOF
