#!/bin/bash

#####################################################
# Script : kernel-sourcepackage.SlackBuild
# Purpose: Build a source package of the Linux Kernel
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 27-May-2006
#####################################################

source /usr/share/slackdev/buildkit.sh

# Package information:
export PKGNAM=kernel-source
# If it's an -rc release, remove the hyphen and rename
# the source archive.  It makes packaging and scripting all play happily.
#export VERSION=${VERSION:-3.2.0rc7}
#
# A stable release:
# With Linux 3.x, it always has a 3 digit number - e.g. 3.2.0 rather than just 3.2:
#export VERSION=${VERSION:-4.3.5}
export VERSION=${VERSION:-4.4.14}

# The architecture name component of the .t?z package file name:
# This used to be "noarch" but it's a package of the kernel source tree that
# has been compiled for ARM, so I believe it should be tagged by architecture.
# Let this be set by the defaults.  Can be overridden below if need be in the future.
#export PKGARCH=${PKGARCH:-arm}
export BUILD=${BUILD:-2}
export PKGSERIES=${PKGSERIES:-k}
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

## ******************************************************************* ##
# For test packages - best to store in another location rather than
# overwriting the working copy in the main tree:
# export PKGSTORE=/tmp/
# mkdir -vpm755 $PKGSTORE/$PKGSERIES
## ******************************************************************* ##

CWD=$PWD
TMP=/tmp/package-kernel-source
rm -rf $TMP
mkdir -pm755 $TMP/usr/src

echo "*****************************************************************"
echo "Building source package slackware/k/$SLACKPACKAGE"
echo "*****************************************************************"

# Extract the previously compiled "armv5" kernel source.
# We use the "armv5" kernels because they're (at the time of writing - May 2010)
# are the most widely used hardware platform for Slackware ARM.
cd $TMP/usr/src
echo " + Extracting compiled source"
tar xf $CWD/compiled-sources/armv5-kernel-$VERSION-compiled.tar
# Ensure that the linux dir name matches the $VERSION of the package so that
# in the a/kernel-modules package, the /lib/modules/*/{build,source}
# symlinks are correct.
mv -fv linux-* linux-$VERSION
ln -vfs linux-$VERSION linux
cd linux || exit 1

# Prepare:
echo " + Cleaning up the source tree"
make clean
make prepare
#rm -fv .version
find . -name '.gitignore' -print0 | xargs -0 rm -rf
find . -name '.tmp*' -print0 | xargs -0 rm -rf

# Install package description.  This modified from the Slackware version
# because we need to mention the additional 'armedslack' directory.
cd $TMP
mkdir -vm755 install
install -vpm644 $CWD/slack-descs/kernel-source install/slack-desc

echo " + Setting sane file ownerships and permissions"
start_fakeroot
chown -R root:root .
chmod -R og-w .

# Replace version number with a _ so it doesn't get confused with
# the package name.
# This is incase we're using any '-rc' releases.
export VERSION="$( echo $VERSION | sed 's?-??g' )"
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

echo " + Building Slackware package"

slackmp # run makepkg
