#!/bin/bash
## src2pkg script for: 	kernel-modules
## Auto-generated by src2pkg-1.9.6
## src2pkg Copyright 2005-2008 Gilbert Ashley <amigo@ibilio.org>

# KNAME  for standard kernel-modules is simply 'modules
# KNAME  gets appended to the name of the package (like 'kernel-$KNAME ')
# in order to avoid that installpkg or upgradepkg overwrite your existing kernel(s),
# you should give this kernel-modules build a unique KNAME like 'my_modules'
KNAME='combo-mine'

# if you want to build a kernel for architecture other than ix86, change this
# the most likely alternatives are: ia64, ppc or x86_64
# this needs to be set so that the script can find the finished bzImage file 
# under $SRC_DIR/arch/$KARCH/boot/bzImage
KARCH='i386'

# edit to change this to the correct kernel version
SOURCE_NAME='linux-2.6.23.tar.bz2'
# uncomment and edit this to reset the default VERSION (needed if using kernel version patches)
# ALT_VERSION=2.6.23.6

# your package build number
BUILD='1'

# you shouldn't need to change anything below ALT_NAME and ALT_VERSION
# reset the default package name(which would be 'linux' in this case)
ALT_NAME="kernel-$KNAME"

# Get the functions and configs
. /usr/libexec/src2pkg/FUNCTIONS ;

# do_all_processes can substitute these 16 steps:

pre_process
find_source
make_dirs
unpack_source
fix_source_perms

#configure_source
#compile_source
#fake_install

if [[ -r $CWD/config-$KNAME-$VERSION ]] ; then
 cp $CWD/config-$KNAME-$VERSION $SRC_DIR/.config
elif [[ -r $CWD/config-$KNAME  ]] ; then
 cp $CWD/config-$KNAME  $SRC_DIR/.config
elif [[ -r $CWD/config ]] ; then
 cp $CWD/config $SRC_DIR/.config
elif [[ -r $CWD/config* ]] ; then
 cp $CWD/config* $SRC_DIR/.config
else
 echo ""
 echo $RED"Error!"$NORMAL" No kernel configuration file found!"
 echo "To build the kernel-modules, place a copy of your"
 echo "kernel-modules configuration file in the current directory"
 echo "and name it config-$KNAME-$VERSION, config-$KNAME  or 'config'."
 echo "Exiting..."
 exit 0
fi

# update the config file
cd $SRC_DIR && make oldconfig 

# build the kernel
cd $SRC_DIR && make bzImage
STATUS=$? 
if [[ $STATUS = 0 ]] ; then
 mkdir -p $PKG_DIR/boot
  cp $SRC_DIR/arch/$KARCH/boot/bzImage $PKG_DIR/boot/vmlinuz-$KNAME-$VERSION
  cp $SRC_DIR/System.map $PKG_DIR/boot/System.map-$KNAME-$VERSION
  cp $SRC_DIR/.config $PKG_DIR/boot/config-$KNAME-$VERSION
else
 echo "Kernel failed to compile cleanly! Exiting..."
 exit 0
fi

# build the modules
# this method has the advantage of not needing to be root to
# install the modules and to create the *.map files
cd $SRC_DIR && make modules
STATUS=$?
if [[ $STATUS = 0 ]] ; then
 mkdir -p $PKG_DIR/lib/modules/$VERSION
 cd $SRC_DIR
 export INSTALL_MOD_PATH=$PKG_DIR
 make _modinst_
 if [[ -x /sbin/depmod ]] ; then
  /sbin/depmod -b $PKG_DIR $VERSION
 fi
else
 echo "Kernel-modules failed to compile cleanly! Exiting..."
 exit 0
fi

# Remove erroneous links(build & source) in /lib/modules
rm -f $PKG_DIR/lib/modules/$VERSION/source $PKG_DIR/lib/modules/$VERSION/build

if ! [[ -e $CWD/slack-desc ]] ; then
cat > $CWD/slack-desc <<EOF
$NAME: kernel-$KNAME-$VERSION (linux kernel-modules)
$NAME:
$NAME: This package contains the linux kernel and kernel-modules. See the file 
$NAME: /boot/config-$KNAME-$VERSION for the details of this build.
$NAME: A kernel module is a piece of object code that can be dynamically
$NAME: loaded into the Linux kernel to provide new kernel functions.  Most of
$NAME: these modules provide support for devices such as CD-ROM drives, tape
$NAME: drives, and ethernet cards.  You can choose which modules to load by
$NAME: editing /etc/rc.d/rc.modules-$VERSION.
$NAME:
$NAME: Packaged by src2pkg

EOF
fi

# This code is straight from Pat Volkerding's SlackBuild
# src2pkg has code that will do nearly the same automatically
# but this is included for best compatibility with init scripts
mkdir -p $PKG_DIR/etc/rc.d
cat $CWD/rc.modules.new > $PKG_DIR/etc/rc.d/rc.modules-${VERSION}.new
chmod 755 $PKG_DIR/etc/rc.d/rc.modules-${VERSION}.new

# Write out the doinst.sh:
cat << EOF > $CWD/doinst.prepend
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/rc.d/rc.modules-${VERSION}.new

# If rc.modules is a real file, back it up:
if [ -r etc/rc.d/rc.modules -a ! -L etc/rc.d/rc.modules ]; then
  cp -a etc/rc.d/rc.modules etc/rc.d/rc.modules.bak 
fi

# A good idea whenever kernel modules are added or changed:
if [ -x sbin/depmod ]; then
  chroot . /sbin/depmod -a ${VERSION} 1> /dev/null 2> /dev/null
fi

EOF
# end code by PatV

fix_pkg_perms
strip_bins
create_docs
compress_man_pages
make_description
make_doinst
make_package
post_process

# src2pkg - Copyright 2005-2008  Gilbert Ashley <amigo@ibiblio.org>
## See the documentation for more help and examples. Below are some of
# the most common Extras and Options for easy cut-and-paste use.
# EXTRA_CONFIGS='' PRE_FIX='' DOCLIST=''
# MAKE_COMMAND='' INSTALL_LINE='' 
# SHELL_INSTALL='YES' CORRECT_PERMS='NO'
# When editing src2pkg scripts to add custom code, use these variables
# to refer to the current directory, the sources or the package tree:
# $CWD (current directory), $SRC_DIR (sources), $PKG_DIR (package tree)
