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

inserting()

{
ADDON=/tmp/addon.$$

(
echo
echo "title Osukiss ($KVER)"
echo -e "\t$RACINE"
echo -e "\tkernel /vmlinuz-$KVER $PARAMS"
echo -e "\tinitrd /initramfs-$KVER.img"
) > $ADDON
sed -i -e "/hiddenmenu$/ r $ADDON" $GRUBFILE
rm $ADDON
}

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 find $CURKER root definition within grub file (exiting, non fatal)" 
    exit 0
    fi

#removing previous entry
  del-kernel-pkg $1 $2
  inserting;
  rm $BACKUP
  fi
