#!/bin/sh -ef
# (C) Mikhail Yakshin <greycat@altlinux.org>, 2007
# (C) Vitaly Lipatov <lav@etersoft.ru>, 2004-2005
#
# Usage: update-kernel [flavour]
# Updates kernel of current flavour (std-up, std26-up, etc.) to newest
# version (with all installed modules). You can set flavour of kernel as
# first argument of the script.

[ -n "$COMFORT_DIR" ] || COMFORT_DIR=/usr/share/comfort
. "$COMFORT_DIR/functions"

Verbose 'Updating kernel-image with all installed modules...'

# Get kernel flavour from running kernel
FLAVOUR=${1:-`uname -r | cut -d "-" -f2,3`}

# Find max version of rpm from std input list
function find_max()
{
	read M && \
	while read R; do
		test -z $R && continue
		res=`rpmvercmp $M $R`
		test $res -eq -1 && M=$R
	done
	echo $M
}

INSTALL="apt-get install -y"

echo "Try to install newest kernel image '$FLAVOUR' type"
NEWESTKERNEL=`$INSTALL kernel-image-$FLAVOUR | grep "\#" | grep -v "\["`
#echo $NEWKERNEL
NEWESTKERNEL=`echo $NEWESTKERNEL | cut -d " " -f 1 | find_max`
if [ -n "$NEWESTKERNEL" ] ; then
	echo "Install new kernel $NEWESTKERNEL"
	$INSTALL $NEWESTKERNEL
fi

Verbose 'Check for newest installed kernel...'
NEWESTKERNEL=`rpm -qa kernel-image-${FLAVOUR}* | find_max`
INSTVER=`rpm -q $NEWESTKERNEL --queryformat "%{VERSION}-$FLAVOUR-%{RELEASE}"`

Verbose "Updating modules for kernel: $INSTVER ..."
ALLMODULES=`rpm -qa "kernel-modules*$FLAVOUR*" | sed -e "s/-$FLAVOUR.*$//g" | sort -u`

for I in $ALLMODULES; do echo $I-$INSTVER; done | xargs $INSTALL
