#!/bin/sh
# Vitaly Lipatov <lav@etersoft.ru>, 2004
#  : .  <cetus@newmail.ru>, 2005-2007
#     /
#      
#     

#    " " 
#  --, 
#    updates,    . 

#   ,  
#     
#  , :

# -  : alt10, 2.4.11, 2.6.12-alt11 etc
# -  : std-up, om-smp etc.

#  ,     
#     . 

# -  -y   apt-get 

echo "Update kernel with modules: Vitaly Lipatov <lav@etersoft.ru>, 2004-2005"

y=
#   (     )
TYPECUR=`uname -r | cut -d "-" -f2,3`
TYPESYS=$TYPECUR
RELEASE=.
IMAGES=`apt-cache pkgnames kernel-image | sed -e "s/kernel-image-//g" | sed -e "s/#.*$//" | sort | uniq`
while [ -n "$1" ]; do
	if [ "$1" = "-y" ]; then
		y=-y
	elif echo $IMAGES | grep $1 &>/dev/null; then
		TYPESYS=$1
	else
		RELEASE=$1
	fi
	shift
done

#  root
SUDO=sudo
if [ "$UID" == "0" ]; then
	SUDO=
fi

#   . , !

# " "   "--
# " (   ?) 
#      . 

# " "    -
# alt6   alt12.  2.6.9  2.6.87.
#   ,  ,  sort 
#  ͣ   .

#      .
#     --   .

# :  ,   ...

echo Current kernel is `uname -r`
echo "Try to select newest kernel image '$TYPESYS' type"
LISTKER=`apt-cache pkgnames kernel-image-$TYPESYS | grep $RELEASE | grep "\#" | tail -n 1`
if [ -z "$LISTKER" ]; then
	echo Kernel not found! 
	exit
fi
echo "Try to install new kernel $LISTKER and update its modules? [y]/n"
read
case "$REPLY" in
	(n|no|N|No|NO|0) exit;;
	(*) ;;
esac
$SUDO apt-get install $LISTKER $y
NEWESTKERNEL=`echo $LISTKER | sed -e "s/#/-/g"`

#    .     
#   .     
# .       ,
#       . 
#        ,  
#      .

INSTVER=`rpm -q $NEWESTKERNEL --queryformat "%{VERSION}-$TYPESYS-%{RELEASE}"`
echo "Updates modules for kernel: $INSTVER ..."
ALLMODULES=`apt-cache pkgnames kernel-modules | sed -e "s/kernel-modules-//g" | sed -e "s/-.*$//g" | sort | uniq | grep -v kernel`
for i in $ALLMODULES; do
	PKGNAME=kernel-modules-$i
	if rpm -q $PKGNAME-$TYPECUR &>/dev/null; then
		echo kernel-modules-$i is installed, try updating...
		$SUDO apt-get install $PKGNAME-$INSTVER $y
		echo $?
	fi
done

