#!/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:
echo "Unpacking Kernel source..."
tar xf $CWD/sources/linux-2.6/linux-$VERSION.tar.bz2
cd linux-$VERSION
slackhousekeeping

# Apply RiscPC patches:
#
patch -lp0 --verbose < $CWD/sources/linux-2.6/patches/EXCLUDE/taglist-uncompress.diff
# RapIDE (IDE expansion/podule card) patch to add in driver:
patch -lp1 --verbose < $CWD/sources/linux-2.6/patches/rapide.patch-2.6.23.12 || failpatch
# Frame buffer:
patch --verbose -lp1 < $CWD/sources/linux-2.6/patches/acornfb3.diff || failpatch

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

# The IDE stuff has problems with the RiscPC, and this change
# makes the build work:
sed -i 's?-Werror-implicit-function-declaration??g' Makefile

# Bring the config file uptodate:
make oldconfig
#read -p "You can run 'make menuconfig' in another session now, then press ENTER" 
# but don't run it here because it doesn't go well with logging :)
# make menuconfig
#read -p "Do it!"

# Build Kernel and modules:
make clean
make $NUMJOBS zImage  || failmake
make $NUMJOBS 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
# Copy the Kernel into the 'kernels' directory:
# If you want to keep these, move the old one out of the way first
# otherwise it gets clobbered.
install -vpm644 arch/arm/boot/zImage $PKGSTORE/../kernels/zImage-$SLKARCH

# Make symlinks:
( cd $PKG/boot
  ln -vfs System.map-$SLKARCH-$VERSION System.map
  ln -vfs zImage-$SLKARCH-$VERSION zImage )

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

# The package description:
install -pvm644 $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

EOF
