#!/bin/sh
#--------------------------------------------------------------
#to remove a kernel entry from grub file type
#--------------------------------------------------------------
#$1 kernel version-release number
#$2 boot file (grub.conf), optionnal
#--------------------------------------------------------------
KVER=$1
GRUBFILE=$2

deleting()

{
sed  -i -e "/title.*($KVER)/,/^$/d" $GRUBFILE
}

if [ -z "$1" ] ; then
  echo "Missing Kernel Version-release parameter (exiting)" 
  exit 1
  fi
if [ -z "$2" ] ; then
  GRUBFILE="/boot/grub/grub.conf"
  fi

if [ -f $GRUBFILE ] ; then
#duplicating file
  BACKUP=$GRUBFILE.$$
  cp $GRUBFILE $BACKUP

#extracting basic information
  CURKER=`uname -r`
  RACINE=$(echo -e `sed  -n -e "/title.*($CURKER)/,/^$/p" $BACKUP | grep "root ("`)
  PARAMS=$(echo -e `sed  -n -e "/title.*($CURKER)/,/^$/p" $BACKUP | grep "kernel " | cut -d ' ' -f3-`)
  if [ -z "$RACINE" ] ; then
    echo "Unable to fine $CURKER root definition (exiting)" 
    exit 2
    fi
  deleting
  rm $BACKUP
  fi
