#!/bin/sh
TMP=/tmp
CWD=`pwd`
PKG=$TMP/package-kernel
PKGMOD=$TMP/package-kernel-modules

ARCH=${ARCH:-sparc}

# We need to know the kernelname, the version, and where it is!
if [ "$#" != "4" ]; then
	echo	""
	echo	"Usage:"
	echo	"	$0 (kernname) (kernelver) (build) (kerneltreedir)"
	echo	"This will create a package from the already-built kernel"
	echo	"at kerneltreedir. The package will take the name kernname"
	echo	"and the version from kernelver. You should also be sure"
	echo	"that you have created a proper slack-desc-kernelver."
	echo	""
	echo	"try: $0 sun4dm_smp 2.4.32 1 /usr/src/somekernel"
	echo	""
	echo	"note: We don't handle the case very well where the extraversion"
	echo	"is missing a leading '-'..."
	echo	""
	exit
fi

KERNNAME=$1
VERSION=$2
BUILD=$3
WHEREDIR=$4


KERNEL=$WHEREDIR/vmlinux
SYSMAP=$WHEREDIR/System.map
CONFIG=$WHEREDIR/.config
SLKDESC=$CWD/slack-desc-$VERSION
SLKMODDESC=$CWD/slack-modules-desc-$VERSION
MODDIR=/lib/modules/$VERSION-$KERNNAME

# find all the stuff we need
if [ ! -r $KERNEL ]; then
	echo "Can't find built kernel in $WHEREDIR!"
	exit
fi

if [ ! -r $SYSMAP ]; then
	echo "Can't find System.map for built kernel in $WHEREDIR!"
	exit
fi

if [ ! -r $CONFIG ]; then
	echo "Can't find the .config for the built kernel in $WHEREDIR!"
	exit
fi

if [ ! -r $SLKDESC ]; then
	echo "Can't find $SLKDESC!"
	exit
fi
if [ ! -r $SLKMODDESC ]; then
	echo "Can't find $SLKMODDESC!"
	exit
fi

if [ ! -d $MODDIR ]; then
	echo "Modules for $KERNEL do not appear to be installed to $MODDIR!"
	exit
fi

cat << EOF

Building kernel-$VERSION-$KERNNAME-$ARCH-$BUILD.tgz
using these source files.  Please check and then hit 
enter to make the package.

KERNEL  = $KERNEL
SYSMAP  = $SYSMAP
CONFIG  = $CONFIG
SLKDESC = $SLKDESC
MODDIR  = $MODDIR

EOF
read junk;

#################################
# kernel package build start
#################################
rm -rf $PKG
mkdir -p $PKG
mkdir -p $PKG/boot
cp $KERNEL $PKG/boot/vmlinux-$VERSION-$KERNNAME
# rdev doesn't make sense on sparc
#rdev -R $PKG/boot/vmlinuz-$KERNNAME-$VERSION 0
#rdev -v $PKG/boot/vmlinuz-$KERNNAME-$VERSION -1
#rdev -r $PKG/boot/vmlinuz-$KERNNAME-$VERSION 0
( cd $PKG/boot ; ln -sf vmlinux-$VERSION-$KERNNAME vmlinux )
cp $SYSMAP $PKG/boot/System.map-$VERSION-$KERNNAME
( cd $PKG/boot ; ln -sf System.map-$VERSION-$KERNNAME System.map )
cp $CONFIG $PKG/boot/config-$VERSION-$KERNNAME
( cd $PKG/boot ; ln -sf config-$VERSION-$KERNNAME config )
mkdir -p $PKG/install
cat $SLKDESC > $PKG/install/slack-desc

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

##################################
# modules package build start
##################################
rm -rf $PKGMOD
mkdir -p $PKGMOD/lib/modules
cp -a $MODDIR $PKGMOD/lib/modules
mkdir -p $PKGMOD/etc/rc.d
cat $CWD/rc.modules.new > $PKGMOD/etc/rc.d/rc.modules.new
chmod 755 $PKGMOD/etc/rc.d/rc.modules.new
mkdir -p $PKGMOD/install
cat $SLKMODDESC > $PKGMOD/install/slack-desc

# Write out the doinst.sh:
cat << EOF > $PKGMOD/install/doinst.sh
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.new

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

EOF

cd $PKGMOD
makepkg -l y -c n $TMP/kernel-modules-$VERSION-$KERNNAME-$ARCH-$BUILD.tgz


# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
	rm -rf $PKG
	rm -rf $PKGMOD
fi
