#!/usr/bin/perl
###############script to update GRUB configuration file (grub.conf) after kernel installation/uninstallation

use lib "/opt/nortel/base/lib";

use warnings;
use strict;

use common_variables;
use patchutil::patches_target;

sub update_grub_conf(); #update GRUB configuration file grub.conf

exit(update_grub_conf());

#######
#INPUT:  NONE
#OUTPUT: $kernel_count - number of the installed kernels, OK
#	 0 - error
######
sub update_grub_conf()
{
 my($found,$str,$kernel);
 my $kernel_count=0;
 unless(open(GRUB,"+<$GRUBCONF")){
    print "Error: cannot open grub.conf for writing\n";
    return $ERROR;
 }
 my @list=<GRUB>;
 $found=0;
 my @grubconf;
 foreach my $str (@list){
    # found string timeout, set timeout $BOOT_TIMEOUT sec
    if ($str=~s/(^timeout\s*)(=|\s)?(\s*)0/$1.($2?$2:'').($3?$3:'').$BOOT_TIMEOUT/ige)
    {
         $found=1; 
    }
    
    # found string default, set default 0
    if ($str=~s/(^default\s*)(=|\s)?(\s*)(\d+)/$1.($2?$2:'').($3?$3:'').0/ige)
    {
        $found=1;
    }

    #found string title with the kernel version
    if($str=~/title(.*)/) 
    {
         $kernel_count++; 
    }
    push(@grubconf,$str);
 }

 close(GRUB);
 
 # if there is any changes - update grub.conf
 if($found==1)
 {
    open(GRUB,"+>","$GRUBCONF");
    foreach (@grubconf){
	print GRUB ;
    }
 }
 return $OK;	
}

