#!/bin/sh

# TODO:
# 1. Integrate with a kernel-everything.Slackbuild ?

# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-kernel-source

VERSION=2.4.33pre2
ARCH=${ARCH:-noarch}
BUILD=${BUILD:-0}

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG
cd $PKG

# Make the path to the sources
mkdir -p usr/src

# Grab the source from our TMP.
# Sub-TODO:
# 1.  check that stuff exists
# 2.  make sure it's been compiled at least once
# 3.  make sure it's been for sparc64
#
( echo
  echo "Copying sources from $TMP ..."
  echo "-----"
  sleep 1
  cd $PKG/usr/src
  cp -av $TMP/linux-$VERSION .
  ln -s linux-$VERSION linux
)

# Make clean, just in case we missed it.
( echo
  echo "Making 'clean'"
  echo "------"
  sleep 1
  cd $PKG/usr/src/linux-$VERSION
  make clean
)

# Make a little cleaner, in case we're using patches
( cd $PKG/usr/src
  find linux-$VERSION -name *.orig -exec rm {} \;
  rm -f linux-$VERSION/.??*
  rm -f linux-$VERSION/Makefile.dist
)

# Fix permissions
( echo
  echo "Fixing permissions"
  echo "-----"
  sleep 1
  cd $PKG/usr/src/linux-$VERSION
  chown -R root:root .
  find . -perm 664 | xargs chmod 644
  find . -perm 775 | xargs chmod 755
)

# Add a default .config for SPARC64
cat $CWD/sun4u.config > $PKG/usr/src/linux-$VERSION/.config

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/kernel-source-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/kernel-source-$VERSION
  rm -rf $PKG
fi
