#!/bin/bash
#######################################
#
# upgrade-tools.sh
# by xeon <xeon[AT]sysroot[DOT]eu>
#
# Auto-upgrade vmware-tools and system
# for the currently running kernel.
# Mainly for Trustix system (both 2.2, 3.0).
# Tested also for SMP machines.
#
# Bugs and ideas welcome.
#
#######################################

KERNEL=`uname -r`
KERNELRELEASE=`echo "$KERNEL" | sed -e "s/smp//"`
SMPMACHINE=false
if [ "`echo $KERNEL | grep smp`" != "" ]; then
	SMPMACHINE=true
fi

echo -e "upgrade-tools-1.5.sh\nby xeon <xeon[AT]sysroot[DOT]eu>\n"

if [ ! -d /usr/src/kernel-source-$KERNELRELEASE ]; then
	echo -e "Cannot find kernel source directory (/usr/src/kernel-source-$KERNELRELEASE).\nYou need to install kernel-source (swup --install kernel-source) to configure vmware-tools (I know, it's a bit difficult without network cards...)"
	exit
fi

echo -n "- Stopping network service..."
service network stop > /dev/null 2>&1
echo "done"

echo -n "- Configuring current kernel ($KERNEL)..."
cd /usr/src
rm -f linux
ln -sf kernel-source-$KERNELRELEASE linux
cd linux
make mrproper > /dev/null 2>&1
cp /boot/config-$KERNEL .config
make oldconfig > /dev/null 2>&1
case $KERNEL in
	(2.4*)
	make dep > /dev/null 2>&1
	;;
	(2.6*)
	make modules_prepare > /dev/null 2>&1
	;;
esac

if [ "$SMPMACHINE" != "" ]; then
	echo "`cat /usr/src/linux/include/linux/version.h | sed -e s/$KERNELRELEASE/$KERNEL/1`" > /usr/src/linux/include/linux/version.h
fi
echo "done"


echo -n "- Configuring vmware-tools..."
echo -e "\n\r\n\r" | vmware-config-tools.pl > /dev/null 2>&1
chkconfig vmware-tools on
echo "done"


LIST=`rpm -qa | grep kernel | grep -v $KERNELRELEASE`

for i in $LIST; do
	echo -n "- Removing $i..."
	rpm -e $i
	rm -rf /usr/src/$i > /dev/null 2>&1
	echo "done"
done


DIRS=`ls -d /usr/src/kernel-source* | grep -v $KERNELRELEASE`
for i in $DIRS; do
	echo -n "- Removing $i..."
	rm -rf $i
	echo "done"
done

echo -n "- Starting network service..."
service network start > /dev/null 2>&1
echo "done"

